From ac0d6572432432199456f556987679b15ebe2a4f Mon Sep 17 00:00:00 2001 From: Konstantin Wohlwend Date: Sat, 21 Dec 2024 12:19:24 -0800 Subject: [PATCH] Prepend mailbox ID to generated email addresses --- apps/e2e/tests/backend/backend-helpers.ts | 42 +++++++++++++-- .../endpoints/api/v1/auth/oauth/token.test.ts | 6 +-- .../api/v1/auth/otp/send-sign-in-code.test.ts | 2 +- .../api/v1/auth/password/reset.test.ts | 2 +- .../v1/auth/password/send-reset-code.test.ts | 4 +- .../api/v1/auth/password/sign-in.test.ts | 2 +- .../api/v1/auth/password/sign-up.test.ts | 4 +- .../contact-channels/contact-channels.test.ts | 28 +++++----- .../legacy-send-verification-code.test.ts | 4 +- .../send-verification-code.test.ts | 4 +- .../backend/endpoints/api/v1/projects.test.ts | 4 +- .../endpoints/api/v1/team-invitations.test.ts | 4 +- .../api/v1/team-member-profiles.test.ts | 4 +- .../endpoints/api/v1/team-memberships.test.ts | 10 ++-- .../backend/endpoints/api/v1/teams.test.ts | 6 +-- .../backend/endpoints/api/v1/users.test.ts | 52 +++++++++---------- apps/e2e/tests/helpers.ts | 28 +--------- 17 files changed, 107 insertions(+), 99 deletions(-) diff --git a/apps/e2e/tests/backend/backend-helpers.ts b/apps/e2e/tests/backend/backend-helpers.ts index f3ac4cecc..3b969dd85 100644 --- a/apps/e2e/tests/backend/backend-helpers.ts +++ b/apps/e2e/tests/backend/backend-helpers.ts @@ -2,11 +2,12 @@ import { InternalProjectsCrud } from "@stackframe/stack-shared/dist/interface/cr import { encodeBase64 } from "@stackframe/stack-shared/dist/utils/bytes"; import { generateSecureRandomString } from "@stackframe/stack-shared/dist/utils/crypto"; import { StackAssertionError, throwErr } from "@stackframe/stack-shared/dist/utils/errors"; -import { filterUndefined } from "@stackframe/stack-shared/dist/utils/objects"; +import { filterUndefined, omit } from "@stackframe/stack-shared/dist/utils/objects"; import { nicify } from "@stackframe/stack-shared/dist/utils/strings"; import * as jose from "jose"; +import { randomUUID } from "node:crypto"; import { expect } from "vitest"; -import { Context, Mailbox, NiceRequestInit, NiceResponse, STACK_BACKEND_BASE_URL, STACK_INTERNAL_PROJECT_ADMIN_KEY, STACK_INTERNAL_PROJECT_CLIENT_KEY, STACK_INTERNAL_PROJECT_ID, STACK_INTERNAL_PROJECT_SERVER_KEY, createMailbox, localRedirectUrl, niceFetch, updateCookiesFromResponse } from "../helpers"; +import { Context, INBUCKET_API_URL, Mailbox, MailboxMessage, NiceRequestInit, NiceResponse, STACK_BACKEND_BASE_URL, STACK_INTERNAL_PROJECT_ADMIN_KEY, STACK_INTERNAL_PROJECT_CLIENT_KEY, STACK_INTERNAL_PROJECT_ID, STACK_INTERNAL_PROJECT_SERVER_KEY, generatedEmailSuffix, localRedirectUrl, niceFetch, updateCookiesFromResponse } from "../helpers"; type BackendContext = { readonly projectKeys: ProjectKeys, @@ -15,12 +16,14 @@ type BackendContext = { readonly refreshToken?: string, readonly accessToken?: string, } | null, + readonly generatedMailboxNamesCount: number, }; export const backendContext = new Context>( () => ({ projectKeys: InternalProjectKeys, - mailbox: createMailbox(), + mailbox: createMailbox(`default-mailbox--${randomUUID()}${generatedEmailSuffix}`), + generatedMailboxNamesCount: 0, userAuth: null, }), (acc, update) => { @@ -31,6 +34,34 @@ export const backendContext = new Context(); + return { + emailAddress: email, + async fetchMessages({ noBody } = {}) { + const res = await niceFetch(new URL(`/api/v1/mailbox/${encodeURIComponent(mailboxName)}`, INBUCKET_API_URL)); + return await Promise.all((res.body as any[]).map(async (message) => { + let fullMessage: any; + if (fullMessageCache.has(message.id)) { + fullMessage = fullMessageCache.get(message.id); + } else { + const fullMessageRes = await niceFetch(new URL(`/api/v1/mailbox/${encodeURIComponent(mailboxName)}/${message.id}`, INBUCKET_API_URL)); + fullMessage = fullMessageRes.body; + fullMessageCache.set(message.id, fullMessage); + } + const messagePart = noBody ? omit(fullMessage, ["body", "attachments"]) : fullMessage; + return new MailboxMessage(messagePart); + })); + }, + }; +} + export type ProjectKeys = "no-project" | { projectId: string, publishableClientKey?: string, @@ -121,6 +152,7 @@ export namespace Auth { const accessToken = backendContext.value.userAuth?.accessToken; if (accessToken) { const aud = jose.decodeJwt(accessToken).aud; + console.log(accessToken, jose.decodeJwt(accessToken)); const jwks = jose.createRemoteJWKSet(new URL(`api/v1/projects/${aud}/.well-known/jwks.json`, STACK_BACKEND_BASE_URL)); const { payload } = await jose.jwtVerify(accessToken, jwks); expect(payload).toEqual({ @@ -475,9 +507,9 @@ export namespace Auth { }, "timeout": 60000, "user": { - "displayName": "@stack-generated.example.com", + "displayName": "default-mailbox--@stack-generated.example.com", "id": "", - "name": "@stack-generated.example.com", + "name": "default-mailbox--@stack-generated.example.com", }, }, }, diff --git a/apps/e2e/tests/backend/endpoints/api/v1/auth/oauth/token.test.ts b/apps/e2e/tests/backend/endpoints/api/v1/auth/oauth/token.test.ts index 0a32bebd2..cf5430076 100644 --- a/apps/e2e/tests/backend/endpoints/api/v1/auth/oauth/token.test.ts +++ b/apps/e2e/tests/backend/endpoints/api/v1/auth/oauth/token.test.ts @@ -42,14 +42,14 @@ describe("with grant_type === 'authorization_code'", async () => { "id": "", "oauth_providers": [ { - "account_id": "@stack-generated.example.com", - "email": "@stack-generated.example.com", + "account_id": "default-mailbox--@stack-generated.example.com", + "email": "default-mailbox--@stack-generated.example.com", "id": "spotify", }, ], "otp_auth_enabled": false, "passkey_auth_enabled": false, - "primary_email": "@stack-generated.example.com", + "primary_email": "default-mailbox--@stack-generated.example.com", "primary_email_verified": false, "profile_image_url": null, "requires_totp_mfa": false, diff --git a/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts b/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts index 4cefb6bea..7c3bb90b8 100644 --- a/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts +++ b/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts @@ -8,7 +8,7 @@ it("should send a sign-in code per e-mail", async ({ expect }) => { MailboxMessage { "from": "Stack Dashboard ", "subject": "Sign in to Stack Dashboard", - "to": ["<@stack-generated.example.com>"], + "to": ["@stack-generated.example.com>"],