|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bramdevries\Oauth\Client\Provider; |
| 4 | + |
| 5 | +use League\OAuth2\Client\Token\AccessToken; |
| 6 | +use Mockery as m; |
| 7 | + |
| 8 | +class SlackTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var Slack |
| 12 | + */ |
| 13 | + protected $provider; |
| 14 | + |
| 15 | + protected function setUp() |
| 16 | + { |
| 17 | + $this->provider = new Slack([ |
| 18 | + 'clientId' => 'foo', |
| 19 | + 'clientSecret' => 'bar', |
| 20 | + 'redirectUri' => 'none', |
| 21 | + ]); |
| 22 | + } |
| 23 | + |
| 24 | + public function testAuthorizationUrl() |
| 25 | + { |
| 26 | + $url = $this->provider->getAuthorizationUrl(); |
| 27 | + $uri = parse_url($url); |
| 28 | + parse_str($uri['query'], $query); |
| 29 | + |
| 30 | + $this->assertArrayHasKey('client_id', $query); |
| 31 | + $this->assertArrayHasKey('redirect_uri', $query); |
| 32 | + $this->assertArrayHasKey('state', $query); |
| 33 | + $this->assertArrayHasKey('scope', $query); |
| 34 | + $this->assertArrayHasKey('response_type', $query); |
| 35 | + $this->assertArrayHasKey('approval_prompt', $query); |
| 36 | + $this->assertNotNull($this->provider->getState()); |
| 37 | + } |
| 38 | + |
| 39 | + public function testScopes() |
| 40 | + { |
| 41 | + $options = ['scope' => [uniqid(), uniqid()]]; |
| 42 | + $url = $this->provider->getAuthorizationUrl($options); |
| 43 | + $this->assertContains(urlencode(implode(',', $options['scope'])), $url); |
| 44 | + } |
| 45 | + |
| 46 | + public function testGetAuthorizationUrl() |
| 47 | + { |
| 48 | + $url = $this->provider->getAuthorizationUrl(); |
| 49 | + $uri = parse_url($url); |
| 50 | + $this->assertEquals('/oauth/authorize', $uri['path']); |
| 51 | + } |
| 52 | + |
| 53 | + public function testGetBaseAccessTokenUrl() |
| 54 | + { |
| 55 | + $params = []; |
| 56 | + $url = $this->provider->getBaseAccessTokenUrl($params); |
| 57 | + $uri = parse_url($url); |
| 58 | + $this->assertEquals('/api/oauth.access', $uri['path']); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGetAccessToken() |
| 62 | + { |
| 63 | + $response = m::mock('Psr\Http\Message\ResponseInterface'); |
| 64 | + |
| 65 | + $response->shouldReceive('getBody')->andReturn('{"ok":"true", "scope":"identify,read,post", "access_token": "mock_access_token"}'); |
| 66 | + $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
| 67 | + $response->shouldReceive('getStatusCode')->andReturn(200); |
| 68 | + |
| 69 | + $client = m::mock('GuzzleHttp\ClientInterface'); |
| 70 | + $client->shouldReceive('send')->times(1)->andReturn($response); |
| 71 | + $this->provider->setHttpClient($client); |
| 72 | + |
| 73 | + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); |
| 74 | + $this->assertEquals('mock_access_token', $token->getToken()); |
| 75 | + $this->assertNull($token->getExpires()); |
| 76 | + $this->assertNull($token->getRefreshToken()); |
| 77 | + $this->assertNull($token->getResourceOwnerId()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testUserData() |
| 81 | + { |
| 82 | + $response = m::mock('Psr\Http\Message\ResponseInterface'); |
| 83 | + $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
| 84 | + $response->shouldReceive('getStatusCode')->andReturn(200); |
| 85 | + |
| 86 | + $response->shouldReceive('getBody')->andReturn('{"ok": true, "url": "https:\/\/myteam.slack.com\/", "team": "My Team", "user": "cal", "team_id": "T1234", "user_id": "U1234"}'); |
| 87 | + |
| 88 | + $client = m::mock('GuzzleHttp\ClientInterface'); |
| 89 | + $client->shouldReceive('send')->andReturn($response); |
| 90 | + |
| 91 | + $this->provider->setHttpClient($client); |
| 92 | + $token = m::mock('League\OAuth2\Client\Token\AccessToken'); |
| 93 | + $token->shouldReceive('getToken')->andReturn('foo'); |
| 94 | + |
| 95 | + $user = $this->provider->getResourceOwner($token); |
| 96 | + $this->assertEquals('U1234', $user->getId()); |
| 97 | + $this->assertEquals([ |
| 98 | + 'ok' => true, |
| 99 | + 'url' => 'https://myteam.slack.com/', |
| 100 | + 'team' => 'My Team', |
| 101 | + 'user' => 'cal', |
| 102 | + 'team_id' => 'T1234', |
| 103 | + 'user_id' => 'U1234', |
| 104 | + ], $user->toArray()); |
| 105 | + } |
| 106 | + |
| 107 | + public function testCanThrowException() |
| 108 | + { |
| 109 | + $this->setExpectedException('League\OAuth2\Client\Provider\Exception\IdentityProviderException', 'code_already_used'); |
| 110 | + |
| 111 | + $response = m::mock('Psr\Http\Message\ResponseInterface'); |
| 112 | + |
| 113 | + $response->shouldReceive('getBody')->andReturn('{"ok":false, "error":"code_already_used"}'); |
| 114 | + $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
| 115 | + $response->shouldReceive('getStatusCode')->andReturn(200); |
| 116 | + |
| 117 | + $client = m::mock('GuzzleHttp\ClientInterface'); |
| 118 | + $client->shouldReceive('send')->times(1)->andReturn($response); |
| 119 | + $this->provider->setHttpClient($client); |
| 120 | + |
| 121 | + $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); |
| 122 | + } |
| 123 | +} |
0 commit comments