-
Notifications
You must be signed in to change notification settings - Fork 816
/
api_5.test.ts
199 lines (174 loc) · 7.49 KB
/
api_5.test.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {
addApiWithoutSchema,
addFeatureFlag,
addFunction,
addRestApi,
addSimpleDDB,
amplifyPushGraphQlWithCognitoPrompt,
amplifyPushUpdate,
checkIfBucketExists,
createNewProjectDir,
deleteProject,
deleteProjectDir,
getAppSyncApi,
getProjectMeta,
initJSProjectWithProfile,
listAttachedRolePolicies,
listRolePolicies,
updateApiSchema,
updateAuthAddAdminQueries,
} from 'amplify-e2e-core';
import { readdirSync, readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
const providerName = 'awscloudformation';
// to deal with bug in cognito-identity-js
(global as any).fetch = require('node-fetch');
// to deal with subscriptions in node env
(global as any).WebSocket = require('ws');
describe('amplify add api (REST)', () => {
let projRoot: string;
beforeEach(async () => {
projRoot = await createNewProjectDir('rest-api');
});
afterEach(async () => {
const meta = getProjectMeta(projRoot);
expect(meta.providers.awscloudformation).toBeDefined();
const {
AuthRoleArn: authRoleArn,
UnauthRoleArn: unauthRoleArn,
DeploymentBucketName: bucketName,
Region: region,
StackId: stackId,
} = meta.providers.awscloudformation;
expect(authRoleArn).toBeDefined();
expect(unauthRoleArn).toBeDefined();
expect(region).toBeDefined();
expect(stackId).toBeDefined();
const bucketExists = await checkIfBucketExists(bucketName, region);
expect(bucketExists).toMatchObject({});
expect(meta.function).toBeDefined();
let seenAtLeastOneFunc = false;
for (let key of Object.keys(meta.function)) {
const { service, build, lastBuildTimeStamp, lastPackageTimeStamp, distZipFilename, lastPushTimeStamp, lastPushDirHash } =
meta.function[key];
expect(service).toBe('Lambda');
expect(build).toBeTruthy();
expect(lastBuildTimeStamp).toBeDefined();
expect(lastPackageTimeStamp).toBeDefined();
expect(distZipFilename).toBeDefined();
expect(lastPushTimeStamp).toBeDefined();
expect(lastPushDirHash).toBeDefined();
seenAtLeastOneFunc = true;
}
expect(seenAtLeastOneFunc).toBeTruthy();
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});
it('init a project, add a DDB, then add a crud rest api', async () => {
const randomId = await global.getRandomId();
const DDB_NAME = `ddb${randomId}`;
await initJSProjectWithProfile(projRoot, {});
await addSimpleDDB(projRoot, { name: DDB_NAME });
await addRestApi(projRoot, { isCrud: true });
await amplifyPushUpdate(projRoot);
const meta = getProjectMeta(projRoot);
expect(meta.storage[DDB_NAME]).toBeDefined();
const { service, lastPushTimeStamp, lastPushDirHash } = meta.storage[DDB_NAME];
expect(service).toBe('DynamoDB');
expect(lastPushTimeStamp).toBeDefined();
expect(lastPushDirHash).toBeDefined();
});
it('init a project, then add a serverless rest api', async () => {
await initJSProjectWithProfile(projRoot, {});
await addRestApi(projRoot, { isCrud: false });
await amplifyPushUpdate(projRoot);
});
it('init a project, create lambda and attach it to an api', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, { existingLambda: true });
await amplifyPushUpdate(projRoot);
});
it('init a project, create lambda and attach multiple rest apis', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, {
existingLambda: true,
restrictAccess: true,
allowGuestUsers: true,
});
await addRestApi(projRoot, {
isFirstRestApi: false,
existingLambda: true,
restrictAccess: true,
allowGuestUsers: true,
});
await addRestApi(projRoot, {
isFirstRestApi: false,
existingLambda: true,
restrictAccess: true,
allowGuestUsers: false,
});
await addRestApi(projRoot, { isFirstRestApi: false, existingLambda: true });
await updateAuthAddAdminQueries(projRoot);
await amplifyPushUpdate(projRoot);
const amplifyMeta = getProjectMeta(projRoot);
const meta = amplifyMeta.providers.awscloudformation;
const { AuthRoleName, UnauthRoleName, Region } = meta;
expect(await listRolePolicies(AuthRoleName, Region)).toEqual([]);
expect(await listRolePolicies(UnauthRoleName, Region)).toEqual([]);
const authPolicies = await listAttachedRolePolicies(AuthRoleName, Region);
expect(authPolicies.length).toBeGreaterThan(0);
for (let i = 0; i < authPolicies.length; i++) {
expect(authPolicies[i].PolicyName).toMatch(/PolicyAPIGWAuth\d/);
}
const unauthPolicies = await listAttachedRolePolicies(UnauthRoleName, Region);
expect(unauthPolicies.length).toBeGreaterThan(0);
for (let i = 0; i < unauthPolicies.length; i++) {
expect(unauthPolicies[i].PolicyName).toMatch(/PolicyAPIGWUnauth\d/);
}
});
it('adds a rest api and then adds a path to the existing api', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, { existingLambda: true });
await addRestApi(projRoot, { isFirstRestApi: false, existingLambda: true, path: '/newpath' });
await amplifyPushUpdate(projRoot);
});
it('migrates malformed project files during push', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, { existingLambda: true, restrictAccess: true });
const apisDirectory = path.join(projRoot, 'amplify', 'backend', 'api');
const apis = readdirSync(apisDirectory);
const apiName = apis[0];
const apiDirectory = path.join(apisDirectory, apiName);
const cfnTemplateFile = path.join(apiDirectory, `${apiName}-cloudformation-template.json`);
const cfnTemplate = JSON.parse(readFileSync(cfnTemplateFile, 'utf8'));
// The ApiId output is required, and will be added automatically if it is missing.
cfnTemplate.Outputs.ApiId = undefined;
writeFileSync(cfnTemplateFile, JSON.stringify(cfnTemplate));
await amplifyPushUpdate(projRoot);
});
it('amplify push prompt for cognito configuration if auth mode is missing', async () => {
const envName = 'devtest';
const projName = 'lambdaauthmode';
await initJSProjectWithProfile(projRoot, { name: projName, envName });
await addFeatureFlag(projRoot, 'graphqltransformer', 'useexperimentalpipelinedtransformer', true);
await addFeatureFlag(projRoot, 'graphqltransformer', 'transformerversion', 2);
await addApiWithoutSchema(projRoot);
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await updateApiSchema(projRoot, projName, 'cognito_simple_model.graphql');
await amplifyPushGraphQlWithCognitoPrompt(projRoot);
const meta = getProjectMeta(projRoot);
const region = meta.providers.awscloudformation.Region;
const { output } = meta.api.lambdaauthmode;
const { GraphQLAPIIdOutput, GraphQLAPIEndpointOutput, GraphQLAPIKeyOutput } = output;
const { graphqlApi } = await getAppSyncApi(GraphQLAPIIdOutput, region);
expect(GraphQLAPIIdOutput).toBeDefined();
expect(GraphQLAPIEndpointOutput).toBeDefined();
expect(GraphQLAPIKeyOutput).toBeDefined();
expect(graphqlApi).toBeDefined();
expect(graphqlApi.apiId).toEqual(GraphQLAPIIdOutput);
});
});