forked from aces/Loris
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API] Add test suite for API (aces#6671)
Add test suite which uses raisinbread and guzzle to perform integration tests for the API.
- Loading branch information
1 parent
2e5dbb9
commit cbffd71
Showing
11 changed files
with
3,257 additions
and
6 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
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,143 @@ | ||
<?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 <simon.pelletier@mcin.ca> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
* @link https://www.github.com/aces/Loris/ | ||
*/ | ||
class LorisApiAuthenticatedTest extends LorisIntegrationTest | ||
{ | ||
|
||
protected $client; | ||
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() | ||
{ | ||
parent::setUp(); | ||
// 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); | ||
} | ||
|
||
/** | ||
* 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/v0.0.3/"; | ||
$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() | ||
{ | ||
$set = [ | ||
'Value' => $this->originalJwtKey | ||
]; | ||
|
||
$where = [ | ||
'ConfigID' => $this->configIdJwt | ||
]; | ||
|
||
$this->DB->update('Config', $set, $where); | ||
parent::tearDown(); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.