Skip to content

Commit ba1e106

Browse files
authored
Add integration test via localstack (#941)
1 parent 496e69e commit ba1e106

File tree

4 files changed

+56
-47
lines changed

4 files changed

+56
-47
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.EXPORT_ALL_VARIABLES:
2+
3+
initialize: start-docker
4+
start-docker:
5+
docker start async_aws_localstack && exit 0 || \
6+
docker start async_aws_localstack-sts && exit 0 || \
7+
docker pull localstack/localstack && \
8+
docker run -d -p 4566:4566 -e SERVICES=sts -v /var/run/docker.sock:/var/run/docker.sock --name async_aws_localstack-sts localstack/localstack && \
9+
docker run --rm --link async_aws_localstack-sts:localstack martin/wait -c localstack:4566
10+
11+
test: initialize
12+
./vendor/bin/simple-phpunit
13+
14+
clean: stop-docker
15+
stop-docker:
16+
docker stop async_aws_localstack-sts || true

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"psr/cache": "^1.0",
1919
"psr/log": "^1.0",
2020
"symfony/deprecation-contracts": "^2.1",
21-
"symfony/http-client": "^4.4 || ^5.0,!=5.2.0",
21+
"symfony/http-client": "^4.4.16 || ^5.1.7,!=5.2.0",
2222
"symfony/http-client-contracts": "^1.1.8 || ^2.0",
2323
"symfony/service-contracts": "^1.0 || ^2.0"
2424
},

tests/Integration/StsClientTest.php

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace AsyncAws\Core\Tests\Integration;
44

5+
use AsyncAws\Core\Credentials\Credentials;
56
use AsyncAws\Core\Credentials\NullProvider;
67
use AsyncAws\Core\Sts\Input\AssumeRoleRequest;
78
use AsyncAws\Core\Sts\Input\AssumeRoleWithWebIdentityRequest;
89
use AsyncAws\Core\Sts\Input\GetCallerIdentityRequest;
9-
use AsyncAws\Core\Sts\Input\PolicyDescriptorType;
10-
use AsyncAws\Core\Sts\Input\Tag;
1110
use AsyncAws\Core\Sts\StsClient;
11+
use AsyncAws\Core\Sts\ValueObject\PolicyDescriptorType;
12+
use AsyncAws\Core\Sts\ValueObject\Tag;
1213
use AsyncAws\Core\Test\TestCase;
1314

1415
class StsClientTest extends TestCase
@@ -18,72 +19,66 @@ public function testAssumeRole(): void
1819
$client = $this->getClient();
1920

2021
$input = new AssumeRoleRequest([
21-
'RoleArn' => 'change me',
22-
'RoleSessionName' => 'change me',
22+
'RoleArn' => 'arn:aws::iam::123456789012:role/demo',
23+
'RoleSessionName' => 'John-session',
2324
'PolicyArns' => [new PolicyDescriptorType([
24-
'arn' => 'change me',
25+
'arn' => 'arn:aws::iam::123456789012:policy/demo',
2526
])],
26-
'Policy' => 'change me',
27-
'DurationSeconds' => 1337,
27+
'Policy' => '{"Version":"2012-10-17","Statement":[{"Sid": "Stmt1","Effect": "Allow","Action": "s3:*","Resource": "*"}]}',
28+
'DurationSeconds' => 300,
2829
'Tags' => [new Tag([
29-
'Key' => 'change me',
30-
'Value' => 'change me',
30+
'Key' => 'Project',
31+
'Value' => 'Pegasus',
3132
])],
32-
'TransitiveTagKeys' => ['change me'],
33-
'ExternalId' => 'change me',
34-
'SerialNumber' => 'change me',
33+
'TransitiveTagKeys' => ['Project', 'Cost-Center'],
34+
'ExternalId' => '123ABC',
35+
'SerialNumber' => '12345678',
3536
'TokenCode' => 'change me',
3637
]);
3738
$result = $client->AssumeRole($input);
3839

39-
$result->resolve();
40-
41-
// self::assertTODO(expected, $result->getCredentials());
42-
// self::assertTODO(expected, $result->getAssumedRoleUser());
43-
self::assertSame(1337, $result->getPackedPolicySize());
40+
self::assertNotNull($result->getCredentials());
41+
self::assertLessThanOrEqual(new \DateTime('+5min'), $result->getCredentials()->getExpiration());
42+
self::assertNotNull($result->getAssumedRoleUser());
43+
self::assertSame('arn:aws:sts::000000000000:assumed-role/demo/John-session', $result->getAssumedRoleUser()->getArn());
44+
self::assertSame(6, $result->getPackedPolicySize());
4445
}
4546

4647
public function testAssumeRoleWithWebIdentity(): void
4748
{
4849
$client = $this->getClient();
4950

5051
$input = new AssumeRoleWithWebIdentityRequest([
51-
'RoleArn' => 'change me',
52-
'RoleSessionName' => 'change me',
53-
'WebIdentityToken' => 'change me',
54-
'ProviderId' => 'change me',
52+
'RoleArn' => 'arn:aws:iam::123456789012:role/FederatedWebIdentityRole',
53+
'RoleSessionName' => 'app1',
54+
'WebIdentityToken' => 'FooBarBaz',
55+
'ProviderId' => 'www.amazon.com',
5556
'PolicyArns' => [new PolicyDescriptorType([
56-
'arn' => 'change me',
57+
'arn' => 'arn:aws:iam::123456789012:policy/q=webidentitydemopolicy1',
58+
]), new PolicyDescriptorType([
59+
'arn' => 'arn:aws:iam::123456789012:policy/webidentitydemopolicy2',
5760
])],
58-
'Policy' => 'change me',
59-
'DurationSeconds' => 1337,
61+
'DurationSeconds' => 300,
6062
]);
6163
$result = $client->AssumeRoleWithWebIdentity($input);
6264

63-
$result->resolve();
64-
65-
// self::assertTODO(expected, $result->getCredentials());
66-
self::assertSame('changeIt', $result->getSubjectFromWebIdentityToken());
67-
// self::assertTODO(expected, $result->getAssumedRoleUser());
68-
self::assertSame(1337, $result->getPackedPolicySize());
69-
self::assertSame('changeIt', $result->getProvider());
70-
self::assertSame('changeIt', $result->getAudience());
65+
self::assertNotNull($result->getCredentials());
66+
self::assertLessThanOrEqual(new \DateTime('+5min'), $result->getCredentials()->getExpiration());
67+
self::assertNotNull($result->getAssumedRoleUser());
68+
self::assertSame('arn:aws:sts::000000000000:assumed-role/FederatedWebIdentityRole/app1', $result->getAssumedRoleUser()->getArn());
69+
self::assertSame(6, $result->getPackedPolicySize());
7170
}
7271

7372
public function testGetCallerIdentity(): void
7473
{
7574
$client = $this->getClient();
7675

77-
$input = new GetCallerIdentityRequest([
78-
79-
]);
76+
$input = new GetCallerIdentityRequest();
8077
$result = $client->GetCallerIdentity($input);
8178

82-
$result->resolve();
83-
84-
self::assertStringContainsString('change it', $result->getUserId());
85-
self::assertStringContainsString('change it', $result->getAccount());
86-
self::assertStringContainsString('change it', $result->getArn());
79+
self::assertNotNull($result->getUserId());
80+
self::assertStringContainsString('000000000000', $result->getAccount());
81+
self::assertStringContainsString('arn:aws:sts::000000000000:user/moto', $result->getArn());
8782
}
8883

8984
public function testNonAwsRegionWithCustomEndpoint(): void
@@ -127,10 +122,8 @@ public function testCustomEndpointSignature(): void
127122

128123
private function getClient(): StsClient
129124
{
130-
self::markTestSkipped('No Docker image for STS');
131-
132125
return new StsClient([
133-
'endpoint' => 'http://localhost',
134-
], new NullProvider());
126+
'endpoint' => 'http://localhost:4566',
127+
], new Credentials('aws_id', 'aws_secret'));
135128
}
136129
}

tests/Unit/Input/AssumeRoleWithWebIdentityRequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function testRequest(): void
1313
$input = new AssumeRoleWithWebIdentityRequest([
1414
'RoleArn' => 'arn:aws:iam::123456789012:role/FederatedWebIdentityRole',
1515
'RoleSessionName' => 'app1',
16-
'WebIdentityToken' => 'FooBarBz',
16+
'WebIdentityToken' => 'FooBarBaz',
1717
'ProviderId' => 'www.amazon.com',
1818
'PolicyArns' => [new PolicyDescriptorType([
1919
'arn' => 'arn:aws:iam::123456789012:policy/q=webidentitydemopolicy1',
@@ -36,7 +36,7 @@ public function testRequest(): void
3636
&ProviderId=www.amazon.com
3737
&RoleSessionName=app1
3838
&RoleArn=arn%3Aaws%3Aiam%3A%3A123456789012%3Arole%2FFederatedWebIdentityRole
39-
&WebIdentityToken=FooBarBz
39+
&WebIdentityToken=FooBarBaz
4040
';
4141

4242
self::assertRequestEqualsHttpRequest($expected, $input->request());

0 commit comments

Comments
 (0)