Skip to content

Commit

Permalink
chore: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolk committed Jul 13, 2023
1 parent 06689c2 commit 2278758
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 53 deletions.
13 changes: 12 additions & 1 deletion packages/amplify-e2e-core/src/utils/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AWSAppSyncClient, { AUTH_TYPE } from 'aws-appsync';
import { CognitoIdentityServiceProvider } from 'aws-sdk';
import fs from 'fs-extra';
import path from 'path';
import { getAwsAndroidConfig, getAwsIOSConfig, getBackendAmplifyMeta, getProjectMeta } from './projectMeta';
import { getAwsAndroidConfig, getAwsIOSConfig, getBackendAmplifyMeta, getCLIInputs, getProjectMeta, setCLIInputs } from './projectMeta';
import { getUserPoolClients } from './sdk-calls';

const tempPassword = 'tempPassword1@';
Expand Down Expand Up @@ -226,3 +226,14 @@ export const assertAppClientSecretInFiles = async (projRoot: string, frontend: '
// compare client secret in meta file with cloud value
expect(clients[0].UserPoolClient.ClientSecret).toEqual(clientSecretInMetaFile);
};

export const updateCLIParametersToGenerateUserPoolClientSecret = (projRoot: string, resourceName?: string) => {
if (!resourceName) {
const meta = getProjectMeta(projRoot);
resourceName = Object.keys(meta.auth)[0];
}
// update parameter to generate client Secret
const parameters = getCLIInputs(projRoot, 'auth', resourceName);
parameters.cognitoConfig.userpoolClientGenerateSecret = true;
setCLIInputs(projRoot, 'auth', resourceName, parameters);
};
8 changes: 2 additions & 6 deletions packages/amplify-e2e-tests/src/__tests__/auth_1c.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import {
getAwsIOSConfig,
getUserPoolClients,
initIosProjectWithProfile,
getCLIInputs,
setCLIInputs,
addAuthWithDefault,
createNewProjectDir,
deleteProjectDir,
getProjectMeta,
updateCLIParametersToGenerateUserPoolClientSecret,
} from '@aws-amplify/amplify-e2e-core';

const defaultsSettings = {
Expand Down Expand Up @@ -40,10 +39,7 @@ describe('amplify add auth...', () => {
let clients = await getUserPoolClients(authMeta.output.UserPoolId, clientIds, meta.providers.awscloudformation.Region);
expect(clients[0].UserPoolClient.ClientSecret).toBeUndefined();

// update parameter to generate client Secret
const parameters = getCLIInputs(projRoot, 'auth', id);
parameters.cognitoConfig.userpoolClientGenerateSecret = true;
setCLIInputs(projRoot, 'auth', id, parameters);
updateCLIParametersToGenerateUserPoolClientSecret(projRoot);

await amplifyPushAuth(projRoot);

Expand Down
8 changes: 2 additions & 6 deletions packages/amplify-e2e-tests/src/__tests__/auth_5g.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
createNewProjectDir,
deleteProjectDir,
initAndroidProjectWithProfile,
getCLIInputs,
setCLIInputs,
updateCLIParametersToGenerateUserPoolClientSecret,
} from '@aws-amplify/amplify-e2e-core';
import {
// eslint-disable-next-line spellcheck/spell-checker
Expand Down Expand Up @@ -51,10 +50,7 @@ describe('headless auth g', () => {
await initAndroidProjectWithProfile(projRoot, defaultsSettings);
await addHeadlessAuth(projRoot, addAuthRequest);

// update parameter to generate client Secret
const parameters = getCLIInputs(projRoot, 'auth', addAuthRequest.resourceName);
parameters.cognitoConfig.userpoolClientGenerateSecret = true;
setCLIInputs(projRoot, 'auth', addAuthRequest.resourceName, parameters);
updateCLIParametersToGenerateUserPoolClientSecret(projRoot, addAuthRequest.resourceName);

await amplifyPushNonInteractive(projRoot);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import {
deleteProject,
deleteProjectDir,
getAppId,
getCLIInputs,
getProjectMeta,
setCLIInputs,
assertAppClientSecretInFiles,
updateAuthAddUserGroups,
addHeadlessAuth,
amplifyPushNonInteractive,
updateHeadlessAuth,
updateCLIParametersToGenerateUserPoolClientSecret,
} from '@aws-amplify/amplify-e2e-core';
import { allowedVersionsToMigrateFrom, versionCheck } from '../../migration-helpers';
import { initAndroidProjectWithProfileV12 } from '../../migration-helpers-v12/init';
import { pullPushForceWithLatestCodebaseValidateParameterAndCfnDrift } from '../../migration-helpers/utils';
import { AddAuthRequest, CognitoUserPoolSigninMethod, CognitoUserProperty, UpdateAuthRequest } from 'amplify-headless-interface';

const defaultsSettings = {
name: 'authTest',
Expand All @@ -36,49 +39,108 @@ describe('amplify add auth...', () => {

beforeEach(async () => {
projRoot = await createNewProjectDir(projectName);
await initAndroidProjectWithProfileV12(projRoot, defaultsSettings);
await addAuthWithDefault(projRoot);
await amplifyPushAuth(projRoot);
let meta = getProjectMeta(projRoot);
let id = Object.keys(meta.auth)[0];
// update parameter to generate client Secret
const parameters = getCLIInputs(projRoot, 'auth', id);
parameters.cognitoConfig.userpoolClientGenerateSecret = true;
setCLIInputs(projRoot, 'auth', id, parameters);
await amplifyPushAuth(projRoot);
});

afterEach(async () => {
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});

it('...should init an Android project and add default auth', async () => {
// assert client secret in projRoot
await assertAppClientSecretInFiles(projRoot, 'android');
const projRoot2 = await createNewProjectDir(`${projectName}2`);
const projRoot3 = await createNewProjectDir(`${projectName}3`);
// using amplify push force here as changes are only related to build files
await pullPushForceWithLatestCodebaseValidateParameterAndCfnDrift(projRoot, projRoot2);
const appId = getAppId(projRoot);
expect(appId).toBeDefined();
const frontendConfig = {
frontend: 'android',
config: {
ResDir: 'app/src/main/res',
},
};
const envName = 'integtest';
try {
await amplifyPullNonInteractive(projRoot3, {
appId,
frontend: frontendConfig,
envName,
});
await amplifyPushForce(projRoot3, true);
await assertAppClientSecretInFiles(projRoot3, 'android');
} finally {
deleteProjectDir(projRoot3);
}
describe('starting interactively with old...', () => {
beforeEach(async () => {
await initAndroidProjectWithProfileV12(projRoot, defaultsSettings);
await addAuthWithDefault(projRoot);
await amplifyPushAuth(projRoot);
updateCLIParametersToGenerateUserPoolClientSecret(projRoot);
});

it('...should init an Android project and add default auth', async () => {
await amplifyPushAuth(projRoot);
// assert client secret in projRoot
await assertAppClientSecretInFiles(projRoot, 'android');
const projRoot2 = await createNewProjectDir(`${projectName}2`);
const projRoot3 = await createNewProjectDir(`${projectName}3`);
// using amplify push force here as changes are only related to build files
await pullPushForceWithLatestCodebaseValidateParameterAndCfnDrift(projRoot, projRoot2);
const appId = getAppId(projRoot);
expect(appId).toBeDefined();
const frontendConfig = {
frontend: 'android',
config: {
ResDir: 'app/src/main/res',
},
};
const envName = 'integtest';
try {
await amplifyPullNonInteractive(projRoot3, {
appId,
frontend: frontendConfig,
envName,
});
await amplifyPushForce(projRoot3, true);
await assertAppClientSecretInFiles(projRoot3, 'android');
} finally {
deleteProjectDir(projRoot3);
}
});

it('update auth and push with latest interactively, write secret', async () => {
await updateAuthAddUserGroups(projRoot, ['group1', 'group2'], { testingWithLatestCodebase: true });
updateCLIParametersToGenerateUserPoolClientSecret(projRoot);
await amplifyPushAuth(projRoot, true);
await assertAppClientSecretInFiles(projRoot, 'android');
});
});

describe('starting non-interactively with old...', () => {
beforeEach(async () => {
await initAndroidProjectWithProfileV12(projRoot, defaultsSettings);
const addAuthRequest: AddAuthRequest = {
version: 2,
resourceName: 'myAuthResource',
serviceConfiguration: {
serviceName: 'Cognito',
includeIdentityPool: false,
userPoolConfiguration: {
requiredSignupAttributes: [CognitoUserProperty.EMAIL, CognitoUserProperty.PHONE_NUMBER],
// eslint-disable-next-line spellcheck/spell-checker
signinMethod: CognitoUserPoolSigninMethod.USERNAME,
},
},
};
await addHeadlessAuth(projRoot, addAuthRequest);
updateCLIParametersToGenerateUserPoolClientSecret(projRoot, addAuthRequest.resourceName);
await amplifyPushNonInteractive(projRoot);
});

it('update auth and push with latest non-interactively, write secret', async () => {
const updateAuthRequest: UpdateAuthRequest = {
version: 2,
serviceModification: {
serviceName: 'Cognito',
userPoolModification: {
autoVerifiedAttributes: [
{
type: 'EMAIL',
},
],
userPoolGroups: [
{
groupName: 'group1',
},
{
groupName: 'group2',
},
],
},
includeIdentityPool: false,
},
};

await updateHeadlessAuth(projRoot, updateAuthRequest, { testingWithLatestCodebase: true });
updateCLIParametersToGenerateUserPoolClientSecret(projRoot);
await amplifyPushNonInteractive(projRoot, true);
await assertAppClientSecretInFiles(projRoot, 'android');
});
});
});

0 comments on commit 2278758

Please sign in to comment.