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

Revert Remove __trigger #1274

Merged
merged 8 commits into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions spec/v1/cloud-functions.spec.ts
Expand Up @@ -41,7 +41,7 @@ describe("makeCloudFunction", () => {
legacyEventType: "providers/provider/eventTypes/event",
};

it("should put a __endpoint on the returned CloudFunction", () => {
it("should put a __trigger/__endpoint on the returned CloudFunction", () => {
const cf = makeCloudFunction({
provider: "mock.provider",
eventType: "mock.event",
Expand All @@ -50,6 +50,14 @@ describe("makeCloudFunction", () => {
handler: () => null,
});

expect(cf.__trigger).to.deep.equal({
eventTrigger: {
eventType: "mock.provider.mock.event",
resource: "resource",
service: "service",
},
});

expect(cf.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand All @@ -64,9 +72,17 @@ describe("makeCloudFunction", () => {
});
});

it("should have legacy event type in __endpoint if provided", () => {
it("should have legacy event type in __trigger/__endpoint if provided", () => {
const cf = makeCloudFunction(cloudFunctionArgs);

expect(cf.__trigger).to.deep.equal({
eventTrigger: {
eventType: "providers/provider/eventTypes/event",
resource: "resource",
service: "service",
},
});

expect(cf.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down
58 changes: 27 additions & 31 deletions spec/v1/function-builder.spec.ts
Expand Up @@ -24,7 +24,6 @@ import { expect } from "chai";
import { clearParams, defineSecret } from "../../src/params";

import * as functions from "../../src/v1";
import { ResetValue } from "../../src/common/options";

describe("FunctionBuilder", () => {
before(() => {
Expand All @@ -41,7 +40,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.region).to.deep.equal(["us-east1"]);
expect(fn.__trigger.regions).to.deep.equal(["us-east1"]);
TheIronDev marked this conversation as resolved.
Show resolved Hide resolved
});

it("should allow multiple supported regions to be set", () => {
Expand All @@ -50,7 +49,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.region).to.deep.equal(["us-east1", "us-central1"]);
expect(fn.__trigger.regions).to.deep.equal(["us-east1", "us-central1"]);
});

it("should allow all supported regions to be set", () => {
Expand All @@ -68,7 +67,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.region).to.deep.equal([
expect(fn.__trigger.regions).to.deep.equal([
"us-central1",
"us-east1",
"us-east4",
Expand Down Expand Up @@ -104,6 +103,11 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.secrets).to.deep.equal([
{
name: "API_KEY",
},
]);
expect(fn.__endpoint.secretEnvironmentVariables).to.deep.equal([
{
key: "API_KEY",
Expand Down Expand Up @@ -136,9 +140,9 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.region).to.deep.equal(["europe-west2"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
expect(fn.__trigger.regions).to.deep.equal(["europe-west2"]);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");
});

it("should allow both valid runtime options and supported region to be set in reverse order", () => {
Expand All @@ -151,9 +155,9 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.region).to.deep.equal(["europe-west1"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
expect(fn.__trigger.regions).to.deep.equal(["europe-west1"]);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");
});

it("should fail if supported region but invalid runtime options are set (reverse order)", () => {
Expand Down Expand Up @@ -223,7 +227,7 @@ describe("FunctionBuilder", () => {
.runWith({ ingressSettings: "ALLOW_INTERNAL_ONLY" })
.https.onRequest(() => undefined);

expect(fn.__endpoint.ingressSettings).to.equal("ALLOW_INTERNAL_ONLY");
expect(fn.__trigger.ingressSettings).to.equal("ALLOW_INTERNAL_ONLY");
});

it("should throw an error if user chooses an invalid ingressSettings", () => {
Expand All @@ -245,11 +249,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

if (!(fn.__endpoint.vpc instanceof ResetValue)) {
expect(fn.__endpoint.vpc.connector).to.equal("test-connector");
} else {
expect.fail("__endpoint.vpc unexpectedly set to RESET_VALUE");
}
expect(fn.__trigger.vpcConnector).to.equal("test-connector");
});

it("should allow a vpcConnectorEgressSettings to be set", () => {
Expand All @@ -261,11 +261,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

if (!(fn.__endpoint.vpc instanceof ResetValue)) {
expect(fn.__endpoint.vpc.egressSettings).to.equal("PRIVATE_RANGES_ONLY");
} else {
expect.fail("__endpoint.vpc unexpectedly set to RESET_VALUE");
}
expect(fn.__trigger.vpcConnectorEgressSettings).to.equal("PRIVATE_RANGES_ONLY");
});

it("should throw an error if user chooses an invalid vpcConnectorEgressSettings", () => {
Expand Down Expand Up @@ -296,14 +292,17 @@ describe("FunctionBuilder", () => {

it("should allow a serviceAccount to be set with generated service account email", () => {
const serviceAccount = "test-service-account@";
const projectId = process.env.GCLOUD_PROJECT;
const fn = functions
.runWith({
serviceAccount,
})
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.serviceAccountEmail).to.equal(`test-service-account@`);
expect(fn.__trigger.serviceAccountEmail).to.equal(
`test-service-account@${projectId}.iam.gserviceaccount.com`
);
});

it("should set a null serviceAccountEmail if service account is set to `default`", () => {
Expand All @@ -315,7 +314,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.serviceAccountEmail).to.equal("default");
expect(fn.__trigger.serviceAccountEmail).to.be.null;
});

it("should throw an error if serviceAccount is set to an invalid value", () => {
Expand Down Expand Up @@ -349,7 +348,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.labels).to.deep.equal({
expect(fn.__trigger.labels).to.deep.equal({
"valid-key": "valid-value",
});
});
Expand Down Expand Up @@ -504,22 +503,19 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.secretEnvironmentVariables).to.deep.equal([
{
key: "API_KEY",
},
]);
expect(fn.__trigger.secrets).to.deep.equal(secrets);
});

it("should throw error given secrets expressed with full resource name", () => {
const sp = defineSecret("projects/my-project/secrets/API_KEY");

expect(() =>
functions.runWith({
secrets: ["projects/my-project/secrets/API_KEY"],
})
).to.throw();
});

it("should throw error given invalid secret config", () => {
const sp = defineSecret("projects/my-project/secrets/API_KEY");
expect(() =>
functions.runWith({
secrets: [sp],
Expand Down
18 changes: 15 additions & 3 deletions spec/v1/providers/analytics.spec.ts
Expand Up @@ -48,6 +48,10 @@ describe("Analytics Functions", () => {
.analytics.event("event")
.onLog((event) => event);

expect(fn.__trigger.regions).to.deep.equal(["us-east1"]);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");

expect(fn.__endpoint.region).to.deep.equal(["us-east1"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
Expand All @@ -57,6 +61,14 @@ describe("Analytics Functions", () => {
it("should return a trigger/endpoint with appropriate values", () => {
const cloudFunction = analytics.event("first_open").onLog(() => null);

expect(cloudFunction.__trigger).to.deep.equal({
eventTrigger: {
eventType: "providers/google.firebase.analytics/eventTypes/event.log",
resource: "projects/project1/events/first_open",
service: "app-measurement.com",
},
});

expect(cloudFunction.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down Expand Up @@ -293,12 +305,12 @@ describe("Analytics Functions", () => {
});

describe("process.env.GCLOUD_PROJECT not set", () => {
it("should not throw if __endpoint is not accessed", () => {
it("should not throw if __trigger is not accessed", () => {
expect(() => analytics.event("event").onLog(() => null)).to.not.throw(Error);
});

it("should throw when __endpoint is accessed", () => {
expect(() => analytics.event("event").onLog(() => null).__endpoint).to.throw(Error);
it("should throw when trigger is accessed", () => {
TheIronDev marked this conversation as resolved.
Show resolved Hide resolved
expect(() => analytics.event("event").onLog(() => null).__trigger).to.throw(Error);
});

it("should not throw when #run is called", () => {
Expand Down
76 changes: 72 additions & 4 deletions spec/v1/providers/auth.spec.ts
Expand Up @@ -47,6 +47,16 @@ describe("Auth Functions", () => {
};

describe("AuthBuilder", () => {
function expectedTrigger(project: string, eventType: string) {
return {
eventTrigger: {
resource: `projects/${project}`,
eventType: `providers/firebase.auth/eventTypes/${eventType}`,
service: "firebaseauth.googleapis.com",
},
};
}

function expectedEndpoint(project: string, eventType: string) {
return {
...MINIMAL_V1_ENDPOINT,
Expand Down Expand Up @@ -86,9 +96,9 @@ describe("Auth Functions", () => {
.auth.user()
.onCreate(() => null);

expect(fn.__endpoint.region).to.deep.equal(["us-east1"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
expect(fn.__trigger.regions).to.deep.equal(["us-east1"]);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");

expect(fn.__endpoint.region).to.deep.equal(["us-east1"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
Expand All @@ -99,6 +109,8 @@ describe("Auth Functions", () => {
it("should return a trigger/endpoint with appropriate values", () => {
const cloudFunction = auth.user().onCreate(() => null);

expect(cloudFunction.__trigger).to.deep.equal(expectedTrigger(project, "user.create"));

expect(cloudFunction.__endpoint).to.deep.equal(expectedEndpoint(project, "user.create"));
});
});
Expand All @@ -107,6 +119,8 @@ describe("Auth Functions", () => {
it("should return a trigger/endpoint with appropriate values", () => {
const cloudFunction = auth.user().onDelete(handler);

expect(cloudFunction.__trigger).to.deep.equal(expectedTrigger(project, "user.delete"));

expect(cloudFunction.__endpoint).to.deep.equal(expectedEndpoint(project, "user.delete"));
});
});
Expand All @@ -115,6 +129,17 @@ describe("Auth Functions", () => {
it("should create the function without options", () => {
const fn = auth.user().beforeCreate(() => Promise.resolve());

expect(fn.__trigger).to.deep.equal({
labels: {},
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeCreate",
options: {
accessToken: false,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down Expand Up @@ -151,6 +176,20 @@ describe("Auth Functions", () => {
})
.beforeCreate(() => Promise.resolve());

expect(fn.__trigger).to.deep.equal({
labels: {},
regions: ["us-east1"],
availableMemoryMb: 256,
timeout: "90s",
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeCreate",
options: {
accessToken: true,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down Expand Up @@ -180,6 +219,17 @@ describe("Auth Functions", () => {
it("should create the function without options", () => {
const fn = auth.user().beforeSignIn(() => Promise.resolve());

expect(fn.__trigger).to.deep.equal({
labels: {},
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeSignIn",
options: {
accessToken: false,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down Expand Up @@ -216,6 +266,20 @@ describe("Auth Functions", () => {
})
.beforeSignIn(() => Promise.resolve());

expect(fn.__trigger).to.deep.equal({
labels: {},
regions: ["us-east1"],
availableMemoryMb: 256,
timeout: "90s",
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeSignIn",
options: {
accessToken: true,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down Expand Up @@ -258,10 +322,14 @@ describe("Auth Functions", () => {
});

describe("process.env.GCLOUD_PROJECT not set", () => {
it("should not throw if __endpoint is not accessed", () => {
it("should not throw if __trigger is not accessed", () => {
expect(() => auth.user().onCreate(() => null)).to.not.throw(Error);
});

it("should throw when trigger is accessed", () => {
expect(() => auth.user().onCreate(() => null).__trigger).to.throw(Error);
});

it("should throw when endpoint is accessed", () => {
expect(() => auth.user().onCreate(() => null).__endpoint).to.throw(Error);
});
Expand Down