|
| 1 | +import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { initializeApp } from 'firebase/app'; |
| 3 | +import { Auth, connectAuthEmulator, getAuth, signOut, deleteUser } from 'firebase/auth'; |
| 4 | +import { GoogleAuthProvider } from 'firebase/auth'; |
| 5 | +import { |
| 6 | + fuiSignInWithEmailAndPassword, |
| 7 | + fuiCreateUserWithEmailAndPassword, |
| 8 | + fuiSignInWithEmailLink, |
| 9 | + fuiSendSignInLinkToEmail, |
| 10 | + fuiSignInAnonymously, |
| 11 | + fuiSendPasswordResetEmail, |
| 12 | + fuiSignInWithOAuth, |
| 13 | +} from '../../src/auth'; |
| 14 | + |
| 15 | +describe('Firebase UI Auth Integration', () => { |
| 16 | + let auth: Auth; |
| 17 | + const testPassword = 'testPassword123!'; |
| 18 | + let testCount = 0; |
| 19 | + |
| 20 | + const getUniqueEmail = () => `test${Date.now()}-${testCount++}@example.com`; |
| 21 | + |
| 22 | + beforeAll(() => { |
| 23 | + const app = initializeApp({ |
| 24 | + apiKey: 'fake-api-key', |
| 25 | + authDomain: 'fake-auth-domain', |
| 26 | + projectId: 'fake-project-id', |
| 27 | + }); |
| 28 | + auth = getAuth(app); |
| 29 | + connectAuthEmulator(auth, 'http://127.0.0.1:9099', { disableWarnings: true }); |
| 30 | + }); |
| 31 | + |
| 32 | + beforeEach(async () => { |
| 33 | + if (auth.currentUser) { |
| 34 | + try { |
| 35 | + await deleteUser(auth.currentUser); |
| 36 | + } catch {} |
| 37 | + await signOut(auth); |
| 38 | + } |
| 39 | + window.localStorage.clear(); |
| 40 | + testCount = 0; |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + if (auth.currentUser) { |
| 45 | + try { |
| 46 | + await deleteUser(auth.currentUser); |
| 47 | + } catch {} |
| 48 | + await signOut(auth); |
| 49 | + } |
| 50 | + window.localStorage.clear(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('Email/Password Authentication', () => { |
| 54 | + it('should create a new user and sign in', async () => { |
| 55 | + const email = getUniqueEmail(); |
| 56 | + |
| 57 | + const createResult = await fuiCreateUserWithEmailAndPassword(auth, email, testPassword); |
| 58 | + expect(createResult.user).toBeDefined(); |
| 59 | + expect(createResult.user.email).toBe(email); |
| 60 | + |
| 61 | + await signOut(auth); |
| 62 | + |
| 63 | + const signInResult = await fuiSignInWithEmailAndPassword(auth, email, testPassword); |
| 64 | + expect(signInResult.user).toBeDefined(); |
| 65 | + expect(signInResult.user.email).toBe(email); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should fail with invalid credentials', async () => { |
| 69 | + const email = getUniqueEmail(); |
| 70 | + await expect(fuiSignInWithEmailAndPassword(auth, email, 'wrongpassword')).rejects.toThrow(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle password reset email', async () => { |
| 74 | + const email = getUniqueEmail(); |
| 75 | + await fuiCreateUserWithEmailAndPassword(auth, email, testPassword); |
| 76 | + await signOut(auth); |
| 77 | + |
| 78 | + await expect(fuiSendPasswordResetEmail(auth, email)).resolves.not.toThrow(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('Anonymous Authentication', () => { |
| 83 | + it('should sign in anonymously', async () => { |
| 84 | + const result = await fuiSignInAnonymously(auth); |
| 85 | + expect(result.user).toBeDefined(); |
| 86 | + expect(result.user.isAnonymous).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should upgrade anonymous user to email/password', async () => { |
| 90 | + const email = getUniqueEmail(); |
| 91 | + |
| 92 | + await fuiSignInAnonymously(auth); |
| 93 | + |
| 94 | + const result = await fuiCreateUserWithEmailAndPassword(auth, email, testPassword, { |
| 95 | + enableAutoUpgradeAnonymous: true, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result.user).toBeDefined(); |
| 99 | + expect(result.user.email).toBe(email); |
| 100 | + expect(result.user.isAnonymous).toBe(false); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('Email Link Authentication', () => { |
| 105 | + it('should initiate email link sign in', async () => { |
| 106 | + const email = getUniqueEmail(); |
| 107 | + await expect(fuiSendSignInLinkToEmail(auth, email)).resolves.not.toThrow(); |
| 108 | + |
| 109 | + expect(window.localStorage.getItem('emailForSignIn')).toBe(email); |
| 110 | + }); |
| 111 | + |
| 112 | + // Note: Full email link sign-in flow can't be tested in integration tests |
| 113 | + // as it requires clicking the email link |
| 114 | + }); |
| 115 | + |
| 116 | + describe('OAuth Authentication', () => { |
| 117 | + it.skip('should initiate OAuth sign in (skipped - requires user interaction)', async () => { |
| 118 | + const provider = new GoogleAuthProvider(); |
| 119 | + await fuiSignInWithOAuth(auth, provider); |
| 120 | + }); |
| 121 | + |
| 122 | + it.skip('should handle OAuth for anonymous upgrade (skipped - requires user interaction)', async () => { |
| 123 | + await fuiSignInAnonymously(auth); |
| 124 | + const provider = new GoogleAuthProvider(); |
| 125 | + await fuiSignInWithOAuth(auth, provider, { enableAutoUpgradeAnonymous: true }); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('Error Handling', () => { |
| 130 | + it('should handle duplicate email registration', async () => { |
| 131 | + const email = getUniqueEmail(); |
| 132 | + |
| 133 | + await fuiCreateUserWithEmailAndPassword(auth, email, testPassword); |
| 134 | + await signOut(auth); |
| 135 | + |
| 136 | + await expect(fuiCreateUserWithEmailAndPassword(auth, email, testPassword)).rejects.toThrow(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should handle non-existent user sign in', async () => { |
| 140 | + const email = getUniqueEmail(); |
| 141 | + await expect(fuiSignInWithEmailAndPassword(auth, email, 'password')).rejects.toThrow(); |
| 142 | + }); |
| 143 | + |
| 144 | + // Note: Firebase Auth has lenient email validation. |
| 145 | + // We test only definitely invalid email formats here. |
| 146 | + it('should handle invalid email formats', async () => { |
| 147 | + const invalidEmails = [ |
| 148 | + 'invalid', // No @ symbol |
| 149 | + '@', // Just @ symbol |
| 150 | + '@domain', // No local part |
| 151 | + 'user@', // No domain |
| 152 | + ]; |
| 153 | + |
| 154 | + for (const email of invalidEmails) { |
| 155 | + await expect(fuiCreateUserWithEmailAndPassword(auth, email, testPassword)).rejects.toThrow(); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + it('should handle password requirements', async () => { |
| 160 | + const email = getUniqueEmail(); |
| 161 | + const weakPasswords = ['', '123', 'short']; |
| 162 | + |
| 163 | + for (const password of weakPasswords) { |
| 164 | + await expect(fuiCreateUserWithEmailAndPassword(auth, email, password)).rejects.toThrow(); |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + it('should handle multiple anonymous account upgrades', async () => { |
| 169 | + const email = getUniqueEmail(); |
| 170 | + |
| 171 | + await fuiSignInAnonymously(auth); |
| 172 | + const result1 = await fuiCreateUserWithEmailAndPassword(auth, email, testPassword, { |
| 173 | + enableAutoUpgradeAnonymous: true, |
| 174 | + }); |
| 175 | + expect(result1.user.isAnonymous).toBe(false); |
| 176 | + |
| 177 | + await signOut(auth); |
| 178 | + await fuiSignInAnonymously(auth); |
| 179 | + |
| 180 | + const email2 = getUniqueEmail(); |
| 181 | + const result2 = await fuiCreateUserWithEmailAndPassword(auth, email2, testPassword, { |
| 182 | + enableAutoUpgradeAnonymous: true, |
| 183 | + }); |
| 184 | + expect(result2.user.isAnonymous).toBe(false); |
| 185 | + expect(result2.user.uid).not.toBe(result1.user.uid); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should handle special characters in email', async () => { |
| 189 | + const email = `test.name+${Date.now()}@example.com`; |
| 190 | + const result = await fuiCreateUserWithEmailAndPassword(auth, email, testPassword); |
| 191 | + expect(result.user.email).toBe(email); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should handle concurrent sign-in attempts', async () => { |
| 195 | + const email = getUniqueEmail(); |
| 196 | + await fuiCreateUserWithEmailAndPassword(auth, email, testPassword); |
| 197 | + await signOut(auth); |
| 198 | + |
| 199 | + const attempts = Array(3) |
| 200 | + .fill(null) |
| 201 | + .map(() => fuiSignInWithEmailAndPassword(auth, email, testPassword)); |
| 202 | + |
| 203 | + const results = await Promise.all(attempts); |
| 204 | + expect(results.every((result) => result.user.email === email)).toBe(true); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments