ApiClientTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class ApiClientTest extends BaseTest
  21. {
  22. public function testClient()
  23. {
  24. $client = new Google_Client();
  25. $client->setAccessType('foo');
  26. $client->setDeveloperKey('foo');
  27. $req = new Google_Http_Request('http://foo.com');
  28. $client->getAuth()->sign($req);
  29. $params = $req->getQueryParams();
  30. $this->assertEquals('foo', $params['key']);
  31. $client->setAccessToken(json_encode(array('access_token' => '1')));
  32. $this->assertEquals("{\"access_token\":\"1\"}", $client->getAccessToken());
  33. }
  34. public function testClientConstructor()
  35. {
  36. $this->assertInstanceOf('Google_Client', $this->getClient());
  37. }
  38. /**
  39. * @expectedException Google_Auth_Exception
  40. */
  41. public function testPrepareInvalidScopes()
  42. {
  43. $client = new Google_Client();
  44. $scopes = $client->prepareScopes();
  45. $this->assertEquals("", $scopes);
  46. }
  47. public function testNoAuthIsNull()
  48. {
  49. $client = new Google_Client();
  50. $this->assertNull($client->getAccessToken());
  51. }
  52. public function testPrepareService()
  53. {
  54. $client = new Google_Client();
  55. $client->setScopes(array("scope1", "scope2"));
  56. $scopes = $client->prepareScopes();
  57. $this->assertEquals("scope1 scope2", $scopes);
  58. $client->setScopes(array("", "scope2"));
  59. $scopes = $client->prepareScopes();
  60. $this->assertEquals(" scope2", $scopes);
  61. $client->setScopes("scope2");
  62. $client->addScope("scope3");
  63. $client->addScope(array("scope4", "scope5"));
  64. $scopes = $client->prepareScopes();
  65. $this->assertEquals("scope2 scope3 scope4 scope5", $scopes);
  66. $client->setClientId('test1');
  67. $client->setRedirectUri('http://localhost/');
  68. $client->setScopes(array("http://test.com", "scope2"));
  69. $scopes = $client->prepareScopes();
  70. $this->assertEquals("http://test.com scope2", $scopes);
  71. $this->assertEquals(
  72. ''
  73. . 'https://accounts.google.com/o/oauth2/auth'
  74. . '?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F'
  75. . '&client_id=test1'
  76. . '&scope=http%3A%2F%2Ftest.com+scope2&access_type=online'
  77. . '&approval_prompt=auto',
  78. $client->createAuthUrl()
  79. );
  80. // This should not trigger a request.
  81. $client->setDefer(true);
  82. $dr_service = new Google_Service_Drive($client);
  83. $this->assertInstanceOf('Google_Http_Request', $dr_service->files->listFiles());
  84. }
  85. public function testSettersGetters()
  86. {
  87. $client = new Google_Client();
  88. $client->setClientId("client1");
  89. $client->setClientSecret('client1secret');
  90. $client->setState('1');
  91. $client->setApprovalPrompt('force');
  92. $client->setAccessType('offline');
  93. $client->setRedirectUri('localhost');
  94. $client->setApplicationName('me');
  95. $this->assertEquals('object', gettype($client->getAuth()));
  96. $this->assertEquals('object', gettype($client->getCache()));
  97. $this->assertEquals('object', gettype($client->getIo()));
  98. $client->setAuth(new Google_Auth_Simple($client));
  99. $client->setAuth(new Google_Auth_OAuth2($client));
  100. try {
  101. $client->setAccessToken(null);
  102. die('Should have thrown an Google_Auth_Exception.');
  103. } catch (Google_Auth_Exception $e) {
  104. $this->assertEquals('Could not json decode the token', $e->getMessage());
  105. }
  106. $token = json_encode(array('access_token' => 'token'));
  107. $client->setAccessToken($token);
  108. $this->assertEquals($token, $client->getAccessToken());
  109. }
  110. /**
  111. * @requires extension Memcached
  112. */
  113. public function testAppEngineAutoConfig()
  114. {
  115. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  116. $client = new Google_Client();
  117. $this->assertInstanceOf('Google_Cache_Memcache', $client->getCache());
  118. $this->assertInstanceOf('Google_Io_Stream', $client->getIo());
  119. unset($_SERVER['SERVER_SOFTWARE']);
  120. }
  121. public function testJsonConfig()
  122. {
  123. // Device config
  124. $config = new Google_Config();
  125. $client = new Google_Client($config);
  126. $device =
  127. '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"'.
  128. ':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://accounts.google.com/o/oauth2/token",'.
  129. '"client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url"'.
  130. ':"","client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'.
  131. '"https://www.googleapis.com/oauth2/v1/certs"}}';
  132. $dObj = json_decode($device);
  133. $client->setAuthConfig($device);
  134. $cfg = $config->getClassConfig('Google_Auth_OAuth2');
  135. $this->assertEquals($cfg['client_id'], $dObj->installed->client_id);
  136. $this->assertEquals($cfg['client_secret'], $dObj->installed->client_secret);
  137. $this->assertEquals($cfg['redirect_uri'], $dObj->installed->redirect_uris[0]);
  138. // Web config
  139. $config = new Google_Config();
  140. $client = new Google_Client($config);
  141. $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"' .
  142. ':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://accounts.google.com/o/oauth2/token"' .
  143. ',"client_email":"123456789@developer.gserviceaccount.com","client_x509_cert_url":'.
  144. '"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com"'.
  145. ',"client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'.
  146. '"https://www.googleapis.com/oauth2/v1/certs"}}';
  147. $wObj = json_decode($web);
  148. $client->setAuthConfig($web);
  149. $cfg = $config->getClassConfig('Google_Auth_OAuth2');
  150. $this->assertEquals($cfg['client_id'], $wObj->web->client_id);
  151. $this->assertEquals($cfg['client_secret'], $wObj->web->client_secret);
  152. $this->assertEquals($cfg['redirect_uri'], '');
  153. }
  154. public function testIniConfig()
  155. {
  156. $config = new Google_Config(__DIR__ . "/testdata/test.ini");
  157. $this->assertEquals('My Test application', $config->getApplicationName());
  158. $this->assertEquals(
  159. 'gjfiwnGinpena3',
  160. $config->getClassConfig('Google_Auth_OAuth2', 'client_secret')
  161. );
  162. $this->assertInternalType(
  163. 'array',
  164. $config->getClassConfig('Google_IO_Abstract')
  165. );
  166. $this->assertEquals(
  167. 100,
  168. $config->getClassConfig('Google_IO_Abstract', 'request_timeout_seconds')
  169. );
  170. }
  171. public function testServiceAccountJson()
  172. {
  173. $client = new Google_Client();
  174. $c = $client->loadServiceAccountJson(
  175. __DIR__ . "/testdata/service-12345.json",
  176. array()
  177. );
  178. $this->assertInstanceOf('Google_Auth_AssertionCredentials', $c);
  179. }
  180. public function testRsaServiceAccountJson()
  181. {
  182. $client = new Google_Client();
  183. $c = $client->loadServiceAccountJson(
  184. __DIR__ . "/testdata/service-rsa-12345.json",
  185. array()
  186. );
  187. $this->assertInstanceOf('Google_Auth_AssertionCredentials', $c);
  188. }
  189. }