diff --git a/tests/e2e/utils/firebaseUtils.ts b/tests/e2e/utils/firebaseUtils.ts index 91e303e7..87a847f7 100644 --- a/tests/e2e/utils/firebaseUtils.ts +++ b/tests/e2e/utils/firebaseUtils.ts @@ -2,7 +2,7 @@ import axios from 'axios' import {config} from '../web/SPEC_CONFIG' -export async function login(email: string, password: string) { +export async function firebaseLogin(email: string, password: string) { const login = await axios.post( `${config.FIREBASE_URL.BASE}${config.FIREBASE_URL.SIGN_IN_PASSWORD}`, { @@ -13,17 +13,37 @@ export async function login(email: string, password: string) { ) return login } +export async function getUserId(email: string, password: string) { + try { + const loginInfo = await firebaseLogin(email, password) + return loginInfo.data.localId + } catch { + return + } +} -export async function signUp(email: string, password: string) { - // const base = 'http://localhost:9099/identitytoolkit.googleapis.com/v1'; - - await axios.post(`${config.FIREBASE_URL.BASE}${config.FIREBASE_URL.SIGNUP}`, { - email, - password, - returnSecureToken: true, - }) - - console.log('Auth created for', email) +export async function firebaseSignUp(email: string, password: string) { + try { + const response = await axios.post(`${config.FIREBASE_URL.BASE}${config.FIREBASE_URL.SIGNUP}`, { + email, + password, + returnSecureToken: true, + }) + const userId = response.data.localId + console.log('User created in Firebase Auth:', {email, userId}) + return userId + } catch (err: any) { + if ( + err.response?.status === 400 || + err.response?.data?.error?.message?.includes('EMAIL_EXISTS') + ) { + return await getUserId(email, password) + } + if (err.code === 'ECONNREFUSED') return + // throw Error('Firebase emulator not running. Start it with:\n yarn test:e2e:services\n') + console.log(err) + throw err + } } export async function deleteAccount(login: any) { diff --git a/tests/e2e/web/utils/deleteUser.ts b/tests/e2e/web/utils/deleteUser.ts index 3446f531..eca1da27 100644 --- a/tests/e2e/web/utils/deleteUser.ts +++ b/tests/e2e/web/utils/deleteUser.ts @@ -1,9 +1,9 @@ import {deleteFromDb} from '../../utils/databaseUtils' -import {deleteAccount, login} from '../../utils/firebaseUtils' +import {deleteAccount, firebaseLogin} from '../../utils/firebaseUtils' export async function deleteUser(email: string, password: string) { try { - const loginInfo = await login(email, password) + const loginInfo = await firebaseLogin(email, password) await deleteFromDb(loginInfo.data.localId) await deleteAccount(loginInfo) } catch (err: any) {