TokenRequestTest.php (9385B)
1 <?php 2 3 /** 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 namespace fkooman\OAuth\Client; 19 20 use Guzzle\Http\Client; 21 use Guzzle\Plugin\History\HistoryPlugin; 22 use Guzzle\Plugin\Mock\MockPlugin; 23 use Guzzle\Http\Message\Response; 24 use fkooman\OAuth\Common\Scope; 25 26 class TokenRequestTest extends \PHPUnit_Framework_TestCase 27 { 28 /** @var array */ 29 private $clientConfig; 30 31 /** @var array */ 32 private $tokenResponse; 33 34 public function setUp() 35 { 36 $this->clientConfig = array(); 37 $this->tokenResponse = array(); 38 39 $this->clientConfig[] = new ClientConfig( 40 array( 41 "client_id" => "foo", 42 "client_secret" => "bar", 43 "authorize_endpoint" => "http://www.example.org/authorize", 44 "token_endpoint" => "http://www.example.org/token", 45 ) 46 ); 47 48 $this->clientConfig[] = new ClientConfig( 49 array( 50 "client_id" => "foo", 51 "client_secret" => "bar", 52 "authorize_endpoint" => "http://www.example.org/authorize", 53 "token_endpoint" => "http://www.example.org/token", 54 "redirect_uri" => "http://foo.example.org/callback", 55 "credentials_in_request_body" => true, 56 ) 57 ); 58 59 $this->clientConfig[] = new ClientConfig( 60 array( 61 "client_id" => "foo", 62 "client_secret" => "bar", 63 "authorize_endpoint" => "http://www.example.org/authorize", 64 "token_endpoint" => "http://www.example.org/token", 65 "redirect_uri" => "http://foo.example.org/callback", 66 "allow_string_expires_in" => true, 67 ) 68 ); 69 70 $this->clientConfig[] = new ClientConfig( 71 array( 72 "client_id" => "foo", 73 "client_secret" => "bar", 74 "authorize_endpoint" => "http://www.example.org/authorize", 75 "token_endpoint" => "http://www.example.org/token", 76 "redirect_uri" => "http://foo.example.org/callback", 77 "use_array_scope" => true, 78 ) 79 ); 80 81 $this->clientConfig[] = new ClientConfig( 82 array( 83 "client_id" => "foo", 84 "client_secret" => "bar", 85 "authorize_endpoint" => "http://www.example.org/authorize", 86 "token_endpoint" => "http://www.example.org/token", 87 "redirect_uri" => "http://foo.example.org/callback", 88 "use_comma_separated_scope" => true, 89 ) 90 ); 91 92 $this->tokenResponse[] = json_encode( 93 array( 94 "access_token" => "foo", 95 "token_type" => "Bearer", 96 ) 97 ); 98 99 $this->tokenResponse[] = "{"; 100 101 $this->tokenResponse[] = json_encode( 102 array( 103 "access_token" => "foo", 104 "token_type" => "Bearer", 105 "expires_in" => "1200", 106 ) 107 ); 108 109 $this->tokenResponse[] = json_encode( 110 array( 111 "access_token" => "foo", 112 "token_type" => "Bearer", 113 "scope" => array("foo", "bar"), 114 ) 115 ); 116 117 $this->tokenResponse[] = json_encode( 118 array( 119 "access_token" => "foo", 120 "token_type" => "Bearer", 121 "scope" => "foo,bar", 122 ) 123 ); 124 } 125 126 public function testWithAuthorizationCode() 127 { 128 $client = new Client(); 129 $mock = new MockPlugin(); 130 $mock->addResponse(new Response(200, null, $this->tokenResponse[0])); 131 $client->addSubscriber($mock); 132 $history = new HistoryPlugin(); 133 $history->setLimit(5); 134 $client->addSubscriber($history); 135 $tokenRequest = new TokenRequest($client, $this->clientConfig[0]); 136 $tokenRequest->withAuthorizationCode("12345"); 137 $lastRequest = $history->getLastRequest(); 138 $this->assertEquals("POST", $lastRequest->getMethod()); 139 $this->assertEquals("code=12345&grant_type=authorization_code", $lastRequest->getPostFields()->__toString()); 140 $this->assertEquals("Basic Zm9vOmJhcg==", $lastRequest->getHeader("Authorization")); 141 $this->assertEquals( 142 "application/x-www-form-urlencoded; charset=utf-8", 143 $lastRequest->getHeader("Content-Type") 144 ); 145 } 146 147 public function testWithAuthorizationCodeCredentialsInRequestBody() 148 { 149 $client = new Client(); 150 $mock = new MockPlugin(); 151 $mock->addResponse(new Response(200, null, $this->tokenResponse[0])); 152 $client->addSubscriber($mock); 153 $history = new HistoryPlugin(); 154 $history->setLimit(5); 155 $client->addSubscriber($history); 156 $tokenRequest = new TokenRequest($client, $this->clientConfig[1]); 157 $tokenRequest->withAuthorizationCode("12345"); 158 $lastRequest = $history->getLastRequest(); 159 $this->assertEquals("POST", $lastRequest->getMethod()); 160 $this->assertEquals( 161 "code=12345&grant_type=authorization_code&redirect_uri=http%3A%2F%2Ffoo.example.org%2Fcallback&client_id=foo&client_secret=bar", 162 $lastRequest->getPostFields()->__toString() 163 ); 164 $this->assertEquals( 165 "application/x-www-form-urlencoded; charset=utf-8", 166 $lastRequest->getHeader("Content-Type") 167 ); 168 } 169 170 public function testAllowStringExpiresIn() 171 { 172 $client = new Client(); 173 $mock = new MockPlugin(); 174 $mock->addResponse(new Response(200, null, $this->tokenResponse[2])); 175 $client->addSubscriber($mock); 176 $history = new HistoryPlugin(); 177 $history->setLimit(5); 178 $client->addSubscriber($history); 179 $tokenRequest = new TokenRequest($client, $this->clientConfig[2]); 180 $tokenResponse = $tokenRequest->withAuthorizationCode("12345"); 181 $this->assertEquals(1200, $tokenResponse->getExpiresIn()); 182 } 183 184 public function testAllowArrayScope() 185 { 186 $client = new Client(); 187 $mock = new MockPlugin(); 188 $mock->addResponse(new Response(200, null, $this->tokenResponse[3])); 189 $client->addSubscriber($mock); 190 $history = new HistoryPlugin(); 191 $history->setLimit(5); 192 $client->addSubscriber($history); 193 $tokenRequest = new TokenRequest($client, $this->clientConfig[3]); 194 $tokenResponse = $tokenRequest->withAuthorizationCode("12345"); 195 $this->assertTrue($tokenResponse->getScope()->equals(Scope::fromString("foo bar"))); 196 } 197 198 public function testAllowCommaSeparatedScope() 199 { 200 $client = new Client(); 201 $mock = new MockPlugin(); 202 $mock->addResponse(new Response(200, null, $this->tokenResponse[4])); 203 $client->addSubscriber($mock); 204 $history = new HistoryPlugin(); 205 $history->setLimit(5); 206 $client->addSubscriber($history); 207 $tokenRequest = new TokenRequest($client, $this->clientConfig[4]); 208 $tokenResponse = $tokenRequest->withAuthorizationCode("12345"); 209 $this->assertTrue($tokenResponse->getScope()->equals(Scope::fromString("foo bar"))); 210 } 211 212 public function testWithRefreshToken() 213 { 214 $client = new Client(); 215 $mock = new MockPlugin(); 216 $mock->addResponse(new Response(200, null, $this->tokenResponse[0])); 217 $client->addSubscriber($mock); 218 $history = new HistoryPlugin(); 219 $history->setLimit(5); 220 $client->addSubscriber($history); 221 $tokenRequest = new TokenRequest($client, $this->clientConfig[0]); 222 $tokenRequest->withRefreshToken("refresh_123_456"); 223 $lastRequest = $history->getLastRequest(); 224 $this->assertEquals("POST", $lastRequest->getMethod()); 225 $this->assertEquals("Basic Zm9vOmJhcg==", $lastRequest->getHeader("Authorization")); 226 $this->assertEquals( 227 "refresh_token=refresh_123_456&grant_type=refresh_token", 228 $lastRequest->getPostFields()->__toString() 229 ); 230 $this->assertEquals( 231 "application/x-www-form-urlencoded; charset=utf-8", 232 $lastRequest->getHeader("Content-Type") 233 ); 234 } 235 236 public function testBrokenJsonResponse() 237 { 238 $client = new Client(); 239 $mock = new MockPlugin(); 240 $mock->addResponse(new Response(200, null, $this->tokenResponse[1])); 241 $client->addSubscriber($mock); 242 $history = new HistoryPlugin(); 243 $history->setLimit(5); 244 $client->addSubscriber($history); 245 $tokenRequest = new TokenRequest($client, $this->clientConfig[0]); 246 $this->assertFalse($tokenRequest->withRefreshToken("refresh_123_456")); 247 } 248 }