ApiMiddlewareTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Shaarli\Api;
  3. use Shaarli\Config\ConfigManager;
  4. use Slim\Container;
  5. use Slim\Http\Environment;
  6. use Slim\Http\Request;
  7. use Slim\Http\Response;
  8. /**
  9. * Class ApiMiddlewareTest
  10. *
  11. * Test the REST API Slim Middleware.
  12. *
  13. * Note that we can't test a valid use case here, because the middleware
  14. * needs to call a valid controller/action during its execution.
  15. *
  16. * @package Api
  17. */
  18. class ApiMiddlewareTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var string datastore to test write operations
  22. */
  23. protected static $testDatastore = 'sandbox/datastore.php';
  24. /**
  25. * @var \ConfigManager instance
  26. */
  27. protected $conf;
  28. /**
  29. * @var \ReferenceLinkDB instance.
  30. */
  31. protected $refDB = null;
  32. /**
  33. * @var Container instance.
  34. */
  35. protected $container;
  36. /**
  37. * Before every test, instantiate a new Api with its config, plugins and links.
  38. */
  39. public function setUp()
  40. {
  41. $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
  42. $this->conf->set('api.secret', 'NapoleonWasALizard');
  43. $this->refDB = new \ReferenceLinkDB();
  44. $this->refDB->write(self::$testDatastore);
  45. $this->container = new Container();
  46. $this->container['conf'] = $this->conf;
  47. }
  48. /**
  49. * After every test, remove the test datastore.
  50. */
  51. public function tearDown()
  52. {
  53. @unlink(self::$testDatastore);
  54. }
  55. /**
  56. * Invoke the middleware with the API disabled:
  57. * should return a 401 error Unauthorized.
  58. */
  59. public function testInvokeMiddlewareApiDisabled()
  60. {
  61. $this->conf->set('api.enabled', false);
  62. $mw = new ApiMiddleware($this->container);
  63. $env = Environment::mock([
  64. 'REQUEST_METHOD' => 'GET',
  65. 'REQUEST_URI' => '/echo',
  66. ]);
  67. $request = Request::createFromEnvironment($env);
  68. $response = new Response();
  69. /** @var Response $response */
  70. $response = $mw($request, $response, null);
  71. $this->assertEquals(401, $response->getStatusCode());
  72. $body = json_decode((string) $response->getBody());
  73. $this->assertEquals('Not authorized', $body);
  74. }
  75. /**
  76. * Invoke the middleware with the API disabled in debug mode:
  77. * should return a 401 error Unauthorized - with a specific message and a stacktrace.
  78. */
  79. public function testInvokeMiddlewareApiDisabledDebug()
  80. {
  81. $this->conf->set('api.enabled', false);
  82. $this->conf->set('dev.debug', true);
  83. $mw = new ApiMiddleware($this->container);
  84. $env = Environment::mock([
  85. 'REQUEST_METHOD' => 'GET',
  86. 'REQUEST_URI' => '/echo',
  87. ]);
  88. $request = Request::createFromEnvironment($env);
  89. $response = new Response();
  90. /** @var Response $response */
  91. $response = $mw($request, $response, null);
  92. $this->assertEquals(401, $response->getStatusCode());
  93. $body = json_decode((string) $response->getBody());
  94. $this->assertEquals('Not authorized: API is disabled', $body->message);
  95. $this->assertContains('ApiAuthorizationException', $body->stacktrace);
  96. }
  97. /**
  98. * Invoke the middleware without a token (debug):
  99. * should return a 401 error Unauthorized - with a specific message and a stacktrace.
  100. */
  101. public function testInvokeMiddlewareNoTokenProvidedDebug()
  102. {
  103. $this->conf->set('dev.debug', true);
  104. $mw = new ApiMiddleware($this->container);
  105. $env = Environment::mock([
  106. 'REQUEST_METHOD' => 'GET',
  107. 'REQUEST_URI' => '/echo',
  108. ]);
  109. $request = Request::createFromEnvironment($env);
  110. $response = new Response();
  111. /** @var Response $response */
  112. $response = $mw($request, $response, null);
  113. $this->assertEquals(401, $response->getStatusCode());
  114. $body = json_decode((string) $response->getBody());
  115. $this->assertEquals('Not authorized: JWT token not provided', $body->message);
  116. $this->assertContains('ApiAuthorizationException', $body->stacktrace);
  117. }
  118. /**
  119. * Invoke the middleware without a secret set in settings (debug):
  120. * should return a 401 error Unauthorized - with a specific message and a stacktrace.
  121. */
  122. public function testInvokeMiddlewareNoSecretSetDebug()
  123. {
  124. $this->conf->set('dev.debug', true);
  125. $this->conf->set('api.secret', '');
  126. $mw = new ApiMiddleware($this->container);
  127. $env = Environment::mock([
  128. 'REQUEST_METHOD' => 'GET',
  129. 'REQUEST_URI' => '/echo',
  130. 'HTTP_AUTHORIZATION'=> 'Bearer jwt',
  131. ]);
  132. $request = Request::createFromEnvironment($env);
  133. $response = new Response();
  134. /** @var Response $response */
  135. $response = $mw($request, $response, null);
  136. $this->assertEquals(401, $response->getStatusCode());
  137. $body = json_decode((string) $response->getBody());
  138. $this->assertEquals('Not authorized: Token secret must be set in Shaarli\'s administration', $body->message);
  139. $this->assertContains('ApiAuthorizationException', $body->stacktrace);
  140. }
  141. /**
  142. * Invoke the middleware with an invalid JWT token header
  143. */
  144. public function testInvalidJwtAuthHeaderDebug()
  145. {
  146. $this->conf->set('dev.debug', true);
  147. $mw = new ApiMiddleware($this->container);
  148. $env = Environment::mock([
  149. 'REQUEST_METHOD' => 'GET',
  150. 'REQUEST_URI' => '/echo',
  151. 'HTTP_AUTHORIZATION'=> 'PolarBearer jwt',
  152. ]);
  153. $request = Request::createFromEnvironment($env);
  154. $response = new Response();
  155. /** @var Response $response */
  156. $response = $mw($request, $response, null);
  157. $this->assertEquals(401, $response->getStatusCode());
  158. $body = json_decode((string) $response->getBody());
  159. $this->assertEquals('Not authorized: Invalid JWT header', $body->message);
  160. $this->assertContains('ApiAuthorizationException', $body->stacktrace);
  161. }
  162. /**
  163. * Invoke the middleware with an invalid JWT token (debug):
  164. * should return a 401 error Unauthorized - with a specific message and a stacktrace.
  165. *
  166. * Note: specific JWT errors tests are handled in ApiUtilsTest.
  167. */
  168. public function testInvokeMiddlewareInvalidJwtDebug()
  169. {
  170. $this->conf->set('dev.debug', true);
  171. $mw = new ApiMiddleware($this->container);
  172. $env = Environment::mock([
  173. 'REQUEST_METHOD' => 'GET',
  174. 'REQUEST_URI' => '/echo',
  175. 'HTTP_AUTHORIZATION'=> 'Bearer jwt',
  176. ]);
  177. $request = Request::createFromEnvironment($env);
  178. $response = new Response();
  179. /** @var Response $response */
  180. $response = $mw($request, $response, null);
  181. $this->assertEquals(401, $response->getStatusCode());
  182. $body = json_decode((string) $response->getBody());
  183. $this->assertEquals('Not authorized: Malformed JWT token', $body->message);
  184. $this->assertContains('ApiAuthorizationException', $body->stacktrace);
  185. }
  186. }