Skip to content

Commit 19c9a9d

Browse files
authored
[DEV-2823] Fix test and linting for cognito functions (#1698)
* Fix linting and remove duplicated log * Fix test * Add changeset
1 parent 3010e2b commit 19c9a9d

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

.changeset/slimy-mails-search.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cognito-functions": minor
3+
---
4+
5+
Fix broken test

apps/cognito-functions/src/__tests__/custom-message-handler.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('Handler', () => {
8888
expect(response.emailMessage).toStrictEqual(expected);
8989
});
9090

91-
it('should throw an error on user confirmed', async () => {
91+
it('should log a warning and return the event on user confirmed', async () => {
9292
const event = makeEvent();
9393
const userVerifiedEvent = {
9494
...event,
@@ -102,13 +102,22 @@ describe('Handler', () => {
102102
},
103103
};
104104

105-
// eslint-disable-next-line functional/no-try-statements
106-
try {
107-
await makeHandler(env)(userVerifiedEvent);
108-
fail('Should not reach this point');
109-
} catch (error) {
110-
expect(error).toStrictEqual(new Error('Operation not permitted'));
111-
}
105+
// Spy on console.warn
106+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
107+
108+
const result = await makeHandler(env)(userVerifiedEvent);
109+
110+
const username = event.request.userAttributes['sub'];
111+
// Verify that the warning was logged
112+
expect(consoleWarnSpy).toHaveBeenCalledWith(
113+
`User ${username} is confirmed and has requested to resend the email. Operation not permitted.`
114+
);
115+
116+
// Verify that the event is returned unmodified
117+
expect(result).toStrictEqual(userVerifiedEvent);
118+
119+
// Restore the original console.warn
120+
consoleWarnSpy.mockRestore();
112121
});
113122

114123
it('should reply with verification link on UpdateUserAttribute event', async () => {

apps/cognito-functions/src/custom-message-handler.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ export const makeHandler =
2222
) {
2323
if (cognitoUserStatus === 'CONFIRMED') {
2424
// eslint-disable-next-line functional/no-expression-statements
25-
console.log(
26-
`User ${username} is confirmed and has requested to resend the email`
27-
);
28-
// eslint-disable-next-line functional/no-throw-statements
2925
console.warn(
3026
`User ${username} is confirmed and has requested to resend the email. Operation not permitted.`
3127
);

0 commit comments

Comments
 (0)