mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
Set up Android project configuration, Gradle build scripts, typography, and default resource assets.
123 lines
4.1 KiB
JavaScript
123 lines
4.1 KiB
JavaScript
const {
|
|
initializeTestEnvironment,
|
|
assertFails,
|
|
assertSucceeds,
|
|
} = require("@firebase/rules-unit-testing");
|
|
const { test, before, after, beforeEach } = require("node:test");
|
|
const fs = require("node:fs");
|
|
|
|
let testEnv;
|
|
const PROJECT_ID = process.env.GCP_PROJECT || "demo-no-project";
|
|
const ALICE_UID = "alice_123";
|
|
const BOB_UID = "bob_456";
|
|
|
|
const [emulatorHost, emulatorPortStr] = (process.env.FIRESTORE_EMULATOR_HOST || "127.0.0.1:8085").split(":");
|
|
const emulatorPort = parseInt(emulatorPortStr, 10);
|
|
|
|
before(async () => {
|
|
const rules = fs.readFileSync("./firestore.rules", "utf8");
|
|
testEnv = await initializeTestEnvironment({
|
|
projectId: PROJECT_ID,
|
|
firestore: {
|
|
rules,
|
|
host: emulatorHost,
|
|
port: emulatorPort,
|
|
},
|
|
});
|
|
});
|
|
|
|
after(async () => {
|
|
if (testEnv) {
|
|
await testEnv.cleanup();
|
|
}
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
if (testEnv) {
|
|
await testEnv.clearFirestore();
|
|
}
|
|
});
|
|
|
|
// 1. Unauthenticated checks
|
|
test("Unauthenticated: cannot read or write meals or custom_foods", async () => {
|
|
const unauthDb = testEnv.unauthenticatedContext().firestore();
|
|
await assertFails(unauthDb.collection("users").doc(ALICE_UID).collection("meals").get());
|
|
await assertFails(unauthDb.collection("users").doc(ALICE_UID).collection("custom_foods").get());
|
|
});
|
|
|
|
// 2. Cross-user isolation
|
|
test("Cross-user: Bob cannot read Alice's meals or custom_foods", async () => {
|
|
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
await context.firestore().collection("users").doc(ALICE_UID).collection("meals").doc("meal_1").set({
|
|
userId: ALICE_UID,
|
|
mealType: "DINNER",
|
|
mealTimestamp: Date.now(),
|
|
hungerBefore: 7,
|
|
satietyAfter: 8,
|
|
foodItemsSerialized: ["Saumon grillé|150|g"],
|
|
createdAt: new Date(),
|
|
});
|
|
await context.firestore().collection("users").doc(ALICE_UID).collection("custom_foods").doc("food_1").set({
|
|
userId: ALICE_UID,
|
|
name: "Quinoa rouge",
|
|
defaultUnit: "g",
|
|
createdAt: new Date(),
|
|
});
|
|
});
|
|
|
|
const bobDb = testEnv.authenticatedContext(BOB_UID).firestore();
|
|
await assertFails(bobDb.collection("users").doc(ALICE_UID).collection("meals").doc("meal_1").get());
|
|
await assertFails(bobDb.collection("users").doc(ALICE_UID).collection("custom_foods").doc("food_1").get());
|
|
});
|
|
|
|
// 3. Authenticated owner can create and update meals with foodItemsSerialized and custom_foods
|
|
test("Owner: Alice can create/update meals with foodItemsSerialized and enrich custom_foods", async () => {
|
|
const aliceDb = testEnv.authenticatedContext(ALICE_UID).firestore();
|
|
const createdAtDate = new Date();
|
|
await assertSucceeds(
|
|
aliceDb.collection("users").doc(ALICE_UID).collection("meals").doc("meal_1").set({
|
|
userId: ALICE_UID,
|
|
mealType: "BREAKFAST",
|
|
mealTimestamp: Date.now() - 86400000,
|
|
hungerBefore: 8,
|
|
satietyAfter: 9,
|
|
note: "Petit-déjeuner complet au calme",
|
|
foodItemsSerialized: ["Flocons d'avoine|60|g", "Lait d'amande|200|ml", "Banane||portion"],
|
|
photosBase64: [],
|
|
createdAt: createdAtDate,
|
|
})
|
|
);
|
|
|
|
await assertSucceeds(
|
|
aliceDb.collection("users").doc(ALICE_UID).collection("meals").doc("meal_1").update({
|
|
foodItemsSerialized: ["Flocons d'avoine|70|g", "Banane|1|portion"],
|
|
note: "Note globale modifiée",
|
|
updatedAt: new Date(),
|
|
})
|
|
);
|
|
|
|
await assertSucceeds(
|
|
aliceDb.collection("users").doc(ALICE_UID).collection("custom_foods").doc("quinoa_rouge").set({
|
|
userId: ALICE_UID,
|
|
name: "Quinoa rouge",
|
|
defaultUnit: "g",
|
|
createdAt: createdAtDate,
|
|
})
|
|
);
|
|
|
|
await assertSucceeds(aliceDb.collection("users").doc(ALICE_UID).collection("custom_foods").get());
|
|
});
|
|
|
|
// 4. Invalid fields rejected
|
|
test("Invalid payload: Invalid unit in custom_foods or >10 hunger rejected", async () => {
|
|
const aliceDb = testEnv.authenticatedContext(ALICE_UID).firestore();
|
|
await assertFails(
|
|
aliceDb.collection("users").doc(ALICE_UID).collection("custom_foods").doc("bad_food").set({
|
|
userId: ALICE_UID,
|
|
name: "Aliment test",
|
|
defaultUnit: "invalid_unit",
|
|
createdAt: new Date(),
|
|
})
|
|
);
|
|
});
|