Skip to content

AI-native design editor. Open-source Figma alternative.

2.0 KB
Raw Blame
import { describe, expect, test } from 'bun:test'

import { createCloudTestDatabase } from '#cloud-tests/helpers/database'
import { createAuthenticationEmailService, createTransactionalEmailService } from '#cloud/server'

describe('authentication email outbox', () => {
  test('enqueues verification, reset, and password-change messages without storing raw tokens', async () => {
    const runtime = await createCloudTestDatabase()
    try {
      const email = createTransactionalEmailService(runtime.database, {
        encryptionSecret: 'authentication-email-test-secret-at-least-32-characters',
        from: '[email protected]'
      })
      const service = createAuthenticationEmailService(email, 'https://cloud.example.com')
      await service.sendVerification({
        email: '[email protected]',
        name: 'Person',
        token: 'verification-token',
        url: 'https://cloud.example.com/api/auth/verify-email?token=verification-token'
      })
      await service.sendPasswordReset({
        email: '[email protected]',
        name: 'Person',
        token: 'reset-token',
        url: 'https://cloud.example.com/api/auth/reset-password/reset-token'
      })
      await service.sendPasswordChanged({
        email: '[email protected]',
        name: 'Person',
        userId: '11111111-1111-4111-8111-111111111111',
        url: 'https://cloud.example.com'
      })
      const messages = await runtime.database
        .selectFrom('transactionalEmail')
        .select(['kind', 'idempotencyKey', 'payloadEncrypted'])
        .orderBy('createdAt')
        .execute()
      expect(messages.map((message) => message.kind)).toEqual([
        'email-verification',
        'password-reset',
        'password-changed'
      ])
      expect(JSON.stringify(messages)).not.toContain('verification-token')
      expect(JSON.stringify(messages)).not.toContain('reset-token')
      expect(messages.every((message) => Boolean(message.payloadEncrypted))).toBe(true)
    } finally {
      await runtime.close()
    }
  })
})