diff --git a/src/Grant/ClientCredentials.php b/src/Grant/ClientCredentials.php new file mode 100644 index 0000000..7a73a79 --- /dev/null +++ b/src/Grant/ClientCredentials.php @@ -0,0 +1,25 @@ +provider = new \League\OAuth2\Client\Provider\Google(array( + 'clientId' => 'mock_client_id', + 'clientSecret' => 'mock_secret', + 'redirectUri' => 'none', + )); + } + + public function testGetAccessToken() + { + $response = m::mock('Guzzle\Http\Message\Response'); + $response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + + $client = m::mock('Guzzle\Service\Client'); + $client->shouldReceive('setBaseUrl')->times(1); + $client->shouldReceive('post->send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('client_credentials'); + $this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $token); + + $grant = new \League\OAuth2\Client\Grant\ClientCredentials(); + $this->assertEquals('client_credentials', (string) $grant); + } +} diff --git a/test/src/Grant/PasswordTest.php b/test/src/Grant/PasswordTest.php new file mode 100644 index 0000000..4070b12 --- /dev/null +++ b/test/src/Grant/PasswordTest.php @@ -0,0 +1,68 @@ +provider = new \League\OAuth2\Client\Provider\Google(array( + 'clientId' => 'mock_client_id', + 'clientSecret' => 'mock_secret', + 'redirectUri' => 'none', + )); + } + + public function testGetAccessToken() + { + $response = m::mock('Guzzle\Http\Message\Response'); + $response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + + $client = m::mock('Guzzle\Service\Client'); + $client->shouldReceive('setBaseUrl')->times(1); + $client->shouldReceive('post->send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('password', array('username' => 'mock_username', 'password' => 'mock_password')); + $this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $token); + + $grant = new \League\OAuth2\Client\Grant\Password(); + $this->assertEquals('password', (string) $grant); + } + + /** + * @expectedException BadMethodCallException + */ + public function testInvalidUsername() + { + $response = m::mock('Guzzle\Http\Message\Response'); + $response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + + $client = m::mock('Guzzle\Service\Client'); + $client->shouldReceive('setBaseUrl')->times(1); + $client->shouldReceive('post->send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $this->provider->getAccessToken('password', array('invalid_username' => 'mock_username', 'password' => 'mock_password')); + } + + /** + * @expectedException BadMethodCallException + */ + public function testInvalidPassword() + { + $response = m::mock('Guzzle\Http\Message\Response'); + $response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + + $client = m::mock('Guzzle\Service\Client'); + $client->shouldReceive('setBaseUrl')->times(1); + $client->shouldReceive('post->send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $this->provider->getAccessToken('password', array('username' => 'mock_username', 'invalid_password' => 'mock_password')); + } +}