Skip to content

Commit

Permalink
fix(lambda): save attributes from user migration lambda on user record
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Apr 13, 2020
1 parent 7223ea6 commit dc7a1c6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/services/triggers/userMigration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,26 @@ describe("UserMigration trigger", () => {
});

describe("when lambda invoke succeeds", () => {
it("saves user", async () => {
mockLambda.invoke.mockResolvedValue({});
it("saves user with attributes from response", async () => {
mockLambda.invoke.mockResolvedValue({
userAttributes: {
email: "[email protected]",
},
});

const user = await userMigration({
userPoolId: "userPoolId",
clientId: "clientId",
username: "[email protected]",
username: "[email protected]", // username may be an email when migration is from a login attempt
password: "password",
userAttributes: [{ Name: "email", Value: "[email protected]" }],
userAttributes: [], // there won't be any attributes yet because we don't know who the user is
});

expect(mockLambda.invoke).toHaveBeenCalledWith("UserMigration", {
clientId: "clientId",
password: "password",
triggerSource: "UserMigration_Authentication",
userAttributes: { email: "[email protected]" },
userAttributes: {},
userPoolId: "userPoolId",
username: "[email protected]",
});
Expand Down
9 changes: 7 additions & 2 deletions src/services/triggers/userMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import * as uuid from "uuid";
import { NotAuthorizedError } from "../../errors";
import { UserPool } from "../index";
import { CognitoUserPoolResponse, Lambda } from "../lambda";
import { attributesToRecord, User, UserAttribute } from "../userPool";
import {
attributesFromRecord,
attributesToRecord,
User,
UserAttribute,
} from "../userPool";

export type UserMigrationTrigger = (params: {
userPoolId: string;
Expand Down Expand Up @@ -41,7 +46,7 @@ export const UserMigration = ({
}

const user: User = {
Attributes: userAttributes,
Attributes: attributesFromRecord(result.userAttributes ?? {}),
Enabled: true,
Password: password,
UserCreateDate: new Date().getTime(),
Expand Down
14 changes: 13 additions & 1 deletion src/services/userPool.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CreateDataStore, DataStore } from "./dataStore";
import {
attributesFromRecord,
attributesInclude,
attributesIncludeMatch,
attributesToRecord,
Expand Down Expand Up @@ -201,10 +202,21 @@ describe("User Pool", () => {
describe("attributesToRecord", () => {
it("converts the attributes to a record", () => {
expect(attributesToRecord(attributes)).toEqual({
email: "[email protected]",
sub: "uuid",
email: "[email protected]",
});
});
});

describe("attributesFromRecord", () => {
it("converts the attributes to a record", () => {
expect(
attributesFromRecord({
sub: "uuid",
email: "[email protected]",
})
).toEqual(attributes);
});
});
});
});
5 changes: 5 additions & 0 deletions src/services/userPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export const attributesToRecord = (
): Record<string, string> =>
attributes.reduce((acc, attr) => ({ ...acc, [attr.Name]: attr.Value }), {});

export const attributesFromRecord = (
attributes: Record<string, string>
): readonly UserAttribute[] =>
Object.entries(attributes).map(([Name, Value]) => ({ Name, Value }));

export interface User {
Username: string;
UserCreateDate: number;
Expand Down

0 comments on commit dc7a1c6

Please sign in to comment.