From cbfdcff2615e901bdc434d06f38a3da8eecbdf8b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 31 Jul 2016 10:46:17 +0200 Subject: Prepare settings for the API in the admin page and during the install API settings: - api.enabled - api.secret The API settings will be initialized (and the secret generated) with an update method. --- tests/Updater/UpdaterTest.php | 40 ++++++++++++++++++++++++++++++++++++++-- tests/UtilsTest.php | 17 +++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 4948fe52..0171daad 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -271,7 +271,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; public function testEscapeConfig() { $sandbox = 'sandbox/config'; - copy(self::$configFile .'.json.php', $sandbox .'.json.php'); + copy(self::$configFile . '.json.php', $sandbox . '.json.php'); $this->conf = new ConfigManager($sandbox); $title = ''; $headerLink = ''; @@ -286,7 +286,43 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertEquals(escape($title), $this->conf->get('general.title')); $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link')); $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url')); - unlink($sandbox .'.json.php'); + unlink($sandbox . '.json.php'); + } + + /** + * Test updateMethodApiSettings(): create default settings for the API (enabled + secret). + */ + public function testUpdateApiSettings() + { + $confFile = 'sandbox/config'; + copy(self::$configFile .'.json.php', $confFile .'.json.php'); + $conf = new ConfigManager($confFile); + $updater = new Updater(array(), array(), $conf, true); + + $this->assertFalse($conf->exists('api.enabled')); + $this->assertFalse($conf->exists('api.secret')); + $updater->updateMethodApiSettings(); + $conf->reload(); + $this->assertTrue($conf->get('api.enabled')); + $this->assertTrue($conf->exists('api.secret')); + unlink($confFile .'.json.php'); + } + + /** + * Test updateMethodApiSettings(): already set, do nothing. + */ + public function testUpdateApiSettingsNothingToDo() + { + $confFile = 'sandbox/config'; + copy(self::$configFile .'.json.php', $confFile .'.json.php'); + $conf = new ConfigManager($confFile); + $conf->set('api.enabled', false); + $conf->set('api.secret', ''); + $updater = new Updater(array(), array(), $conf, true); + $updater->updateMethodApiSettings(); + $this->assertFalse($conf->get('api.enabled')); + $this->assertEmpty($conf->get('api.secret')); + unlink($confFile .'.json.php'); } /** diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 6a7870c4..0cf9a921 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -253,4 +253,21 @@ class UtilsTest extends PHPUnit_Framework_TestCase is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=') ); } + + /** + * Test generateSecretApi. + */ + public function testGenerateSecretApi() + { + $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar'))); + } + + /** + * Test generateSecretApi with invalid parameters. + */ + public function testGenerateSecretApiInvalid() + { + $this->assertFalse(generate_api_secret('', '')); + $this->assertFalse(generate_api_secret(false, false)); + } } -- cgit v1.2.3 From 18e6796726d73d7dc90ecdd16c181493941f5487 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 15 Dec 2016 10:13:00 +0100 Subject: REST API structure using Slim framework * REST API routes are handle by Slim. * Every API controller go through ApiMiddleware which handles security. * First service implemented `/info`, for tests purpose. --- tests/api/ApiMiddlewareTest.php | 184 +++++++++++++++++++++++++++++++++ tests/api/ApiUtilsTest.php | 206 +++++++++++++++++++++++++++++++++++++ tests/api/controllers/InfoTest.php | 113 ++++++++++++++++++++ 3 files changed, 503 insertions(+) create mode 100644 tests/api/ApiMiddlewareTest.php create mode 100644 tests/api/ApiUtilsTest.php create mode 100644 tests/api/controllers/InfoTest.php (limited to 'tests') diff --git a/tests/api/ApiMiddlewareTest.php b/tests/api/ApiMiddlewareTest.php new file mode 100644 index 00000000..4d4dd9b9 --- /dev/null +++ b/tests/api/ApiMiddlewareTest.php @@ -0,0 +1,184 @@ +conf = new \ConfigManager('tests/utils/config/configJson.json.php'); + $this->conf->set('api.secret', 'NapoleonWasALizard'); + + $this->refDB = new \ReferenceLinkDB(); + $this->refDB->write(self::$testDatastore); + + $this->container = new Container(); + $this->container['conf'] = $this->conf; + } + + /** + * After every test, remove the test datastore. + */ + public function tearDown() + { + @unlink(self::$testDatastore); + } + + /** + * Invoke the middleware with the API disabled: + * should return a 401 error Unauthorized. + */ + public function testInvokeMiddlewareApiDisabled() + { + $this->conf->set('api.enabled', false); + $mw = new ApiMiddleware($this->container); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/echo', + ]); + $request = Request::createFromEnvironment($env); + $response = new Response(); + /** @var Response $response */ + $response = $mw($request, $response, null); + + $this->assertEquals(401, $response->getStatusCode()); + $body = json_decode((string) $response->getBody()); + $this->assertEquals('Not authorized', $body); + } + + /** + * Invoke the middleware with the API disabled in debug mode: + * should return a 401 error Unauthorized - with a specific message and a stacktrace. + */ + public function testInvokeMiddlewareApiDisabledDebug() + { + $this->conf->set('api.enabled', false); + $this->conf->set('dev.debug', true); + $mw = new ApiMiddleware($this->container); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/echo', + ]); + $request = Request::createFromEnvironment($env); + $response = new Response(); + /** @var Response $response */ + $response = $mw($request, $response, null); + + $this->assertEquals(401, $response->getStatusCode()); + $body = json_decode((string) $response->getBody()); + $this->assertEquals('Not authorized: API is disabled', $body->message); + $this->assertContains('ApiAuthorizationException', $body->stacktrace); + } + + /** + * Invoke the middleware without a token (debug): + * should return a 401 error Unauthorized - with a specific message and a stacktrace. + */ + public function testInvokeMiddlewareNoTokenProvidedDebug() + { + $this->conf->set('dev.debug', true); + $mw = new ApiMiddleware($this->container); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/echo', + ]); + $request = Request::createFromEnvironment($env); + $response = new Response(); + /** @var Response $response */ + $response = $mw($request, $response, null); + + $this->assertEquals(401, $response->getStatusCode()); + $body = json_decode((string) $response->getBody()); + $this->assertEquals('Not authorized: JWT token not provided', $body->message); + $this->assertContains('ApiAuthorizationException', $body->stacktrace); + } + + /** + * Invoke the middleware without a secret set in settings (debug): + * should return a 401 error Unauthorized - with a specific message and a stacktrace. + */ + public function testInvokeMiddlewareNoSecretSetDebug() + { + $this->conf->set('dev.debug', true); + $this->conf->set('api.secret', ''); + $mw = new ApiMiddleware($this->container); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/echo', + 'HTTP_JWT'=> 'jwt', + ]); + $request = Request::createFromEnvironment($env); + $response = new Response(); + /** @var Response $response */ + $response = $mw($request, $response, null); + + $this->assertEquals(401, $response->getStatusCode()); + $body = json_decode((string) $response->getBody()); + $this->assertEquals('Not authorized: Token secret must be set in Shaarli\'s administration', $body->message); + $this->assertContains('ApiAuthorizationException', $body->stacktrace); + } + + /** + * Invoke the middleware without an invalid JWT token (debug): + * should return a 401 error Unauthorized - with a specific message and a stacktrace. + * + * Note: specific JWT errors tests are handled in ApiUtilsTest. + */ + public function testInvokeMiddlewareInvalidJwtDebug() + { + $this->conf->set('dev.debug', true); + $mw = new ApiMiddleware($this->container); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/echo', + 'HTTP_JWT'=> 'bad jwt', + ]); + $request = Request::createFromEnvironment($env); + $response = new Response(); + /** @var Response $response */ + $response = $mw($request, $response, null); + + $this->assertEquals(401, $response->getStatusCode()); + $body = json_decode((string) $response->getBody()); + $this->assertEquals('Not authorized: Malformed JWT token', $body->message); + $this->assertContains('ApiAuthorizationException', $body->stacktrace); + } +} diff --git a/tests/api/ApiUtilsTest.php b/tests/api/ApiUtilsTest.php new file mode 100644 index 00000000..10da1459 --- /dev/null +++ b/tests/api/ApiUtilsTest.php @@ -0,0 +1,206 @@ +generateCustomJwtToken('notJSON', '{"JSON":1}', 'secret'); + ApiUtils::validateJwtToken($token, 'secret'); + } + + /** + * Test validateJwtToken() with a JWT token with a an invalid payload (not JSON). + * + * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException + * @expectedExceptionMessage Invalid JWT payload + */ + public function testValidateJwtTokenInvalidPayload() + { + $token = $this->generateCustomJwtToken('{"JSON":1}', 'notJSON', 'secret'); + ApiUtils::validateJwtToken($token, 'secret'); + } + + /** + * Test validateJwtToken() with a JWT token without issued time. + * + * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException + * @expectedExceptionMessage Invalid JWT issued time + */ + public function testValidateJwtTokenInvalidTimeEmpty() + { + $token = $this->generateCustomJwtToken('{"JSON":1}', '{"JSON":1}', 'secret'); + ApiUtils::validateJwtToken($token, 'secret'); + } + + /** + * Test validateJwtToken() with an expired JWT token. + * + * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException + * @expectedExceptionMessage Invalid JWT issued time + */ + public function testValidateJwtTokenInvalidTimeExpired() + { + $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() - 600) . '}', 'secret'); + ApiUtils::validateJwtToken($token, 'secret'); + } + + /** + * Test validateJwtToken() with a JWT token issued in the future. + * + * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException + * @expectedExceptionMessage Invalid JWT issued time + */ + public function testValidateJwtTokenInvalidTimeFuture() + { + $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() + 60) . '}', 'secret'); + ApiUtils::validateJwtToken($token, 'secret'); + } +} diff --git a/tests/api/controllers/InfoTest.php b/tests/api/controllers/InfoTest.php new file mode 100644 index 00000000..2916eed8 --- /dev/null +++ b/tests/api/controllers/InfoTest.php @@ -0,0 +1,113 @@ +conf = new \ConfigManager('tests/utils/config/configJson.json.php'); + $this->refDB = new \ReferenceLinkDB(); + $this->refDB->write(self::$testDatastore); + + $this->container = new Container(); + $this->container['conf'] = $this->conf; + $this->container['db'] = new \LinkDB(self::$testDatastore, true, false); + + $this->controller = new Info($this->container); + } + + /** + * After every test, remove the test datastore. + */ + public function tearDown() + { + @unlink(self::$testDatastore); + } + + /** + * Test /info service. + */ + public function testGetInfo() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + ]); + $request = Request::createFromEnvironment($env); + + $response = $this->controller->getInfo($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + + $this->assertEquals(8, $data['global_counter']); + $this->assertEquals(2, $data['private_counter']); + $this->assertEquals('Shaarli', $data['settings']['title']); + $this->assertEquals('?', $data['settings']['header_link']); + $this->assertEquals('UTC', $data['settings']['timezone']); + $this->assertEquals(\ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']); + $this->assertEquals(false, $data['settings']['default_private_links']); + + $title = 'My links'; + $headerLink = 'http://shaarli.tld'; + $timezone = 'Europe/Paris'; + $enabledPlugins = array('foo', 'bar'); + $defaultPrivateLinks = true; + $this->conf->set('general.title', $title); + $this->conf->set('general.header_link', $headerLink); + $this->conf->set('general.timezone', $timezone); + $this->conf->set('general.enabled_plugins', $enabledPlugins); + $this->conf->set('privacy.default_private_links', $defaultPrivateLinks); + + $response = $this->controller->getInfo($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + + $this->assertEquals(8, $data['global_counter']); + $this->assertEquals(2, $data['private_counter']); + $this->assertEquals($title, $data['settings']['title']); + $this->assertEquals($headerLink, $data['settings']['header_link']); + $this->assertEquals($timezone, $data['settings']['timezone']); + $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']); + $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']); + } +} -- cgit v1.2.3