Skip to content

Commit d018293

Browse files
authored
test: using existing resources in e2e tests (#1320)
* test(client-s3): support AWS_SMOKE_TEST_BUCKET env * test(client-cognito-identity): using existing identity pool in e2e test
1 parent 16aea66 commit d018293

File tree

8 files changed

+114
-85
lines changed

8 files changed

+114
-85
lines changed

clients/client-cognito-identity/e2e/CognitoIdentity.ispec.ts

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,23 @@
55
*/
66
import { expect } from "chai";
77
import { CognitoIdentity } from "../index";
8-
import { Credentials } from "@aws-sdk/types";
9-
import { IAM } from "@aws-sdk/client-iam";
108
// There will be default values of defaultRegion, credentials, and isBrowser variable in browser tests.
119
// Define the values for Node.js tests
1210
const region: string | undefined =
1311
(globalThis as any).defaultRegion || undefined;
14-
const credentials: Credentials | undefined =
15-
(globalThis as any).credentials || undefined;
12+
const IdentityPoolId =
13+
(globalThis as any)?.window?.__env__?.AWS_SMOKE_TEST_IDENTITY_POOL_ID ||
14+
process?.env?.AWS_SMOKE_TEST_IDENTITY_POOL_ID;
1615

1716
describe("@aws-sdk/client-cognito-identity", function () {
18-
this.timeout(50000);
19-
// Required in run
2017
const unAuthClient = new CognitoIdentity({
2118
region
2219
});
23-
const authClient = new CognitoIdentity({
24-
credentials,
25-
region
26-
});
27-
const iam = new IAM({ credentials, region });
28-
const identityPoolName = `aws_sdk_unit_test_${Date.now()}`;
29-
const identityPoolRoleName = `${identityPoolName}-role`;
30-
let identityPoolRole: string;
31-
let identityPoolId: string;
32-
33-
before(async () => {
34-
// Create an identity pool
35-
const createIdentityPoolResult = await authClient.createIdentityPool({
36-
IdentityPoolName: identityPoolName,
37-
AllowUnauthenticatedIdentities: true
38-
});
39-
expect(createIdentityPoolResult.$metadata.httpStatusCode).to.equal(200);
40-
expect(typeof createIdentityPoolResult.IdentityPoolId).to.equal("string");
41-
identityPoolId = createIdentityPoolResult.IdentityPoolId as string;
42-
43-
// Create a role to be attached to identity pool
44-
const { Role } = await iam.createRole({
45-
RoleName: identityPoolRoleName,
46-
AssumeRolePolicyDocument: `{
47-
"Version": "2012-10-17",
48-
"Statement": [
49-
{
50-
"Effect": "Allow",
51-
"Principal": {
52-
"Federated": "cognito-identity.amazonaws.com"
53-
},
54-
"Action": "sts:AssumeRoleWithWebIdentity"
55-
}
56-
]
57-
}`
58-
});
59-
identityPoolRole = Role?.Arn as string;
60-
61-
// wait for role creating
62-
// TODO: add RoleExists waiter
63-
await new Promise(resolve => setTimeout(resolve, 5000));
64-
65-
// Set role to identity pool
66-
await authClient.setIdentityPoolRoles({
67-
IdentityPoolId: identityPoolId,
68-
Roles: {
69-
unauthenticated: identityPoolRole!
70-
}
71-
});
72-
73-
// wait for role propagating
74-
// TODO: add waiters
75-
await new Promise(resolve => setTimeout(resolve, 10000));
76-
});
77-
78-
after(async () => {
79-
// Delete the identity pool
80-
if (identityPoolId) {
81-
await authClient.deleteIdentityPool({
82-
IdentityPoolId: identityPoolId
83-
});
84-
}
85-
// Delete identity pool role
86-
if (identityPoolRole) {
87-
await iam.deleteRole({ RoleName: identityPoolRoleName });
88-
}
89-
});
9020

9121
it("should successfully fetch Id and get credentials", async () => {
9222
// Test getId()
9323
const getIdResult = await unAuthClient.getId({
94-
IdentityPoolId: identityPoolId
24+
IdentityPoolId
9525
});
9626
expect(getIdResult.$metadata.httpStatusCode).to.equal(200);
9727
expect(typeof getIdResult.IdentityId).to.equal("string");
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Integration test resources
2+
3+
Certain resources need to be created to make sure the integration test
4+
has backend resources to test against. Follow steps bellow to create them:
5+
6+
1. Create a Cognito Identity Pool
7+
8+
```console
9+
aws cognito-identity create-identity-pool --identity-pool-name [NAME] --allow-unauthenticated-identities --output text --query '[IdentityPoolId]'
10+
```
11+
12+
It outputs `IdentityPoolId`.
13+
14+
1. Create a IAM Role
15+
16+
1. If you create the Identity Pool with AWS console and allow creating roles on your behalf, you can skip this step
17+
18+
1. Command line:
19+
20+
```console
21+
aws iam create-role --role-name=[ROLE_NAME] --assume-role-policy-document '{
22+
"Statement": [
23+
{
24+
"Effect": "Allow",
25+
"Principal": {
26+
"Federated": "cognito-identity.amazonaws.com"
27+
},
28+
"Action": "sts:AssumeRoleWithWebIdentity"
29+
}
30+
]
31+
}' --output text --query 'Role.Arn'
32+
```
33+
34+
It outputs `RoleArn`
35+
36+
1. Set the unauthenticated role of the Identity Pool created in Step 1 to the role created in Step 2
37+
38+
```console
39+
aws cognito-identity set-identity-pool-roles --identity-pool-id [IdentityPoolId] --roles unauthenticated=[RoleArn]
40+
```
41+
42+
1. Run the test with ENV. For example:
43+
44+
```console
45+
AWS_SMOKE_TEST_REGION=[Region] AWS_SMOKE_TEST_IDENTITY_POOL_ID=[IdentityPoolId] yarn test:e2e
46+
```

clients/client-cognito-identity/karma.conf.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function (config) {
44
frameworks: ["mocha", "chai"],
55
files: ["e2e/**/*.ispec.ts"],
66
preprocessors: {
7-
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials"]
7+
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials", "env"]
88
},
99
webpackMiddleware: {
1010
stats: "minimal"
@@ -35,6 +35,10 @@ module.exports = function (config) {
3535
},
3636
devtool: "inline-source-map"
3737
},
38+
envPreprocessor: [
39+
"AWS_SMOKE_TEST_REGION",
40+
"AWS_SMOKE_TEST_IDENTITY_POOL_ID"
41+
],
3842
plugins: [
3943
"@aws-sdk/karma-credential-loader",
4044
"karma-chrome-launcher",
@@ -43,7 +47,8 @@ module.exports = function (config) {
4347
"karma-chai",
4448
"karma-webpack",
4549
"karma-coverage",
46-
"karma-sourcemap-loader"
50+
"karma-sourcemap-loader",
51+
"karma-env-preprocessor"
4752
],
4853
reporters: ["progress"],
4954
port: 9876,

clients/client-s3/e2e/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Integration test resources
2+
3+
Certain resources need to be created to make sure the integration test
4+
has backend resources to test against. Follow steps bellow to create them:
5+
6+
1. Create S3 Bucket
7+
8+
1. Create Bucket with:
9+
10+
```console
11+
aws s3 mb s3://[BucketName] --region [Region]
12+
```
13+
14+
1. Put CORS config to the bucket
15+
16+
```console
17+
aws s3api put-bucket-cors --bucket [BucketName] --cors-configuration '{
18+
"CORSRules": [
19+
{
20+
"AllowedOrigins": ["*"],
21+
"AllowedMethods": ["GET", "PUT", "POST", "DELETE", "HEAD"],
22+
"MaxAgeSeconds": 3000,
23+
"ExposeHeaders": ["ETag", "Date"],
24+
"AllowedHeaders": ["*"]
25+
}
26+
]
27+
}'
28+
```
29+
30+
1. supply the bucket name to the test but setting the ENV `AWS_SMOKE_TEST_BUCKET`
31+
32+
1. Run the test with ENV. For example:
33+
34+
```console
35+
AWS_SMOKE_TEST_REGION=[Region] AWS_SMOKE_TEST_BUCKET=[Bucket] yarn test:e2e
36+
```

clients/client-s3/e2e/S3.ispec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const region: string | undefined =
1717
const credentials: Credentials | undefined =
1818
(globalThis as any).credentials || undefined;
1919
const isBrowser: boolean | undefined = (globalThis as any).isBrowser || false;
20+
const Bucket =
21+
(globalThis as any)?.window?.__env__?.AWS_SMOKE_TEST_BUCKET ||
22+
process?.env?.AWS_SMOKE_TEST_BUCKET;
2023

21-
// this bucket requires enabling CORS:
22-
// AllowedOrigin(*), AllowedMethod(GET, PUT, POST, DELETE, HEAD), ExposeHeader(ETag), AllowedHeader(*)
23-
const Bucket = "aws-sdk-unit-test";
2424
let Key = `${Date.now()}`;
2525

2626
describe("@aws-sdk/client-s3", () => {
@@ -79,7 +79,7 @@ describe("@aws-sdk/client-s3", () => {
7979
});
8080
expect(result.$metadata.httpStatusCode).to.equal(200);
8181
});
82-
} else {
82+
} else {
8383
it("should succeed with Node.js readable stream body", async () => {
8484
const length = 10 * 1000; // 10KB
8585
const chunkSize = 10;
@@ -136,8 +136,12 @@ describe("@aws-sdk/client-s3", () => {
136136
});
137137

138138
describe("ListObjects", () => {
139-
before(() => {
139+
before(async () => {
140140
Key = `${Date.now()}`;
141+
await client.putObject({ Bucket, Key, Body: "foo" });
142+
});
143+
after(async () => {
144+
await client.deleteObject({ Bucket, Key });
141145
});
142146
it("should succeed with valid bucket", async () => {
143147
const result = await client.listObjects({
@@ -247,7 +251,7 @@ describe("@aws-sdk/client-s3", () => {
247251
});
248252
expect(listUploadsResult.$metadata.httpStatusCode).to.equal(200);
249253
expect(
250-
listUploadsResult.Uploads?.map(upload => upload.UploadId)
254+
(listUploadsResult.Uploads || []).map(upload => upload.UploadId)
251255
).not.to.contain(toAbort);
252256
});
253257
});

clients/client-s3/karma.conf.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function (config) {
44
frameworks: ["mocha", "chai"],
55
files: ["e2e/**/*.ispec.ts"],
66
preprocessors: {
7-
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials"]
7+
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials", "env"]
88
},
99
webpackMiddleware: {
1010
stats: "minimal"
@@ -35,6 +35,7 @@ module.exports = function (config) {
3535
},
3636
devtool: "inline-source-map"
3737
},
38+
envPreprocessor: ["AWS_SMOKE_TEST_REGION", "AWS_SMOKE_TEST_BUCKET"],
3839
plugins: [
3940
"@aws-sdk/karma-credential-loader",
4041
"karma-chrome-launcher",
@@ -43,7 +44,8 @@ module.exports = function (config) {
4344
"karma-chai",
4445
"karma-webpack",
4546
"karma-coverage",
46-
"karma-sourcemap-loader"
47+
"karma-sourcemap-loader",
48+
"karma-env-preprocessor"
4749
],
4850
reporters: ["progress"],
4951
port: 9876,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"karma-chai": "^0.1.0",
5757
"karma-chrome-launcher": "^3.1.0",
5858
"karma-coverage": "^2.0.2",
59+
"karma-env-preprocessor": "^0.1.1",
5960
"karma-firefox-launcher": "^1.3.0",
6061
"karma-jasmine": "^3.3.1",
6162
"karma-mocha": "^2.0.1",
@@ -69,9 +70,9 @@
6970
"puppeteer": "^4.0.0",
7071
"ts-loader": "^7.0.5",
7172
"typescript": "~3.8.3",
73+
"verdaccio": "^4.7.2",
7274
"webpack": "^4.43.0",
7375
"webpack-cli": "^3.3.12",
74-
"verdaccio": "^4.7.2",
7576
"yarn": "1.22.4"
7677
},
7778
"workspaces": {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7409,6 +7409,11 @@ karma-coverage@^2.0.2:
74097409
istanbul-reports "^3.0.0"
74107410
minimatch "^3.0.4"
74117411

7412+
karma-env-preprocessor@^0.1.1:
7413+
version "0.1.1"
7414+
resolved "https://registry.yarnpkg.com/karma-env-preprocessor/-/karma-env-preprocessor-0.1.1.tgz#bbe8c87d59c00edb76070bd3c31b4b39d5dc7e15"
7415+
integrity sha1-u+jIfVnADtt2BwvTwxtLOdXcfhU=
7416+
74127417
karma-firefox-launcher@^1.3.0:
74137418
version "1.3.0"
74147419
resolved "https://registry.yarnpkg.com/karma-firefox-launcher/-/karma-firefox-launcher-1.3.0.tgz#ebcbb1d1ddfada6be900eb8fae25bcf2dcdc8171"

0 commit comments

Comments
 (0)