|
| 1 | +import { describe } from "vitest"; |
| 2 | +import { it } from "../../../../../helpers"; |
| 3 | +import { Auth, backendContext, InternalProjectKeys, niceBackendFetch, Project } from "../../../../backend-helpers"; |
| 4 | + |
| 5 | +describe("unauthorized requests", () => { |
| 6 | + it("should return 401 when invalid authorization is provided", async ({ expect }) => { |
| 7 | + const response = await niceBackendFetch( |
| 8 | + "/api/v1/internal/failed-emails-digest", |
| 9 | + { |
| 10 | + method: "POST", |
| 11 | + accessType: "server", |
| 12 | + headers: { |
| 13 | + "Authorization": "Bearer some_invalid_secret", |
| 14 | + } |
| 15 | + } |
| 16 | + ); |
| 17 | + expect(response).toMatchInlineSnapshot(` |
| 18 | + NiceResponse { |
| 19 | + "status": 401, |
| 20 | + "body": "Unauthorized", |
| 21 | + "headers": Headers { <some fields may have been hidden> }, |
| 22 | + } |
| 23 | + `); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should return 400 when no authorization header is provided", async ({ expect }) => { |
| 27 | + const response = await niceBackendFetch( |
| 28 | + "/api/v1/internal/failed-emails-digest", |
| 29 | + { |
| 30 | + method: "POST", |
| 31 | + accessType: "server", |
| 32 | + } |
| 33 | + ); |
| 34 | + expect(response.status).toBe(400); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should return 401 when authorization header is malformed", async ({ expect }) => { |
| 38 | + const response = await niceBackendFetch( |
| 39 | + "/api/v1/internal/failed-emails-digest", |
| 40 | + { |
| 41 | + method: "POST", |
| 42 | + accessType: "server", |
| 43 | + headers: { |
| 44 | + "Authorization": "InvalidFormat", |
| 45 | + } |
| 46 | + } |
| 47 | + ); |
| 48 | + expect(response).toMatchInlineSnapshot(` |
| 49 | + NiceResponse { |
| 50 | + "status": 401, |
| 51 | + "body": "Unauthorized", |
| 52 | + "headers": Headers { <some fields may have been hidden> }, |
| 53 | + } |
| 54 | + `); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("with valid credentials", () => { |
| 59 | + it("should return 200 and process failed emails digest", async ({ expect }) => { |
| 60 | + backendContext.set({ |
| 61 | + projectKeys: InternalProjectKeys, |
| 62 | + userAuth: null, |
| 63 | + }); |
| 64 | + await Auth.Otp.signIn(); |
| 65 | + const adminAccessToken = backendContext.value.userAuth?.accessToken; |
| 66 | + const { projectId } = await Project.create({ |
| 67 | + display_name: "Test Failed Emails Project", |
| 68 | + config: { |
| 69 | + email_config: { |
| 70 | + type: "standard", |
| 71 | + host: "invalid-smtp-host.example.com", |
| 72 | + port: 587, |
| 73 | + username: "invalid_user", |
| 74 | + password: "invalid_password", |
| 75 | + sender_name: "Test Project", |
| 76 | + sender_email: "test@invalid-domain.example.com", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + backendContext.set({ |
| 82 | + projectKeys: { |
| 83 | + projectId, |
| 84 | + }, |
| 85 | + userAuth: null, |
| 86 | + }); |
| 87 | + |
| 88 | + const testEmailResponse = await niceBackendFetch("/api/v1/internal/send-test-email", { |
| 89 | + method: "POST", |
| 90 | + accessType: "admin", |
| 91 | + headers: { |
| 92 | + "x-stack-admin-access-token": adminAccessToken, |
| 93 | + }, |
| 94 | + body: { |
| 95 | + "recipient_email": "test-email-recipient@stackframe.co", |
| 96 | + "email_config": { |
| 97 | + "host": "this-is-not-a-valid-host.example.com", |
| 98 | + "port": 123, |
| 99 | + "username": "123", |
| 100 | + "password": "123", |
| 101 | + "sender_email": "123@g.co", |
| 102 | + "sender_name": "123" |
| 103 | + } |
| 104 | + }, |
| 105 | + }); |
| 106 | + expect(testEmailResponse).toMatchInlineSnapshot(` |
| 107 | + NiceResponse { |
| 108 | + "status": 200, |
| 109 | + "body": { |
| 110 | + "error_message": "Failed to connect to the email host. Please make sure the email host configuration is correct.", |
| 111 | + "success": false, |
| 112 | + }, |
| 113 | + "headers": Headers { <some fields may have been hidden> }, |
| 114 | + } |
| 115 | + `); |
| 116 | + |
| 117 | + const response = await niceBackendFetch("/api/v1/internal/failed-emails-digest", { |
| 118 | + method: "POST", |
| 119 | + headers: { "Authorization": "Bearer mock_cron_secret" } |
| 120 | + }); |
| 121 | + expect(response.status).toBe(200); |
| 122 | + console.log(response.body); |
| 123 | + |
| 124 | + const failedEmailsByTenancy = response.body.failed_emails_by_tenancy; |
| 125 | + const mockProjectFailedEmails = failedEmailsByTenancy.filter( |
| 126 | + (batch: any) => batch.tenant_owner_email === backendContext.value.mailbox.emailAddress |
| 127 | + ); |
| 128 | + expect(mockProjectFailedEmails).toMatchInlineSnapshot(` |
| 129 | + [ |
| 130 | + { |
| 131 | + "emails": [ |
| 132 | + { |
| 133 | + "subject": "Test Email from Stack Auth", |
| 134 | + "to": ["test-email-recipient@stackframe.co"], |
| 135 | + }, |
| 136 | + ], |
| 137 | + "project_id": "<stripped UUID>", |
| 138 | + "tenancy_id": "<stripped UUID>", |
| 139 | + "tenant_owner_email": "default-mailbox--<stripped UUID>@stack-generated.example.com", |
| 140 | + }, |
| 141 | + ] |
| 142 | + `); |
| 143 | + |
| 144 | + const messages = await backendContext.value.mailbox.fetchMessages(); |
| 145 | + const digestEmail = messages.find(msg => msg.subject === "Failed emails digest"); |
| 146 | + expect(digestEmail).toBeDefined(); |
| 147 | + expect(digestEmail!.from).toBe("Stack Auth <noreply@example.com>"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("should return 200 and not send digest email when all emails are successful", async ({ expect }) => { |
| 151 | + await Auth.Otp.signIn(); |
| 152 | + const { projectId } = await Project.create({ |
| 153 | + display_name: "Test Successful Emails Project", |
| 154 | + config: { |
| 155 | + email_config: { |
| 156 | + type: "standard", |
| 157 | + host: "localhost", |
| 158 | + port: 2500, |
| 159 | + username: "test", |
| 160 | + password: "test", |
| 161 | + sender_name: "Test Project", |
| 162 | + sender_email: "test@example.com", |
| 163 | + }, |
| 164 | + }, |
| 165 | + }); |
| 166 | + |
| 167 | + const response = await niceBackendFetch("/api/v1/internal/failed-emails-digest", { |
| 168 | + method: "POST", |
| 169 | + headers: { "Authorization": "Bearer mock_cron_secret" } |
| 170 | + }); |
| 171 | + expect(response.status).toBe(200); |
| 172 | + |
| 173 | + const failedEmailsByTenancy = response.body.failed_emails_by_tenancy; |
| 174 | + const mockProjectFailedEmails = failedEmailsByTenancy.filter( |
| 175 | + (batch: any) => batch.tenant_owner_email === backendContext.value.mailbox.emailAddress |
| 176 | + ); |
| 177 | + expect(mockProjectFailedEmails).toMatchInlineSnapshot(`[]`); |
| 178 | + |
| 179 | + const messages = await backendContext.value.mailbox.fetchMessages(); |
| 180 | + const digestEmail = messages.find(msg => msg.subject === "Failed emails digest"); |
| 181 | + expect(digestEmail).toBeUndefined(); |
| 182 | + }); |
| 183 | +}); |
0 commit comments