| 1 | import path from 'node:path'; |
| 2 | import { createDatabase, type |
| 3 | import { eq } |
| 4 | import { migrate } |
| 5 | import { afterAll, beforeAll |
| 6 | |
| 7 | import { type Auth, |
| 8 | |
| 9 | const CONNECTION = process |
| 10 | const MIGRATIONS = path |
| 11 | import.meta.dirname, |
| 12 | '../../../../packages/db/drizzle', |
| 13 | ); |
| 14 | |
| 15 | const PRIMARY = ' |
| 16 | const OTHER = ' |
| 17 | const OTHER_EXTRA = ' |
| 18 | const EXTRA = ' |
| 19 | const UNVERIFIED = ' |
| 20 | const PASSWORD = ' |
| 21 | |
| 22 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 23 | describe.skipIf(!CONNECTION |
| 24 | let db: |
| 25 | let pool: |
| 26 | let auth: |
| 27 | let userId: |
| 28 | let otherUserId: |
| 29 | |
| 30 | async function sessionHeaders |
| 31 | const { headers |
| 32 | body: { email, |
| 33 | returnHeaders: true, |
| 34 | }); |
| 35 | const cookie = |
| 36 | return new Headers |
| 37 | } |
| 38 | |
| 39 | beforeAll(async () |
| 40 | ({ db, pool |
| 41 | await migrate(db |
| 42 | |
| 43 | auth = createAuth( |
| 44 | secret: 'auth-spec-secret-auth-spec-secret', |
| 45 | baseURL: 'http://localhost:3001', |
| 46 | }); |
| 47 | |
| 48 | await db.delete |
| 49 | await db.delete |
| 50 | const signUp = |
| 51 | body: { name: ' |
| 52 | }); |
| 53 | userId = signUp. |
| 54 | |
| 55 | const other = |
| 56 | body: { name: ' |
| 57 | }); |
| 58 | otherUserId = other. |
| 59 | await db.insert |
| 60 | id: 'uem_auth_spec_other', |
| 61 | userId: otherUserId, |
| 62 | email: OTHER_EXTRA, |
| 63 | verified: true, |
| 64 | }); |
| 65 | |
| 66 | await db.insert |
| 67 | { |
| 68 | id: 'uem_auth_spec_extra', |
| 69 | userId, |
| 70 | email: EXTRA, |
| 71 | verified: true, |
| 72 | }, |
| 73 | { |
| 74 | id: 'uem_auth_spec_unverified', |
| 75 | userId, |
| 76 | email: UNVERIFIED, |
| 77 | verified: false, |
| 78 | }, |
| 79 | ]); |
| 80 | }); |
| 81 | |
| 82 | afterAll(async () |
| 83 | await db.delete |
| 84 | await db.delete |
| 85 | await pool.end |
| 86 | }); |
| 87 | |
| 88 | it('signs in with the primary address', |
| 89 | const result = |
| 90 | body: { email: PRIMARY |
| 91 | }); |
| 92 | expect(result.user |
| 93 | }); |
| 94 | |
| 95 | it('signs in with a verified extra address', |
| 96 | const result = |
| 97 | body: { email: EXTRA |
| 98 | }); |
| 99 | // Better Auth still only ever sees the account address. |
| 100 | expect(result.user |
| 101 | expect(result.user |
| 102 | }); |
| 103 | |
| 104 | it('refuses to change to an address another account holds', |
| 105 | const headers = |
| 106 | |
| 107 | // Another account's primary. |
| 108 | await expect( |
| 109 | auth.api.changeEmail |
| 110 | ).rejects.toThrow( |
| 111 | |
| 112 | // And another account's verified extra, which Better Auth alone would happily hand over. |
| 113 | await expect( |
| 114 | auth.api.changeEmail |
| 115 | ).rejects.toThrow( |
| 116 | }); |
| 117 | |
| 118 | it('refuses to change to an unconfirmed address of its own', |
| 119 | const headers = |
| 120 | |
| 121 | await expect( |
| 122 | auth.api.changeEmail |
| 123 | ).rejects.toThrow( |
| 124 | }); |
| 125 | |
| 126 | it('takes over one of its own confirmed extras', |
| 127 | const headers = |
| 128 | |
| 129 | await auth.api |
| 130 | |
| 131 | const [user] |
| 132 | .select() |
| 133 | .from(schema.user |
| 134 | .where(eq(schema |
| 135 | expect(user!.email |
| 136 | |
| 137 | // The address is on the account now, so it is no longer an extra. |
| 138 | const rows = |
| 139 | .select() |
| 140 | .from(schema.userEmail |
| 141 | .where(eq(schema |
| 142 | expect(rows.map |
| 143 | }); |
| 144 | |
| 145 | it('refuses an unverified extra address', |
| 146 | await expect( |
| 147 | auth.api.signInEmail |
| 148 | body: { email: UNVERIFIED |
| 149 | }), |
| 150 | ).rejects.toThrow() |
| 151 | }); |
| 152 | }); |