-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #399 from ToposInstitute/fix-user-creation-in-tests
Mitigate failed test cleanups by logging in if the user already exists
- Loading branch information
Showing
4 changed files
with
41 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { type DocHandle, Repo, isValidDocumentId } from "@automerge/automerge-repo"; | ||
import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket"; | ||
import { type FirebaseOptions, initializeApp } from "firebase/app"; | ||
import { createUserWithEmailAndPassword, deleteUser, getAuth, signOut } from "firebase/auth"; | ||
import { deleteUser, getAuth, signOut } from "firebase/auth"; | ||
import * as uuid from "uuid"; | ||
import { assert, afterAll, describe, test } from "vitest"; | ||
|
||
import { initTestUserAuth, unwrap, unwrapErr } from "../util/test_util.ts"; | ||
import { createRpcClient } from "./rpc.ts"; | ||
import { unwrap, unwrapErr } from "./test_util.ts"; | ||
|
||
const serverUrl = import.meta.env.VITE_SERVER_URL; | ||
const repoUrl = import.meta.env.VITE_AUTOMERGE_REPO_URL; | ||
|
@@ -79,7 +79,7 @@ describe("Authorized RPC", async () => { | |
const auth = getAuth(firebaseApp); | ||
const email = "[email protected]"; | ||
const password = "foobar"; | ||
await createUserWithEmailAndPassword(auth, email, password); | ||
await initTestUserAuth(auth, email, password); | ||
|
||
const user = auth.currentUser; | ||
afterAll(async () => user && (await deleteUser(user))); | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,12 @@ | ||
import { type FirebaseOptions, initializeApp } from "firebase/app"; | ||
import { | ||
createUserWithEmailAndPassword, | ||
deleteUser, | ||
getAuth, | ||
signInWithEmailAndPassword, | ||
signOut, | ||
} from "firebase/auth"; | ||
import { deleteUser, getAuth, signInWithEmailAndPassword, signOut } from "firebase/auth"; | ||
import invariant from "tiny-invariant"; | ||
import { v4 } from "uuid"; | ||
import { assert, afterAll, describe, test } from "vitest"; | ||
|
||
import type { UserProfile } from "catcolab-api"; | ||
import { initTestUserAuth, unwrap, unwrapErr } from "../util/test_util.ts"; | ||
import { createRpcClient } from "./rpc.ts"; | ||
import { unwrap, unwrapErr } from "./test_util.ts"; | ||
|
||
const serverUrl = import.meta.env.VITE_SERVER_URL; | ||
const firebaseOptions = JSON.parse(import.meta.env.VITE_FIREBASE_OPTIONS) as FirebaseOptions; | ||
|
@@ -24,22 +18,17 @@ describe("RPC for user profiles", async () => { | |
const auth = getAuth(firebaseApp); | ||
const email = "[email protected]"; | ||
const password = "foobar"; | ||
await createUserWithEmailAndPassword(auth, email, password); | ||
await initTestUserAuth(auth, email, password); | ||
|
||
const user = auth.currentUser; | ||
|
||
afterAll(async () => user && (await deleteUser(user))); | ||
|
||
const signUpResult = await rpc.sign_up_or_sign_in.mutate(); | ||
test.sequential("should allow sign up when authenticated", () => { | ||
assert.strictEqual(signUpResult.tag, "Ok"); | ||
}); | ||
|
||
const defaultProfile = unwrap(await rpc.get_active_user_profile.query()); | ||
test.sequential("should get empty profile after user creation", () => { | ||
assert.strictEqual(defaultProfile.username, null); | ||
assert.strictEqual(defaultProfile.displayName, null); | ||
}); | ||
|
||
const username = `test-user-${v4()}`; | ||
const status = unwrap(await rpc.username_status.query(username)); | ||
test.sequential("fresh username should be available", () => { | ||
|
@@ -95,7 +84,7 @@ describe("Sharing documents between users", async () => { | |
const auth = getAuth(firebaseApp); | ||
const shareeEmail = "[email protected]"; | ||
const password = "foobar"; | ||
await createUserWithEmailAndPassword(auth, shareeEmail, password); | ||
await initTestUserAuth(auth, shareeEmail, password); | ||
|
||
const shareeUser = auth.currentUser; | ||
invariant(shareeUser); | ||
|
@@ -115,7 +104,7 @@ describe("Sharing documents between users", async () => { | |
|
||
// Set up account for the user who will share the document (the "sharer"). | ||
const sharerEmail = "[email protected]"; | ||
await createUserWithEmailAndPassword(auth, sharerEmail, password); | ||
await initTestUserAuth(auth, sharerEmail, password); | ||
|
||
const sharerUser = auth.currentUser; | ||
invariant(sharerUser); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { RpcResult } from "catcolab-api"; | ||
import { FirebaseError } from "firebase/app"; | ||
import { | ||
type Auth, | ||
createUserWithEmailAndPassword, | ||
signInWithEmailAndPassword, | ||
} from "firebase/auth"; | ||
import { assert } from "vitest"; | ||
|
||
/** Unwrap the `Ok` variant of a RPC result. */ | ||
export function unwrap<T>(result: RpcResult<T>): T { | ||
assert.strictEqual(result.tag, "Ok"); | ||
return (result as RpcResult<T> & { tag: "Ok" }).content; | ||
} | ||
|
||
/** Unwrap the `Err` variant of a RPC result. */ | ||
export function unwrapErr<T>(result: RpcResult<T>): { code: number; message: string } { | ||
assert.strictEqual(result.tag, "Err"); | ||
return result as RpcResult<T> & { tag: "Err" }; | ||
} | ||
|
||
export async function initTestUserAuth(auth: Auth, email: string, password: string) { | ||
try { | ||
await createUserWithEmailAndPassword(auth, email, password); | ||
} catch (e) { | ||
if (e instanceof FirebaseError && e.code === "auth/email-already-in-use") { | ||
await signInWithEmailAndPassword(auth, email, password); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} |