Skip to content

Commit

Permalink
test: add headless auth e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Foyle committed Sep 23, 2020
1 parent c1c3ed5 commit a924a48
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 10 deletions.
27 changes: 21 additions & 6 deletions packages/amplify-e2e-core/src/utils/headless.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { AddApiRequest, UpdateApiRequest } from 'amplify-headless-interface';
import { AddApiRequest, AddAuthRequest, UpdateApiRequest, UpdateAuthRequest } from 'amplify-headless-interface';
import execa from 'execa';
import { getCLIPath } from '..';

export const addHeadlessApi = (cwd: string, request: AddApiRequest) => {
return executeHeadlessCommand(cwd, 'api', 'add', request);
export const addHeadlessApi = async (cwd: string, request: AddApiRequest) => {
await executeHeadlessCommand(cwd, 'api', 'add', request);
};

export const updateHeadlessApi = async (cwd: string, request: UpdateApiRequest) => {
return executeHeadlessCommand(cwd, 'api', 'update', request);
await executeHeadlessCommand(cwd, 'api', 'update', request);
};

export const removeHeadlessApi = async (cwd: string, apiName: string) => {
await execa(getCLIPath(), ['remove', 'api', apiName, '--yes'], { cwd });
await headlessRemoveResource(cwd, 'api', apiName);
};

export const addHeadlessAuth = async (cwd: string, request: AddAuthRequest) => {
await executeHeadlessCommand(cwd, 'auth', 'add', request);
};

export const updateHeadlessAuth = async (cwd: string, request: UpdateAuthRequest) => {
await executeHeadlessCommand(cwd, 'auth', 'update', request);
};

export const removeHeadlessAuth = async (cwd: string, authName: string) => {
await headlessRemoveResource(cwd, 'auth', authName);
};

const headlessRemoveResource = async (cwd: string, category: string, resourceName: string) => {
await execa(getCLIPath(), ['remove', category, resourceName, '--yes'], { cwd });
};
const executeHeadlessCommand = async (cwd: string, category: string, operation: string, request: AnyHeadlessRequest) => {
await execa(getCLIPath(), [operation, category, '--headless'], { input: JSON.stringify(request), cwd });
};

type AnyHeadlessRequest = AddApiRequest | UpdateApiRequest;
type AnyHeadlessRequest = AddApiRequest | UpdateApiRequest | AddAuthRequest | UpdateAuthRequest;
86 changes: 82 additions & 4 deletions packages/amplify-e2e-tests/src/__tests__/auth_2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import {
initIosProjectWithProfile,
deleteProject,
amplifyPushAuth,
amplifyPush,
addHeadlessAuth,
updateHeadlessAuth,
removeHeadlessAuth,
} from 'amplify-e2e-core';
import {
addAuthWithDefault,
removeAuthWithDefault,
addAuthWithDefaultSocial,
addAuthWithGroupTrigger,
addAuthWithRecaptchaTrigger,
addAuthWithCustomTrigger,
addAuthWithSignInSignOutUrl,
updateAuthWithoutCustomTrigger,
updateAuthRemoveRecaptchaTrigger,
updateAuthSignInSignOutUrl,
addAuthViaAPIWithTrigger,
addAuthWithMaxOptions,
addAuthUserPoolOnly,
getBackendAmplifyMeta,
Expand All @@ -33,6 +32,8 @@ import {
getUserPoolClients,
getLambdaFunction,
} from 'amplify-e2e-core';
import { AddAuthRequest, CognitoUserPoolSigninMethod, CognitoUserProperty, UpdateAuthRequest } from 'amplify-headless-interface';
import _ from 'lodash';

const defaultsSettings = {
name: 'authTest',
Expand Down Expand Up @@ -192,3 +193,80 @@ describe('amplify updating auth...', () => {
expect(meta.Auth.Default.authenticationFlowType).toEqual('USER_SRP_AUTH');
});
});

describe('headless auth', () => {
let projRoot: string;
beforeEach(async () => {
projRoot = await createNewProjectDir('auth-update');
});

afterEach(async () => {
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});
it('adds auth resource', async () => {
const addAuthRequest: AddAuthRequest = {
version: 1,
resourceName: 'myAuthResource',
serviceConfiguration: {
serviceName: 'Cognito',
includeIdentityPool: false,
userPoolConfiguration: {
requiredSignupAttributes: [CognitoUserProperty.EMAIL, CognitoUserProperty.PHONE_NUMBER],
signinMethod: CognitoUserPoolSigninMethod.USERNAME,
},
},
};

await initJSProjectWithProfile(projRoot, defaultsSettings);
await addHeadlessAuth(projRoot, addAuthRequest);
await amplifyPushAuth(projRoot);
const meta = getProjectMeta(projRoot);
const id = Object.keys(meta.auth).map(key => meta.auth[key])[0].output.UserPoolId;
const userPool = await getUserPool(id, meta.providers.awscloudformation.Region);
expect(userPool.UserPool).toBeDefined();
});

it('updates existing auth resource', async () => {
const updateAuthRequest: UpdateAuthRequest = {
version: 1,
serviceModification: {
serviceName: 'Cognito',
userPoolModification: {
userPoolGroups: [
{
groupName: 'group1',
},
{
groupName: 'group2',
},
],
},
includeIdentityPool: true,
identityPoolModification: {
unauthenticatedLogin: true,
},
},
};

await initJSProjectWithProfile(projRoot, defaultsSettings);
await addAuthWithDefault(projRoot, {});
await updateHeadlessAuth(projRoot, updateAuthRequest);
await amplifyPushAuth(projRoot);
const meta = getProjectMeta(projRoot);
const id = Object.keys(meta.auth).map(key => meta.auth[key])[0].output.UserPoolId;
const userPool = await getUserPool(id, meta.providers.awscloudformation.Region);
expect(userPool.UserPool).toBeDefined();
expect(_.get(meta, ['auth', 'userPoolGroups'])).toBeDefined();
});

it('removes auth resource', async () => {
await initJSProjectWithProfile(projRoot, defaultsSettings);
await addAuthWithDefault(projRoot, {});
const { auth: authBefore } = getBackendAmplifyMeta(projRoot);
const authResourceName = _.keys(authBefore).find(() => true); // first element or undefined
await removeHeadlessAuth(projRoot, authResourceName);
const { auth: authAfter } = getBackendAmplifyMeta(projRoot);
expect(_.isEmpty(authAfter)).toBe(true);
});
});

0 comments on commit a924a48

Please sign in to comment.