-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use structured logging for email utils (#5230)
* use structured logging for email utils * add module name mapping for uuid and jest --------- Co-authored-by: Vincent <[email protected]>
- Loading branch information
Showing
4 changed files
with
44 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,24 @@ | |
import { test, expect, jest } from "@jest/globals"; | ||
import type { createTransport, Transporter } from "nodemailer"; | ||
import { randomToken } from "./email"; | ||
import type * as loggerModule from "../app/functions/server/logging"; | ||
|
||
jest.mock("nodemailer", () => { | ||
return { | ||
createTransport: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest.mock("../app/functions/server/logging", () => { | ||
return { | ||
logger: { | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
test("EmailUtils.sendEmail before .init() fails", async () => { | ||
expect.assertions(1); | ||
|
||
|
@@ -25,9 +36,9 @@ test("EmailUtils.sendEmail before .init() fails", async () => { | |
}); | ||
|
||
test("EmailUtils.init with empty host uses jsonTransport", async () => { | ||
const mockedConsoleInfo = jest | ||
.spyOn(console, "info") | ||
.mockImplementation(() => undefined); | ||
const mockedLoggerModule = jest.requireMock<typeof loggerModule>( | ||
"../app/functions/server/logging", | ||
); | ||
const mockedNodemailer: { | ||
createTransport: jest.MockedFunction<typeof createTransport>; | ||
} = jest.requireMock("nodemailer"); | ||
|
@@ -37,7 +48,7 @@ test("EmailUtils.init with empty host uses jsonTransport", async () => { | |
expect(mockedNodemailer.createTransport).toHaveBeenCalledWith({ | ||
jsonTransport: true, | ||
}); | ||
expect(mockedConsoleInfo).toHaveBeenCalledWith("smtpUrl-empty", { | ||
expect(mockedLoggerModule.logger.info).toHaveBeenCalledWith("smtpUrl-empty", { | ||
message: "EmailUtils will log a JSON response instead of sending emails.", | ||
}); | ||
}); | ||
|
@@ -65,9 +76,9 @@ test("EmailUtils.sendEmail with recipient, subject, template, context calls gTra | |
const mockedNodemailer: { | ||
createTransport: jest.MockedFunction<typeof createTransport>; | ||
} = jest.requireMock("nodemailer"); | ||
const mockedConsoleInfo = jest | ||
.spyOn(console, "info") | ||
.mockImplementation(() => undefined); | ||
const mockedLoggerModule = jest.requireMock<typeof loggerModule>( | ||
"../app/functions/server/logging", | ||
); | ||
const { initEmail, sendEmail } = await import("./email"); | ||
|
||
const testSmtpUrl = "smtps://test:test@test:1"; | ||
|
@@ -86,13 +97,16 @@ test("EmailUtils.sendEmail with recipient, subject, template, context calls gTra | |
expect( | ||
await sendEmail("[email protected]", "subject", "<html>test</html>"), | ||
).toBe(sendMailInfo); | ||
expect(mockedConsoleInfo).toHaveBeenCalledWith("sent_email", sendMailInfo); | ||
expect(mockedLoggerModule.logger.info).toHaveBeenCalledWith( | ||
"sent_email", | ||
sendMailInfo, | ||
); | ||
}); | ||
|
||
test("EmailUtils.sendEmail rejects with error", async () => { | ||
const mockedConsoleError = jest | ||
.spyOn(console, "error") | ||
.mockImplementation(() => undefined); | ||
const mockedLoggerModule = jest.requireMock<typeof loggerModule>( | ||
"../app/functions/server/logging", | ||
); | ||
const mockedNodemailer: { | ||
createTransport: jest.MockedFunction<typeof createTransport>; | ||
} = jest.requireMock("nodemailer"); | ||
|
@@ -109,16 +123,16 @@ test("EmailUtils.sendEmail rejects with error", async () => { | |
await expect(() => | ||
sendEmail("[email protected]", "subject", "<html>test</html>"), | ||
).rejects.toThrow("error"); | ||
expect(mockedConsoleError).toHaveBeenCalled(); | ||
expect(mockedLoggerModule.logger.error).toHaveBeenCalled(); | ||
}); | ||
|
||
test("EmailUtils.init with empty host uses jsonTransport. logs messages", async () => { | ||
const mockedNodemailer: { | ||
createTransport: jest.MockedFunction<typeof createTransport>; | ||
} = jest.requireMock("nodemailer"); | ||
const mockedConsoleInfo = jest | ||
.spyOn(console, "info") | ||
.mockImplementation(() => undefined); | ||
const mockedLoggerModule = jest.requireMock<typeof loggerModule>( | ||
"../app/functions/server/logging", | ||
); | ||
const { initEmail, sendEmail } = await import("./email"); | ||
|
||
const sendMailInfo = { messageId: "test id", response: "test response" }; | ||
|
@@ -134,7 +148,10 @@ test("EmailUtils.init with empty host uses jsonTransport. logs messages", async | |
expect( | ||
await sendEmail("[email protected]", "subject", "<html>test</html>"), | ||
).toBe(sendMailInfo); | ||
expect(mockedConsoleInfo).toHaveBeenCalledWith("sent_email", sendMailInfo); | ||
expect(mockedLoggerModule.logger.info).toHaveBeenCalledWith( | ||
"sent_email", | ||
sendMailInfo, | ||
); | ||
}); | ||
|
||
test("randomToken returns a random token of 2xlength (because of hex)", () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters