| 1 | import path from 'node:path'; |
| 2 | import { createDatabase, type |
| 3 | import { ConfigService } |
| 4 | import { Test, type |
| 5 | import { eq } |
| 6 | import { migrate } |
| 7 | import { |
| 8 | afterAll, |
| 9 | beforeAll, |
| 10 | beforeEach, |
| 11 | describe, |
| 12 | expect, |
| 13 | it, |
| 14 | vi, |
| 15 | } from 'vitest'; |
| 16 | |
| 17 | import { DATABASE } |
| 18 | import { MailService } |
| 19 | import { UsersService } |
| 20 | import { |
| 21 | EmailAlreadyTakenError, |
| 22 | EmailAlreadyVerifiedError, |
| 23 | EmailNotVerifiedError, |
| 24 | InvalidVerificationTokenError, |
| 25 | ResendTooSoonError, |
| 26 | } from './emails.errors.js'; |
| 27 | import { EmailsService } |
| 28 | |
| 29 | const CONNECTION = process |
| 30 | const MIGRATIONS = path |
| 31 | import.meta.dirname, |
| 32 | '../../../../../packages/db/drizzle', |
| 33 | ); |
| 34 | |
| 35 | const USER_ID = ' |
| 36 | const PRIMARY = ' |
| 37 | |
| 38 | const config = { |
| 39 | get: (key: |
| 40 | ({ |
| 41 | WEB_APP_URL: 'http://localhost:3000', |
| 42 | EMAIL_PROXY: 'https://relay.test', |
| 43 | })[key], |
| 44 | getOrThrow: (key: |
| 45 | ({ BETTER_AUTH_URL: 'http://localhost:3001' |
| 46 | } as unknown as |
| 47 | |
| 48 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 49 | describe.skipIf(!CONNECTION |
| 50 | let db: |
| 51 | let pool: |
| 52 | let service: |
| 53 | const mail = |
| 54 | |
| 55 | /** The token only ever leaves the row through the mail, so read it back. */ |
| 56 | async function tokenFor |
| 57 | const [row] |
| 58 | .select() |
| 59 | .from(schema.userEmail |
| 60 | .where(eq(schema |
| 61 | return row!.token |
| 62 | } |
| 63 | |
| 64 | /** Pushes a row's token past the resend throttle. */ |
| 65 | async function ageToken |
| 66 | await db |
| 67 | .update(schema.userEmail |
| 68 | .set({ tokenExpiresAt: |
| 69 | .where(eq(schema |
| 70 | } |
| 71 | |
| 72 | beforeAll(async () |
| 73 | ({ db, pool |
| 74 | await migrate(db |
| 75 | |
| 76 | const module: |
| 77 | providers: [ |
| 78 | EmailsService, |
| 79 | UsersService, |
| 80 | { provide: DATABASE, |
| 81 | { provide: MailService, |
| 82 | { provide: ConfigService, |
| 83 | ], |
| 84 | }).compile(); |
| 85 | service = module. |
| 86 | }); |
| 87 | |
| 88 | afterAll(async () |
| 89 | await db.delete |
| 90 | await pool.end |
| 91 | }); |
| 92 | |
| 93 | beforeEach(async () |
| 94 | mail.sendVerifyAliasEmail.mockReset |
| 95 | await db.delete |
| 96 | await db.insert |
| 97 | id: USER_ID, |
| 98 | name: 'Spec', |
| 99 | email: PRIMARY, |
| 100 | emailVerified: true, |
| 101 | }); |
| 102 | }); |
| 103 | |
| 104 | it('adds an address unverified and mails it a link', |
| 105 | const added = |
| 106 | |
| 107 | expect(added.email |
| 108 | expect(added.verified |
| 109 | |
| 110 | const [to, |
| 111 | expect(to).toBe |
| 112 | expect(context.verifyUrl |
| 113 | 'http://localhost:3001/api/emails/verify?token=', |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | it('refuses an address another account already holds', |
| 118 | await expect(service |
| 119 | EmailAlreadyTakenError, |
| 120 | ); |
| 121 | |
| 122 | await service.add |
| 123 | await expect( |
| 124 | service.add(USER_ID |
| 125 | ).rejects.toBeInstanceOf( |
| 126 | }); |
| 127 | |
| 128 | it('verifies once and rejects a replayed link', |
| 129 | await service.add |
| 130 | const token = |
| 131 | |
| 132 | await expect(service |
| 133 | email: 'confirm@example.com', |
| 134 | }); |
| 135 | |
| 136 | const { emails |
| 137 | expect( |
| 138 | emails.find((e |
| 139 | ).toBe(true); |
| 140 | |
| 141 | await expect(service |
| 142 | InvalidVerificationTokenError, |
| 143 | ); |
| 144 | }); |
| 145 | |
| 146 | it('attributes commits already indexed under the address', |
| 147 | const [repository] |
| 148 | .insert(schema.repository |
| 149 | .values({ |
| 150 | name: 'spec', |
| 151 | slug: `emails-spec-${Date |
| 152 | ownerId: USER_ID, |
| 153 | }) |
| 154 | .returning(); |
| 155 | |
| 156 | await db.insert |
| 157 | repositoryId: repository!.id |
| 158 | authorEmail: 'old-laptop@example.com', |
| 159 | authorName: 'Spec', |
| 160 | day: '2026-03-02', |
| 161 | commits: 3, |
| 162 | }); |
| 163 | |
| 164 | await service.add |
| 165 | await service.verify |
| 166 | |
| 167 | const [row] |
| 168 | .select() |
| 169 | .from(schema.repositoryContribution |
| 170 | .where(eq(schema |
| 171 | expect(row!.authorId |
| 172 | }); |
| 173 | |
| 174 | it('resends a fresh link and retires the old one', |
| 175 | await service.add |
| 176 | const first = |
| 177 | const { id |
| 178 | (entry) => |
| 179 | )!; |
| 180 | |
| 181 | // The throttle reads the stored expiry, so age the row instead of waiting. |
| 182 | await ageToken('again@example.com |
| 183 | await service.resend |
| 184 | |
| 185 | const second = |
| 186 | expect(second).not |
| 187 | expect(mail.sendVerifyAliasEmail |
| 188 | |
| 189 | // The link that was replaced is dead. |
| 190 | await expect(service |
| 191 | InvalidVerificationTokenError, |
| 192 | ); |
| 193 | await expect(service |
| 194 | email: 'again@example.com', |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | it('refuses a resend straight after the last one', |
| 199 | await service.add |
| 200 | const { id |
| 201 | (entry) => |
| 202 | )!; |
| 203 | |
| 204 | await expect(service |
| 205 | ResendTooSoonError, |
| 206 | ); |
| 207 | }); |
| 208 | |
| 209 | it('refuses a resend for an address already verified', |
| 210 | await service.add |
| 211 | const { id |
| 212 | (entry) => |
| 213 | )!; |
| 214 | await service.verify |
| 215 | |
| 216 | await expect(service |
| 217 | EmailAlreadyVerifiedError, |
| 218 | ); |
| 219 | }); |
| 220 | |
| 221 | it('swaps a verified address with the primary, keeping the old one', |
| 222 | await service.add |
| 223 | const added = |
| 224 | (e) => |
| 225 | )!; |
| 226 | |
| 227 | await expect(service |
| 228 | EmailNotVerifiedError, |
| 229 | ); |
| 230 | |
| 231 | await service.verify |
| 232 | const { emails |
| 233 | |
| 234 | expect(emails[0]) |
| 235 | email: 'next@example.com', |
| 236 | primary: true, |
| 237 | verified: true, |
| 238 | }); |
| 239 | // The old primary stays on the account, so sign-in with it keeps working. |
| 240 | expect(emails.slice |
| 241 | expect.objectContaining({ |
| 242 | ]); |
| 243 | }); |
| 244 | |
| 245 | it('removes an address', |
| 246 | await service.add |
| 247 | const added = |
| 248 | (e) => |
| 249 | )!; |
| 250 | |
| 251 | await service.remove |
| 252 | |
| 253 | const { emails |
| 254 | expect(emails).toHaveLength |
| 255 | expect(emails[0] |
| 256 | }); |
| 257 | }); |