-
Notifications
You must be signed in to change notification settings - Fork 817
/
sandbox-mode-helpers.test.ts
131 lines (109 loc) · 4.46 KB
/
sandbox-mode-helpers.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
import { showSandboxModePrompts, showGlobalSandboxModeWarning, schemaHasSandboxModeEnabled } from '../../utils/sandbox-mode-helpers';
import { $TSContext } from 'amplify-cli-core';
import chalk from 'chalk';
import * as prompts from 'amplify-prompts';
import * as apiKeyHelpers from '../../utils/api-key-helpers';
let ctx;
let apiKeyPresent = true;
describe('sandbox mode helpers', () => {
beforeEach(() => {
const envName = 'dev';
ctx = {
amplify: {
getEnvInfo() {
return { envName };
},
invokePluginMethod: jest.fn(),
},
} as unknown as $TSContext;
jest.spyOn(prompts.printer, 'warn').mockImplementation();
jest.spyOn(apiKeyHelpers, 'hasApiKey').mockReturnValue(apiKeyPresent);
});
describe('showSandboxModePrompts', () => {
describe('missing api key', () => {
beforeAll(() => {
apiKeyPresent = false;
});
it('displays warning', async () => {
await showSandboxModePrompts(ctx);
expect(prompts.printer.warn).toBeCalledWith(
` Global Sandbox Mode has been enabled, which requires a valid API key. If you'd like to disable, remove ${chalk.green(
'"input AMPLIFY { globalAuthRule: AuthRule = { allow: public } }"',
)} from your GraphQL schema and run 'amplify push' again. If you'd like to proceed with sandbox mode disabled, do not create an API Key.\n`,
);
expect(ctx.amplify.invokePluginMethod).toBeCalledWith(ctx, 'api', undefined, 'promptToAddApiKey', [ctx]);
});
});
});
describe('showGlobalSandboxModeWarning', () => {
it('prints sandbox api key message', () => {
showGlobalSandboxModeWarning();
expect(prompts.printer.warn).toBeCalledWith(
` WARNING: Your GraphQL API currently allows public create, read, update, and delete access to all models via an API Key. To configure PRODUCTION-READY authorization rules, review: https://docs.amplify.aws/cli/graphql-transformer/auth\n`,
);
});
});
describe('schemaHasSandboxModeEnabled', () => {
it('parses sandbox AMPLIFY input on schema', () => {
const schema = `
input AMPLIFY { globalAuthRule: AuthRule = { allow: public } }
`;
expect(schemaHasSandboxModeEnabled(schema)).toEqual(true);
});
it('passes through when AMPLIFY input is not present', () => {
const schema = `
type Todo @model {
id: ID!
content: String
}
`;
expect(schemaHasSandboxModeEnabled(schema)).toEqual(false);
});
describe('input AMPLIFY has incorrect values', () => {
it('checks for "globalAuthRule"', () => {
const schema = `
input AMPLIFY { auth_rule: AuthenticationRule = { allow: public } }
`;
expect(() => schemaHasSandboxModeEnabled(schema)).toThrow(
Error('input AMPLIFY requires "globalAuthRule" field. Learn more here: https://docs.amplify.aws/cli/graphql-transformer/auth'),
);
});
it('allows "global_auth_rule"', () => {
const schema = `
input AMPLIFY { global_auth_rule: AuthRule = { allow: public } }
`;
expect(schemaHasSandboxModeEnabled(schema)).toEqual(true);
});
it('guards for AuthRule', () => {
const schema = `
input AMPLIFY { globalAuthRule: AuthenticationRule = { allow: public } }
`;
expect(() => schemaHasSandboxModeEnabled(schema)).toThrow(
Error(
'There was a problem with your auth configuration. Learn more about auth here: https://docs.amplify.aws/cli/graphql-transformer/auth',
),
);
});
it('checks for "allow" field name', () => {
const schema = `
input AMPLIFY { globalAuthRule: AuthRule = { allows: public } }
`;
expect(() => schemaHasSandboxModeEnabled(schema)).toThrow(
Error(
'There was a problem with your auth configuration. Learn more about auth here: https://docs.amplify.aws/cli/graphql-transformer/auth',
),
);
});
it('checks for "public" value from "allow" field', () => {
const schema = `
input AMPLIFY { globalAuthRule: AuthRule = { allow: private } }
`;
expect(() => schemaHasSandboxModeEnabled(schema)).toThrowError(
Error(
'There was a problem with your auth configuration. Learn more about auth here: https://docs.amplify.aws/cli/graphql-transformer/auth',
),
);
});
});
});
});