[6351] | 1 | <?php |
---|
| 2 | require __DIR__ . '/../lib/OAuth2.php'; |
---|
| 3 | require __DIR__ . '/../lib/IOAuth2Storage.php'; |
---|
| 4 | require __DIR__ . '/../lib/IOAuth2GrantCode.php'; |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * OAuth2 test cases that invovle capturing output. |
---|
| 8 | */ |
---|
| 9 | class OAuth2OutputTest extends PHPUnit_Extensions_OutputTestCase { |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * @var OAuth2 |
---|
| 13 | */ |
---|
| 14 | private $fixture; |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Tests OAuth2->grantAccessToken() with successful Auth code grant |
---|
| 18 | * |
---|
| 19 | */ |
---|
| 20 | public function testGrantAccessTokenWithGrantAuthCodeSuccess() { |
---|
| 21 | $inputData = array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'redirect_uri' => 'http://www.example.com/my/subdir', 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo'); |
---|
| 22 | $storedToken = array('redirect_uri' => 'http://www.example.com', 'client_id' => 'my_little_app', 'expires' => time() + 60); |
---|
| 23 | |
---|
| 24 | $mockStorage = $this->createBaseMock('IOAuth2GrantCode'); |
---|
| 25 | $mockStorage->expects($this->any()) |
---|
| 26 | ->method('getAuthCode') |
---|
| 27 | ->will($this->returnValue($storedToken)); |
---|
| 28 | |
---|
| 29 | // Successful token grant will return a JSON encoded token: |
---|
| 30 | $this->expectOutputRegex('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/'); |
---|
| 31 | $this->fixture = new OAuth2($mockStorage); |
---|
| 32 | $this->fixture->grantAccessToken($inputData, array()); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Tests OAuth2->grantAccessToken() with successful Auth code grant, but without redreict_uri in the input |
---|
| 37 | */ |
---|
| 38 | public function testGrantAccessTokenWithGrantAuthCodeSuccessWithoutRedirect() { |
---|
| 39 | $inputData = array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo'); |
---|
| 40 | $storedToken = array('redirect_uri' => 'http://www.example.com', 'client_id' => 'my_little_app', 'expires' => time() + 60); |
---|
| 41 | |
---|
| 42 | $mockStorage = $this->createBaseMock('IOAuth2GrantCode'); |
---|
| 43 | $mockStorage->expects($this->any()) |
---|
| 44 | ->method('getAuthCode') |
---|
| 45 | ->will($this->returnValue($storedToken)); |
---|
| 46 | |
---|
| 47 | // Successful token grant will return a JSON encoded token: |
---|
| 48 | $this->expectOutputRegex('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/'); |
---|
| 49 | $this->fixture = new OAuth2($mockStorage); |
---|
| 50 | $this->fixture->setVariable(OAuth2::CONFIG_ENFORCE_INPUT_REDIRECT, false); |
---|
| 51 | $this->fixture->grantAccessToken($inputData, array()); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // Utility methods |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * |
---|
| 58 | * @param string $interfaceName |
---|
| 59 | */ |
---|
| 60 | protected function createBaseMock($interfaceName) { |
---|
| 61 | $mockStorage = $this->getMock($interfaceName); |
---|
| 62 | $mockStorage->expects($this->any()) |
---|
| 63 | ->method('checkClientCredentials') |
---|
| 64 | ->will($this->returnValue(TRUE)); // Always return true for any combination of user/pass |
---|
| 65 | $mockStorage->expects($this->any()) |
---|
| 66 | ->method('checkRestrictedGrantType') |
---|
| 67 | ->will($this->returnValue(TRUE)); // Always return true for any combination of user/pass |
---|
| 68 | |
---|
| 69 | return $mockStorage; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | } |
---|