From 082e5d957cb4cec31945d6baaf180249b32bc37d Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 21 Jul 2015 14:18:06 -0500 Subject: [PATCH] Wrote tests covering HttpAdapter - Test that it returns a GuestIdentity if no Authorization header is present. - Test that it returns boolean false if an invalid Authorization token is present. - Test that it returns an AuthenticatedIdentity if the Authorization token is valid. --- test/Authentication/HttpAdapterTest.php | 88 +++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 test/Authentication/HttpAdapterTest.php diff --git a/test/Authentication/HttpAdapterTest.php b/test/Authentication/HttpAdapterTest.php new file mode 100644 index 0000000..03b0efb --- /dev/null +++ b/test/Authentication/HttpAdapterTest.php @@ -0,0 +1,88 @@ +authentication = new AuthenticationService(new NonPersistent()); + + $this->request = $request = new HttpRequest(); + $this->response = $response = new HttpResponse(); + + $mvcEvent = new MvcEvent(); + $mvcEvent->setRequest($request) + ->setResponse($response); + + $this->event = new MvcAuthEvent( + $mvcEvent, + $this->authentication, + $this->getMock('ZF\MvcAuth\Authorization\AuthorizationInterface') + ); + } + + public function testAuthenticateReturnsGuestIdentityIfNoAuthorizationHeaderProvided() + { + $httpAuth = new HttpAuth([ + 'accept_schemes' => 'basic', + 'realm' => 'My Web Site', + 'digest_domains' => '/', + 'nonce_timeout' => 3600, + ]); + $httpAuth->setBasicResolver(new HttpAuth\ApacheResolver(__DIR__ . '/../TestAsset/htpasswd')); + + $adapter = new HttpAdapter($httpAuth, $this->authentication); + $result = $adapter->authenticate($this->request, $this->response, $this->event); + $this->assertInstanceOf('ZF\MvcAuth\Identity\GuestIdentity', $result); + } + + public function testAuthenticateReturnsFalseIfInvalidCredentialsProvidedInAuthorizationHeader() + { + $httpAuth = new HttpAuth([ + 'accept_schemes' => 'basic', + 'realm' => 'My Web Site', + 'digest_domains' => '/', + 'nonce_timeout' => 3600, + ]); + $httpAuth->setBasicResolver(new HttpAuth\ApacheResolver(__DIR__ . '/../TestAsset/htpasswd')); + + $adapter = new HttpAdapter($httpAuth, $this->authentication); + + $this->request->getHeaders()->addHeaderLine('Authorization', 'Bearer BOGUS TOKEN'); + + $this->assertFalse($adapter->authenticate($this->request, $this->response, $this->event)); + } + + public function testAuthenticateReturnsAuthenticatedIdentityIfValidCredentialsProvidedInAuthorizationHeader() + { + $httpAuth = new HttpAuth([ + 'accept_schemes' => 'basic', + 'realm' => 'My Web Site', + 'digest_domains' => '/', + 'nonce_timeout' => 3600, + ]); + $httpAuth->setBasicResolver(new HttpAuth\ApacheResolver(__DIR__ . '/../TestAsset/htpasswd')); + + $adapter = new HttpAdapter($httpAuth, $this->authentication); + + $this->request->getHeaders()->addHeaderLine('Authorization: Basic dXNlcjp1c2Vy'); + $result = $adapter->authenticate($this->request, $this->response, $this->event); + $this->assertInstanceOf('ZF\MvcAuth\Identity\AuthenticatedIdentity', $result); + } +}