Skip to content

Commit

Permalink
Merge pull request #399 from ToposInstitute/fix-user-creation-in-tests
Browse files Browse the repository at this point in the history
Mitigate failed test cleanups by logging in if the user already exists
  • Loading branch information
epatters authored Feb 13, 2025
2 parents 0113e4e + 1d25d78 commit 01c1b8c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
6 changes: 3 additions & 3 deletions packages/frontend/src/api/document_rpc.test.ts
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;
Expand Down Expand Up @@ -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)));
Expand Down
15 changes: 0 additions & 15 deletions packages/frontend/src/api/test_util.ts

This file was deleted.

23 changes: 6 additions & 17 deletions packages/frontend/src/api/user_rpc.test.ts
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;
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions packages/frontend/src/util/test_util.ts
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;
}
}
}

0 comments on commit 01c1b8c

Please sign in to comment.