diff --git a/src/services/triggers/userMigration.test.ts b/src/services/triggers/userMigration.test.ts index b13feb78..ad1e7909 100644 --- a/src/services/triggers/userMigration.test.ts +++ b/src/services/triggers/userMigration.test.ts @@ -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: "example@example.com", + }, + }); const user = await userMigration({ userPoolId: "userPoolId", clientId: "clientId", - username: "example@example.com", + username: "example@example.com", // username may be an email when migration is from a login attempt password: "password", - userAttributes: [{ Name: "email", Value: "example@example.com" }], + 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: "example@example.com" }, + userAttributes: {}, userPoolId: "userPoolId", username: "example@example.com", }); diff --git a/src/services/triggers/userMigration.ts b/src/services/triggers/userMigration.ts index 570d4848..d1eb970e 100644 --- a/src/services/triggers/userMigration.ts +++ b/src/services/triggers/userMigration.ts @@ -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; @@ -41,7 +46,7 @@ export const UserMigration = ({ } const user: User = { - Attributes: userAttributes, + Attributes: attributesFromRecord(result.userAttributes ?? {}), Enabled: true, Password: password, UserCreateDate: new Date().getTime(), diff --git a/src/services/userPool.test.ts b/src/services/userPool.test.ts index fe65e986..f7584761 100644 --- a/src/services/userPool.test.ts +++ b/src/services/userPool.test.ts @@ -1,5 +1,6 @@ import { CreateDataStore, DataStore } from "./dataStore"; import { + attributesFromRecord, attributesInclude, attributesIncludeMatch, attributesToRecord, @@ -201,10 +202,21 @@ describe("User Pool", () => { describe("attributesToRecord", () => { it("converts the attributes to a record", () => { expect(attributesToRecord(attributes)).toEqual({ - email: "example@example.com", sub: "uuid", + email: "example@example.com", }); }); }); + + describe("attributesFromRecord", () => { + it("converts the attributes to a record", () => { + expect( + attributesFromRecord({ + sub: "uuid", + email: "example@example.com", + }) + ).toEqual(attributes); + }); + }); }); }); diff --git a/src/services/userPool.ts b/src/services/userPool.ts index ea9fa036..c4791316 100644 --- a/src/services/userPool.ts +++ b/src/services/userPool.ts @@ -24,6 +24,11 @@ export const attributesToRecord = ( ): Record => attributes.reduce((acc, attr) => ({ ...acc, [attr.Name]: attr.Value }), {}); +export const attributesFromRecord = ( + attributes: Record +): readonly UserAttribute[] => + Object.entries(attributes).map(([Name, Value]) => ({ Name, Value })); + export interface User { Username: string; UserCreateDate: number;