Skip to content

Commit

Permalink
Merge branch '10553-bug' into 10553-bug-test
Browse files Browse the repository at this point in the history
  • Loading branch information
John Cruz committed Dec 3, 2024
2 parents 8e77cb4 + ecae0df commit 27d3d50
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ describe('Verify User Pending Email', () => {
applicationContext
.getPersistenceGateway()
.getCasesByEmailTotal.mockReturnValue(TOTAL_CASE_COUNT);

applicationContext.getNotificationGateway().sendNotificationToUser =
jest.fn();
});

describe('userTokenHasExpired', () => {
Expand Down Expand Up @@ -126,29 +129,43 @@ describe('Verify User Pending Email', () => {
});

it('should throw unauthorized error when user does not have permission to verify emails', async () => {
await expect(
verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: 'abc',
},
mockPetitionsClerkUser,
),
).rejects.toThrow('Unauthorized to manage emails');
await verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: 'abc',
},
mockPetitionsClerkUser,
);
const sendNotificationToUserCalls =
applicationContext.getNotificationGateway().sendNotificationToUser.mock
.calls;
expect(sendNotificationToUserCalls.length).toEqual(1);
expect(sendNotificationToUserCalls[0][0].message).toEqual({
action: 'set_verify_email_notification',
message: 'Unauthorized to manage emails',
});
});

it('should throw an unauthorized error when the token passed as an argument does not match stored token on user', async () => {
await expect(
verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: 'abc',
},
mockPrivatePractitionerUser,
),
).rejects.toThrow('Tokens do not match');
await verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: 'abc',
},
mockPrivatePractitionerUser,
);

const sendNotificationToUserCalls =
applicationContext.getNotificationGateway().sendNotificationToUser.mock
.calls;

expect(sendNotificationToUserCalls.length).toEqual(1);
expect(sendNotificationToUserCalls[0][0].message).toEqual({
action: 'set_verify_email_notification',
message: 'Tokens do not match',
});
});

it('should throw an unauthorized error when the token passed as an argument and the token store on the user are both undefined', async () => {
Expand All @@ -159,16 +176,24 @@ describe('Verify User Pending Email', () => {
pendingEmailVerificationToken: undefined,
});

await expect(
verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: undefined as any,
},
mockPrivatePractitionerUser,
),
).rejects.toThrow('Tokens do not match');
await verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: undefined as any,
},
mockPrivatePractitionerUser,
);

const sendNotificationToUserCalls =
applicationContext.getNotificationGateway().sendNotificationToUser.mock
.calls;

expect(sendNotificationToUserCalls.length).toEqual(1);
expect(sendNotificationToUserCalls[0][0].message).toEqual({
action: 'set_verify_email_notification',
message: 'Tokens do not match',
});
});

it('should throw an unauthorized error when there is no token timestamp', async () => {
Expand All @@ -179,16 +204,25 @@ describe('Verify User Pending Email', () => {
pendingEmailVerificationTokenTimestamp: undefined,
});

await expect(
verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: TOKEN,
},
mockPrivatePractitionerUser,
),
).rejects.toThrow('Link has expired');
await verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: TOKEN,
},
mockPrivatePractitionerUser,
);

const sendNotificationToUserCalls =
applicationContext.getNotificationGateway().sendNotificationToUser.mock
.calls;

expect(sendNotificationToUserCalls.length).toEqual(1);
expect(sendNotificationToUserCalls[0][0].message).toEqual({
action: 'set_verify_email_notification',
message: 'Link has expired',
messageType: 'expiredToken',
});
});

it('should throw an unauthorized error when token timestamp is expired', async () => {
Expand All @@ -199,33 +233,48 @@ describe('Verify User Pending Email', () => {
pendingEmailVerificationTokenTimestamp: TOKEN_TIMESTAMP_EXPIRED,
});

await expect(
verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: TOKEN,
},
mockPrivatePractitionerUser,
),
).rejects.toThrow('Link has expired');
await verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: TOKEN,
},
mockPrivatePractitionerUser,
);

const sendNotificationToUserCalls =
applicationContext.getNotificationGateway().sendNotificationToUser.mock
.calls;

expect(sendNotificationToUserCalls.length).toEqual(1);
expect(sendNotificationToUserCalls[0][0].message).toEqual({
action: 'set_verify_email_notification',
message: 'Link has expired',
messageType: 'expiredToken',
});
});

it('should throw an error when the pendingEmail address is not available in cognito', async () => {
applicationContext
.getPersistenceGateway()
.isEmailAvailable.mockReturnValue(false);
await verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: TOKEN,
},
mockPrivatePractitionerUser,
);
const sendNotificationToUserCalls =
applicationContext.getNotificationGateway().sendNotificationToUser.mock
.calls;

await expect(
verifyUserPendingEmailInteractor(
applicationContext,
{
clientConnectionId: TEST_CLIENT_CONNECTION_ID,
token: TOKEN,
},
mockPrivatePractitionerUser,
),
).rejects.toThrow('Email is not available');
expect(sendNotificationToUserCalls.length).toEqual(1);
expect(sendNotificationToUserCalls[0][0].message).toEqual({
action: 'set_verify_email_notification',
message: 'Email is not available',
});
});

it('should update the cognito email when tokens match', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const verifyUserPendingEmailInteractor = async (
clientConnectionId,
message: {
action: 'set_verify_email_notification',
message: 'Unauthorized to manage emails',
},
userId: (authorizedUser as UnknownAuthUser)?.userId!,
});
Expand Down Expand Up @@ -67,6 +68,7 @@ export const verifyUserPendingEmailInteractor = async (
clientConnectionId,
message: {
action: 'set_verify_email_notification',
message: 'Tokens do not match',
},
userId: user.userId,
});
Expand All @@ -83,6 +85,7 @@ export const verifyUserPendingEmailInteractor = async (
clientConnectionId,
message: {
action: 'set_verify_email_notification',
message: 'Link has expired',
messageType: 'expiredToken',
},
userId: user.userId,
Expand All @@ -104,6 +107,7 @@ export const verifyUserPendingEmailInteractor = async (
clientConnectionId,
message: {
action: 'set_verify_email_notification',
message: 'Email is not available',
},
userId: user.userId,
});
Expand Down Expand Up @@ -174,6 +178,7 @@ export const verifyUserPendingEmailInteractor = async (
clientConnectionId,
message: {
action: 'set_verify_email_notification',
message: 'Email has been updated',
messageType: 'success',
},
userId: user.userId,
Expand Down
3 changes: 2 additions & 1 deletion web-api/src/notifications/sendNotificationToUserTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ type UserContactUpdateErrorNotification = {

type SetVerifyEmailNotification = {
action: 'set_verify_email_notification';
messageType?: 'expiredToken' | 'succes';
message: string;
messageType?: 'expiredToken' | 'success';
};

export type NotificationMessage =
Expand Down

0 comments on commit 27d3d50

Please sign in to comment.