PluginProbe
Authorizer / 3.13.0
Authorizer v3.13.0
3.15.3 3.15.2 3.15.1 3.15.0 3.14.3 3.14.4 3.14.2 3.14.1 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.10 2.9.11 2.9.12 2.9.13 2.9.2 2.9.3 2.9.6 All 126 releases
authorizer / vendor / league / oauth2-github / test / src / Provider / GithubTest.php

GithubTest.php in Authorizer 3.13.0, at vendor/league/oauth2-github/test/src/Provider/GithubTest.php

216 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace League\OAuth2\Client\Test\Provider;
2
3 use Mockery as m;
4
5 class GithubTest extends \PHPUnit_Framework_TestCase
6 {
7 protected $provider;
8
9 protected function setUp()
10 {
11 $this->provider = new \League\OAuth2\Client\Provider\Github([
12 'clientId' => 'mock_client_id',
13 'clientSecret' => 'mock_secret',
14 'redirectUri' => 'none',
15 ]);
16 }
17
18 public function tearDown()
19 {
20 m::close();
21 parent::tearDown();
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
40 public function testScopes()
41 {
42 $options = ['scope' => [uniqid(),uniqid()]];
43
44 $url = $this->provider->getAuthorizationUrl($options);
45
46 $this->assertContains(urlencode(implode(',', $options['scope'])), $url);
47 }
48
49 public function testGetAuthorizationUrl()
50 {
51 $url = $this->provider->getAuthorizationUrl();
52 $uri = parse_url($url);
53
54 $this->assertEquals('/login/oauth/authorize', $uri['path']);
55 }
56
57 public function testGetBaseAccessTokenUrl()
58 {
59 $params = [];
60
61 $url = $this->provider->getBaseAccessTokenUrl($params);
62 $uri = parse_url($url);
63
64 $this->assertEquals('/login/oauth/access_token', $uri['path']);
65 }
66
67 public function testGetAccessToken()
68 {
69 $response = m::mock('Psr\Http\Message\ResponseInterface');
70 $response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "scope":"repo,gist", "token_type":"bearer"}');
71 $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
72 $response->shouldReceive('getStatusCode')->andReturn(200);
73
74 $client = m::mock('GuzzleHttp\ClientInterface');
75 $client->shouldReceive('send')->times(1)->andReturn($response);
76 $this->provider->setHttpClient($client);
77
78 $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
79
80 $this->assertEquals('mock_access_token', $token->getToken());
81 $this->assertNull($token->getExpires());
82 $this->assertNull($token->getRefreshToken());
83 $this->assertNull($token->getResourceOwnerId());
84 }
85
86 public function testGithubEnterpriseDomainUrls()
87 {
88 $this->provider->domain = 'https://github.company.com';
89
90
91 $response = m::mock('Psr\Http\Message\ResponseInterface');
92 $response->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}');
93 $response->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
94 $response->shouldReceive('getStatusCode')->andReturn(200);
95
96 $client = m::mock('GuzzleHttp\ClientInterface');
97 $client->shouldReceive('send')->times(1)->andReturn($response);
98 $this->provider->setHttpClient($client);
99
100 $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
101
102 $this->assertEquals($this->provider->domain.'/login/oauth/authorize', $this->provider->getBaseAuthorizationUrl());
103 $this->assertEquals($this->provider->domain.'/login/oauth/access_token', $this->provider->getBaseAccessTokenUrl([]));
104 $this->assertEquals($this->provider->domain.'/api/v3/user', $this->provider->getResourceOwnerDetailsUrl($token));
105 //$this->assertEquals($this->provider->domain.'/api/v3/user/emails', $this->provider->urlUserEmails($token));
106 }
107
108 public function testUserData()
109 {
110 $userId = rand(1000,9999);
111 $name = uniqid();
112 $nickname = uniqid();
113 $email = uniqid();
114
115 $postResponse = m::mock('Psr\Http\Message\ResponseInterface');
116 $postResponse->shouldReceive('getBody')->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}');
117 $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
118 $postResponse->shouldReceive('getStatusCode')->andReturn(200);
119
120 $userResponse = m::mock('Psr\Http\Message\ResponseInterface');
121 $userResponse->shouldReceive('getBody')->andReturn('{"id": '.$userId.', "login": "'.$nickname.'", "name": "'.$name.'", "email": "'.$email.'"}');
122 $userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
123 $userResponse->shouldReceive('getStatusCode')->andReturn(200);
124
125 $client = m::mock('GuzzleHttp\ClientInterface');
126 $client->shouldReceive('send')
127 ->times(2)
128 ->andReturn($postResponse, $userResponse);
129 $this->provider->setHttpClient($client);
130
131 $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
132 $user = $this->provider->getResourceOwner($token);
133
134 $this->assertEquals($userId, $user->getId());
135 $this->assertEquals($userId, $user->toArray()['id']);
136 $this->assertEquals($name, $user->getName());
137 $this->assertEquals($name, $user->toArray()['name']);
138 $this->assertEquals($nickname, $user->getNickname());
139 $this->assertEquals($nickname, $user->toArray()['login']);
140 $this->assertEquals($email, $user->getEmail());
141 $this->assertEquals($email, $user->toArray()['email']);
142 $this->assertContains($nickname, $user->getUrl());
143 }
144
145 public function testUserEmails()
146 {
147 /*
148 $userId = rand(1000,9999);
149 $name = uniqid();
150 $nickname = uniqid();
151 $email = uniqid();
152
153 $postResponse = m::mock('Psr\Http\Message\ResponseInterface');
154 $postResponse->shouldReceive('getBody')->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}');
155 $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
156
157 $userResponse = m::mock('Psr\Http\Message\ResponseInterface');
158 $userResponse->shouldReceive('getBody')->andReturn('[{"email":"mock_email_1","primary":false,"verified":true},{"email":"mock_email_2","primary":false,"verified":true},{"email":"mock_email_3","primary":true,"verified":true}]');
159 $userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
160
161 $client = m::mock('GuzzleHttp\ClientInterface');
162 $client->shouldReceive('send')
163 ->times(2)
164 ->andReturn($postResponse, $userResponse);
165 $this->provider->setHttpClient($client);
166
167 $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
168 $emails = $this->provider->getUserEmails($token);
169
170 $this->assertEquals($userId, $user->getUserId());
171 $this->assertEquals($name, $user->getName());
172 $this->assertEquals($nickname, $user->getNickname());
173 $this->assertEquals($email, $user->getEmail());
174 $this->assertContains($nickname, $user->getUrl());
175 */
176 }
177
178 /**
179 * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
180 **/
181 public function testExceptionThrownWhenErrorObjectReceived()
182 {
183 $status = rand(400,600);
184 $postResponse = m::mock('Psr\Http\Message\ResponseInterface');
185 $postResponse->shouldReceive('getBody')->andReturn('{"message": "Validation Failed","errors": [{"resource": "Issue","field": "title","code": "missing_field"}]}');
186 $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
187 $postResponse->shouldReceive('getStatusCode')->andReturn($status);
188
189 $client = m::mock('GuzzleHttp\ClientInterface');
190 $client->shouldReceive('send')
191 ->times(1)
192 ->andReturn($postResponse);
193 $this->provider->setHttpClient($client);
194 $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
195 }
196
197 /**
198 * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
199 **/
200 public function testExceptionThrownWhenOAuthErrorReceived()
201 {
202 $status = 200;
203 $postResponse = m::mock('Psr\Http\Message\ResponseInterface');
204 $postResponse->shouldReceive('getBody')->andReturn('{"error": "bad_verification_code","error_description": "The code passed is incorrect or expired.","error_uri": "https://developer.github.com/v3/oauth/#bad-verification-code"}');
205 $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
206 $postResponse->shouldReceive('getStatusCode')->andReturn($status);
207
208 $client = m::mock('GuzzleHttp\ClientInterface');
209 $client->shouldReceive('send')
210 ->times(1)
211 ->andReturn($postResponse);
212 $this->provider->setHttpClient($client);
213 $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
214 }
215 }
216