Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev scrum 79 #52

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Finalised Firestore rules tests
Robert-M-Lucas committed Apr 30, 2024
commit 3bfd382ee4786b2bd70f47f024e33f65dc54db9b
75 changes: 68 additions & 7 deletions src/utils/firestore.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {initializeTestEnvironment, RulesTestEnvironment} from "@firebase/rules-unit-testing";
import fs from "node:fs";
import {describe, expect, test} from "vitest";
import {faker} from "@faker-js/faker";
import {faker, fakerEN_GB} from "@faker-js/faker";
import {setTestDBContext} from "./firebase.ts";
import {getUserPrefs, setUserPrefs, UserPrefs} from "./user_prefs.ts";
import _ from "lodash";
import {collection, deleteDoc, doc} from "firebase/firestore";
import {getTransactionsByDocName, Transaction, writeNewTransaction} from "./transaction.ts";

export async function getTestEnv(): Promise<RulesTestEnvironment> {
return await initializeTestEnvironment({
@@ -43,15 +45,74 @@ describe("Firestore Rules Tests", () => {
const read_pref = await getUserPrefs(user);
expect(_.isEqual(prefs, read_pref), "Unauthorised read").toBeFalsy();

try {
await expect(async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await setUserPrefs(user, UserPrefs.default());
expect.fail("Unauthorised write");
}
catch (e) {
// Expected
}
}, "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();
const user = { uid: faker.string.alphanumeric(20) }
const context = t.authenticatedContext(user.uid);
setTestDBContext(context.firestore());

const transaction = new Transaction(
fakerEN_GB.location.streetAddress() + ", " + fakerEN_GB.location.city() + ", " + fakerEN_GB.location.zipCode(),
parseFloat(faker.finance.amount({min: -1000, max: 1000})),
faker.word.noun(),
faker.finance.currency().code,
faker.date.past().valueOf(),
faker.lorem.sentence(),
faker.internet.emoji(),
faker.word.noun(),
faker.lorem.sentence(),
user.uid
);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const written_transaction = await writeNewTransaction(user, transaction);

const no_auth_context = t.unauthenticatedContext();
setTestDBContext(no_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();

await t.cleanup();
});