From 878518ced705cf2fc1cafb73f49670667b34e97a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Jul 2016 13:33:55 +0200 Subject: [PATCH] Created AuthenticateActionTest --- .../test/Action/AuthenticateActionTest.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 module/Rest/test/Action/AuthenticateActionTest.php diff --git a/module/Rest/test/Action/AuthenticateActionTest.php b/module/Rest/test/Action/AuthenticateActionTest.php new file mode 100644 index 00000000..d61da421 --- /dev/null +++ b/module/Rest/test/Action/AuthenticateActionTest.php @@ -0,0 +1,75 @@ +tokenService = $this->prophesize(RestTokenService::class); + $this->action = new AuthenticateAction($this->tokenService->reveal(), Translator::factory([])); + } + + /** + * @test + */ + public function notProvidingAuthDataReturnsError() + { + $resp = $this->action->__invoke(ServerRequestFactory::fromGlobals(), new Response()); + $this->assertEquals(400, $resp->getStatusCode()); + } + + /** + * @test + */ + public function properCredentialsReturnTokenInResponse() + { + $this->tokenService->createToken('foo', 'bar')->willReturn( + (new RestToken())->setToken('abc-ABC') + )->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withParsedBody([ + 'username' => 'foo', + 'password' => 'bar', + ]); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + + $response->getBody()->rewind(); + $this->assertEquals(['token' => 'abc-ABC'], json_decode($response->getBody()->getContents(), true)); + } + + /** + * @test + */ + public function authenticationExceptionsReturnErrorResponse() + { + $this->tokenService->createToken('foo', 'bar')->willThrow(new AuthenticationException()) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withParsedBody([ + 'username' => 'foo', + 'password' => 'bar', + ]); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(401, $response->getStatusCode()); + } +}