/library/Google/vendor/google/auth/tests/FetchAuthTokenTest.php

https://gitlab.com/vangtrangbac123/zinkaiuit.tk · PHP · 170 lines · 127 code · 27 blank · 16 comment · 0 complexity · 015d14f17725e0de1962692196dbb1d2 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2010 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth\tests;
  18. use Google\Auth\Credentials\AppIdentityCredentials;
  19. use Google\Auth\Credentials\GCECredentials;
  20. use Google\Auth\Credentials\ServiceAccountCredentials;
  21. use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
  22. use Google\Auth\Credentials\UserRefreshCredentials;
  23. use Google\Auth\CredentialsLoader;
  24. use Google\Auth\FetchAuthTokenInterface;
  25. use Google\Auth\OAuth2;
  26. class FetchAuthTokenTest extends BaseTest
  27. {
  28. /** @dataProvider provideAuthTokenFetcher */
  29. public function testGetLastReceivedToken(FetchAuthTokenInterface $fetcher)
  30. {
  31. $accessToken = $fetcher->getLastReceivedToken();
  32. $this->assertNotNull($accessToken);
  33. $this->assertArrayHasKey('access_token', $accessToken);
  34. $this->assertArrayHasKey('expires_at', $accessToken);
  35. $this->assertEquals('xyz', $accessToken['access_token']);
  36. $this->assertEquals(strtotime('2001'), $accessToken['expires_at']);
  37. }
  38. public function provideAuthTokenFetcher()
  39. {
  40. $scopes = ['https://www.googleapis.com/auth/drive.readonly'];
  41. $jsonPath = sprintf(
  42. '%s/fixtures/.config/%s',
  43. __DIR__,
  44. CredentialsLoader::WELL_KNOWN_PATH
  45. );
  46. $jsonPath2 = sprintf(
  47. '%s/fixtures2/.config/%s',
  48. __DIR__,
  49. CredentialsLoader::WELL_KNOWN_PATH
  50. );
  51. return [
  52. [$this->getAppIdentityCredentials()],
  53. [$this->getGCECredentials()],
  54. [$this->getServiceAccountCredentials($scopes, $jsonPath)],
  55. [$this->getServiceAccountJwtAccessCredentials($jsonPath)],
  56. [$this->getUserRefreshCredentials($scopes, $jsonPath2)],
  57. [$this->getOAuth2()],
  58. ];
  59. }
  60. private function getAppIdentityCredentials()
  61. {
  62. $class = new \ReflectionClass(
  63. 'Google\Auth\Credentials\AppIdentityCredentials'
  64. );
  65. $property = $class->getProperty('lastReceivedToken');
  66. $property->setAccessible(true);
  67. $credentials = new AppIdentityCredentials();
  68. $property->setValue($credentials, [
  69. 'access_token' => 'xyz',
  70. 'expiration_time' => strtotime('2001'),
  71. ]);
  72. return $credentials;
  73. }
  74. private function getGCECredentials()
  75. {
  76. $class = new \ReflectionClass(
  77. 'Google\Auth\Credentials\GCECredentials'
  78. );
  79. $property = $class->getProperty('lastReceivedToken');
  80. $property->setAccessible(true);
  81. $credentials = new GCECredentials();
  82. $property->setValue($credentials, [
  83. 'access_token' => 'xyz',
  84. 'expires_at' => strtotime('2001'),
  85. ]);
  86. return $credentials;
  87. }
  88. private function getServiceAccountCredentials($scopes, $jsonPath)
  89. {
  90. $class = new \ReflectionClass(
  91. 'Google\Auth\Credentials\ServiceAccountCredentials'
  92. );
  93. $property = $class->getProperty('auth');
  94. $property->setAccessible(true);
  95. $credentials = new ServiceAccountCredentials($scopes, $jsonPath);
  96. $property->setValue($credentials, $this->getOAuth2Mock());
  97. return $credentials;
  98. }
  99. private function getServiceAccountJwtAccessCredentials($jsonPath)
  100. {
  101. $class = new \ReflectionClass(
  102. 'Google\Auth\Credentials\ServiceAccountJwtAccessCredentials'
  103. );
  104. $property = $class->getProperty('auth');
  105. $property->setAccessible(true);
  106. $credentials = new ServiceAccountJwtAccessCredentials($jsonPath);
  107. $property->setValue($credentials, $this->getOAuth2Mock());
  108. return $credentials;
  109. }
  110. private function getUserRefreshCredentials($scopes, $jsonPath)
  111. {
  112. $class = new \ReflectionClass(
  113. 'Google\Auth\Credentials\UserRefreshCredentials'
  114. );
  115. $property = $class->getProperty('auth');
  116. $property->setAccessible(true);
  117. $credentials = new UserRefreshCredentials($scopes, $jsonPath);
  118. $property->setValue($credentials, $this->getOAuth2Mock());
  119. return $credentials;
  120. }
  121. private function getOAuth2()
  122. {
  123. $oauth = new OAuth2([
  124. 'access_token' => 'xyz',
  125. 'expires_at' => strtotime('2001'),
  126. ]);
  127. return $oauth;
  128. }
  129. private function getOAuth2Mock()
  130. {
  131. $mock = $this->getMockBuilder('Google\Auth\OAuth2')
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $mock
  135. ->expects($this->once())
  136. ->method('getLastReceivedToken')
  137. ->will($this->returnValue([
  138. 'access_token' => 'xyz',
  139. 'expires_at' => strtotime('2001'),
  140. ]));
  141. return $mock;
  142. }
  143. }