|
| 1 | +import { afterAll, describe, expect, it, vi } from 'vitest'; |
| 2 | +// eslint-disable-next-line import/no-relative-packages -- couldn't find a better way to include it |
| 3 | +import { getTestInstance } from '../../../../better-auth/packages/better-auth/src/test-utils/test-instance'; |
| 4 | +import emailHarmony, { type UserWithNormalizedEmail } from '.'; |
| 5 | +import { allEmail, allEmailSignIn, emailForget, emailSignUp } from './matchers'; |
| 6 | +// eslint-disable-next-line import/no-relative-packages -- couldn't find a better way to include it |
| 7 | +import type { BetterAuthPlugin } from '../../../../better-auth/packages/better-auth/src/types'; |
| 8 | + |
| 9 | +interface SQLiteDB { |
| 10 | + close: () => Promise<void>; |
| 11 | +} |
| 12 | + |
| 13 | +describe('Mapped schema', async () => { |
| 14 | + const mockSendEmail = vi.fn(); |
| 15 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars -- false positive |
| 16 | + let token = ''; |
| 17 | + |
| 18 | + const { client, db, auth } = await getTestInstance( |
| 19 | + { |
| 20 | + emailAndPassword: { |
| 21 | + enabled: true, |
| 22 | + async sendResetPassword({ url }) { |
| 23 | + token = url.split('?')[0]?.split('/').pop() ?? ''; |
| 24 | + await mockSendEmail(); |
| 25 | + } |
| 26 | + }, |
| 27 | + plugins: [ |
| 28 | + emailHarmony({ |
| 29 | + allowNormalizedSignin: true, |
| 30 | + matchers: { |
| 31 | + signIn: [emailForget, allEmailSignIn, emailSignUp], |
| 32 | + validation: [emailForget, allEmail] |
| 33 | + }, |
| 34 | + schema: { |
| 35 | + user: { |
| 36 | + fields: { |
| 37 | + normalizedEmail: 'normalized_email' |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + }) as BetterAuthPlugin |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + disableTestUser: true |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + afterAll(async () => { |
| 50 | + // TODO: Open PR for better-auth/src/test-utils/test-instance |
| 51 | + await (auth.options.database as unknown as SQLiteDB).close(); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('signup', () => { |
| 55 | + it('should normalize email', async () => { |
| 56 | + const rawEmail = 'new.email+test@googlemail.com'; |
| 57 | + await client.signUp.email({ |
| 58 | + email: rawEmail, |
| 59 | + password: 'new-password', |
| 60 | + name: 'new-name' |
| 61 | + }); |
| 62 | + const userYes = await db.findOne<UserWithNormalizedEmail>({ |
| 63 | + model: 'user', |
| 64 | + where: [ |
| 65 | + { |
| 66 | + field: 'email', |
| 67 | + value: rawEmail |
| 68 | + } |
| 69 | + ] |
| 70 | + }); |
| 71 | + // expect(userNo?.email).toBeUndefined(); |
| 72 | + expect(userYes?.email).toBe(rawEmail); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should reject temporary emails', async () => { |
| 76 | + const rawEmail = 'email@mailinator.com'; |
| 77 | + const { error } = await client.signUp.email({ |
| 78 | + email: rawEmail, |
| 79 | + password: 'new-password', |
| 80 | + name: 'new-name' |
| 81 | + }); |
| 82 | + expect(error).not.toBeNull(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should prevent signups with email variations', async () => { |
| 86 | + const rawEmail = 'test.mail+test1@googlemail.com'; |
| 87 | + await client.signUp.email({ |
| 88 | + email: rawEmail, |
| 89 | + password: 'new-password', |
| 90 | + name: 'new-name' |
| 91 | + }); |
| 92 | + const user = await db.findOne<UserWithNormalizedEmail>({ |
| 93 | + model: 'user', |
| 94 | + where: [ |
| 95 | + { |
| 96 | + field: 'normalizedEmail', |
| 97 | + value: 'testmail@gmail.com' |
| 98 | + } |
| 99 | + ] |
| 100 | + }); |
| 101 | + expect(user?.email).toBe(rawEmail); |
| 102 | + |
| 103 | + // Duplicate signup attempt |
| 104 | + const { error } = await client.signUp.email({ |
| 105 | + email: 'testmail+test2@googlemail.com', |
| 106 | + password: 'new-password', |
| 107 | + name: 'new-name' |
| 108 | + }); |
| 109 | + |
| 110 | + expect(error?.status).toBe(422); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments