// Verifies client-owned account helpers build the expected Cognito IDP requests. jest.mock("../cognito/cognito-fetch"); import { cognitoFetch } from "../cognito/cognito-fetch"; import { changePassword, getUserAttributeVerificationCode, globalSignOut, revokeToken, updateUserAttributes, verifyUserAttribute, } from "../"; const mockedFetch = cognitoFetch as jest.MockedFunction; const region = "us-west-2"; const accessToken = "access-token"; const clientId = "test-client-id"; beforeEach(() => { mockedFetch.mockReset(); mockedFetch.mockResolvedValue({}); }); describe("changePassword", () => { it("builds a ChangePassword request", async () => { await changePassword({ region, accessToken, previousPassword: "OldPassw0rd!", proposedPassword: "NewPassw0rd!", }); expect(mockedFetch).toHaveBeenCalledWith({ region, operation: "ChangePassword", args: { AccessToken: accessToken, PreviousPassword: "OldPassw0rd!", ProposedPassword: "NewPassw0rd!", }, debug: expect.any(Function), }); }); it("omits PreviousPassword when not provided", async () => { await changePassword({ region, accessToken, proposedPassword: "NewPassw0rd!", }); expect(mockedFetch.mock.calls[0][0].args).toEqual({ AccessToken: accessToken, ProposedPassword: "NewPassw0rd!", }); }); }); describe("updateUserAttributes", () => { it("builds an UpdateUserAttributes request", async () => { await updateUserAttributes({ region, accessToken, userAttributes: [ { Name: "email", Value: "new@example.com" }, { Name: "custom:tenantId", Value: "tenant-123" }, ], clientMetadata: { source: "settings" }, }); expect(mockedFetch.mock.calls[0][0]).toMatchObject({ region, operation: "UpdateUserAttributes", args: { AccessToken: accessToken, UserAttributes: [ { Name: "email", Value: "new@example.com" }, { Name: "custom:tenantId", Value: "tenant-123" }, ], ClientMetadata: { source: "settings" }, }, }); }); }); describe("getUserAttributeVerificationCode", () => { it("builds a GetUserAttributeVerificationCode request", async () => { await getUserAttributeVerificationCode({ region, accessToken, attributeName: "email", clientMetadata: { source: "settings" }, }); expect(mockedFetch.mock.calls[0][0]).toMatchObject({ region, operation: "GetUserAttributeVerificationCode", args: { AccessToken: accessToken, AttributeName: "email", ClientMetadata: { source: "settings" }, }, }); }); }); describe("verifyUserAttribute", () => { it("builds a VerifyUserAttribute request", async () => { await verifyUserAttribute({ region, accessToken, attributeName: "email", code: "123456", }); expect(mockedFetch.mock.calls[0][0]).toMatchObject({ region, operation: "VerifyUserAttribute", args: { AccessToken: accessToken, AttributeName: "email", Code: "123456", }, }); }); }); describe("globalSignOut", () => { it("builds a GlobalSignOut request", async () => { await globalSignOut({ region, accessToken }); expect(mockedFetch.mock.calls[0][0]).toMatchObject({ region, operation: "GlobalSignOut", args: { AccessToken: accessToken }, }); }); }); describe("revokeToken", () => { it("builds a RevokeToken request", async () => { await revokeToken({ region, clientId, token: "refresh-token", clientSecret: "client-secret", }); expect(mockedFetch.mock.calls[0][0]).toMatchObject({ region, operation: "RevokeToken", args: { ClientId: clientId, Token: "refresh-token", ClientSecret: "client-secret", }, }); }); it("omits ClientSecret when not provided", async () => { await revokeToken({ region, clientId, token: "refresh-token", }); expect(mockedFetch.mock.calls[0][0].args).toEqual({ ClientId: clientId, Token: "refresh-token", }); }); });