Skip to content

Commit

Permalink
feat(api): adminUpdateUserAttributes full support
Browse files Browse the repository at this point in the history
Fixes this API call not actually doing anything. Now will update the attributes, trying to respect Cognito's validation behaviour. Sends messages including calling the CustomMessage trigger.
  • Loading branch information
jagregory committed Dec 10, 2021
1 parent 63fcf47 commit d3c5ebe
Show file tree
Hide file tree
Showing 15 changed files with 590 additions and 340 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ A _Good Enough_ offline emulator for [Amazon Cognito](https://aws.amazon.com/cog
| AdminSetUserSettings ||
| AdminUpdateAuthEventFeedback ||
| AdminUpdateDeviceStatus ||
| AdminUpdateUserAttributes | 🕒 (partial support) |
| AdminUpdateUserAttributes | |
| AdminUserGlobalSignOut ||
| AssociateSoftwareToken ||
| ChangePassword | 🕒 (partial support) |
Expand Down Expand Up @@ -456,4 +456,4 @@ This will configure NodeJS to start the inspector on port `9230`.
Open Chrome and navigate to `chrome://inspect`. Click the `Open dedicated DevTools for Node` link which will open a new
DevTools window. You can then open the Sources tab and browse to a Cognito Local file, or press `Cmd+P` or `Ctrl+P` to
open the file navigator and open `src/bin/start.ts` or a target you want to debug then place a breakpoint and run your
code that uses Cognito Local or a CLI command.
code that uses Cognito Local or a CLI command.
2 changes: 1 addition & 1 deletion integration-tests/aws-sdk/adminConfirmSignUp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { withCognitoSdk } from "./setup";
describe(
"CognitoIdentityServiceProvider.adminConfirmSignUp",
withCognitoSdk((Cognito) => {
it("creates a user with only the required parameters", async () => {
it("confirms a user", async () => {
const client = Cognito();

await client
Expand Down
57 changes: 57 additions & 0 deletions integration-tests/aws-sdk/adminUpdateUserAttributes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { UUID } from "../../src/__tests__/patterns";
import { withCognitoSdk } from "./setup";

describe(
"CognitoIdentityServiceProvider.adminUpdateUserAttributes",
withCognitoSdk((Cognito) => {
it("updates a user's attributes", async () => {
const client = Cognito();

await client
.adminCreateUser({
UserAttributes: [
{ Name: "email", Value: "[email protected]" },
{ Name: "phone_number", Value: "0400000000" },
],
Username: "abc",
UserPoolId: "test",
})
.promise();

let user = await client
.adminGetUser({
UserPoolId: "test",
Username: "abc",
})
.promise();

expect(user.UserAttributes).toEqual([
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "phone_number", Value: "0400000000" },
]);

await client
.adminUpdateUserAttributes({
UserPoolId: "test",
Username: "abc",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
})
.promise();

user = await client
.adminGetUser({
UserPoolId: "test",
Username: "abc",
})
.promise();

expect(user.UserAttributes).toEqual([
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "phone_number", Value: "0400000000" },
{ Name: "email_verified", Value: "false" },
]);
});
})
);
10 changes: 6 additions & 4 deletions src/__tests__/mockUserPoolService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { UserPoolService } from "../services";
import { UserPoolServiceFactory } from "../services/userPoolService";
import { UserPool, UserPoolServiceFactory } from "../services/userPoolService";

export const newMockUserPoolService = (): jest.Mocked<UserPoolService> => ({
config: {
export const newMockUserPoolService = (
config: UserPool = {
Id: "test",
},
}
): jest.Mocked<UserPoolService> => ({
config,
createAppClient: jest.fn(),
deleteUser: jest.fn(),
getUserByRefreshToken: jest.fn(),
Expand Down
3 changes: 2 additions & 1 deletion src/services/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ interface EventCommonParameters {
userPoolId: string;
}

interface CustomMessageEvent extends EventCommonParameters {
interface CustomMessageEvent extends Omit<EventCommonParameters, "clientId"> {
clientId: string | null;
clientMetadata: Record<string, string> | undefined;
codeParameter: string;
triggerSource:
Expand Down
32 changes: 32 additions & 0 deletions src/services/messageDelivery/deliveryMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { VerifiedAttributesListType } from "aws-sdk/clients/cognitoidentityserviceprovider";
import { attributeValue, User } from "../userPoolService";
import { DeliveryDetails } from "./messageDelivery";

export const selectAppropriateDeliveryMethod = (
desiredDeliveryMediums: VerifiedAttributesListType,
user: User
): DeliveryDetails | null => {
if (desiredDeliveryMediums.includes("phone_number")) {
const phoneNumber = attributeValue("phone_number", user.Attributes);
if (phoneNumber) {
return {
AttributeName: "phone_number",
DeliveryMedium: "SMS",
Destination: phoneNumber,
};
}
}

if (desiredDeliveryMediums.includes("email")) {
const email = attributeValue("email", user.Attributes);
if (email) {
return {
AttributeName: "email",
DeliveryMedium: "EMAIL",
Destination: email,
};
}
}

return null;
};
Loading

0 comments on commit d3c5ebe

Please sign in to comment.