Skip to content

Commit

Permalink
Test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Lucas committed May 2, 2024
1 parent c1980ac commit f0929a9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
64 changes: 62 additions & 2 deletions src/utils/firestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function getTestEnv(): Promise<RulesTestEnvironment> {
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());
Expand All @@ -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
Expand All @@ -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());
Expand All @@ -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());

Expand Down
6 changes: 3 additions & 3 deletions src/utils/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f0929a9

Please sign in to comment.