| 1 | import path from 'node:path'; |
| 2 | import { runWithEndpointContext } |
| 3 | import { createDatabase, type |
| 4 | import { eq, inArray |
| 5 | import { migrate } |
| 6 | import { afterAll, beforeAll |
| 7 | |
| 8 | import { type Auth, |
| 9 | |
| 10 | const CONNECTION = process |
| 11 | const MIGRATIONS = path |
| 12 | import.meta.dirname, |
| 13 | '../../../../packages/db/drizzle', |
| 14 | ); |
| 15 | |
| 16 | const PRIMARY = ' |
| 17 | const OTHER = ' |
| 18 | const OTHER_EXTRA = ' |
| 19 | const EXTRA = ' |
| 20 | const UNVERIFIED = ' |
| 21 | const PASSWORD = ' |
| 22 | |
| 23 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 24 | describe.skipIf(!CONNECTION |
| 25 | let db: |
| 26 | let pool: |
| 27 | let auth: |
| 28 | let userId: |
| 29 | let otherUserId: |
| 30 | |
| 31 | async function sessionHeaders |
| 32 | const { headers |
| 33 | body: { email, |
| 34 | returnHeaders: true, |
| 35 | }); |
| 36 | const cookie = |
| 37 | return new Headers |
| 38 | } |
| 39 | |
| 40 | beforeAll(async () |
| 41 | ({ db, pool |
| 42 | await migrate(db |
| 43 | |
| 44 | auth = createAuth( |
| 45 | secret: 'auth-spec-secret-auth-spec-secret', |
| 46 | baseURL: 'http://localhost:3001', |
| 47 | }); |
| 48 | |
| 49 | await db.delete |
| 50 | await db.delete |
| 51 | const signUp = |
| 52 | body: { name: ' |
| 53 | }); |
| 54 | userId = signUp. |
| 55 | |
| 56 | const other = |
| 57 | body: { name: ' |
| 58 | }); |
| 59 | otherUserId = other. |
| 60 | await db.insert |
| 61 | id: 'uem_auth_spec_other', |
| 62 | userId: otherUserId, |
| 63 | email: OTHER_EXTRA, |
| 64 | verified: true, |
| 65 | }); |
| 66 | |
| 67 | await db.insert |
| 68 | { |
| 69 | id: 'uem_auth_spec_extra', |
| 70 | userId, |
| 71 | email: EXTRA, |
| 72 | verified: true, |
| 73 | }, |
| 74 | { |
| 75 | id: 'uem_auth_spec_unverified', |
| 76 | userId, |
| 77 | email: UNVERIFIED, |
| 78 | verified: false, |
| 79 | }, |
| 80 | ]); |
| 81 | }); |
| 82 | |
| 83 | afterAll(async () |
| 84 | await db.delete |
| 85 | await db.delete |
| 86 | await pool.end |
| 87 | }); |
| 88 | |
| 89 | it('signs in with the primary address', |
| 90 | const result = |
| 91 | body: { email: PRIMARY |
| 92 | }); |
| 93 | expect(result.user |
| 94 | }); |
| 95 | |
| 96 | it('signs in with a verified extra address', |
| 97 | const result = |
| 98 | body: { email: EXTRA |
| 99 | }); |
| 100 | // Better Auth still only ever sees the account address. |
| 101 | expect(result.user |
| 102 | expect(result.user |
| 103 | }); |
| 104 | |
| 105 | it('refuses to change to an address another account holds', |
| 106 | const headers = |
| 107 | |
| 108 | // Another account's primary. |
| 109 | await expect( |
| 110 | auth.api.changeEmail |
| 111 | ).rejects.toThrow( |
| 112 | |
| 113 | // And another account's verified extra, which Better Auth alone would happily hand over. |
| 114 | await expect( |
| 115 | auth.api.changeEmail |
| 116 | ).rejects.toThrow( |
| 117 | }); |
| 118 | |
| 119 | it('refuses to change to an unconfirmed address of its own', |
| 120 | const headers = |
| 121 | |
| 122 | await expect( |
| 123 | auth.api.changeEmail |
| 124 | ).rejects.toThrow( |
| 125 | }); |
| 126 | |
| 127 | it('takes over one of its own confirmed extras', |
| 128 | const headers = |
| 129 | |
| 130 | await auth.api |
| 131 | |
| 132 | const [user] |
| 133 | .select() |
| 134 | .from(schema.user |
| 135 | .where(eq(schema |
| 136 | expect(user!.email |
| 137 | |
| 138 | // The address is on the account now, so it is no longer an extra. |
| 139 | const rows = |
| 140 | .select() |
| 141 | .from(schema.userEmail |
| 142 | .where(eq(schema |
| 143 | expect(rows.map |
| 144 | }); |
| 145 | |
| 146 | it('refuses an unverified extra address', |
| 147 | await expect( |
| 148 | auth.api.signInEmail |
| 149 | body: { email: UNVERIFIED |
| 150 | }), |
| 151 | ).rejects.toThrow() |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | describe.skipIf(!CONNECTION |
| 156 | let db: |
| 157 | let pool: |
| 158 | |
| 159 | beforeAll(async () |
| 160 | ({ db, pool |
| 161 | await migrate(db |
| 162 | await db.delete |
| 163 | await db |
| 164 | .delete(schema.organization |
| 165 | .where(eq(schema |
| 166 | await db.insert |
| 167 | id: 'user_gh_spec', |
| 168 | name: 'GitHub Spec', |
| 169 | email: 'gh-spec@example.com', |
| 170 | username: 'gh_spec_taken', |
| 171 | }); |
| 172 | await db.insert |
| 173 | id: 'org_gh_spec', |
| 174 | name: 'GitHub Spec Org', |
| 175 | slug: 'gh_spec_org', |
| 176 | createdAt: new Date |
| 177 | }); |
| 178 | }); |
| 179 | |
| 180 | afterAll(async () |
| 181 | await db.delete |
| 182 | await db |
| 183 | .delete(schema.organization |
| 184 | .where(eq(schema |
| 185 | await pool.end |
| 186 | }); |
| 187 | |
| 188 | it('turns hyphens into underscores and lowercases', |
| 189 | expect(await usernameForGitHubLogin |
| 190 | 'gh_spec_free', |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | it('pads a login shorter than three characters', |
| 195 | expect(await usernameForGitHubLogin |
| 196 | }); |
| 197 | |
| 198 | it('cuts a long login so a suffix still fits', |
| 199 | expect(await usernameForGitHubLogin |
| 200 | 'a'.repeat(26 |
| 201 | ); |
| 202 | }); |
| 203 | |
| 204 | it('suffixes a name a user, an organization or a route holds', |
| 205 | expect(await usernameForGitHubLogin |
| 206 | 'gh_spec_taken1', |
| 207 | ); |
| 208 | expect(await usernameForGitHubLogin |
| 209 | 'gh_spec_org1', |
| 210 | ); |
| 211 | expect(await usernameForGitHubLogin |
| 212 | }); |
| 213 | }); |
| 214 | |
| 215 | describe.skipIf(!CONNECTION |
| 216 | const RACE_EMAILS = |
| 217 | let db: |
| 218 | let pool: |
| 219 | let auth: |
| 220 | |
| 221 | const createUser = |
| 222 | const { adapter |
| 223 | return adapter.create |
| 224 | model: 'user', |
| 225 | data: { |
| 226 | name: 'Race', |
| 227 | email, |
| 228 | emailVerified: true, |
| 229 | username: 'gh_race', |
| 230 | displayUsername: 'gh_race', |
| 231 | createdAt: new Date |
| 232 | updatedAt: new Date |
| 233 | }, |
| 234 | }); |
| 235 | }; |
| 236 | |
| 237 | beforeAll(async () |
| 238 | ({ db, pool |
| 239 | await migrate(db |
| 240 | auth = createAuth( |
| 241 | secret: 'auth-spec-secret-auth-spec-secret', |
| 242 | baseURL: 'http://localhost:3001', |
| 243 | }); |
| 244 | await db.delete |
| 245 | await db.insert |
| 246 | id: 'user_gh_race', |
| 247 | name: 'Race Winner', |
| 248 | email: 'gh-race-winner@example.com', |
| 249 | username: 'gh_race', |
| 250 | }); |
| 251 | }); |
| 252 | |
| 253 | afterAll(async () |
| 254 | await db.delete |
| 255 | await db.delete |
| 256 | await pool.end |
| 257 | }); |
| 258 | |
| 259 | it('takes the next free name during the GitHub callback', |
| 260 | const created = |
| 261 | { path: '/callback/:id' |
| 262 | () => createUser( |
| 263 | ); |
| 264 | expect(created.username |
| 265 | }); |
| 266 | |
| 267 | it('leaves a conflict anywhere else to fail', |
| 268 | await expect( |
| 269 | runWithEndpointContext({ path: |
| 270 | createUser(RACE_EMAILS[1] |
| 271 | ), |
| 272 | ).rejects.toThrow() |
| 273 | }); |
| 274 | }); |