Skip to content

Commit

Permalink
[API] Add test suite for API (aces#6671)
Browse files Browse the repository at this point in the history
Add test suite which uses raisinbread and guzzle to perform integration tests for the API.
  • Loading branch information
spell00 authored and AlexandraLivadas committed Jun 29, 2021
1 parent 2e5dbb9 commit cbffd71
Show file tree
Hide file tree
Showing 11 changed files with 3,257 additions and 6 deletions.
12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -19,24 +19,24 @@
},
"devDependencies": {
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/core": "^7.11.0",
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
"@babel/preset-env": "^7.6.3",
"@babel/preset-react": "^7.6.3",
"c3": "^0.7.15",
"d3": "^5.15.0",
"css-loader": "^3.4.2",
"alex": ">=8.0.1",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
"c3": "^0.7.15",
"css-loader": "^3.4.2",
"d3": "^5.15.0",
"eslint": "^5.16.0",
"eslint-config-google": "0.9.1",
"eslint-loader": "^2.2.1",
"eslint-plugin-react": "^7.16.0",
"style-loader": "^1.1.3",
"terser-webpack-plugin": "^1.3.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.0",
"alex": ">=8.0.1"
"webpack-cli": "^3.3.0"
},
"scripts": {
"lint:javascript": "./test/run-js-linter.sh",
Expand Down
143 changes: 143 additions & 0 deletions raisinbread/test/api/LorisApiAuthenticatedTest.php
@@ -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();
}

}

0 comments on commit cbffd71

Please sign in to comment.