Skip to content

Commit

Permalink
chore: latest headless add auth app secret
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolk committed Jul 12, 2023
1 parent b179aa4 commit 06689c2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 28 deletions.
11 changes: 11 additions & 0 deletions packages/amplify-e2e-core/src/init/amplifyPush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ export const amplifyPushLegacy = async (cwd: string): Promise<void> => {
.runAsync();
};

/**
* Function to test amplify push with --yes
*/
export const amplifyPushNonInteractive = async (cwd: string, testingWithLatestCodebase = false): Promise<void> => {
await spawn(getCLIPath(testingWithLatestCodebase), ['push', '--yes'], {
cwd,
stripColors: true,
noOutputTimeout: pushTimeoutMS,
}).runAsync();
};

/**
* Function to test amplify push with codegen for graphql API
*/
Expand Down
32 changes: 31 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,8 @@ import AWSAppSyncClient, { AUTH_TYPE } from 'aws-appsync';
import { CognitoIdentityServiceProvider } from 'aws-sdk';
import fs from 'fs-extra';
import path from 'path';
import { getBackendAmplifyMeta, getProjectMeta } from './projectMeta';
import { getAwsAndroidConfig, getAwsIOSConfig, getBackendAmplifyMeta, getProjectMeta } from './projectMeta';
import { getUserPoolClients } from './sdk-calls';

const tempPassword = 'tempPassword1@';

Expand Down Expand Up @@ -196,3 +197,32 @@ export function getAppClientIDWeb(projectDir: string) {

return cognitoResource.output.AppClientIDWeb;
}

/**
* asserts app client secret in projects files and on cloud
*/
export const assertAppClientSecretInFiles = async (projRoot: string, frontend: 'android' | 'ios'): Promise<void> => {
let config;
switch (frontend) {
case 'android':
config = await getAwsAndroidConfig(projRoot);
break;
case 'ios':
config = await getAwsIOSConfig(projRoot);
break;
}
const clientSecretInAwsConfig = config.CognitoUserPool.Default.AppClientSecret;
expect(clientSecretInAwsConfig).toBeDefined();
const meta = getProjectMeta(projRoot);
const id = Object.keys(meta.auth)[0];
const authMeta = meta.auth[id];
const clientIds = [authMeta.output.AppClientID];
const clientSecretInMetaFile = authMeta.output.AppClientSecret;
// compare client secret in meta file and ios config file
expect(clientSecretInMetaFile).toBeDefined();
expect(clientSecretInAwsConfig).toEqual(clientSecretInMetaFile);
const clients = await getUserPoolClients(authMeta.output.UserPoolId, clientIds, meta.providers.awscloudformation.Region);
expect(clients[0].UserPoolClient.ClientSecret).toBeDefined();
// compare client secret in meta file with cloud value
expect(clients[0].UserPoolClient.ClientSecret).toEqual(clientSecretInMetaFile);
};
63 changes: 63 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/auth_5g.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable import/no-extraneous-dependencies */
import {
deleteProject,
amplifyPushNonInteractive,
addHeadlessAuth,
createNewProjectDir,
deleteProjectDir,
initAndroidProjectWithProfile,
getCLIInputs,
setCLIInputs,
} from '@aws-amplify/amplify-e2e-core';
import {
// eslint-disable-next-line spellcheck/spell-checker
AddAuthRequest,
CognitoUserPoolSigninMethod,
CognitoUserProperty,
} from 'amplify-headless-interface';
import { assertAppClientSecretInFiles } from '@aws-amplify/amplify-e2e-core/src/utils/auth-utils';

const PROJECT_NAME = 'authTest';
const defaultsSettings = {
name: PROJECT_NAME,
};

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

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

it('adds auth resource to android project', async () => {
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 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);

await amplifyPushNonInteractive(projRoot);

await assertAppClientSecretInFiles(projRoot, 'android');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
getCLIInputs,
getProjectMeta,
setCLIInputs,
assertAppClientSecretInFiles,
} from '@aws-amplify/amplify-e2e-core';
import { allowedVersionsToMigrateFrom, versionCheck } from '../../migration-helpers';
import { initAndroidProjectWithProfileV12 } from '../../migration-helpers-v12/init';
import { assertAppClientSecretInFiles, pullPushForceWithLatestCodebaseValidateParameterAndCfnDrift } from '../../migration-helpers/utils';
import { pullPushForceWithLatestCodebaseValidateParameterAndCfnDrift } from '../../migration-helpers/utils';

const defaultsSettings = {
name: 'authTest',
Expand Down Expand Up @@ -54,7 +55,7 @@ describe('amplify add auth...', () => {

it('...should init an Android project and add default auth', async () => {
// assert client secret in projRoot
await assertAppClientSecretInFiles(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
Expand All @@ -75,7 +76,7 @@ describe('amplify add auth...', () => {
envName,
});
await amplifyPushForce(projRoot3, true);
await assertAppClientSecretInFiles(projRoot3);
await assertAppClientSecretInFiles(projRoot3, 'android');
} finally {
deleteProjectDir(projRoot3);
}
Expand Down
24 changes: 0 additions & 24 deletions packages/amplify-migration-tests/src/migration-helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import {
createNewProjectDir,
deleteProjectDir,
getAppId,
getAwsAndroidConfig,
getBackendConfig,
getCLIInputs,
getCloudFormationTemplate,
getParameters,
getProjectMeta,
getUserPoolClients,
parametersExists,
} from '@aws-amplify/amplify-e2e-core';
import * as cfnDiff from '@aws-cdk/cloudformation-diff';
Expand Down Expand Up @@ -192,24 +189,3 @@ export const pullPushForceWithLatestCodebaseValidateParameterAndCfnDrift = async
deleteProjectDir(projRoot2);
}
};

/**
* asserts app client secret in projects files and on cloud
*/
export const assertAppClientSecretInFiles = async (projRoot: string): Promise<void> => {
const config = await getAwsAndroidConfig(projRoot);
const clientSecretInAwsIOSConfig = config.CognitoUserPool.Default.AppClientSecret;
expect(clientSecretInAwsIOSConfig).toBeDefined();
const meta = getProjectMeta(projRoot);
const id = Object.keys(meta.auth)[0];
const authMeta = meta.auth[id];
const clientIds = [authMeta.output.AppClientID];
const clientSecretInMetaFile = authMeta.output.AppClientSecret;
// compare client secret in meta file and ios config file
expect(clientSecretInMetaFile).toBeDefined();
expect(clientSecretInAwsIOSConfig).toEqual(clientSecretInMetaFile);
const clients = await getUserPoolClients(authMeta.output.UserPoolId, clientIds, meta.providers.awscloudformation.Region);
expect(clients[0].UserPoolClient.ClientSecret).toBeDefined();
// compare client secret in meta file with cloud value
expect(clients[0].UserPoolClient.ClientSecret).toEqual(clientSecretInMetaFile);
};

0 comments on commit 06689c2

Please sign in to comment.