From 37331c4dc84a9a1c75d9eb1ebbb1257bb77ab797 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 5 Feb 2013 16:20:56 +0000 Subject: [PATCH] Added resource server tests --- tests/resource/ResourceServerTest.php | 159 ++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 tests/resource/ResourceServerTest.php diff --git a/tests/resource/ResourceServerTest.php b/tests/resource/ResourceServerTest.php new file mode 100644 index 00000000..5f7361f7 --- /dev/null +++ b/tests/resource/ResourceServerTest.php @@ -0,0 +1,159 @@ +client = M::mock('OAuth2\Storage\ClientInterface'); + $this->session = M::mock('OAuth2\Storage\SessionInterface'); + //$this->scope = M::mock('OAuth2\Storage\ScopeInterface'); + } + + private function returnDefault() + { + return new OAuth2\ResourceServer($this->session); + } + + public function test_setRequest() + { + $s = $this->returnDefault(); + $request = new OAuth2\Util\Request(); + $s->setRequest($request); + + $reflector = new ReflectionClass($s); + $requestProperty = $reflector->getProperty('request'); + $requestProperty->setAccessible(true); + $v = $requestProperty->getValue($s); + + $this->assertTrue($v instanceof OAuth2\Util\RequestInterface); + } + + public function test_getRequest() + { + $s = $this->returnDefault(); + $request = new OAuth2\Util\Request(); + $s->setRequest($request); + $v = $s->getRequest(); + + $this->assertTrue($v instanceof OAuth2\Util\RequestInterface); + } + + public function test_getTokenKey() + { + $s = $this->returnDefault(); + $this->assertEquals('oauth_token', $s->getTokenKey()); + } + + public function test_setTokenKey() + { + $s = $this->returnDefault(); + $s->setTokenKey('access_token'); + + $reflector = new ReflectionClass($s); + $requestProperty = $reflector->getProperty('tokenKey'); + $requestProperty->setAccessible(true); + $v = $requestProperty->getValue($s); + + $this->assertEquals('access_token', $v); + } + + /** + * @expectedException OAuth2\Exception\MissingAccessTokenException + */ + public function test_determineAccessToken_missingToken() + { + $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer'; + $request = new OAuth2\Util\Request(array(), array(), array(), array(), $_SERVER); + + $s = $this->returnDefault(); + $s->setRequest($request); + + $reflector = new ReflectionClass($s); + $method = $reflector->getMethod('determineAccessToken'); + $method->setAccessible(true); + + $result = $method->invoke($s); + } + + public function test_determineAccessToken_fromHeader() + { + $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer abcdef'; + $request = new OAuth2\Util\Request(array(), array(), array(), array(), $_SERVER); + + $s = $this->returnDefault(); + $s->setRequest($request); + + $reflector = new ReflectionClass($s); + $method = $reflector->getMethod('determineAccessToken'); + $method->setAccessible(true); + + $result = $method->invoke($s); + + $this->assertEquals('abcdef', $result); + } + + public function test_determineAccessToken_fromMethod() + { + $s = $this->returnDefault(); + + $_GET[$s->getTokenKey()] = 'abcdef'; + $_SERVER['REQUEST_METHOD'] = 'get'; + + $request = new OAuth2\Util\Request($_GET, array(), array(), array(), $_SERVER); + $s->setRequest($request); + + $reflector = new ReflectionClass($s); + $method = $reflector->getMethod('determineAccessToken'); + $method->setAccessible(true); + + $result = $method->invoke($s); + + $this->assertEquals('abcdef', $result); + } + + public function test_isValid_notValid() + { + $this->session->shouldReceive('validateAccessToken')->andReturn(false); + + $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer abcdef'; + $request = new OAuth2\Util\Request(array(), array(), array(), array(), $_SERVER); + + $s = $this->returnDefault(); + $s->setRequest($request); + + $this->assertFalse($s->isValid()); + } + + public function test_isValid_valid() + { + $this->session->shouldReceive('validateAccessToken')->andReturn(array( + 'id' => 1, + 'owner_type' => 'user', + 'owner_id' => 123 + )); + $this->session->shouldReceive('getScopes')->andReturn(array('foo', 'bar')); + + $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer abcdef'; + $request = new OAuth2\Util\Request(array(), array(), array(), array(), $_SERVER); + + $s = $this->returnDefault(); + $s->setRequest($request); + + $this->assertTrue($s->isValid()); + $this->assertEquals(123, $s->getOwnerId()); + $this->assertEquals('user', $s->getOwnerType()); + $this->assertEquals('abcdef', $s->getAccessToken()); + $this->assertTrue($s->hasScope('foo')); + $this->assertTrue($s->hasScope('bar')); + $this->assertTrue($s->hasScope(array('foo', 'bar'))); + $this->assertFalse($s->hasScope(array('foobar'))); + $this->assertFalse($s->hasScope('foobar')); + $this->assertFalse($s->hasScope(new StdClass)); + } +} \ No newline at end of file