|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 4 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 5 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 6 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 7 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 8 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 9 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 10 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 11 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 12 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 13 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 14 | + * |
| 15 | + * This software consists of voluntary contributions made by many individuals |
| 16 | + * and is licensed under the MIT license. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace ZfrOAuth2ModuleTest\Server\Authentication\Adapter; |
| 20 | + |
| 21 | +use PHPUnit_Framework_TestCase; |
| 22 | +use Zend\Authentication\AuthenticationService; |
| 23 | +use Zend\Http\Request as HttpRequest; |
| 24 | +use ZfrOAuth2\Server\Entity\AccessToken; |
| 25 | +use ZfrOAuth2\Server\Exception\OAuth2Exception; |
| 26 | +use ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Marco Pivetta <[email protected]> |
| 30 | + * @licence MIT |
| 31 | + * |
| 32 | + * @coversNothing |
| 33 | + */ |
| 34 | +class AuthenticationFunctionalTest extends PHPUnit_Framework_TestCase |
| 35 | +{ |
| 36 | + /** |
| 37 | + * @var \ZfrOAuth2\Server\ResourceServer|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $resourceServer; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var AccessTokenStorage |
| 43 | + */ |
| 44 | + private $authenticationStorage; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var AuthenticationService |
| 48 | + */ |
| 49 | + private $authenticationService; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Zend\Mvc\MvcEvent|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $mvcEvent; |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritDoc} |
| 58 | + */ |
| 59 | + protected function setUp() |
| 60 | + { |
| 61 | + $this->mvcEvent = $this->getMock('Zend\Mvc\MvcEvent'); |
| 62 | + $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); |
| 63 | + $this->resourceServer = $this->getMock('ZfrOAuth2\Server\ResourceServer', [], [], '', false); |
| 64 | + $this->authenticationStorage = new AccessTokenStorage($this->resourceServer, $application); |
| 65 | + $this->authenticationService = new AuthenticationService($this->authenticationStorage); |
| 66 | + |
| 67 | + $application->expects($this->any())->method('getMvcEvent')->will($this->returnValue($this->mvcEvent)); |
| 68 | + } |
| 69 | + |
| 70 | + public function testSuccessAuthenticationOnValidToken() |
| 71 | + { |
| 72 | + $request = new HttpRequest(); |
| 73 | + |
| 74 | + $this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request)); |
| 75 | + |
| 76 | + $token = new AccessToken(); |
| 77 | + $owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface'); |
| 78 | + $token->setOwner($owner); |
| 79 | + |
| 80 | + $this |
| 81 | + ->resourceServer |
| 82 | + ->expects($this->atLeastOnce()) |
| 83 | + ->method('isRequestValid') |
| 84 | + ->with($request) |
| 85 | + ->will($this->returnValue(true)); |
| 86 | + |
| 87 | + $this |
| 88 | + ->resourceServer |
| 89 | + ->expects($this->atLeastOnce()) |
| 90 | + ->method('getAccessToken') |
| 91 | + ->with($request) |
| 92 | + ->will($this->returnValue($token)); |
| 93 | + |
| 94 | + |
| 95 | + $this->assertTrue($this->authenticationService->hasIdentity()); |
| 96 | + $this->assertSame($owner, $this->authenticationService->getIdentity()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testFailAuthenticationOnNoToken() |
| 100 | + { |
| 101 | + $request = new HttpRequest(); |
| 102 | + |
| 103 | + $this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request)); |
| 104 | + |
| 105 | + $token = new AccessToken(); |
| 106 | + $owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface'); |
| 107 | + $token->setOwner($owner); |
| 108 | + |
| 109 | + $this |
| 110 | + ->resourceServer |
| 111 | + ->expects($this->atLeastOnce()) |
| 112 | + ->method('isRequestValid') |
| 113 | + ->with($request) |
| 114 | + ->will($this->returnValue(false)); |
| 115 | + |
| 116 | + $this->resourceServer->expects($this->never())->method('getAccessToken'); |
| 117 | + |
| 118 | + $this->assertFalse($this->authenticationService->hasIdentity()); |
| 119 | + $this->assertNull($this->authenticationService->getIdentity()); |
| 120 | + } |
| 121 | + |
| 122 | + public function testFailAuthenticationOnExpiredToken() |
| 123 | + { |
| 124 | + $request = new HttpRequest(); |
| 125 | + |
| 126 | + $this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request)); |
| 127 | + |
| 128 | + $token = new AccessToken(); |
| 129 | + $owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface'); |
| 130 | + $token->setOwner($owner); |
| 131 | + |
| 132 | + $this |
| 133 | + ->resourceServer |
| 134 | + ->expects($this->atLeastOnce()) |
| 135 | + ->method('isRequestValid') |
| 136 | + ->with($request) |
| 137 | + ->will($this->returnValue(true)); |
| 138 | + |
| 139 | + $this |
| 140 | + ->resourceServer |
| 141 | + ->expects($this->atLeastOnce()) |
| 142 | + ->method('getAccessToken') |
| 143 | + ->with($request) |
| 144 | + ->will($this->throwException(new OAuth2Exception('Expired token', 123))); |
| 145 | + |
| 146 | + $this->setExpectedException('ZfrOAuth2\Server\Exception\OAuth2Exception', 'Expired token', 123); |
| 147 | + |
| 148 | + $this->authenticationService->getIdentity(); |
| 149 | + } |
| 150 | + |
| 151 | + public function testFailAuthenticationOnNoRequest() |
| 152 | + { |
| 153 | + $this->resourceServer->expects($this->never())->method('isRequestValid'); |
| 154 | + $this->resourceServer->expects($this->never())->method('getAccessToken'); |
| 155 | + |
| 156 | + $this->assertFalse($this->authenticationService->hasIdentity()); |
| 157 | + $this->assertNull($this->authenticationService->getIdentity()); |
| 158 | + } |
| 159 | + |
| 160 | + public function testFailAuthenticationOnNonHttpRequest() |
| 161 | + { |
| 162 | + $request = $this->getMock('Zend\Stdlib\RequestInterface'); |
| 163 | + |
| 164 | + $this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request)); |
| 165 | + |
| 166 | + $this->resourceServer->expects($this->never())->method('isRequestValid'); |
| 167 | + $this->resourceServer->expects($this->never())->method('getAccessToken'); |
| 168 | + |
| 169 | + $this->assertFalse($this->authenticationService->hasIdentity()); |
| 170 | + $this->assertNull($this->authenticationService->getIdentity()); |
| 171 | + } |
| 172 | +} |
0 commit comments