RsaKeyPairCredentialTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ClientException;
  6. use AlibabaCloud\Client\Exception\ServerException;
  7. use AlibabaCloud\Client\Credentials\StsCredential;
  8. use AlibabaCloud\Client\Tests\Mock\Services\Ecs\DescribeAccessPointsRequest;
  9. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  10. /**
  11. * Class RsaKeyPairCredentialTest
  12. *
  13. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials
  14. */
  15. class RsaKeyPairCredentialTest extends TestCase
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $clientName = 'RsaKeyPairCredentialTest';
  21. /**
  22. * @throws ClientException
  23. */
  24. public function setUp()
  25. {
  26. $regionId = 'ap-northeast-1';
  27. $publicKeyId = \AlibabaCloud\Client\env('PUBLIC_KEY_ID');
  28. $privateKeyFile = VirtualRsaKeyPairCredential::privateKeyFileUrl();
  29. AlibabaCloud::rsaKeyPairClient($publicKeyId, $privateKeyFile)
  30. ->regionId($regionId)
  31. ->name($this->clientName);
  32. }
  33. /**
  34. * @throws ClientException
  35. */
  36. public function tearDown()
  37. {
  38. AlibabaCloud::del($this->clientName);
  39. }
  40. /**
  41. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  42. * @expectedExceptionMessageRegExp /NoPermission: You are not authorized to do this action. You should be authorized by RAM./
  43. * @throws ClientException
  44. * @throws ServerException
  45. */
  46. public function testGetSessionCredential()
  47. {
  48. $credential = AlibabaCloud::get($this->clientName)
  49. ->getSessionCredential(30, 25);
  50. self::assertInstanceOf(StsCredential::class, $credential);
  51. }
  52. /**
  53. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  54. * @expectedExceptionMessageRegExp /NoPermission: You are not authorized to do this action. You should be authorized by RAM./
  55. * @throws ClientException
  56. * @throws ServerException
  57. */
  58. public function testEcsInJapan()
  59. {
  60. $result = (new DescribeAccessPointsRequest())
  61. ->client($this->clientName)
  62. ->connectTimeout(20)
  63. ->timeout(25)
  64. ->request();
  65. static::assertArrayHasKey('AccessPointSet', $result);
  66. }
  67. /**
  68. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  69. * @expectedExceptionMessageRegExp /NoPermission: You are not authorized to do this action. You should be authorized by RAM./
  70. * @throws ClientException
  71. * @throws ServerException
  72. */
  73. public function testEcsNotInJapan()
  74. {
  75. // Setup
  76. $regionId = \AlibabaCloud\Client\env('REGION_ID', 'cn-hangzhou');
  77. // Test
  78. (new DescribeAccessPointsRequest())
  79. ->client($this->clientName)
  80. ->regionId($regionId)
  81. ->connectTimeout(25)
  82. ->timeout(30)
  83. ->request();
  84. }
  85. }