TrelloServerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php namespace League\OAuth1\Client\Tests;
  2. use InvalidArgumentException;
  3. use League\OAuth1\Client\Credentials\ClientCredentials;
  4. use League\OAuth1\Client\Server\Trello;
  5. use Mockery as m;
  6. use PHPUnit\Framework\TestCase;
  7. use Psr\Http\Message\ResponseInterface;
  8. class TrelloTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. m::close();
  13. parent::tearDown();
  14. }
  15. public function testCreatingWithArray()
  16. {
  17. $server = new Trello($this->getMockClientCredentials());
  18. $credentials = $server->getClientCredentials();
  19. $this->assertInstanceOf('League\OAuth1\Client\Credentials\ClientCredentialsInterface', $credentials);
  20. $this->assertEquals($this->getApplicationKey(), $credentials->getIdentifier());
  21. $this->assertEquals('mysecret', $credentials->getSecret());
  22. $this->assertEquals('http://app.dev/', $credentials->getCallbackUri());
  23. }
  24. public function testCreatingWithObject()
  25. {
  26. $credentials = new ClientCredentials;
  27. $credentials->setIdentifier('myidentifier');
  28. $credentials->setSecret('mysecret');
  29. $credentials->setCallbackUri('http://app.dev/');
  30. $server = new Trello($credentials);
  31. $this->assertEquals($credentials, $server->getClientCredentials());
  32. }
  33. public function testGettingTemporaryCredentials()
  34. {
  35. $server = m::mock('League\OAuth1\Client\Server\Trello[createHttpClient]', [$this->getMockClientCredentials()]);
  36. $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
  37. $me = $this;
  38. $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetRequestToken', m::on(function ($options) use ($me) {
  39. $headers = $options['headers'];
  40. $me->assertTrue(isset($headers['Authorization']));
  41. // OAuth protocol specifies a strict number of
  42. // headers should be sent, in the correct order.
  43. // We'll validate that here.
  44. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="' . preg_quote('http%3A%2F%2Fapp.dev%2F', '/') . '", oauth_signature=".*?"/';
  45. $matches = preg_match($pattern, $headers['Authorization']);
  46. $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  47. return true;
  48. }))->once()->andReturn($response = m::mock(ResponseInterface::class));
  49. $response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true');
  50. $credentials = $server->getTemporaryCredentials();
  51. $this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials);
  52. $this->assertEquals('temporarycredentialsidentifier', $credentials->getIdentifier());
  53. $this->assertEquals('temporarycredentialssecret', $credentials->getSecret());
  54. }
  55. public function testGettingDefaultAuthorizationUrl()
  56. {
  57. $server = new Trello($this->getMockClientCredentials());
  58. $expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=1day&oauth_token=foo';
  59. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  60. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  61. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  62. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  63. }
  64. public function testGettingAuthorizationUrlWithExpirationAfterConstructingWithExpiration()
  65. {
  66. $credentials = $this->getMockClientCredentials();
  67. $expiration = $this->getApplicationExpiration(2);
  68. $credentials['expiration'] = $expiration;
  69. $server = new Trello($credentials);
  70. $expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=' . urlencode($expiration) . '&oauth_token=foo';
  71. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  72. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  73. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  74. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  75. }
  76. public function testGettingAuthorizationUrlWithExpirationAfterSettingExpiration()
  77. {
  78. $expiration = $this->getApplicationExpiration(2);
  79. $server = new Trello($this->getMockClientCredentials());
  80. $server->setApplicationExpiration($expiration);
  81. $expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=' . urlencode($expiration) . '&oauth_token=foo';
  82. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  83. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  84. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  85. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  86. }
  87. public function testGettingAuthorizationUrlWithNameAfterConstructingWithName()
  88. {
  89. $credentials = $this->getMockClientCredentials();
  90. $name = $this->getApplicationName();
  91. $credentials['name'] = $name;
  92. $server = new Trello($credentials);
  93. $expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=1day&name=' . urlencode($name) . '&oauth_token=foo';
  94. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  95. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  96. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  97. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  98. }
  99. public function testGettingAuthorizationUrlWithNameAfterSettingName()
  100. {
  101. $name = $this->getApplicationName();
  102. $server = new Trello($this->getMockClientCredentials());
  103. $server->setApplicationName($name);
  104. $expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=1day&name=' . urlencode($name) . '&oauth_token=foo';
  105. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  106. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  107. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  108. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  109. }
  110. public function testGettingAuthorizationUrlWithScopeAfterConstructingWithScope()
  111. {
  112. $credentials = $this->getMockClientCredentials();
  113. $scope = $this->getApplicationScope(false);
  114. $credentials['scope'] = $scope;
  115. $server = new Trello($credentials);
  116. $expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=' . urlencode($scope) . '&expiration=1day&oauth_token=foo';
  117. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  118. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  119. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  120. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  121. }
  122. public function testGettingAuthorizationUrlWithScopeAfterSettingScope()
  123. {
  124. $scope = $this->getApplicationScope(false);
  125. $server = new Trello($this->getMockClientCredentials());
  126. $server->setApplicationScope($scope);
  127. $expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=' . urlencode($scope) . '&expiration=1day&oauth_token=foo';
  128. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  129. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  130. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  131. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  132. }
  133. public function testGettingTokenCredentialsFailsWithManInTheMiddle()
  134. {
  135. $server = new Trello($this->getMockClientCredentials());
  136. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  137. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  138. $this->expectException(InvalidArgumentException::class);
  139. $server->getTokenCredentials($credentials, 'bar', 'verifier');
  140. }
  141. public function testGettingTokenCredentials()
  142. {
  143. $server = m::mock('League\OAuth1\Client\Server\Trello[createHttpClient]', [$this->getMockClientCredentials()]);
  144. $temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  145. $temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
  146. $temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
  147. $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
  148. $me = $this;
  149. $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetAccessToken', m::on(function ($options) use ($me) {
  150. $headers = $options['headers'];
  151. $body = $options['form_params'];
  152. $me->assertTrue(isset($headers['Authorization']));
  153. // OAuth protocol specifies a strict number of
  154. // headers should be sent, in the correct order.
  155. // We'll validate that here.
  156. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/';
  157. $matches = preg_match($pattern, $headers['Authorization']);
  158. $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  159. $me->assertSame($body, ['oauth_verifier' => 'myverifiercode']);
  160. return true;
  161. }))->once()->andReturn($response = m::mock(ResponseInterface::class));
  162. $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
  163. $credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
  164. $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
  165. $this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
  166. $this->assertEquals('tokencredentialssecret', $credentials->getSecret());
  167. }
  168. public function testGettingUserDetails()
  169. {
  170. $server = m::mock('League\OAuth1\Client\Server\Trello[createHttpClient,protocolHeader]', [$this->getMockClientCredentials()]);
  171. $temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TokenCredentials');
  172. $temporaryCredentials->shouldReceive('getIdentifier')->andReturn('tokencredentialsidentifier');
  173. $temporaryCredentials->shouldReceive('getSecret')->andReturn('tokencredentialssecret');
  174. $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
  175. $me = $this;
  176. $client->shouldReceive('get')->with('https://trello.com/1/members/me?key=' . $this->getApplicationKey() . '&token=' . $this->getAccessToken(), m::on(function ($options) use ($me) {
  177. $headers = $options['headers'];
  178. $me->assertTrue(isset($headers['Authorization']));
  179. // OAuth protocol specifies a strict number of
  180. // headers should be sent, in the correct order.
  181. // We'll validate that here.
  182. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/';
  183. $matches = preg_match($pattern, $headers['Authorization']);
  184. $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  185. return true;
  186. }))->once()->andReturn($response = m::mock(ResponseInterface::class));
  187. $response->shouldReceive('getBody')->once()->andReturn($this->getUserPayload());
  188. $user = $server
  189. ->setAccessToken($this->getAccessToken())
  190. ->getUserDetails($temporaryCredentials);
  191. $this->assertInstanceOf('League\OAuth1\Client\Server\User', $user);
  192. $this->assertEquals('Matilda Wormwood', $user->name);
  193. $this->assertEquals('545df696e29c0dddaed31967', $server->getUserUid($temporaryCredentials));
  194. $this->assertEquals(null, $server->getUserEmail($temporaryCredentials));
  195. $this->assertEquals('matildawormwood12', $server->getUserScreenName($temporaryCredentials));
  196. }
  197. protected function getMockClientCredentials()
  198. {
  199. return [
  200. 'identifier' => $this->getApplicationKey(),
  201. 'secret' => 'mysecret',
  202. 'callback_uri' => 'http://app.dev/',
  203. ];
  204. }
  205. protected function getAccessToken()
  206. {
  207. return 'lmnopqrstuvwxyz';
  208. }
  209. protected function getApplicationKey()
  210. {
  211. return 'abcdefghijk';
  212. }
  213. protected function getApplicationExpiration($days = 0)
  214. {
  215. return is_numeric($days) && $days > 0 ? $days . 'day' . ($days == 1 ? '' : 's') : 'never';
  216. }
  217. protected function getApplicationName()
  218. {
  219. return 'fizz buzz';
  220. }
  221. protected function getApplicationScope($readonly = true)
  222. {
  223. return $readonly ? 'read' : 'read,write';
  224. }
  225. private function getUserPayload()
  226. {
  227. return '{
  228. "id": "545df696e29c0dddaed31967",
  229. "avatarHash": null,
  230. "bio": "I have magical powers",
  231. "bioData": null,
  232. "confirmed": true,
  233. "fullName": "Matilda Wormwood",
  234. "idPremOrgsAdmin": [],
  235. "initials": "MW",
  236. "memberType": "normal",
  237. "products": [],
  238. "status": "idle",
  239. "url": "https://trello.com/matildawormwood12",
  240. "username": "matildawormwood12",
  241. "avatarSource": "none",
  242. "email": null,
  243. "gravatarHash": "39aaaada0224f26f0bb8f1965326dcb7",
  244. "idBoards": [
  245. "545df696e29c0dddaed31968",
  246. "545e01d6c7b2dd962b5b46cb"
  247. ],
  248. "idOrganizations": [
  249. "54adfd79f9aea14f84009a85",
  250. "54adfde13b0e706947bc4789"
  251. ],
  252. "loginTypes": null,
  253. "oneTimeMessagesDismissed": [],
  254. "prefs": {
  255. "sendSummaries": true,
  256. "minutesBetweenSummaries": 1,
  257. "minutesBeforeDeadlineToNotify": 1440,
  258. "colorBlind": false,
  259. "timezoneInfo": {
  260. "timezoneNext": "CDT",
  261. "dateNext": "2015-03-08T08:00:00.000Z",
  262. "offsetNext": 300,
  263. "timezoneCurrent": "CST",
  264. "offsetCurrent": 360
  265. }
  266. },
  267. "trophies": [],
  268. "uploadedAvatarHash": null,
  269. "premiumFeatures": [],
  270. "idBoardsPinned": null
  271. }';
  272. }
  273. }