forked from aws-amplify/amplify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(amplify-category-api): prompt api key creation on amplify push (a…
…ws-amplify#8124) * feat(amplify-category-api): prompt api key create when invalid with sandbox mode * test(amplify-category-api): add unit tests for provider utils * test(amplify-category-api): fix test for adding api key prompt * refactor(cli): refactor api key prompt * refactor(amplify-category-api): add api key with gql compiled
- Loading branch information
1 parent
28d2748
commit e682189
Showing
8 changed files
with
92 additions
and
3 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
44 changes: 44 additions & 0 deletions
44
...category-api/src/__tests__/provider-utils/awscloudformation/prompt-to-add-api-key.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,44 @@ | ||
import { $TSContext } from 'amplify-cli-core'; | ||
import * as prompts from 'amplify-prompts'; | ||
import { promptToAddApiKey } from '../../../provider-utils/awscloudformation/prompt-to-add-api-key'; | ||
import * as walkthrough from '../../../provider-utils/awscloudformation/service-walkthroughs/appSync-walkthrough'; | ||
import * as cfnApiArtifactHandler from '../../../provider-utils/awscloudformation/cfn-api-artifact-handler'; | ||
|
||
jest.mock('../../../provider-utils/awscloudformation/service-walkthroughs/appSync-walkthrough', () => ({ | ||
askApiKeyQuestions: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../provider-utils/awscloudformation/cfn-api-artifact-handler', () => ({ | ||
getCfnApiArtifactHandler: jest.fn(() => { | ||
return { updateArtifacts: jest.fn() }; | ||
}), | ||
})); | ||
|
||
jest.mock('amplify-prompts', () => ({ | ||
prompter: { | ||
confirmContinue: jest.fn().mockImplementation(() => true), | ||
}, | ||
})); | ||
|
||
describe('prompt to add Api Key', () => { | ||
it('runs through expected user flow: print info, update files', async () => { | ||
const envName = 'envone'; | ||
const ctx = { | ||
amplify: { | ||
getEnvInfo() { | ||
return { envName }; | ||
}, | ||
}, | ||
} as unknown as $TSContext; | ||
|
||
jest.spyOn(prompts.prompter, 'confirmContinue'); | ||
jest.spyOn(walkthrough, 'askApiKeyQuestions'); | ||
jest.spyOn(cfnApiArtifactHandler, 'getCfnApiArtifactHandler'); | ||
|
||
await promptToAddApiKey(ctx); | ||
|
||
expect(prompts.prompter.confirmContinue).toHaveBeenCalledWith('Would you like to create an API Key?'); | ||
expect(walkthrough.askApiKeyQuestions).toHaveBeenCalledTimes(1); | ||
expect(cfnApiArtifactHandler.getCfnApiArtifactHandler).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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
20 changes: 20 additions & 0 deletions
20
packages/amplify-category-api/src/provider-utils/awscloudformation/prompt-to-add-api-key.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,20 @@ | ||
import { $TSContext } from 'amplify-cli-core'; | ||
import { askApiKeyQuestions } from './service-walkthroughs/appSync-walkthrough'; | ||
import { authConfigToAppSyncAuthType } from './utils/auth-config-to-app-sync-auth-type-bi-di-mapper'; | ||
import { getCfnApiArtifactHandler } from './cfn-api-artifact-handler'; | ||
import { prompter } from 'amplify-prompts'; | ||
|
||
export async function promptToAddApiKey(context: $TSContext): Promise<void> { | ||
if (await prompter.confirmContinue('Would you like to create an API Key?')) { | ||
const apiKeyConfig = await askApiKeyQuestions(); | ||
const authConfig = [apiKeyConfig]; | ||
|
||
await getCfnApiArtifactHandler(context).updateArtifacts({ | ||
version: 1, | ||
serviceModification: { | ||
serviceName: 'AppSync', | ||
additionalAuthTypes: authConfig.map(authConfigToAppSyncAuthType), | ||
}, | ||
}); | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
...es/amplify-category-api/src/provider-utils/awscloudformation/utils/global-sandbox-mode.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
19 changes: 19 additions & 0 deletions
19
packages/amplify-cli/src/extensions/amplify-helpers/prompt-sandbox-mode-api-key.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,19 @@ | ||
import chalk from 'chalk'; | ||
import { $TSContext } from 'amplify-cli-core'; | ||
import { promptToAddApiKey } from 'amplify-category-api'; | ||
import { globalSandboxModeEnabled, showGlobalSandboxModeWarning } from './show-global-sandbox-mode-warning'; | ||
import { apiKeyIsActive, hasApiKey } from './get-api-key-config'; | ||
|
||
export async function promptSandboxModeApiKey(context: $TSContext): Promise<void> { | ||
if (globalSandboxModeEnabled(context)) { | ||
if (!apiKeyIsActive() || !hasApiKey()) { | ||
context.print.info(` | ||
⚠️ WARNING: Global Sandbox Mode has been enabled, which requires a valid API key. If | ||
you'd like to disable, remove ${chalk.green('"type AMPLIFY_GLOBAL @allow_public_data_access_with_api_key"')} | ||
from your GraphQL schema and run 'amplify push' again. If you'd like to proceed with | ||
sandbox mode disabled in '${context.amplify.getEnvInfo().envName}', do not create an API Key. | ||
`); | ||
await promptToAddApiKey(context); | ||
} else showGlobalSandboxModeWarning(context); | ||
} | ||
} |
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