Skip to content

Commit

Permalink
Merge branch 'master' into hsinpei/storage-emulator-multiple-targets-5
Browse files Browse the repository at this point in the history
  • Loading branch information
tohhsinpei committed Mar 18, 2022
2 parents 0510f3c + 089bea2 commit 4fc31d0
Show file tree
Hide file tree
Showing 21 changed files with 564 additions and 239 deletions.
129 changes: 93 additions & 36 deletions schema/firebase-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,54 +326,111 @@
"type": "object"
},
"functions": {
"additionalProperties": false,
"properties": {
"ignore": {
"items": {
"type": "string"
},
"type": "array"
},
"postdeploy": {
"anyOf": [
{
"anyOf": [
{
"additionalProperties": false,
"properties": {
"ignore": {
"items": {
"type": "string"
},
"type": "array"
},
{
"postdeploy": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"type": "string"
}
]
},
"predeploy": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"type": "string"
}
]
},
"runtime": {
"enum": [
"nodejs10",
"nodejs12",
"nodejs14",
"nodejs16"
],
"type": "string"
},
"source": {
"type": "string"
}
]
},
"type": "object"
},
"predeploy": {
"anyOf": [
{
"items": {
{
"items": {
"additionalProperties": false,
"properties": {
"ignore": {
"items": {
"type": "string"
},
"type": "array"
},
"postdeploy": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"type": "string"
}
]
},
"predeploy": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"type": "string"
}
]
},
"runtime": {
"enum": [
"nodejs10",
"nodejs12",
"nodejs14",
"nodejs16"
],
"type": "string"
},
"type": "array"
"source": {
"type": "string"
}
},
{
"type": "string"
}
]
},
"runtime": {
"enum": [
"nodejs10",
"nodejs12",
"nodejs14",
"nodejs16"
],
"type": "string"
},
"source": {
"type": "string"
"type": "object"
},
"type": "array"
}
},
"type": "object"
]
},
"hosting": {
"anyOf": [
Expand Down
18 changes: 18 additions & 0 deletions scripts/integration-helpers/framework.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch, { Response } from "node-fetch";

import { CLIProcess } from "./cli";
import { Emulators } from "../../src/emulator/types";

const FIREBASE_PROJECT_ZONE = "us-central1";

Expand Down Expand Up @@ -256,6 +257,23 @@ export class TriggerEndToEndTest {
return this.cliProcess ? this.cliProcess.stop() : Promise.resolve();
}

applyTargets(emulatorType: Emulators, target: string, resource: string): Promise<void> {
const cli = new CLIProcess("default", this.workdir);
const started = cli.start(
"target:apply",
this.project,
[emulatorType, target, resource],
(data: unknown) => {
if (typeof data !== "string" && !Buffer.isBuffer(data)) {
throw new Error(`data is not a string or buffer (${typeof data})`);
}
return data.includes(`Applied ${emulatorType} target`);
}
);
this.cliProcess = cli;
return started;
}

invokeHttpFunction(name: string, zone = FIREBASE_PROJECT_ZONE): Promise<Response> {
const url = `http://localhost:${[this.functionsEmulatorPort, this.project, zone, name].join(
"/"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"storage": [
{
"target": "allowNone",
"rules": "allowNone.rules"
},
{
"target": "allowAll",
"rules": "allowAll.rules"
}
],
"emulators": {
"storage": {
"port": 9199
}
}
}


64 changes: 64 additions & 0 deletions scripts/storage-emulator-integration/multiple-targets/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import supertest = require("supertest");
import { Emulators } from "../../../src/emulator/types";
import { TriggerEndToEndTest } from "../../integration-helpers/framework";
import {
EMULATORS_SHUTDOWN_DELAY_MS,
FIREBASE_EMULATOR_CONFIG,
getStorageEmulatorHost,
readEmulatorConfig,
TEST_SETUP_TIMEOUT,
} from "../utils";

const FIREBASE_PROJECT = process.env.FBTOOLS_TARGET_PROJECT || "fake-project-id";

describe("Multiple Storage Deploy Targets", () => {
let test: TriggerEndToEndTest;
const allowNoneBucket = `${FIREBASE_PROJECT}.appspot.com`;
const allowAllBucket = `${FIREBASE_PROJECT}-2.appspot.com`;
const emulatorConfig = readEmulatorConfig(FIREBASE_EMULATOR_CONFIG);
const STORAGE_EMULATOR_HOST = getStorageEmulatorHost(emulatorConfig);

before(async function (this) {
this.timeout(TEST_SETUP_TIMEOUT);

test = new TriggerEndToEndTest(FIREBASE_PROJECT, __dirname, emulatorConfig);
await test.applyTargets(Emulators.STORAGE, "allowNone", allowNoneBucket);
await test.applyTargets(Emulators.STORAGE, "allowAll", allowAllBucket);
await test.startEmulators(["--only", Emulators.STORAGE]);
});

it("should enforce different rules for different targets", async () => {
const uploadURL = await supertest(STORAGE_EMULATOR_HOST)
.post(`/v0/b/${allowNoneBucket}/o/test_upload.jpg?uploadType=resumable&name=test_upload.jpg`)
.set({ "X-Goog-Upload-Protocol": "resumable", "X-Goog-Upload-Command": "start" })
.expect(200)
.then((res) => new URL(res.header["x-goog-upload-url"]));

await supertest(STORAGE_EMULATOR_HOST)
.put(uploadURL.pathname + uploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "upload, finalize",
})
.expect(403);

const otherUploadURL = await supertest(STORAGE_EMULATOR_HOST)
.post(`/v0/b/${allowAllBucket}/o/test_upload.jpg?uploadType=resumable&name=test_upload.jpg`)
.set({ "X-Goog-Upload-Protocol": "resumable", "X-Goog-Upload-Command": "start" })
.expect(200)
.then((res) => new URL(res.header["x-goog-upload-url"]));

await supertest(STORAGE_EMULATOR_HOST)
.put(otherUploadURL.pathname + otherUploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "upload, finalize",
})
.expect(200);
});

after(async function (this) {
this.timeout(EMULATORS_SHUTDOWN_DELAY_MS);
await test.stopEmulators();
});
});
2 changes: 2 additions & 0 deletions scripts/storage-emulator-integration/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ firebase setup:emulators:storage

mocha scripts/storage-emulator-integration/rules/*.test.ts

mocha scripts/storage-emulator-integration/multiple-targets/tests.ts

mocha scripts/storage-emulator-integration/tests.ts

0 comments on commit 4fc31d0

Please sign in to comment.