Skip to content

Commit

Permalink
fix(amplify-category-api): change auth directive type and fix codegen…
Browse files Browse the repository at this point in the history
… bug (#8639)
  • Loading branch information
danielleadams committed Nov 3, 2021
1 parent 48f8cfd commit b8d838d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
Expand Up @@ -4,7 +4,7 @@ describe('global sandbox mode GraphQL directive', () => {
it('returns input AMPLIFY with code comment', () => {
expect(defineGlobalSandboxMode()).toEqual(`# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql-transformer/auth
input AMPLIFY { global_auth_rule: AuthorizationRule = { allow: public } } # FOR TESTING ONLY!\n
input AMPLIFY { global_auth_rule: AuthRule = { allow: public } } # FOR TESTING ONLY!\n
`);
});
});
@@ -1,6 +1,6 @@
export function defineGlobalSandboxMode(): string {
return `# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql-transformer/auth
input AMPLIFY { global_auth_rule: AuthorizationRule = { allow: public } } # FOR TESTING ONLY!\n
input AMPLIFY { global_auth_rule: AuthRule = { allow: public } } # FOR TESTING ONLY!\n
`;
}
@@ -1,5 +1,5 @@
input AMPLIFY {
global_auth_rule: AuthorizationRule = { allow: public }
global_auth_rule: AuthRule = { allow: public }
}

type Todo @model {
Expand Down
Expand Up @@ -8,6 +8,7 @@ import {
updateApiSchema,
apiGqlCompile,
amplifyPush,
generateModels,
} from 'amplify-e2e-core';
import { createNewProjectDir, deleteProjectDir } from 'amplify-e2e-core';

Expand All @@ -29,19 +30,22 @@ describe('global sandbox mode', () => {
it('compiles schema with one model and pushes to cloud', async () => {
await addApiWithOneModel(projectDir);
await apiGqlCompile(projectDir, true);
await generateModels(projectDir);
await amplifyPush(projectDir, true);
});

it.skip('compiles schema with three models and pushes to cloud', async () => {
await addApiWithThreeModels(projectDir);
await apiGqlCompile(projectDir, true);
await generateModels(projectDir);
await amplifyPush(projectDir, true);
});

it('compiles schema user-added schema and pushes to cloud', async () => {
await addApiWithoutSchema(projectDir, { apiName });
updateApiSchema(projectDir, apiName, 'model_with_sandbox_mode.graphql');
await apiGqlCompile(projectDir, true);
await generateModels(projectDir);
await amplifyPush(projectDir, true);
});
});
Expand Up @@ -103,7 +103,6 @@ scalar AWSPhone
scalar AWSIPAddress
scalar BigInt
scalar Double
scalar AuthorizationRule
`);

export const EXTRA_DIRECTIVES_DOCUMENT = parse(`
Expand Down Expand Up @@ -160,7 +159,13 @@ export const validateAuthModes = (authConfig: AppSyncAuthConfiguration) => {
for (let i = 0; i < authModes.length; i++) {
const mode = authModes[i];

if (mode !== 'API_KEY' && mode !== 'AMAZON_COGNITO_USER_POOLS' && mode !== 'AWS_IAM' && mode !== 'OPENID_CONNECT' && mode !== 'AWS_LAMBDA') {
if (
mode !== 'API_KEY' &&
mode !== 'AMAZON_COGNITO_USER_POOLS' &&
mode !== 'AWS_IAM' &&
mode !== 'OPENID_CONNECT' &&
mode !== 'AWS_LAMBDA'
) {
throw new Error(`Invalid auth mode ${mode}`);
}
}
Expand Down
Expand Up @@ -35,7 +35,7 @@ describe('sandbox mode helpers', () => {
expect(prompts.printer.info).toBeCalledWith(
`
⚠️ WARNING: Global Sandbox Mode has been enabled, which requires a valid API key. If
you'd like to disable, remove ${chalk.green('"input AMPLIFY { global_auth_rule: AuthorizationRule = { allow: public } }"')}
you'd like to disable, remove ${chalk.green('"input AMPLIFY { global_auth_rule: 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.
`,
Expand All @@ -62,7 +62,7 @@ sandbox mode disabled, do not create an API Key.
describe('schemaHasSandboxModeEnabled', () => {
it('parses sandbox AMPLIFY input on schema', () => {
const schema = `
input AMPLIFY { global_auth_rule: AuthorizationRule = { allow: public } }
input AMPLIFY { global_auth_rule: AuthRule = { allow: public } }
`;

expect(schemaHasSandboxModeEnabled(schema)).toEqual(true);
Expand Down Expand Up @@ -90,7 +90,7 @@ sandbox mode disabled, do not create an API Key.
);
});

it('guards for AuthorizationRule', () => {
it('guards for AuthRule', () => {
const schema = `
input AMPLIFY { global_auth_rule: AuthenticationRule = { allow: public } }
`;
Expand All @@ -104,7 +104,7 @@ sandbox mode disabled, do not create an API Key.

it('checks for "allow" field name', () => {
const schema = `
input AMPLIFY { global_auth_rule: AuthorizationRule = { allows: public } }
input AMPLIFY { global_auth_rule: AuthRule = { allows: public } }
`;

expect(() => schemaHasSandboxModeEnabled(schema)).toThrow(
Expand All @@ -116,7 +116,7 @@ sandbox mode disabled, do not create an API Key.

it('checks for "public" value from "allow" field', () => {
const schema = `
input AMPLIFY { global_auth_rule: AuthorizationRule = { allow: private } }
input AMPLIFY { global_auth_rule: AuthRule = { allow: private } }
`;

expect(() => schemaHasSandboxModeEnabled(schema)).toThrowError(
Expand Down
Expand Up @@ -6,7 +6,7 @@ import { parse } from 'graphql';

const AMPLIFY = 'AMPLIFY';
const GLOBAL_AUTH_RULE = 'global_auth_rule';
const AUTHORIZATION_RULE = 'AuthorizationRule';
const AUTHORIZATION_RULE = 'AuthRule';
const ALLOW = 'allow';
const PUBLIC = 'public';

Expand All @@ -15,7 +15,7 @@ export async function showSandboxModePrompts(context: $TSContext): Promise<any>
printer.info(
`
⚠️ WARNING: Global Sandbox Mode has been enabled, which requires a valid API key. If
you'd like to disable, remove ${chalk.green('"input AMPLIFY { global_auth_rule: AuthorizationRule = { allow: public } }"')}
you'd like to disable, remove ${chalk.green('"input AMPLIFY { global_auth_rule: 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.
`,
Expand Down

0 comments on commit b8d838d

Please sign in to comment.