-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] Test multiple versions (#8778)
This attempts to add tests for multiple versions of the API. The tests from v0.0.4-dev are copied to v0.0.3 classes and the version in the new classes changed to v0.0.3.
- Loading branch information
Showing
14 changed files
with
4,341 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 267 additions & 0 deletions
267
raisinbread/test/api/LorisApiAuthenticated_v0_0_3_Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
<?php | ||
|
||
require_once __DIR__ . | ||
"/../../../test/integrationtests/LorisIntegrationTest.class.inc"; | ||
use GuzzleHttp\Client; | ||
|
||
/** | ||
* PHPUnit class for API test suite. This script sends HTTP requests to every | ||
* endpoints of the api module and look at the response content, status code and | ||
* headers where it applies. All endpoints are accessible at <host>/api/<version>/ | ||
* (e.g. the endpoint of the version 0.0.3 of the API "/projects" URI for the host | ||
* "example.loris.ca" would be https://example.loris.ca/api/v0.0.3/projects) | ||
* | ||
* @category API | ||
* @package Tests | ||
* @subpackage Integration | ||
* @author Simon Pelletier <[email protected]> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
* @link https://github.com/aces/Loris/ | ||
*/ | ||
class LorisApiAuthenticated_v0_0_3_Test extends LorisIntegrationTest | ||
{ | ||
|
||
protected $client; | ||
protected $version; | ||
protected $headers; | ||
protected $base_uri; | ||
protected $originalJwtKey; | ||
protected $configIdJwt; | ||
|
||
/** | ||
* Overrides LorisIntegrationTest::setUp() to store the current JWT key | ||
* and replaces it for an acceptable one. | ||
* | ||
* @return void | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->_version = 'v0.0.3'; | ||
|
||
// store the original JWT key for restoring it later | ||
$jwtConfig = $this->DB->pselect( | ||
' | ||
SELECT | ||
Value, ConfigID | ||
FROM | ||
Config | ||
WHERE | ||
ConfigID= | ||
(SELECT ID FROM ConfigSettings WHERE Name="JWTKey") | ||
', | ||
[] | ||
)[0] ?? null; | ||
|
||
if ($jwtConfig === null) { | ||
throw new \LorisException('There is no Config for "JWTKey"'); | ||
} | ||
|
||
$this->originalJwtKey = $jwtConfig['Value']; | ||
$this->configIdJwt = $jwtConfig['ConfigID']; | ||
|
||
// generating a random JWTkey | ||
$new_id = bin2hex(random_bytes(30)) . 'A1!'; | ||
|
||
$set = [ | ||
'Value' => $new_id | ||
]; | ||
|
||
$where = [ | ||
'ConfigID' => $this->configIdJwt | ||
]; | ||
|
||
$this->DB->update('Config', $set, $where); | ||
|
||
$this->apiLogin('UnitTester', $this->validPassword); | ||
|
||
$this->DB->insert( | ||
"candidate", | ||
[ | ||
'CandID' => '900000', | ||
'PSCID' => 'TST0001', | ||
'RegistrationCenterID' => 1, | ||
'RegistrationProjectID' => 1, | ||
'Active' => 'Y', | ||
'UserID' => 1, | ||
'Entity_type' => 'Human', | ||
'Sex' => 'Female' | ||
] | ||
); | ||
$this->DB->insert( | ||
'session', | ||
[ | ||
'ID' => '999999', | ||
'CandID' => '900000', | ||
'Visit_label' => 'V1', | ||
'CenterID' => 1, | ||
'ProjectID' => 1, | ||
'Current_stage' => 'Not Started', | ||
] | ||
); | ||
$this->DB->insert( | ||
'test_names', | ||
[ | ||
'ID' => '999999', | ||
'Test_name' => 'testtest', | ||
'Full_name' => 'Test Test', | ||
'Sub_group' => 1, | ||
] | ||
); | ||
$this->DB->insert( | ||
'flag', | ||
[ | ||
'ID' => '999999', | ||
'SessionID' => '999999', | ||
'Test_name' => 'testtest', | ||
'CommentID' => '11111111111111111', | ||
] | ||
); | ||
$this->DB->insert( | ||
'flag', | ||
[ | ||
'ID' => '999999', | ||
'SessionID' => '999999', | ||
'Test_name' => 'testtest', | ||
'CommentID' => 'DDE_11111111111111111', | ||
] | ||
); | ||
|
||
// 1 is inserted by LorisIntegrationTest | ||
$this->DB->insert( | ||
'user_project_rel', | ||
[ | ||
'ProjectID' => '2', | ||
'UserID' => '999990', | ||
], | ||
); | ||
|
||
// 1 is inserted by LorisIntegrationTest | ||
$this->DB->insert( | ||
'user_psc_rel', | ||
[ | ||
'CenterID' => '2', | ||
'UserID' => '999990', | ||
], | ||
); | ||
$this->DB->insert( | ||
'user_psc_rel', | ||
[ | ||
'CenterID' => '3', | ||
'UserID' => '999990', | ||
], | ||
); | ||
$this->DB->insert( | ||
'user_psc_rel', | ||
[ | ||
'CenterID' => 4, | ||
'UserID' => 999990, | ||
], | ||
); | ||
|
||
} | ||
|
||
/** | ||
* Used to log in with GuzzleHttp\Client | ||
* | ||
* @param string $username The username to log in as | ||
* @param string $password The (plain text) password to login as. | ||
* | ||
* @return void | ||
*/ | ||
public function apiLogin($username, $password) | ||
{ | ||
$this->base_uri = "$this->url/api/$this->_version/"; | ||
$this->client = new Client(['base_uri' => $this->base_uri]); | ||
$response = $this->client->request( | ||
'POST', | ||
"login", | ||
[ | ||
'json' => ['username' => $username, | ||
'password' => $password | ||
] | ||
] | ||
); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$token = json_decode( | ||
$response->getBody()->getContents() | ||
)->token ?? null; | ||
|
||
if ($token === null) { | ||
throw new \LorisException("Login failed"); | ||
} | ||
$headers = [ | ||
'Authorization' => "Bearer $token", | ||
'Accept' => 'application/json' | ||
]; | ||
$this->headers = $headers; | ||
} | ||
|
||
/** | ||
* Used to test login | ||
* | ||
* @return void | ||
*/ | ||
function testLoginSuccess() | ||
{ | ||
$this->assertArrayHasKey('Authorization', $this->headers); | ||
$this->assertArrayHasKey('Accept', $this->headers); | ||
} | ||
|
||
/** | ||
* Overrides LorisIntegrationTest::tearDown() to set the original key back. | ||
* | ||
* @return void | ||
*/ | ||
public function tearDown(): void | ||
{ | ||
// Only delete the ones we setup in setUp. | ||
$this->DB->delete( | ||
"user_project_rel", | ||
[ | ||
"UserID" => '999990', | ||
"ProjectID" => '2', | ||
], | ||
); | ||
$this->DB->delete( | ||
"user_psc_rel", | ||
[ | ||
"UserID" => '999990', | ||
"CenterID" => '2', | ||
], | ||
); | ||
$this->DB->delete( | ||
"user_psc_rel", | ||
[ | ||
"UserID" => '999990', | ||
"CenterID" => '3', | ||
], | ||
); | ||
$this->DB->delete( | ||
"user_psc_rel", | ||
[ | ||
"UserID" => '999990', | ||
"CenterID" => '4', | ||
], | ||
); | ||
|
||
$this->DB->delete("session", ['CandID' => '900000']); | ||
$this->DB->delete("candidate", ['CandID' => '900000']); | ||
$this->DB->delete("flag", ['ID' => '999999']); | ||
$this->DB->delete("test_names", ['ID' => '999999']); | ||
|
||
$set = [ | ||
'Value' => $this->originalJwtKey | ||
]; | ||
|
||
$where = [ | ||
'ConfigID' => $this->configIdJwt | ||
]; | ||
|
||
$this->DB->update('Config', $set, $where); | ||
parent::tearDown(); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.