Skip to content

Commit

Permalink
fix: e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akshbhu committed Jun 6, 2022
1 parent 74962ba commit 5bc8cb4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,168 +238,7 @@ exports.handler = async function (event, context) {
}
`;
exports[`generate Auth Trigger Template successfully generate auth Trigger Template 1 1`] = `
Object {
"AWSTemplateFormatVersion": "2010-09-09",
"Conditions": Object {
"ShouldNotCreateEnvResources": Object {
"Fn::Equals": Array [
Object {
"Ref": "env",
},
"NONE",
],
},
},
"Description": "Custom Resource stack for Auth Trigger created using Amplify CLI",
"Parameters": Object {
"env": Object {
"Type": "String",
},
"userpoolArn": Object {
"Type": "String",
},
"userpoolId": Object {
"Type": "String",
},
},
"Resources": Object {
"CustomAuthTriggerResource": Object {
"DeletionPolicy": "Delete",
"Properties": Object {
"ServiceToken": Object {
"Fn::GetAtt": Array [
"authTriggerFn7FCFA449",
"Arn",
],
},
"lambdaConfig": Array [],
"userpoolId": Object {
"Ref": "userpoolId",
},
},
"Type": "Custom::CustomAuthTriggerResourceOutputs",
"UpdateReplacePolicy": "Delete",
},
"authTriggerFn7FCFA449": Object {
"DependsOn": Array [
"authTriggerFnServiceRoleDefaultPolicyEC9285A8",
"authTriggerFnServiceRole08093B67",
],
"Properties": Object {
"Code": Object {
"ZipFile": "const response = require('cfn-response');
const aws = require('aws-sdk');
exports.handler = async function (event, context) {
try {
const userPoolId = event.ResourceProperties.userpoolId;
const lambdaConfig = event.ResourceProperties.lambdaConfig;
const config = {};
lambdaConfig.forEach(lambda => (config[\`\${lambda.triggerType}\`] = lambda.lambdaFunctionArn));
if (event.RequestType == 'Delete') {
const authParams = { UserPoolId: userPoolId, LambdaConfig: {} };
const cognitoclient = new aws.CognitoIdentityServiceProvider();
try {
const result = await cognitoclient.updateUserPool(authParams).promise();
console.log('delete response data ' + JSON.stringify(result));
await response.send(event, context, response.SUCCESS, {});
} catch (err) {
console.log(err.stack);
await response.send(event, context, response.FAILED, { err });
}
}
if (event.RequestType == 'Update' || event.RequestType == 'Create') {
const authParams = { UserPoolId: userPoolId, LambdaConfig: config };
console.log(authParams);
const cognitoclient = new aws.CognitoIdentityServiceProvider();
try {
const result = await cognitoclient.updateUserPool(authParams).promise();
console.log('createOrUpdate response data ' + JSON.stringify(result));
await response.send(event, context, response.SUCCESS, { result });
} catch (err) {
console.log(err.stack);
await response.send(event, context, response.FAILED, { err });
}
}
} catch (err) {
console.log(err.stack);
await response.send(event, context, response.FAILED, { err });
}
};
",
},
"Handler": "index.handler",
"Role": Object {
"Fn::GetAtt": Array [
"authTriggerFnServiceRole08093B67",
"Arn",
],
},
"Runtime": "nodejs12.x",
},
"Type": "AWS::Lambda::Function",
},
"authTriggerFnServiceRole08093B67": Object {
"Properties": Object {
"AssumeRolePolicyDocument": Object {
"Statement": Array [
Object {
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": Object {
"Service": "lambda.amazonaws.com",
},
},
],
"Version": "2012-10-17",
},
"ManagedPolicyArns": Array [
Object {
"Fn::Join": Array [
"",
Array [
"arn:",
Object {
"Ref": "AWS::Partition",
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
],
],
},
],
},
"Type": "AWS::IAM::Role",
},
"authTriggerFnServiceRoleDefaultPolicyEC9285A8": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"cognito-idp:DescribeUserPoolClient",
"cognito-idp:UpdateUserPool",
],
"Effect": "Allow",
"Resource": "*",
},
],
"Version": "2012-10-17",
},
"PolicyName": "authTriggerFnServiceRoleDefaultPolicyEC9285A8",
"Roles": Array [
Object {
"Ref": "authTriggerFnServiceRole08093B67",
},
],
},
"Type": "AWS::IAM::Policy",
},
},
}
`;
exports[`generate Auth Trigger Template successfully generate auth Trigger Template 2`] = `
exports[`generate Auth Trigger Template successfully generate auth Trigger Template without permissions 1`] = `
Object {
"AWSTemplateFormatVersion": "2010-09-09",
"Conditions": Object {
Expand Down Expand Up @@ -446,7 +285,7 @@ Object {
"Ref": "functionrandomFnArn",
},
"lambdaFunctionName": "randomFn",
"triggerType": "PostConfirmation",
"triggerType": "PreAuthentication",
},
],
"userpoolId": Object {
Expand All @@ -456,7 +295,7 @@ Object {
"Type": "Custom::CustomAuthTriggerResourceOutputs",
"UpdateReplacePolicy": "Delete",
},
"UserPoolPostConfirmationLambdaInvokePermission": Object {
"UserPoolPreAuthenticationLambdaInvokePermission": Object {
"Properties": Object {
"Action": "lambda:InvokeFunction",
"FunctionName": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ describe('generate Auth Trigger Template', () => {
expect(cfn).toMatchSnapshot();
});

it('successfully generate auth Trigger Template', async () => {
it('successfully generate auth Trigger Template without permissions', async () => {
const mockAuthTriggerConnections: AuthTriggerConnection[] = [
{
lambdaFunctionName: 'randomFn',
triggerType: TriggerType.PostConfirmation,
triggerType: TriggerType.PreAuthentication,
lambdaFunctionArn: 'randomArn',
},
];
Expand Down
27 changes: 15 additions & 12 deletions packages/amplify-e2e-core/src/utils/sdk-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,25 @@ export const getUserPoolClients = async (userPoolId: string, clientIds: string[]
return res;
};

export const adminConfirmSignUp = async (userPoolId: string, region: string, userName: string) => {
export const addUserToUserPool = async (userPoolId: string, region: string) => {
const provider = new CognitoIdentityServiceProvider({ region });
const params = {
UserPoolId: userPoolId /* required */,
Username: userName /* required */,
Username: 'testUser' /* required */,
UserAttributes: [
{
Name: 'demoKey' /* required */,
Value: 'randomValue',
},
],
ValidationData: [
{
Name: 'demoKey' /* required */,
Value: 'demoValue',
},
],
};
await provider.adminConfirmSignUp(params).promise();
};

export const adminListGroupForUser = async (userPoolId: string, region: string, userName: string) => {
const provider = new CognitoIdentityServiceProvider({ region });
const params = {
UserPoolId: userPoolId /* required */,
Username: userName /* required */,
};
return await provider.adminListGroupsForUser(params).promise();
const res = await provider.adminCreateUser(params);
};

export const getBot = async (botName: string, region: string) => {
Expand Down
16 changes: 11 additions & 5 deletions packages/amplify-e2e-tests/src/__tests__/auth_2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
adminConfirmSignUp,
invokeFunction,
adminListGroupForUser,
adminCreateUser,
adminSetUserPassword,
addUserToUserPool,
} from 'amplify-e2e-core';
import { addAuthWithDefaultSocial, addAuthWithGroupTrigger, addAuthWithRecaptchaTrigger, addAuthViaAPIWithTrigger } from 'amplify-e2e-core';
import {
Expand Down Expand Up @@ -110,7 +113,7 @@ describe.only('amplify add auth...', () => {
expect(lambdaFunction.Configuration.Environment.Variables.GROUP).toEqual('mygroup');
});

it.only('...should allow the user to add auth via API category, with a trigger and function dependsOn API', async () => {
it('...should allow the user to add auth via API category, with a trigger and function dependsOn API', async () => {
await initJSProjectWithProfile(projRoot, defaultsSettings);
await addAuthwithUserPoolGroupsViaAPIWithTrigger(projRoot, { transformerVersion: 1 });
await updateFunction(
Expand All @@ -137,10 +140,13 @@ describe.only('amplify add auth...', () => {
const userPool = await getUserPool(id, region);
const clientIds = [authMeta.output.AppClientIDWeb, authMeta.output.AppClientID];
const clients = await getUserPoolClients(id, clientIds, region);
const userName = 'testUser';
await adminConfirmSignUp(id, region, userName);
const result = await adminListGroupForUser(id, region, userName);
expect(result.Groups.some(group => group.GroupName === 'mygroup')).toBeTruthy();
await addUserToUserPool(id, region);
const lambdaEvent = {
userPoolId: id,
userName: 'testUser',
};
const result = await invokeFunction(functionName, JSON.stringify(lambdaEvent), region);
expect(result.StatusCode).toBe(200);
expect(userPool.UserPool).toBeDefined();
expect(Object.keys(userPool.UserPool.LambdaConfig)[0]).toBe('PostConfirmation');
expect(Object.values(userPool.UserPool.LambdaConfig)[0]).toBe(meta.function[functionName.split('-')[0]].output.Arn);
Expand Down

0 comments on commit 5bc8cb4

Please sign in to comment.