Skip to content

Commit 587586d

Browse files
author
Vieltojarvi
committed
e2e tests
1 parent c7defa8 commit 587586d

File tree

6 files changed

+120
-29
lines changed

6 files changed

+120
-29
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//import { SeedTestProjectCreator } from "../../test-project-setup/seed_test_project.js";
2-
//import { defineSandboxTest } from "./sandbox.test.template.js";
1+
import { SeedTestProjectCreator } from '../../test-project-setup/seed_test_project.js';
2+
import { defineSandboxTest } from './sandbox.test.template.js';
33

4-
//defineSandboxTest(new SeedTestProjectCreator());
4+
defineSandboxTest(new SeedTestProjectCreator());

packages/integration-tests/src/test-project-setup/seed_test_project.ts

Lines changed: 96 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import {
77
CognitoIdentityProviderClient,
88
ListUsersCommand,
99
} from '@aws-sdk/client-cognito-identity-provider';
10-
import { /*GetRoleCommand,*/ IAMClient } from '@aws-sdk/client-iam';
10+
import {
11+
AttachRolePolicyCommand,
12+
CreatePolicyCommand,
13+
IAMClient,
14+
} from '@aws-sdk/client-iam';
1115
import { DeployedResourcesFinder } from '../find_deployed_resource.js';
12-
import fs from 'fs/promises';
16+
import fsp from 'fs/promises';
1317
import assert from 'node:assert';
1418
import { createEmptyAmplifyProject } from './create_empty_amplify_project.js';
1519
import { BackendIdentifier } from '@aws-amplify/plugin-types';
@@ -21,9 +25,12 @@ import {
2125
HttpLink,
2226
InMemoryCache,
2327
gql,
24-
} from '@apollo/client';
28+
} from '@apollo/client/core';
2529
import { AUTH_TYPE, createAuthLink } from 'aws-appsync-auth-link';
2630
import { AmplifyAuthCredentialsFactory } from '../amplify_auth_credentials_factory.js';
31+
import { execa, execaSync } from 'execa';
32+
import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts';
33+
import { shortUuid } from '../short_uuid.js';
2734

2835
/**
2936
* Creates test project for seed
@@ -47,7 +54,10 @@ export class SeedTestProjectCreator implements TestProjectCreator {
4754
private readonly iamClient: IAMClient = new IAMClient(
4855
e2eToolingClientConfig
4956
),
50-
private readonly resourceFinder: DeployedResourcesFinder = new DeployedResourcesFinder()
57+
private readonly resourceFinder: DeployedResourcesFinder = new DeployedResourcesFinder(),
58+
private readonly stsClient: STSClient = new STSClient(
59+
e2eToolingClientConfig
60+
)
5161
) {}
5262

5363
createProject = async (e2eProjectDir: string): Promise<TestProjectBase> => {
@@ -62,9 +72,10 @@ export class SeedTestProjectCreator implements TestProjectCreator {
6272
this.amplifyClient,
6373
this.cognitoIdentityProviderClient,
6474
this.iamClient,
65-
this.resourceFinder
75+
this.resourceFinder,
76+
this.stsClient
6677
);
67-
await fs.cp(
78+
await fsp.cp(
6879
project.sourceProjectAmplifyDirURL,
6980
project.projectAmplifyDirPath,
7081
{
@@ -76,7 +87,7 @@ export class SeedTestProjectCreator implements TestProjectCreator {
7687
}
7788

7889
class SeedTestProject extends TestProjectBase {
79-
readonly sourceProjectDirPath = '../../src/test-projects/seed';
90+
readonly sourceProjectDirPath = '../../src/test-projects/seed-test-project';
8091

8192
readonly sourceProjectAmplifyDirSuffix = `${this.sourceProjectDirPath}/amplify`;
8293

@@ -93,7 +104,8 @@ class SeedTestProject extends TestProjectBase {
93104
amplifyClient: AmplifyClient,
94105
private readonly cognitoIdentityProviderClient: CognitoIdentityProviderClient,
95106
private readonly iamClient: IAMClient,
96-
private readonly resourceFinder: DeployedResourcesFinder
107+
private readonly resourceFinder: DeployedResourcesFinder,
108+
private readonly stsClient: STSClient
97109
) {
98110
super(
99111
name,
@@ -109,19 +121,56 @@ class SeedTestProject extends TestProjectBase {
109121
environment?: Record<string, string>
110122
) {
111123
await super.deploy(backendIdentifier, environment);
112-
await ampxCli(['sandbox', 'seed', 'generate-policy'], this.projectDirPath, {
113-
env: environment,
114-
}).run();
115-
//await this.attachToRole(backendIdentifier);
124+
125+
console.log('Executing seed policy command');
126+
const command = execaSync('npx', ['which', 'ampx'], {
127+
cwd: this.projectDirPath,
128+
}).stdout.trim();
129+
const seedPolicyProcess = await execa(
130+
command,
131+
['sandbox', 'seed', 'generate-policy'],
132+
{
133+
cwd: this.projectDirPath,
134+
env: environment,
135+
}
136+
);
137+
await this.attachToRole(seedPolicyProcess.stdout, backendIdentifier);
138+
139+
console.log(seedPolicyProcess.stdout);
140+
const clientConfig = await generateClientConfig(backendIdentifier, '1.3');
141+
if (!clientConfig.custom) {
142+
throw new Error('Client config missing custom section');
143+
}
144+
const seedRoleArn = clientConfig.custom.seedRoleArn as string;
145+
146+
const seedCredentials = await this.stsClient.send(
147+
new AssumeRoleCommand({
148+
RoleArn: seedRoleArn,
149+
RoleSessionName: `seedSession`,
150+
})
151+
);
152+
153+
assert.ok(seedCredentials.Credentials);
154+
assert.ok(seedCredentials.Credentials.AccessKeyId);
155+
assert.ok(seedCredentials.Credentials.SessionToken);
156+
assert.ok(seedCredentials.Credentials.SecretAccessKey);
157+
158+
console.log('executing seed command');
116159
await ampxCli(['sandbox', 'seed'], this.projectDirPath, {
117-
env: environment,
160+
env: {
161+
AWS_ACCESS_KEY_ID: seedCredentials.Credentials!.AccessKeyId,
162+
AWS_SECRET_ACCESS_KEY: seedCredentials.Credentials!.SecretAccessKey,
163+
AWS_SESSION_TOKEN: seedCredentials.Credentials!.SessionToken,
164+
...environment,
165+
},
118166
}).run();
119167
}
120168

121169
override async assertPostDeployment(
122170
backendId: BackendIdentifier
123171
): Promise<void> {
124172
await super.assertPostDeployment(backendId);
173+
const testUsername = '[email protected]';
125174
const clientConfig = await generateClientConfig(backendId, '1.3');
126175

127176
const cognitoId = await this.resourceFinder.findByBackendIdentifier(
@@ -131,11 +180,20 @@ class SeedTestProject extends TestProjectBase {
131180
);
132181

133182
const users = await this.cognitoIdentityProviderClient.send(
134-
new ListUsersCommand({ UserPoolId: cognitoId[0] })
183+
new ListUsersCommand({
184+
UserPoolId: cognitoId[0],
185+
Filter: '"email"^="testUser"',
186+
AttributesToGet: ['email'],
187+
})
135188
);
136189

190+
if (users.Users && users.Users.length < 1) {
191+
throw new Error('Users are missing');
192+
}
193+
137194
assert.ok(users.Users);
138-
assert.strictEqual(users.Users[0].Username, '[email protected]');
195+
assert.ok(users.Users[0].Attributes);
196+
assert.strictEqual(users.Users[0].Attributes[0]!.Value, testUsername);
139197

140198
if (!clientConfig.auth) {
141199
throw new Error('Client config missing auth section');
@@ -183,13 +241,29 @@ class SeedTestProject extends TestProjectBase {
183241
});
184242

185243
assert.strictEqual(
186-
content.data.content,
187-
'Todo list item for [email protected]'
244+
content.data.listTodos.items[0].content,
245+
`Todo list item for ${testUsername}`
246+
);
247+
}
248+
249+
async attachToRole(policyString: string, backendId: BackendIdentifier) {
250+
const policy = await this.iamClient.send(
251+
new CreatePolicyCommand({
252+
PolicyName: `seedPolicy_${shortUuid()}`,
253+
PolicyDocument: policyString,
254+
})
255+
);
256+
257+
const clientConfig = await generateClientConfig(backendId, '1.3');
258+
if (!clientConfig.custom) {
259+
throw new Error('Client config missing custom section');
260+
}
261+
262+
await this.iamClient.send(
263+
new AttachRolePolicyCommand({
264+
RoleName: clientConfig.custom.seedRoleName as string,
265+
PolicyArn: policy.Policy?.Arn,
266+
})
188267
);
189268
}
190-
/*
191-
async attachToRole(backendId: BackendIdentifier) {
192-
// somehow need to get the deployment role to add the policy to it
193-
const role = (await this.iamClient.send(new GetRoleCommand({ RoleName: 'SeedRole'})));
194-
}*/
195269
}

packages/integration-tests/src/test-projects/data-storage-auth-with-triggers-ts/amplify/func-src/handler_node16.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getResponse } from './response_generator.js';
55
import fetch from 'node-fetch';
66
global.fetch = fetch as never;
77

8+
// node 16 does not include Blob, so we need to polyfill it
89
import { Blob } from 'node-fetch';
910
global.Blob = Blob as never;
1011

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineBackend } from '@aws-amplify/backend';
22
import { auth } from './auth/resource.js';
33
import { data } from './data/resource.js';
4-
import { AccountPrincipal, Role } from 'aws-cdk-lib/aws-iam';
4+
import { AccountPrincipal, ManagedPolicy, Role } from 'aws-cdk-lib/aws-iam';
55
import { RemovalPolicy } from 'aws-cdk-lib';
66

77
/**
@@ -12,8 +12,23 @@ const backend = defineBackend({
1212
data,
1313
});
1414

15-
const seedRole = new Role(backend.stack, 'SeedRole', {
16-
assumedBy: new AccountPrincipal(backend.stack.account),
15+
const seedRoleStack = backend.createStack('seed-policy');
16+
17+
// need AmplifyBackendDeployFullAccess to be able to generate configs, would rather not add these permissions directly to the seed policy
18+
const seedRole = new Role(seedRoleStack, 'SeedRole', {
19+
assumedBy: new AccountPrincipal(seedRoleStack.account),
1720
path: '/',
21+
managedPolicies: [
22+
ManagedPolicy.fromAwsManagedPolicyName(
23+
'service-role/AmplifyBackendDeployFullAccess'
24+
),
25+
],
1826
});
1927
seedRole.applyRemovalPolicy(RemovalPolicy.DESTROY);
28+
29+
backend.addOutput({
30+
custom: {
31+
seedRoleArn: seedRole.roleArn,
32+
seedRoleName: seedRole.roleName,
33+
},
34+
});

packages/integration-tests/src/test-projects/seed-test-project/amplify/seed/seed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Amplify.configure(outputs);
1010

1111
const dataClient = generateClient<Schema>();
1212

13-
const username1 = 'test@testing.com';
13+
const username1 = 'testUser@testing.com';
1414
const password1 = 'T3st_Passw0rd*';
1515

1616
const user1 = await createAndSignUpUser({

scripts/check_dependencies.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ await new DependenciesValidator(
1616
'@aws-amplify/cli-core': {
1717
allowList: [
1818
'@aws-amplify/backend-cli',
19+
'@aws-amplify/integration-tests',
1920
'@aws-amplify/sandbox',
2021
'@aws-amplify/seed',
2122
'create-amplify',

0 commit comments

Comments
 (0)