diff --git a/src/utils/firestore.test.ts b/src/utils/firestore.test.ts index eecfdbb..9a2f4a9 100644 --- a/src/utils/firestore.test.ts +++ b/src/utils/firestore.test.ts @@ -22,6 +22,7 @@ export async function getTestEnv(): Promise { describe("Firestore Rules Tests", () => { test("Read/Update/Delete/Create UserPrefs Unauthenticated", async () => { const t = await getTestEnv(); + // * Auth Context const user = { uid: faker.string.alphanumeric(20) } const context = t.authenticatedContext(user.uid); setTestDBContext(context.firestore()); @@ -37,8 +38,10 @@ describe("Firestore Rules Tests", () => { expect(_.isEqual(prefs, written_prefs), "Prefs not written").toBeTruthy(); - const no_auth_context = t.unauthenticatedContext(); - setTestDBContext(no_auth_context.firestore()); + // * Wrong Auth Context + const wrong_user = { uid: faker.string.alphanumeric(20) } + const wrong_auth_context = t.authenticatedContext(wrong_user.uid); + setTestDBContext(wrong_auth_context.firestore()); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error @@ -63,11 +66,40 @@ describe("Firestore Rules Tests", () => { await deleteDoc(collection(context.firestore(), "UserPrefs"), user.uid); }, "Unauthorised delete").rejects.toThrowError(); + + // * No Auth Context + const no_auth_context = t.unauthenticatedContext(); + setTestDBContext(no_auth_context.firestore()); + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + const read_pref_b = await getUserPrefs(user); + expect(_.isEqual(prefs, read_pref_b), "Unauthorised read").toBeFalsy(); + + await expect(async () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + await setUserPrefs(user, UserPrefs.default()); + }, "Unauthorised write").rejects.toThrowError(); + + await expect(async () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + await setUserPrefs({ uid: faker.string.alphanumeric(20) }, UserPrefs.default()); + }, "Unauthorised create").rejects.toThrowError(); + + await expect(async () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + await deleteDoc(collection(context.firestore(), "UserPrefs"), user.uid); + }, "Unauthorised delete").rejects.toThrowError(); + await t.cleanup(); }); test("Read/Update/Delete/Create Transaction Unauthenticated", async () => { const t = await getTestEnv(); + // * Auth context const user = { uid: faker.string.alphanumeric(20) } const context = t.authenticatedContext(user.uid); setTestDBContext(context.firestore()); @@ -89,6 +121,34 @@ describe("Firestore Rules Tests", () => { // @ts-expect-error const written_transaction = await writeNewTransaction(user, transaction); + // * Wrong Auth Context + const wrong_user = { uid: faker.string.alphanumeric(20) } + const wrong_auth_context = t.authenticatedContext(wrong_user.uid); + setTestDBContext(wrong_auth_context.firestore()); + + await expect(async () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + await getTransactionsByDocName(user, written_transaction.forceGetDocName()); + }, "Unauthorised read").rejects.toThrowError(); + + await expect(async () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + await setUserPrefs(user, UserPrefs.default()); + }, "Unauthorised write").rejects.toThrowError(); + + await expect(async () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + await setUserPrefs({ uid: faker.string.alphanumeric(20) }, UserPrefs.default()); + }, "Unauthorised create").rejects.toThrowError(); + + await expect(async () => { + await deleteDoc(doc(collection(context.firestore(), "UserPrefs"), user.uid)); + }, "Unauthorised delete").rejects.toThrowError(); + + // * No auth context const no_auth_context = t.unauthenticatedContext(); setTestDBContext(no_auth_context.firestore()); diff --git a/src/utils/transaction.test.ts b/src/utils/transaction.test.ts index dd0c743..8fd193d 100644 --- a/src/utils/transaction.test.ts +++ b/src/utils/transaction.test.ts @@ -97,7 +97,7 @@ describe("Firestore Transaction Tests", () => { expect(_.isEqual(sorted_transactions, sorted_fetched_transactions), "Written transactions not fetched").toBeTruthy(); await t.cleanup(); - }, 10_000); + }, 20_000); test("Read All Test", async () => { const t = await getTestEnv(); @@ -125,7 +125,7 @@ describe("Firestore Transaction Tests", () => { updated_transactions.reduce((curr, el) => curr || _.isEqual(el, new_transaction), false) , "Written transaction not fetched").toBeTruthy(); await t.cleanup(); - }, 10_000); + }, 20_000); test("Delete Test", async () => { const t = await getTestEnv(); @@ -193,7 +193,7 @@ describe("Firestore Transaction Tests", () => { expect(overwritten_transactions.reduce((curr, t) => curr && (t.name == new_transaction_name), true)).toBeTruthy(); await t.cleanup(); - }, 20_000); + }, 40_000); test("Overwrite Test", async () => { const t = await getTestEnv();