|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { Constants } from "@courselit/common-models"; |
| 6 | +import { getNotificationMessageAndHref } from "@courselit/common-logic"; |
| 7 | +import { addMailJob } from "../../../../domain/handler"; |
| 8 | +import { EmailChannel } from "../email"; |
| 9 | + |
| 10 | +jest.mock("@courselit/common-logic", () => ({ |
| 11 | + getNotificationMessageAndHref: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock("../../../../domain/handler", () => ({ |
| 15 | + addMailJob: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +const mockedGetNotificationMessageAndHref = |
| 19 | + getNotificationMessageAndHref as jest.Mock; |
| 20 | +const mockedAddMailJob = addMailJob as jest.Mock; |
| 21 | + |
| 22 | +function makePayload(overrides: Partial<any> = {}): any { |
| 23 | + return { |
| 24 | + domain: { |
| 25 | + _id: "domain-id", |
| 26 | + name: "school", |
| 27 | + settings: { |
| 28 | + title: "School", |
| 29 | + }, |
| 30 | + }, |
| 31 | + actorUserId: "actor-id", |
| 32 | + actor: { |
| 33 | + userId: "actor-id", |
| 34 | + name: "Test Instructor", |
| 35 | + email: "instructor@example.com", |
| 36 | + avatar: { |
| 37 | + file: "https://cdn.example.com/avatar.png", |
| 38 | + thumbnail: "https://cdn.example.com/avatar-thumb.png", |
| 39 | + }, |
| 40 | + }, |
| 41 | + recipient: { |
| 42 | + userId: "recipient-id", |
| 43 | + email: "student@example.com", |
| 44 | + unsubscribeToken: "unsubscribe-token", |
| 45 | + subscribedToUpdates: true, |
| 46 | + }, |
| 47 | + activityType: Constants.ActivityType.ENROLLED, |
| 48 | + entityId: "entity-id", |
| 49 | + entityTargetId: "target-id", |
| 50 | + metadata: {}, |
| 51 | + ...overrides, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +describe("EmailChannel", () => { |
| 56 | + beforeEach(() => { |
| 57 | + jest.clearAllMocks(); |
| 58 | + process.env.PROTOCOL = "https"; |
| 59 | + process.env.DOMAIN = "courselit.test"; |
| 60 | + process.env.MULTITENANT = "true"; |
| 61 | + process.env.EMAIL_FROM = "hello@courselit.test"; |
| 62 | + |
| 63 | + mockedGetNotificationMessageAndHref.mockResolvedValue({ |
| 64 | + message: |
| 65 | + "Test Instructor granted your request to join Test Course community", |
| 66 | + href: "https://school.courselit.test/community/post", |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("renders a notification email with actor avatar, CTA, footer unsubscribe, branding, and unsubscribe headers", async () => { |
| 71 | + await new EmailChannel().send(makePayload()); |
| 72 | + |
| 73 | + expect(mockedAddMailJob).toHaveBeenCalledTimes(1); |
| 74 | + const mail = mockedAddMailJob.mock.calls[0][0]; |
| 75 | + |
| 76 | + expect(mail.subject).toBe( |
| 77 | + "Test Instructor granted your request to join Test Course community", |
| 78 | + ); |
| 79 | + expect(mail.body).toContain("Test Instructor"); |
| 80 | + expect(mail.body).toContain("https://cdn.example.com/avatar.png"); |
| 81 | + expect(mail.body).toContain("padding:24px 24px 10px 24px"); |
| 82 | + expect(mail.body).toContain( |
| 83 | + "Test Instructor granted your request to join Test Course community", |
| 84 | + ); |
| 85 | + expect(mail.body).toContain("View notification"); |
| 86 | + expect(mail.body).toContain( |
| 87 | + "https://school.courselit.test/community/post", |
| 88 | + ); |
| 89 | + expect(mail.body).toContain("Unsubscribe from email notifications"); |
| 90 | + expect(mail.body).toContain( |
| 91 | + "https://school.courselit.test/api/unsubscribe/unsubscribe-token", |
| 92 | + ); |
| 93 | + expect(mail.body).toContain("Powered by"); |
| 94 | + expect(mail.body).toContain("CourseLit"); |
| 95 | + expect(mail.body.indexOf("View notification")).toBeLessThan( |
| 96 | + mail.body.indexOf("Unsubscribe from email notifications"), |
| 97 | + ); |
| 98 | + expect(mail.body).toContain("background-color:#000000"); |
| 99 | + expect(mail.body).not.toContain("background-color:#07077b"); |
| 100 | + expect(mail.body).toContain("padding:12px 24px 56px 24px"); |
| 101 | + expect(mail.body).toContain("padding:32px 24px 16px 24px"); |
| 102 | + expect(mail.body).toMatch(/font-size:\s*12px/); |
| 103 | + expect(mail.headers).toEqual({ |
| 104 | + "List-Unsubscribe": |
| 105 | + "<https://school.courselit.test/api/unsubscribe/unsubscribe-token>", |
| 106 | + "List-Unsubscribe-Post": "List-Unsubscribe=One-Click", |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("omits the actor avatar image when avatar is missing", async () => { |
| 111 | + await new EmailChannel().send( |
| 112 | + makePayload({ |
| 113 | + actor: { |
| 114 | + userId: "actor-id", |
| 115 | + name: "Test Instructor", |
| 116 | + email: "instructor@example.com", |
| 117 | + avatar: {}, |
| 118 | + }, |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + const mail = mockedAddMailJob.mock.calls[0][0]; |
| 123 | + expect(mail.body).toContain("Test Instructor"); |
| 124 | + expect(mail.body).not.toContain('alt="TI"'); |
| 125 | + expect(mail.body).not.toContain("data:image/svg+xml"); |
| 126 | + expect(mail.body).not.toContain("https://cdn.example.com/avatar.png"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("renders dynamic notification text as text instead of Markdown links", async () => { |
| 130 | + mockedGetNotificationMessageAndHref.mockResolvedValue({ |
| 131 | + message: |
| 132 | + "Test Instructor replied to [a post](https://evil.example)", |
| 133 | + href: "https://school.courselit.test/community/post", |
| 134 | + }); |
| 135 | + |
| 136 | + await new EmailChannel().send( |
| 137 | + makePayload({ |
| 138 | + actor: { |
| 139 | + userId: "actor-id", |
| 140 | + name: "[Test Teacher](https://evil.example)", |
| 141 | + email: "instructor@example.com", |
| 142 | + avatar: {}, |
| 143 | + }, |
| 144 | + }), |
| 145 | + ); |
| 146 | + |
| 147 | + const mail = mockedAddMailJob.mock.calls[0][0]; |
| 148 | + expect(mail.body).toContain( |
| 149 | + "[Test Teacher](https://evil.example)", |
| 150 | + ); |
| 151 | + expect(mail.body).toContain( |
| 152 | + "Test Instructor replied to [a post](", |
| 153 | + ); |
| 154 | + expect(mail.body).not.toContain('href="https://evil.example"'); |
| 155 | + }); |
| 156 | + |
| 157 | + it("omits the actor avatar image when actor avatar URL uses an unsafe scheme", async () => { |
| 158 | + await new EmailChannel().send( |
| 159 | + makePayload({ |
| 160 | + actor: { |
| 161 | + userId: "actor-id", |
| 162 | + name: "<Test", |
| 163 | + email: "instructor@example.com", |
| 164 | + avatar: { |
| 165 | + file: "javascript:alert(1)", |
| 166 | + }, |
| 167 | + }, |
| 168 | + }), |
| 169 | + ); |
| 170 | + |
| 171 | + const mail = mockedAddMailJob.mock.calls[0][0]; |
| 172 | + expect(mail.body).toContain("<Test"); |
| 173 | + expect(mail.body).not.toContain('alt="?"'); |
| 174 | + expect(mail.body).not.toContain("data:image/svg+xml"); |
| 175 | + expect(mail.body).not.toContain("javascript:alert(1)"); |
| 176 | + }); |
| 177 | + |
| 178 | + it("hides CourseLit branding when domain branding is hidden", async () => { |
| 179 | + await new EmailChannel().send( |
| 180 | + makePayload({ |
| 181 | + domain: { |
| 182 | + _id: "domain-id", |
| 183 | + name: "school", |
| 184 | + settings: { |
| 185 | + title: "School", |
| 186 | + hideCourseLitBranding: true, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }), |
| 190 | + ); |
| 191 | + |
| 192 | + const mail = mockedAddMailJob.mock.calls[0][0]; |
| 193 | + expect(mail.body).not.toContain("Powered by"); |
| 194 | + expect(mail.body).not.toContain("CourseLit"); |
| 195 | + }); |
| 196 | + |
| 197 | + it("does not send when the recipient is unsubscribed from updates", async () => { |
| 198 | + await new EmailChannel().send( |
| 199 | + makePayload({ |
| 200 | + recipient: { |
| 201 | + userId: "recipient-id", |
| 202 | + email: "student@example.com", |
| 203 | + unsubscribeToken: "unsubscribe-token", |
| 204 | + subscribedToUpdates: false, |
| 205 | + }, |
| 206 | + }), |
| 207 | + ); |
| 208 | + |
| 209 | + expect(mockedAddMailJob).not.toHaveBeenCalled(); |
| 210 | + }); |
| 211 | + |
| 212 | + it("does not send when the recipient cannot receive unsubscribe-managed email", async () => { |
| 213 | + await new EmailChannel().send( |
| 214 | + makePayload({ |
| 215 | + recipient: { |
| 216 | + userId: "recipient-id", |
| 217 | + email: "", |
| 218 | + unsubscribeToken: "unsubscribe-token", |
| 219 | + subscribedToUpdates: true, |
| 220 | + }, |
| 221 | + }), |
| 222 | + ); |
| 223 | + |
| 224 | + await new EmailChannel().send( |
| 225 | + makePayload({ |
| 226 | + recipient: { |
| 227 | + userId: "recipient-id", |
| 228 | + email: "student@example.com", |
| 229 | + unsubscribeToken: "", |
| 230 | + subscribedToUpdates: true, |
| 231 | + }, |
| 232 | + }), |
| 233 | + ); |
| 234 | + |
| 235 | + expect(mockedAddMailJob).not.toHaveBeenCalled(); |
| 236 | + }); |
| 237 | + |
| 238 | + it("does not send when notification details do not include a message and href", async () => { |
| 239 | + mockedGetNotificationMessageAndHref.mockResolvedValue({ |
| 240 | + message: "", |
| 241 | + href: "", |
| 242 | + }); |
| 243 | + |
| 244 | + await new EmailChannel().send(makePayload()); |
| 245 | + |
| 246 | + expect(mockedAddMailJob).not.toHaveBeenCalled(); |
| 247 | + }); |
| 248 | +}); |
0 commit comments