/
StsTest.php
73 lines (61 loc) · 2.62 KB
/
StsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace OSS\Tests;
include_once __DIR__ . DIRECTORY_SEPARATOR.'../../../vendor/openaliyuns/aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';
use Sts\Request\V20150401 as Sts;
class StsTest extends \PHPUnit_Framework_TestCase
{
private $client;
public function setUp()
{
date_default_timezone_set("UTC");
}
public function testAssumeRole()
{
$request = new Sts\AssumeRoleRequest();
$request->setRoleSessionName(self::CLIENT_NAME);
$request->setRoleArn(self::ROLE_ARN);
$request->setPolicy(self::POLICY);
$request->setDurationSeconds(self::EXPIRE_TIME);
$iClientProfile = \DefaultProfile::getProfile(self::REGION_ID, self::ACCESS_KEY_ID, self::ACCESS_KEY_SECRET);
$this->client = new \DefaultAcsClient($iClientProfile);
$response = $this->client->getAcsResponse($request);
$this->assertTrue(isset($response->AssumedRoleUser));
$this->assertTrue(isset($response->Credentials));
$this->assertEquals($response->AssumedRoleUser->Arn, self::ROLE_ARN.'/'.self::CLIENT_NAME);
$time = substr($response->Credentials->Expiration, 0, 10).' '.substr($response->Credentials->Expiration, 11, 8);
$this->assertEquals(strtotime($time)-strtotime("now"),self::EXPIRE_TIME);
}
public function testGetCallerIdentity()
{
$request = new Sts\GetCallerIdentityRequest();
$iClientProfile = \DefaultProfile::getProfile(self::REGION_ID, self::ACCESS_KEY_ID, self::ACCESS_KEY_SECRET);
$this->client = new \DefaultAcsClient($iClientProfile);
$response = $this->client->getAcsResponse($request);
$this->assertTrue(isset($response->AccountId));
$this->assertTrue(isset($response->Arn));
$this->assertTrue(isset($response->RequestId));
$this->assertTrue(isset($response->UserId));
}
const REGION_ID = "cn-shanghai";
const ENDPOINT = "sts.cn-shanghai.aliyuncs.com";
const ACCESS_KEY_ID = "LTAIVdxMrOBUSWoS";
const ACCESS_KEY_SECRET = "vtGoCcfxjf76gK2ZTwHabtRaUPzlfQ";
const CLIENT_NAME = "sts";
const EXPIRE_TIME = "3600";
const ROLE_ARN = "acs:ram::1521081174204619:role/test";
const POLICY = <<<POLICY
{
"Statement": [
{
"Action": [
"oss:Get*",
"oss:List*"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "1"
}
POLICY;
}