RamRoleArnProviderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Credentials\Providers;
  3. use ReflectionException;
  4. use AlibabaCloud\Client\SDK;
  5. use PHPUnit\Framework\TestCase;
  6. use AlibabaCloud\Client\AlibabaCloud;
  7. use AlibabaCloud\Client\Clients\RamRoleArnClient;
  8. use AlibabaCloud\Client\Credentials\StsCredential;
  9. use AlibabaCloud\Client\Exception\ClientException;
  10. use AlibabaCloud\Client\Exception\ServerException;
  11. use AlibabaCloud\Client\Credentials\Providers\RamRoleArnProvider;
  12. /**
  13. * Class RamRoleArnProviderTest
  14. *
  15. * @package AlibabaCloud\Client\Tests\Unit\Credentials\Providers
  16. *
  17. * @coversDefaultClass \AlibabaCloud\Client\Credentials\Providers\RamRoleArnProvider
  18. */
  19. class RamRoleArnProviderTest extends TestCase
  20. {
  21. /**
  22. * @after
  23. */
  24. protected function finalize()
  25. {
  26. parent::tearDown();
  27. AlibabaCloud::cancelMock();
  28. }
  29. /**
  30. * @throws ClientException
  31. */
  32. public function testGet()
  33. {
  34. // Setup
  35. $client = new RamRoleArnClient(
  36. 'foo',
  37. 'bar',
  38. 'arn',
  39. 'name'
  40. );
  41. $provider = new RamRoleArnProvider($client);
  42. // Test
  43. try {
  44. $actual = $provider->get();
  45. self::assertInstanceOf(StsCredential::class, $actual);
  46. } catch (ClientException $e) {
  47. self::assertEquals(SDK::SERVER_UNREACHABLE, $e->getErrorCode());
  48. } catch (ServerException $e) {
  49. self::assertEquals('InvalidAccessKeyId.NotFound', $e->getErrorCode());
  50. }
  51. }
  52. /**
  53. * @throws ClientException
  54. * @throws ServerException
  55. * @throws ReflectionException
  56. */
  57. public function testGetInCache()
  58. {
  59. // Setup
  60. $client = new RamRoleArnClient(
  61. 'foo',
  62. 'bar',
  63. 'arn',
  64. 'name'
  65. );
  66. $provider = new RamRoleArnProvider($client);
  67. // Test
  68. $cacheMethod = new \ReflectionMethod(
  69. RamRoleArnProvider::class,
  70. 'cache'
  71. );
  72. $cacheMethod->setAccessible(true);
  73. $result = [
  74. 'Expiration' => '2049-10-01 11:11:11',
  75. 'AccessKeyId' => 'foo',
  76. 'AccessKeySecret' => 'bar',
  77. 'SecurityToken' => 'token',
  78. ];
  79. $cacheMethod->invokeArgs($provider, [$result]);
  80. $actual = $provider->get();
  81. // Assert
  82. self::assertInstanceOf(StsCredential::class, $actual);
  83. }
  84. /**
  85. * @throws ClientException
  86. * @throws ServerException
  87. */
  88. public function testNoCredentials()
  89. {
  90. $this->expectException(ServerException::class);
  91. $this->expectExceptionMessage("SDK.InvalidCredential: Result contains no credentials RequestId:");
  92. AlibabaCloud::mockResponse();
  93. $client = AlibabaCloud::ramRoleArnClient('id', 'secret', 'arn', 'session');
  94. $provider = new RamRoleArnProvider($client);
  95. $provider->get();
  96. }
  97. /**
  98. * @throws ClientException
  99. * @throws ServerException
  100. */
  101. public function testOk()
  102. {
  103. AlibabaCloud::mockResponse(
  104. 200,
  105. [],
  106. '{
  107. "RequestId": "88FEA385-EF5D-4A8A-8C00-A07DAE3BFD44",
  108. "AssumedRoleUser": {
  109. "AssumedRoleId": "********************",
  110. "Arn": "********************"
  111. },
  112. "Credentials": {
  113. "AccessKeySecret": "********************",
  114. "AccessKeyId": "STS.**************",
  115. "Expiration": "2020-02-25T03:56:19Z",
  116. "SecurityToken": "**************"
  117. }
  118. }'
  119. );
  120. $client = AlibabaCloud::ramRoleArnClient('id', 'secret', 'arn', 'session');
  121. $provider = new RamRoleArnProvider($client);
  122. $credential = $provider->get();
  123. self::assertInstanceOf(StsCredential::class, $credential);
  124. }
  125. }