-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add sandbox mode warning to amplify status
- Loading branch information
1 parent
acaff15
commit f2860fd
Showing
19 changed files
with
420 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/amplify-cli/src/__tests__/extensions/amplify-helpers/get-api-key-config.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import fs from 'fs'; | ||
import { getAppSyncApiConfig, getApiKeyConfig, apiKeyIsActive, hasApiKey } from '../../../extensions/amplify-helpers/get-api-key-config'; | ||
import { stateManager } from 'amplify-cli-core'; | ||
|
||
let amplifyMeta; | ||
|
||
jest.mock('amplify-cli-core', () => { | ||
const original = jest.requireActual('amplify-cli-core'); | ||
return { | ||
...original, | ||
stateManager: { | ||
metaFileExists: jest.fn(), | ||
getMeta: jest.fn().mockImplementation(() => JSON.parse(amplifyMeta.toString())), | ||
}, | ||
}; | ||
}); | ||
|
||
const stateManager_mock = stateManager as jest.Mocked<typeof stateManager>; | ||
|
||
describe('getAppSyncApiConfig', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta.json`); | ||
}); | ||
|
||
it('returns the api object', async () => { | ||
const result = getAppSyncApiConfig(); | ||
|
||
expect(result).toStrictEqual({ | ||
service: 'AppSync', | ||
providerPlugin: 'awscloudformation', | ||
output: { | ||
authConfig: { | ||
defaultAuthentication: { | ||
authenticationType: 'AWS_IAM', | ||
}, | ||
additionalAuthenticationProviders: [ | ||
{ | ||
authenticationType: 'API_KEY', | ||
apiKeyConfig: { | ||
apiKeyExpirationDays: 2, | ||
apiKeyExpirationDate: '2021-08-20T20:38:07.585Z', | ||
description: '', | ||
}, | ||
}, | ||
], | ||
}, | ||
globalSandboxModeConfig: { | ||
dev: { | ||
enabled: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getApiKeyConfig', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta.json`); | ||
}); | ||
|
||
it('returns the api key config', () => { | ||
const result = getApiKeyConfig(); | ||
|
||
expect(result).toStrictEqual({ | ||
apiKeyExpirationDays: 2, | ||
apiKeyExpirationDate: '2021-08-20T20:38:07.585Z', | ||
description: '', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('apiKeyIsActive', () => { | ||
describe('with expired key', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta.json`); | ||
}); | ||
|
||
it('returns false', () => { | ||
expect(apiKeyIsActive()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('with no api key config', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta-3.json`); | ||
}); | ||
|
||
it('returns false', () => { | ||
expect(apiKeyIsActive()).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('hasApiKey', () => { | ||
describe('if api key config is present', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta.json`); | ||
}); | ||
|
||
it('returns true if api key is present', () => { | ||
expect(hasApiKey()).toBe(true); | ||
}); | ||
}); | ||
}); |
105 changes: 105 additions & 0 deletions
105
...ify-cli/src/__tests__/extensions/amplify-helpers/show-global-sandbox-mode-warning.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
globalSandboxModeEnabled, | ||
showGlobalSandboxModeWarning, | ||
} from '../../../extensions/amplify-helpers/show-global-sandbox-mode-warning'; | ||
import { $TSContext } from '../../../../../amplify-cli-core/lib'; | ||
import fs from 'fs'; | ||
import chalk from 'chalk'; | ||
|
||
let ctx, amplifyMeta; | ||
|
||
jest.mock('amplify-cli-core', () => ({ | ||
stateManager: { | ||
getMeta: jest.fn(() => JSON.parse(amplifyMeta.toString())), | ||
}, | ||
})); | ||
|
||
describe('global sandbox mode warning', () => { | ||
beforeEach(() => { | ||
const envName = 'dev'; | ||
ctx = { | ||
amplify: { | ||
getEnvInfo() { | ||
return { envName }; | ||
}, | ||
}, | ||
print: { | ||
info() { | ||
// noop | ||
}, | ||
}, | ||
} as unknown as $TSContext; | ||
}); | ||
|
||
describe('globalSandboxModeEnabled', () => { | ||
describe('enabled', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta.json`); | ||
}); | ||
|
||
it('returns true', async () => { | ||
expect(globalSandboxModeEnabled(ctx)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('not specified', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta-2.json`); | ||
}); | ||
|
||
it('returns false', async () => { | ||
expect(globalSandboxModeEnabled(ctx)).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('showGlobalSandboxModeWarning', () => { | ||
describe('sandbox mode enabled', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta.json`); | ||
}); | ||
|
||
it('prints warning message', async () => { | ||
jest.spyOn(ctx.print, 'info'); | ||
|
||
await showGlobalSandboxModeWarning(ctx); | ||
|
||
expect(ctx.print.info).toBeCalledWith(` | ||
⚠️ WARNING: ${chalk.green('"type AMPLIFY_GLOBAL @allow_public_data_access_with_api_key"')} in your GraphQL schema | ||
allows public create, read, update, and delete access to all models via API Key. This | ||
should only be used for testing purposes. API Key expiration date is: 8/20/2021 | ||
To configure PRODUCTION-READY authorization rules, review: https://docs.amplify.aws/cli/graphql-transformer/auth | ||
`); | ||
}); | ||
}); | ||
|
||
describe('sandbox mode not specified', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta-2.json`); | ||
}); | ||
|
||
it('does not print warning', async () => { | ||
jest.spyOn(ctx.print, 'info'); | ||
|
||
await showGlobalSandboxModeWarning(ctx); | ||
|
||
expect(ctx.print.info).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe('no api key config', () => { | ||
beforeAll(() => { | ||
amplifyMeta = fs.readFileSync(`${__dirname}/testData/mockLocalCloud/amplify-meta-3.json`); | ||
}); | ||
|
||
it('does not print warning', async () => { | ||
jest.spyOn(ctx.print, 'info'); | ||
|
||
await showGlobalSandboxModeWarning(ctx); | ||
|
||
expect(ctx.print.info).toBeCalledTimes(0); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.