diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c1d44da..5b8d5a4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -7,6 +7,29 @@ on: - main jobs: + integration-tests: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python: ["3.10", "3.12"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python }} + - name: Setup Node 18 + uses: actions/setup-node@v3 + with: + node-version: 18 + - name: Install Node dependencies + run: | + cd integration_tests && npm install + - name: Run Integration Tests + run: | + cd integration_tests && npm start + test: runs-on: ubuntu-latest strategy: diff --git a/.gitignore b/.gitignore index 68426ac..6339aeb 100644 --- a/.gitignore +++ b/.gitignore @@ -139,6 +139,3 @@ doc/dist .idea .vscode/* !.vscode/settings.json -integration_tests/serviceAccount.json -integration_tests/functions/firebase_functions_*.tar.gz -integration_tests/functions/requirements.txt diff --git a/integration_tests/.env.example b/integration_tests/.env.example index c4e6136..78c2577 100644 --- a/integration_tests/.env.example +++ b/integration_tests/.env.example @@ -2,8 +2,7 @@ REGION=us-central1 PROJECT_ID= DATABASE_URL= STORAGE_BUCKET= -NODE_VERSION=18 -FIREBASE_ADMIN=^10.0.0 +FIREBASE_ADMIN=6.5.0 FIREBASE_APP_ID= FIREBASE_MEASUREMENT_ID= FIREBASE_AUTH_DOMAIN= diff --git a/integration_tests/.gitignore b/integration_tests/.gitignore index 4be1cd1..ca33218 100644 --- a/integration_tests/.gitignore +++ b/integration_tests/.gitignore @@ -63,4 +63,8 @@ node_modules/ .yarn-integrity # dotenv environment variables file -.env \ No newline at end of file +.env +/functions/functions.yaml +/serviceAccount.json +/functions/requirements.txt +/functions/firebase_functions.tar.gz diff --git a/integration_tests/functions/main.py b/integration_tests/functions/main.py index 2ae3899..71163fa 100644 --- a/integration_tests/functions/main.py +++ b/integration_tests/functions/main.py @@ -1,7 +1,4 @@ -# Welcome to Cloud Functions for Firebase for Python! -# To get started, simply uncomment the below code or create your own. -# Deploy with `firebase deploy` - from firebase_admin import initialize_app +from v2.firestore_tests import * initialize_app() diff --git a/integration_tests/functions/v2/firestore_tests.py b/integration_tests/functions/v2/firestore_tests.py index d46325d..636dddc 100644 --- a/integration_tests/functions/v2/firestore_tests.py +++ b/integration_tests/functions/v2/firestore_tests.py @@ -1,55 +1,67 @@ from firebase_admin import firestore from firebase_functions import firestore_fn, logger -from integration_tests.functions.region import REGION +from region import REGION -@firestore_fn.on_document_created(document='tests/{documentId}', region=REGION, timeout_sec=540) -def firestoreOnDocumentCreatedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): - logger.debug(event=event) +@firestore_fn.on_document_created(document='tests/{documentId}', + region=REGION, + timeout_sec=540) +def firestoreOnDocumentCreatedTests( + event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): documentId = event.params['documentId'] - firestore.client().collection('firestoreOnDocumentCreatedTests').document(documentId).set({ - 'time': event.time, - 'id': event.id, - 'type': event.type, - 'source': event.source, - }) + firestore.client().collection('firestoreOnDocumentCreatedTests').document( + documentId).set({ + 'time': event.time, + 'id': event.id, + 'type': event.type, + 'source': event.source, + }) -@firestore_fn.on_document_deleted(document='tests/{documentId}', region=REGION, timeout_sec=540) -def firestoreOnDocumentDeletedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): - logger.debug(event=event) +@firestore_fn.on_document_deleted(document='tests/{documentId}', + region=REGION, + timeout_sec=540) +def firestoreOnDocumentDeletedTests( + event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): documentId = event.params['documentId'] - firestore.client().collection('firestoreOnDocumentDeletedTests').document(documentId).set({ - 'time': event.time, - 'id': event.id, - 'type': event.type, - 'source': event.source, - }) + firestore.client().collection('firestoreOnDocumentDeletedTests').document( + documentId).set({ + 'time': event.time, + 'id': event.id, + 'type': event.type, + 'source': event.source, + }) -@firestore_fn.on_document_updated(document='tests/{documentId}', region=REGION, timeout_sec=540) -def firestoreOnDocumentUpdatedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): - logger.debug(event=event) +@firestore_fn.on_document_updated(document='tests/{documentId}', + region=REGION, + timeout_sec=540) +def firestoreOnDocumentUpdatedTests( + event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): documentId = event.params['documentId'] - firestore.client().collection('firestoreOnDocumentUpdatedTests').document(documentId).set({ - 'time': event.time, - 'id': event.id, - 'type': event.type, - 'source': event.source, - }) + firestore.client().collection('firestoreOnDocumentUpdatedTests').document( + documentId).set({ + 'time': event.time, + 'id': event.id, + 'type': event.type, + 'source': event.source, + }) -@firestore_fn.on_document_written(document='tests/{documentId}', region=REGION, timeout_sec=540) -def firestoreOnDocumentWrittenTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): - logger.debug(event=event) +@firestore_fn.on_document_written(document='tests/{documentId}', + region=REGION, + timeout_sec=540) +def firestoreOnDocumentWrittenTests( + event: firestore_fn.Event[firestore_fn.DocumentSnapshot]): documentId = event.params['documentId'] - firestore.client().collection('firestoreOnDocumentWrittenTests').document(documentId).set({ - 'time': event.time, - 'id': event.id, - 'type': event.type, - 'source': event.source, - }) + firestore.client().collection('firestoreOnDocumentWrittenTests').document( + documentId).set({ + 'time': event.time, + 'id': event.id, + 'type': event.type, + 'source': event.source, + }) diff --git a/integration_tests/package-lock.json b/integration_tests/package-lock.json index 5bd1fe0..3728ca6 100644 --- a/integration_tests/package-lock.json +++ b/integration_tests/package-lock.json @@ -16,10 +16,11 @@ }, "devDependencies": { "@types/firebase": "^3.2.1", - "@types/jest": "^29.5.11", + "@types/jest": "^29.5.12", "@types/js-yaml": "^4.0.9", "@types/node-fetch": "2", "jest": "^29.7.0", + "prettier": "^3.3.3", "ts-jest": "^29.1.1" } }, @@ -3184,9 +3185,9 @@ } }, "node_modules/@types/jest": { - "version": "29.5.11", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.11.tgz", - "integrity": "sha512-S2mHmYIVe13vrm6q4kN6fLYYAka15ALQki/vgDC3mIukEOx8WJlv0kQPM+d4w8Gp6u0uSdKND04IlTXBv0rwnQ==", + "version": "29.5.12", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.12.tgz", + "integrity": "sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==", "dev": true, "dependencies": { "expect": "^29.0.0", @@ -9674,6 +9675,21 @@ "node": ">= 0.8.0" } }, + "node_modules/prettier": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.3.tgz", + "integrity": "sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", diff --git a/integration_tests/package.json b/integration_tests/package.json index bb584a1..36871f5 100644 --- a/integration_tests/package.json +++ b/integration_tests/package.json @@ -14,15 +14,16 @@ "scripts": { "copyfiles": "cp ./serviceAccount.json ./dist/serviceAccount.json", "build": "tsc && npm run copyfiles", - "test": "jest --detectOpenHandles", + "test": "jest --detectOpenHandles tests/v2/firestore.test.ts", "start": "npm run build && node dist/run.js" }, "devDependencies": { "@types/firebase": "^3.2.1", - "@types/jest": "^29.5.11", + "@types/jest": "^29.5.12", "@types/js-yaml": "^4.0.9", "@types/node-fetch": "2", "jest": "^29.7.0", + "prettier": "^3.3.3", "ts-jest": "^29.1.1" } } diff --git a/integration_tests/run.ts b/integration_tests/run.ts index 9c58e06..6c7459c 100644 --- a/integration_tests/run.ts +++ b/integration_tests/run.ts @@ -99,7 +99,12 @@ async function discoverAndModifyEndpoints() { const killServer = await delegate.serveAdmin(port.toString(), {}, env); console.log("Started on port", port); - const originalYaml = await detectFromPort(port, config.projectId, config.runtime, 10000); + const originalYaml = await detectFromPort( + port, + config.projectId, + config.runtime, + 10000 + ); modifiedYaml = { ...originalYaml, @@ -206,7 +211,11 @@ function cleanFiles(): void { process.chdir("../"); // go back to integration_test } -const spawnAsync = (command: string, args: string[], options: any): Promise => { +const spawnAsync = ( + command: string, + args: string[], + options: any +): Promise => { return new Promise((resolve, reject) => { const child = spawn(command, args, options); @@ -231,19 +240,23 @@ const spawnAsync = (command: string, args: string[], options: any): Promise { try { - console.log("Starting Node.js Tests..."); + console.log("Starting Python Tests..."); const output = await spawnAsync("npm", ["test"], { env: { ...process.env, - GOOGLE_APPLICATION_CREDENTIALS: path.join(__dirname, "serviceAccount.json"), + GOOGLE_APPLICATION_CREDENTIALS: path.join( + __dirname, + "serviceAccount.json" + ), TEST_RUN_ID, }, stdio: "inherit", }); console.log(output); - console.log("Node.js Tests Completed."); + console.log("Python Tests Completed."); } catch (error) { console.error("Error during testing:", error); + throw error; } } @@ -272,6 +285,7 @@ async function runIntegrationTests(): Promise { await runTests(); } catch (err) { console.error("Error occurred during integration tests", err); + throw new Error("Integration tests failed"); } finally { await handleCleanUp(); } @@ -282,4 +296,7 @@ runIntegrationTests() console.log("Integration tests completed"); process.exit(0); }) - .catch((error) => console.error("An error occurred during integration tests", error)); + .catch((error) => { + console.error("An error occurred during integration tests", error); + process.exit(1); + }); diff --git a/integration_tests/setup.ts b/integration_tests/setup.ts index 1b29d35..dbfa595 100644 --- a/integration_tests/setup.ts +++ b/integration_tests/setup.ts @@ -18,11 +18,21 @@ function buildSdk(testRunId: string) { process.chdir(path.join(DIR, "..")); // go up to root // remove existing build - fs.rmdirSync("dist", { recursive: true }); + fs.rmSync("dist", { recursive: true, force: true }); + // remove existing venv + fs.rmSync("venv", { recursive: true, force: true }); + + // make virtual environment for building + execSync("python3 -m venv venv", { stdio: "inherit" }); // build the package - execSync("python -m pip install --upgrade build", { stdio: "inherit" }); - execSync("python -m build -s", { stdio: "inherit" }); + execSync( + "source venv/bin/activate && python -m pip install --upgrade build", + { stdio: "inherit" }, + ); + execSync("source venv/bin/activate && python -m build -s", { + stdio: "inherit", + }); // move the generated tarball package to functions const generatedFile = fs @@ -33,7 +43,7 @@ function buildSdk(testRunId: string) { const targetPath = path.join( "integration_tests", "functions", - `firebase_functions_${testRunId}.tar.gz` + `firebase_functions.tar.gz`, ); fs.renameSync(path.join("dist", generatedFile), targetPath); console.log("SDK moved to", targetPath); @@ -52,11 +62,11 @@ function createRequirementsTxt(testRunId: string, firebaseAdmin: string) { let requirementsContent = fs.readFileSync(requirementsPath, "utf8"); requirementsContent = requirementsContent.replace( /__LOCAL_FIREBASE_FUNCTIONS__/g, - `firebase_functions_${testRunId}.tar.gz` + `firebase_functions.tar.gz`, ); requirementsContent = requirementsContent.replace( /__FIREBASE_ADMIN__/g, - firebaseAdmin + firebaseAdmin, ); fs.writeFileSync(requirementsPath, requirementsContent); @@ -73,7 +83,6 @@ function installDependencies() { } execSync("python3 -m venv venv", { stdio: "inherit" }); - execSync("source venv/bin/activate", { stdio: "inherit" }); - execSync("python3 -m pip install -r requirements.txt", { stdio: "inherit" }); + execSync("source venv/bin/activate && python3 -m pip install -r requirements.txt", { stdio: "inherit" }); process.chdir("../"); // go back to integration_test } diff --git a/integration_tests/tests/v1/auth.test.ts b/integration_tests/tests/v1/auth.test.ts deleted file mode 100644 index 277df3d..0000000 --- a/integration_tests/tests/v1/auth.test.ts +++ /dev/null @@ -1,285 +0,0 @@ -import admin from "firebase-admin"; -import { timeout } from "../utils"; -import { initializeFirebase } from "../firebaseSetup"; -import { UserRecord } from "firebase-admin/lib/auth/user-record"; -import { initializeApp } from "firebase/app"; -import { getAuth, createUserWithEmailAndPassword, UserCredential } from "firebase/auth"; - -describe("Firebase Auth (v1)", () => { - let userIds: string[] = []; - const projectId = process.env.PROJECT_ID; - const testId = process.env.TEST_RUN_ID; - const config = { - apiKey: process.env.FIREBASE_API_KEY, - authDomain: process.env.FIREBASE_AUTH_DOMAIN, - databaseURL: process.env.DATABASE_URL, - projectId, - storageBucket: process.env.STORAGE_BUCKET, - appId: process.env.FIREBASE_APP_ID, - measurementId: process.env.FIREBASE_MEASUREMENT_ID, - }; - const app = initializeApp(config); - - if (!testId || !projectId) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - for (const userId in userIds) { - await admin.firestore().collection("userProfiles").doc(userId).delete(); - await admin.firestore().collection("authUserOnCreateTests").doc(userId).delete(); - await admin.firestore().collection("authUserOnDeleteTests").doc(userId).delete(); - await admin.firestore().collection("authBeforeCreateTests").doc(userId).delete(); - await admin.firestore().collection("authBeforeSignInTests").doc(userId).delete(); - } - }); - - describe("user onCreate trigger", () => { - let userRecord: UserRecord; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - userRecord = await admin.auth().createUser({ - email: `${testId}@fake-create.com`, - password: "secret", - displayName: `${testId}`, - }); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("authUserOnCreateTests") - .doc(userRecord.uid) - .get(); - - loggedContext = logSnapshot.data(); - - userIds.push(userRecord.uid); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await admin.auth().deleteUser(userRecord.uid); - }); - - it("should perform expected actions", async () => { - const userProfile = await admin - .firestore() - .collection("userProfiles") - .doc(userRecord.uid) - .get(); - expect(userProfile.exists).toBeTruthy(); - }); - - it("should have a project as resource", () => { - expect(loggedContext?.resource.name).toMatch(`projects/${projectId}`); - }); - - it("should not have a path", () => { - expect(loggedContext?.path).toBeUndefined(); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firebase.auth.user.create"); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have auth", () => { - expect(loggedContext?.auth).toBeUndefined(); - }); - - it("should not have an action", () => { - expect(loggedContext?.action).toBeUndefined(); - }); - - it("should have properly defined metadata", () => { - const parsedMetadata = JSON.parse(loggedContext?.metadata); - // TODO: better handle date format mismatch and precision - const expectedCreationTime = new Date(userRecord.metadata.creationTime) - .toISOString() - .replace(/\.\d{3}/, ""); - const expectedMetadata = { - ...userRecord.metadata, - creationTime: expectedCreationTime, - }; - - expect(expectedMetadata).toEqual(expect.objectContaining(parsedMetadata)); - }); - }); - - describe("user onDelete trigger", () => { - let userRecord: UserRecord; - let logSnapshot; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - userRecord = await admin.auth().createUser({ - email: `${testId}@fake-delete.com`, - password: "secret", - displayName: `${testId}`, - }); - - await admin.auth().deleteUser(userRecord.uid); - - await timeout(20000); - - logSnapshot = await admin - .firestore() - .collection("authUserOnDeleteTests") - .doc(userRecord.uid) - .get(); - loggedContext = logSnapshot.data(); - - userIds.push(userRecord.uid); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should have a project as resource", () => { - expect(loggedContext?.resource.name).toMatch(`projects/${projectId}`); - }); - - it("should not have a path", () => { - expect(loggedContext?.path).toBeUndefined(); - }); - - it("should have the correct eventType", async () => { - expect(loggedContext?.eventType).toEqual("google.firebase.auth.user.delete"); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have auth", () => { - expect(loggedContext?.auth).toBeUndefined(); - }); - - it("should not have an action", () => { - expect(loggedContext?.action).toBeUndefined(); - }); - }); - - describe("user beforeCreate trigger", () => { - let userRecord: UserCredential; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - userRecord = await createUserWithEmailAndPassword( - getAuth(app), - `${testId}@fake-before-create.com`, - "secret" - ); - - await timeout(15000); - - const logSnapshot = await admin - .firestore() - .collection("authBeforeCreateTests") - .doc(userRecord.user.uid) - .get(); - - loggedContext = logSnapshot.data(); - - userIds.push(userRecord.user.uid); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await admin.auth().deleteUser(userRecord.user.uid); - }); - - it("should have a project as resource", () => { - expect(loggedContext?.resource.name).toMatch(`projects/${projectId}`); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual( - "providers/cloud.auth/eventTypes/user.beforeCreate:password" - ); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - }); - - describe("user beforeSignIn trigger", () => { - let userRecord: UserCredential; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - userRecord = await createUserWithEmailAndPassword( - getAuth(app), - `${testId}@fake-before-signin.com`, - "secret" - ); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("authBeforeSignInTests") - .doc(userRecord.user.uid) - .get(); - - loggedContext = logSnapshot.data(); - - userIds.push(userRecord.user.uid); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await admin.auth().deleteUser(userRecord.user.uid); - }); - - it("should have a project as resource", () => { - expect(loggedContext?.resource.name).toMatch(`projects/${projectId}`); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual( - "providers/cloud.auth/eventTypes/user.beforeSignIn:password" - ); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - }); -}); diff --git a/integration_tests/tests/v1/database.test.ts b/integration_tests/tests/v1/database.test.ts deleted file mode 100644 index 0f767dc..0000000 --- a/integration_tests/tests/v1/database.test.ts +++ /dev/null @@ -1,322 +0,0 @@ -import admin from "firebase-admin"; -import { timeout } from "../utils"; -import { initializeFirebase } from "../firebaseSetup"; -import { Reference } from "@firebase/database-types"; - -describe("Firebase Database (v1)", () => { - const projectId = process.env.PROJECT_ID; - const testId = process.env.TEST_RUN_ID; - - if (!testId || !projectId) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - await admin.firestore().collection("databaseRefOnCreateTests").doc(testId).delete(); - await admin.firestore().collection("databaseRefOnDeleteTests").doc(testId).delete(); - await admin.firestore().collection("databaseRefOnUpdateTests").doc(testId).delete(); - await admin.firestore().collection("databaseRefOnWriteTests").doc(testId).delete(); - }); - - async function setupRef(refPath: string) { - const ref = admin.database().ref(refPath); - await ref.set({ ".sv": "timestamp" }); - return ref; - } - - async function teardownRef(ref: Reference) { - if (ref) { - try { - await ref.remove(); - } catch (err) { - console.log("Teardown error", err); - } - } - } - - function getLoggedContext(collectionName: string, testId: string) { - return admin - .firestore() - .collection(collectionName) - .doc(testId) - .get() - .then((logSnapshot) => logSnapshot.data()); - } - - describe("ref onCreate trigger", () => { - let ref: Reference; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - ref = await setupRef(`dbTests/${testId}/start`); - await timeout(20000); - loggedContext = await getLoggedContext("databaseRefOnCreateTests", testId); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await teardownRef(ref); - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should give refs access to admin data", async () => { - await ref.parent?.child("adminOnly").update({ allowed: 1 }); - - const adminDataSnapshot = await ref.parent?.child("adminOnly").once("value"); - const adminData = adminDataSnapshot?.val(); - - expect(adminData).toEqual({ allowed: 1 }); - }); - - it("should have a correct ref url", () => { - expect(loggedContext?.url).toMatch( - new RegExp(`^https://${projectId}(-default-rtdb)*.firebaseio.com/dbTests/${testId}/start$`) - ); - }); - - it("should have refs resources", () => { - expect(loggedContext?.resource.name).toMatch( - new RegExp( - `^projects/_/instances/${projectId}(-default-rtdb)*/refs/dbTests/${testId}/start` - ) - ); - }); - - it("should not include path", () => { - expect(loggedContext?.path).toBeUndefined(); - }); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firebase.database.ref.create"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have action", () => { - expect(loggedContext?.action).toBeUndefined(); - }); - - it("should have admin authType", () => { - expect(loggedContext?.authType).toEqual("ADMIN"); - }); - }); - - describe("ref onDelete trigger", () => { - let ref: Reference; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - ref = await setupRef(`dbTests/${testId}/start`); - await ref.remove(); - await timeout(20000); - loggedContext = await getLoggedContext("databaseRefOnDeleteTests", testId); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should have a correct ref url", () => { - expect(loggedContext?.url).toMatch( - new RegExp(`^https://${projectId}(-default-rtdb)*.firebaseio.com/dbTests/${testId}/start$`) - ); - }); - - it("should have refs resources", () => { - expect(loggedContext?.resource.name).toMatch( - new RegExp( - `^projects/_/instances/${projectId}(-default-rtdb)*/refs/dbTests/${testId}/start$` - ) - ); - }); - - it("should not include path", () => { - expect(loggedContext?.path).toBeUndefined(); - }); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firebase.database.ref.delete"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have action", () => { - expect(loggedContext?.action).toBeUndefined(); - }); - - it("should have admin authType", () => { - expect(loggedContext?.authType).toEqual("ADMIN"); - }); - }); - - describe("ref onUpdate trigger", () => { - let ref: Reference; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - ref = await setupRef(`dbTests/${testId}/start`); - await ref.update({ updated: true }); - await timeout(20000); - loggedContext = await getLoggedContext("databaseRefOnUpdateTests", testId); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await teardownRef(ref); - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should give refs access to admin data", async () => { - await ref.parent?.child("adminOnly").update({ allowed: 1 }); - - const adminDataSnapshot = await ref.parent?.child("adminOnly").once("value"); - const adminData = adminDataSnapshot?.val(); - - expect(adminData).toEqual({ allowed: 1 }); - }); - - it("should have a correct ref url", () => { - expect(loggedContext?.url).toMatch( - new RegExp(`^https://${projectId}(-default-rtdb)*.firebaseio.com/dbTests/${testId}/start$`) - ); - }); - - it("should have refs resources", () => { - expect(loggedContext?.resource.name).toMatch( - new RegExp( - `^projects/_/instances/${projectId}(-default-rtdb)*/refs/dbTests/${testId}/start$` - ) - ); - }); - - it("should not include path", () => { - expect(loggedContext?.path).toBeUndefined(); - }); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firebase.database.ref.update"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have action", () => { - expect(loggedContext?.action).toBeUndefined(); - }); - - it("should have admin authType", () => { - expect(loggedContext?.authType).toEqual("ADMIN"); - }); - - it("should log onUpdate event with updated data", async () => { - const parsedData = JSON.parse(loggedContext?.data ?? {}); - expect(parsedData).toEqual({ updated: true }); - }); - }); - - describe("ref onWrite trigger", () => { - let ref: Reference; - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - ref = await setupRef(`dbTests/${testId}/start`); - - await timeout(20000); - - loggedContext = await getLoggedContext("databaseRefOnWriteTests", testId); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await teardownRef(ref); - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should give refs access to admin data", async () => { - await ref.parent?.child("adminOnly").update({ allowed: 1 }); - - const adminDataSnapshot = await ref.parent?.child("adminOnly").once("value"); - const adminData = adminDataSnapshot?.val(); - - expect(adminData).toEqual({ allowed: 1 }); - }); - - it("should have a correct ref url", () => { - expect(loggedContext?.url).toMatch( - new RegExp(`^https://${projectId}(-default-rtdb)*.firebaseio.com/dbTests/${testId}/start$`) - ); - }); - - it("should have refs resources", () => { - expect(loggedContext?.resource.name).toMatch( - new RegExp( - `^projects/_/instances/${projectId}(-default-rtdb)*/refs/dbTests/${testId}/start$` - ) - ); - }); - - it("should not include path", () => { - expect(loggedContext?.path).toBeUndefined(); - }); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firebase.database.ref.write"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have action", () => { - expect(loggedContext?.action).toBeUndefined(); - }); - - it("should have admin authType", () => { - expect(loggedContext?.authType).toEqual("ADMIN"); - }); - }); -}); diff --git a/integration_tests/tests/v1/firestore.test.ts b/integration_tests/tests/v1/firestore.test.ts deleted file mode 100644 index a986523..0000000 --- a/integration_tests/tests/v1/firestore.test.ts +++ /dev/null @@ -1,263 +0,0 @@ -import admin from "firebase-admin"; -import { timeout } from "../utils"; -import { initializeFirebase } from "../firebaseSetup"; - -describe("Cloud Firestore (v1)", () => { - const projectId = process.env.PROJECT_ID; - const testId = process.env.TEST_RUN_ID; - - if (!testId || !projectId) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - await admin.firestore().collection("firestoreDocumentOnCreateTests").doc(testId).delete(); - await admin.firestore().collection("firestoreDocumentOnDeleteTests").doc(testId).delete(); - await admin.firestore().collection("firestoreDocumentOnUpdateTests").doc(testId).delete(); - await admin.firestore().collection("firestoreDocumentOnWriteTests").doc(testId).delete(); - }); - - describe("Document onCreate trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - let dataSnapshot: admin.firestore.DocumentSnapshot; - let docRef: admin.firestore.DocumentReference; - - beforeAll(async () => { - docRef = admin.firestore().collection("tests").doc(testId); - await docRef.set({ test: testId }); - dataSnapshot = await docRef.get(); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("firestoreDocumentOnCreateTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await admin.firestore().collection("tests").doc(testId).delete(); - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should give refs access to admin data", async () => { - const result = await docRef.set({ allowed: 1 }, { merge: true }); - expect(result).toBeTruthy(); - }); - - it("should have well-formed resource", () => { - expect(loggedContext?.resource.name).toMatch( - `projects/${projectId}/databases/(default)/documents/tests/${testId}` - ); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firestore.document.create"); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should have the correct data", () => { - expect(dataSnapshot.data()).toEqual({ test: testId }); - }); - }); - - describe("Document onDelete trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - let dataSnapshot: admin.firestore.DocumentSnapshot; - let docRef: admin.firestore.DocumentReference; - - beforeAll(async () => { - docRef = admin.firestore().collection("tests").doc(testId); - await docRef.set({ test: testId }); - dataSnapshot = await docRef.get(); - - await docRef.delete(); - - await timeout(20000); - - // Refresh snapshot - dataSnapshot = await docRef.get(); - - const logSnapshot = await admin - .firestore() - .collection("firestoreDocumentOnDeleteTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await admin.firestore().collection("tests").doc(testId).delete(); - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should have well-formed resource", () => { - expect(loggedContext?.resource.name).toMatch( - `projects/${projectId}/databases/(default)/documents/tests/${testId}` - ); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firestore.document.delete"); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have the data", () => { - expect(dataSnapshot.data()).toBeUndefined(); - }); - }); - - describe("Document onUpdate trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - let dataSnapshot: admin.firestore.DocumentSnapshot; - let docRef: admin.firestore.DocumentReference; - - beforeAll(async () => { - docRef = admin.firestore().collection("tests").doc(testId); - await docRef.set({}); - dataSnapshot = await docRef.get(); - - await docRef.update({ test: testId }); - - await timeout(20000); - - // Refresh snapshot - dataSnapshot = await docRef.get(); - - const logSnapshot = await admin - .firestore() - .collection("firestoreDocumentOnUpdateTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await admin.firestore().collection("tests").doc(testId).delete(); - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should have well-formed resource", () => { - expect(loggedContext?.resource.name).toMatch( - `projects/${projectId}/databases/(default)/documents/tests/${testId}` - ); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firestore.document.update"); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have the data", () => { - expect(dataSnapshot.data()).toStrictEqual({ test: testId }); - }); - }); - - describe("Document onWrite trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - let dataSnapshot: admin.firestore.DocumentSnapshot; - let docRef: admin.firestore.DocumentReference; - - beforeAll(async () => { - docRef = admin.firestore().collection("tests").doc(testId); - await docRef.set({ test: testId }); - dataSnapshot = await docRef.get(); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("firestoreDocumentOnWriteTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - await admin.firestore().collection("tests").doc(testId).delete(); - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should give refs access to admin data", async () => { - const result = await docRef.set({ allowed: 1 }, { merge: true }); - expect(result).toBeTruthy(); - }); - - it("should have well-formed resource", () => { - expect(loggedContext?.resource.name).toMatch( - `projects/${projectId}/databases/(default)/documents/tests/${testId}` - ); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firestore.document.write"); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have a timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should have the correct data", () => { - expect(dataSnapshot.data()).toEqual({ test: testId }); - }); - }); -}); diff --git a/integration_tests/tests/v1/pubsub.test.ts b/integration_tests/tests/v1/pubsub.test.ts deleted file mode 100644 index af1a5f2..0000000 --- a/integration_tests/tests/v1/pubsub.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -import admin from "firebase-admin"; -import { timeout } from "../utils"; -import { PubSub } from "@google-cloud/pubsub"; -import { initializeFirebase } from "../firebaseSetup"; - -describe("Pub/Sub (v1)", () => { - const projectId = process.env.PROJECT_ID; - const testId = process.env.TEST_RUN_ID; - const region = process.env.REGION; - const serviceAccountPath = process.env.GOOGLE_APPLICATION_CREDENTIALS; - const topicName = `firebase-schedule-${testId}-v1-pubsubScheduleTests-${region}`; - - if (!testId || !projectId || !region || !serviceAccountPath) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - await admin.firestore().collection("pubsubOnPublishTests").doc(testId).delete(); - await admin.firestore().collection("pubsubScheduleTests").doc(topicName).delete(); - }); - - describe("onPublish trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const serviceAccount = await import(serviceAccountPath); - const topic = new PubSub({ - credentials: serviceAccount.default, - projectId, - }).topic("pubsubTests"); - - await topic.publish(Buffer.from(JSON.stringify({ testId }))); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("pubsubOnPublishTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should have a topic as resource", () => { - expect(loggedContext?.resource.name).toEqual( - `projects/${process.env.PROJECT_ID}/topics/pubsubTests` - ); - }); - - it("should not have a path", () => { - expect(loggedContext?.path).toBeUndefined(); - }); - - it("should have the correct eventType", () => { - expect(loggedContext?.eventType).toEqual("google.pubsub.topic.publish"); - }); - - it("should have an eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have action", () => { - expect(loggedContext?.action).toBeUndefined(); - }); - - it("should have admin auth", () => { - expect(loggedContext?.auth).toBeUndefined(); - }); - - it("should have pubsub data", () => { - const decodedMessage = JSON.parse(loggedContext?.message); - const decoded = new Buffer(decodedMessage.data, "base64").toString(); - const parsed = JSON.parse(decoded); - expect(parsed.testId).toEqual(testId); - }); - }); - - describe("schedule trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const pubsub = new PubSub(); - - const message = Buffer.from(JSON.stringify({ testId })); - - await pubsub.topic(topicName).publish(message); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("pubsubScheduleTests") - .doc(topicName) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should have been called", () => { - expect(loggedContext).toBeDefined(); - }); - }); -}); diff --git a/integration_tests/tests/v1/remoteConfig.test.ts b/integration_tests/tests/v1/remoteConfig.test.ts deleted file mode 100644 index c05bb5d..0000000 --- a/integration_tests/tests/v1/remoteConfig.test.ts +++ /dev/null @@ -1,74 +0,0 @@ -import admin from "firebase-admin"; -import { timeout } from "../utils"; -import { initializeFirebase } from "../firebaseSetup"; -import fetch from "node-fetch"; - -describe("Firebase Remote Config (v1)", () => { - const projectId = process.env.PROJECT_ID; - const testId = process.env.TEST_RUN_ID; - - if (!testId || !projectId) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - await admin.firestore().collection("remoteConfigOnUpdateTests").doc(testId).delete(); - }); - - describe("onUpdate trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const accessToken = await admin.credential.applicationDefault().getAccessToken(); - const resp = await fetch( - `https://firebaseremoteconfig.googleapis.com/v1/projects/${projectId}/remoteConfig`, - { - method: "PUT", - headers: { - Authorization: `Bearer ${accessToken.access_token}`, - "Content-Type": "application/json; UTF-8", - "Accept-Encoding": "gzip", - "If-Match": "*", - }, - body: JSON.stringify({ version: { description: testId } }), - } - ); - if (!resp.ok) { - throw new Error(resp.statusText); - } - await timeout(20000); - const logSnapshot = await admin - .firestore() - .collection("remoteConfigOnUpdateTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should have refs resources", () => - expect(loggedContext?.resource.name).toMatch(`projects/${process.env.PROJECT_ID}`)); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.firebase.remoteconfig.update"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - - it("should not have auth", () => { - expect(loggedContext?.auth).toBeUndefined(); - }); - }); -}); diff --git a/integration_tests/tests/v1/storage.test.ts b/integration_tests/tests/v1/storage.test.ts deleted file mode 100644 index 4d8e251..0000000 --- a/integration_tests/tests/v1/storage.test.ts +++ /dev/null @@ -1,186 +0,0 @@ -import * as admin from "firebase-admin"; -import { timeout } from "../utils"; -import { initializeFirebase } from "../firebaseSetup"; - -async function uploadBufferToFirebase(buffer: Buffer, fileName: string) { - const bucket = admin.storage().bucket(); - - const file = bucket.file(fileName); - await file.save(buffer, { - metadata: { - contentType: "text/plain", - }, - }); -} - -describe("Firebase Storage", () => { - const testId = process.env.TEST_RUN_ID; - if (!testId) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - await admin.firestore().collection("storageOnFinalizeTests").doc(testId).delete(); - await admin.firestore().collection("storageOnDeleteTests").doc(testId).delete(); - await admin.firestore().collection("storageOnMetadataUpdateTests").doc(testId).delete(); - }); - - describe("object onFinalize trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const testContent = testId; - const buffer = Buffer.from(testContent, "utf-8"); - - await uploadBufferToFirebase(buffer, testId + ".txt"); - - await timeout(20000); - const logSnapshot = await admin - .firestore() - .collection("storageOnFinalizeTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - const file = admin - .storage() - .bucket() - .file(testId + ".txt"); - - const [exists] = await file.exists(); - if (exists) { - await file.delete(); - } - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.storage.object.finalize"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - }); - - describe("object onDelete trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const testContent = testId; - const buffer = Buffer.from(testContent, "utf-8"); - - await uploadBufferToFirebase(buffer, testId + ".txt"); - - await timeout(5000); // Short delay before delete - - const file = admin - .storage() - .bucket() - .file(testId + ".txt"); - await file.delete(); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("storageOnDeleteTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.storage.object.delete"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - }); - - describe("object onMetadataUpdate trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const testContent = testId; - const buffer = Buffer.from(testContent, "utf-8"); - - await uploadBufferToFirebase(buffer, testId + ".txt"); - - // Trigger metadata update - const file = admin - .storage() - .bucket() - .file(testId + ".txt"); - await file.setMetadata({ contentType: "application/json" }); - - await timeout(20000); - - const logSnapshot = await admin - .firestore() - .collection("storageOnMetadataUpdateTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - afterAll(async () => { - const file = admin - .storage() - .bucket() - .file(testId + ".txt"); - - const [exists] = await file.exists(); - if (exists) { - await file.delete(); - } - }); - - it("should not have event.app", () => { - expect(loggedContext?.app).toBeUndefined(); - }); - - it("should have the right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.storage.object.metadataUpdate"); - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have timestamp", () => { - expect(loggedContext?.timestamp).toBeDefined(); - }); - }); -}); diff --git a/integration_tests/tests/v1/tasks.test.ts b/integration_tests/tests/v1/tasks.test.ts deleted file mode 100644 index 2b985ba..0000000 --- a/integration_tests/tests/v1/tasks.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import * as admin from "firebase-admin"; -import { initializeFirebase } from "../firebaseSetup"; -import { createTask, timeout } from "../utils"; - -describe("Cloud Tasks (v1)", () => { - const region = process.env.REGION; - const testId = process.env.TEST_RUN_ID; - const projectId = process.env.PROJECT_ID; - const queueName = `${testId}-v1-tasksOnDispatchTests`; - - if (!testId || !projectId || !region) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - await admin.firestore().collection("tasksOnDispatchTests").doc(testId).delete(); - }); - - describe("onDispatch trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const url = `https://${region}-${projectId}.cloudfunctions.net/${testId}-v1-tasksOnDispatchTests`; - await createTask(projectId, queueName, region, url, { data: { testId } }); - - await timeout(20000); - const logSnapshot = await admin - .firestore() - .collection("tasksOnDispatchTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should have correct event id", () => { - expect(loggedContext?.id).toBeDefined(); - }); - }); -}); diff --git a/integration_tests/tests/v1/testLab.test.ts b/integration_tests/tests/v1/testLab.test.ts deleted file mode 100644 index b6a7e5d..0000000 --- a/integration_tests/tests/v1/testLab.test.ts +++ /dev/null @@ -1,52 +0,0 @@ -import admin from "firebase-admin"; -import { startTestRun, timeout } from "../utils"; -import { initializeFirebase } from "../firebaseSetup"; - -describe("TestLab (v1)", () => { - const projectId = process.env.PROJECT_ID; - const testId = process.env.TEST_RUN_ID; - - if (!testId || !projectId) { - throw new Error("Environment configured incorrectly."); - } - - beforeAll(async () => { - await initializeFirebase(); - }); - - afterAll(async () => { - await admin.firestore().collection("testLabOnCompleteTests").doc(testId).delete(); - }); - - describe("test matrix onComplete trigger", () => { - let loggedContext: admin.firestore.DocumentData | undefined; - - beforeAll(async () => { - const accessToken = await admin.credential.applicationDefault().getAccessToken(); - await startTestRun(projectId, testId, accessToken.access_token); - await timeout(20000); - const logSnapshot = await admin - .firestore() - .collection("testLabOnCompleteTests") - .doc(testId) - .get(); - loggedContext = logSnapshot.data(); - if (!loggedContext) { - throw new Error("loggedContext is undefined"); - } - }); - - it("should have eventId", () => { - expect(loggedContext?.eventId).toBeDefined(); - }); - - it("should have right eventType", () => { - expect(loggedContext?.eventType).toEqual("google.testing.testMatrix.complete"); - }); - - it("should be in state 'INVALID'", () => { - const matrix = JSON.parse(loggedContext?.matrix); - expect(matrix?.state).toEqual("INVALID"); - }); - }); -}); diff --git a/integration_tests/tests/v2/firestore.test.ts b/integration_tests/tests/v2/firestore.test.ts index 7d2efac..fa75b68 100644 --- a/integration_tests/tests/v2/firestore.test.ts +++ b/integration_tests/tests/v2/firestore.test.ts @@ -15,10 +15,26 @@ describe("Cloud Firestore (v2)", () => { }); afterAll(async () => { - await admin.firestore().collection("firestoreOnDocumentCreatedTests").doc(testId).delete(); - await admin.firestore().collection("firestoreOnDocumentDeletedTests").doc(testId).delete(); - await admin.firestore().collection("firestoreOnDocumentUpdatedTests").doc(testId).delete(); - await admin.firestore().collection("firestoreOnDocumentWrittenTests").doc(testId).delete(); + await admin + .firestore() + .collection("firestoreOnDocumentCreatedTests") + .doc(testId) + .delete(); + await admin + .firestore() + .collection("firestoreOnDocumentDeletedTests") + .doc(testId) + .delete(); + await admin + .firestore() + .collection("firestoreOnDocumentUpdatedTests") + .doc(testId) + .delete(); + await admin + .firestore() + .collection("firestoreOnDocumentWrittenTests") + .doc(testId) + .delete(); }); describe("Document created trigger", () => { @@ -61,7 +77,9 @@ describe("Cloud Firestore (v2)", () => { }); it("should have the correct type", () => { - expect(loggedContext?.type).toEqual("google.cloud.firestore.document.v1.created"); + expect(loggedContext?.type).toEqual( + "google.cloud.firestore.document.v1.created" + ); }); it("should have an id", () => { @@ -117,7 +135,9 @@ describe("Cloud Firestore (v2)", () => { }); it("should have the correct type", () => { - expect(loggedContext?.type).toEqual("google.cloud.firestore.document.v1.deleted"); + expect(loggedContext?.type).toEqual( + "google.cloud.firestore.document.v1.deleted" + ); }); it("should have an id", () => { @@ -173,7 +193,9 @@ describe("Cloud Firestore (v2)", () => { }); it("should have the correct type", () => { - expect(loggedContext?.type).toEqual("google.cloud.firestore.document.v1.updated"); + expect(loggedContext?.type).toEqual( + "google.cloud.firestore.document.v1.updated" + ); }); it("should have an id", () => { @@ -229,7 +251,9 @@ describe("Cloud Firestore (v2)", () => { }); it("should have the correct type", () => { - expect(loggedContext?.type).toEqual("google.cloud.firestore.document.v1.written"); + expect(loggedContext?.type).toEqual( + "google.cloud.firestore.document.v1.written" + ); }); it("should have an id", () => {