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/ApiUtilsTest.php | 206 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 tests/api/ApiUtilsTest.php (limited to 'tests/api/ApiUtilsTest.php') 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'); + } +} -- cgit v1.2.3