Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
inlined committed Mar 1, 2024
1 parent 5a94583 commit 3365a38
Show file tree
Hide file tree
Showing 36 changed files with 363 additions and 75 deletions.
29 changes: 29 additions & 0 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import { expect } from "chai";

import {
onInit,
Event,
EventContext,
makeCloudFunction,
Expand All @@ -41,6 +42,34 @@ describe("makeCloudFunction", () => {
legacyEventType: "providers/provider/eventTypes/event",
};

it("should call the onInit callback", async () => {
const test: Event = {
context: {
eventId: "00000",
timestamp: "2016-11-04T21:29:03.496Z",
eventType: "provider.event",
resource: {
service: "provider",
name: "resource",
},
},
data: "data",
};
const cf = makeCloudFunction({
provider: "mock.provider",
eventType: "mock.event",
service: "service",
triggerResource: () => "resource",
handler: () => null,
});

let hello;
onInit(() => (hello = "world"));
expect(hello).is.undefined;
await cf(test.data, test.context);
expect(hello).equals("world");
});

it("should put a __trigger/__endpoint on the returned CloudFunction", () => {
const cf = makeCloudFunction({
provider: "mock.provider",
Expand Down
7 changes: 5 additions & 2 deletions spec/v1/providers/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("Analytics Functions", () => {
});

describe("#dataConstructor", () => {
it("should handle an event with the appropriate fields", () => {
it("should handle an event with the appropriate fields", async () => {
const cloudFunction = analytics
.event("first_open")
.onLog((data: analytics.AnalyticsEvent) => data);
Expand All @@ -109,13 +109,16 @@ describe("Analytics Functions", () => {
},
};

return expect(cloudFunction(event.data, event.context)).to.eventually.deep.equal({
let hello;
functions.onInit(() => hello = "world");
await expect(cloudFunction(event.data, event.context)).to.eventually.deep.equal({
params: {},
user: {
userId: "hi!",
userProperties: {},
},
});
expect(hello).equals("world");
});

it("should remove xValues", () => {
Expand Down
26 changes: 26 additions & 0 deletions spec/v1/providers/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,30 @@ describe("Auth Functions", () => {
expect(cf.run).to.not.throw(Error);
});
});

describe("onInit", () => {
beforeEach(() => {
functions.onInit(() => {});
process.env.GCLOUD_PROJECT = "project";
});

after(() => {
functions.onInit(null);
delete process.env.GCLOUD_PROJECT;
})

it("initailizes before onCreate", async () => {
let hello;
functions.onInit(() => hello = "world");
await auth.user().onCreate(() => null)(event.data, event.context);
expect(hello).equals("world");
});

it("initailizes before onDelete", async () => {
let hello;
functions.onInit(() => hello = "world");
await auth.user().onDelete(() => null)(event.data, event.context);
expect(hello).equals("world");
});
});
});
32 changes: 24 additions & 8 deletions spec/v1/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", () => {
it("should return a handler that emits events with a proper DataSnapshot", async () => {
const event = {
data: {
data: null,
Expand All @@ -133,7 +133,11 @@ describe("Database Functions", () => {
expect(change.after.val()).to.deep.equal({ foo: "bar" });
});

return handler(event.data, event.context);
let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
});

it("Should have params of the correct type", () => {
Expand Down Expand Up @@ -177,7 +181,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", () => {
it("should return a handler that emits events with a proper DataSnapshot", async () => {
const event = {
data: {
data: null,
Expand All @@ -195,7 +199,11 @@ describe("Database Functions", () => {
expect(data.val()).to.deep.equal({ foo: "bar" });
});

return handler(event.data, event.context);
let hello;
functions.onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
});

it("Should have params of the correct type", () => {
Expand Down Expand Up @@ -239,7 +247,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", () => {
it("should return a handler that emits events with a proper DataSnapshot", async () => {
const event = {
data: {
data: null,
Expand All @@ -257,7 +265,11 @@ describe("Database Functions", () => {
expect(change.after.val()).to.deep.equal({ foo: "bar" });
});

return handler(event.data, event.context);
let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
});

it("Should have params of the correct type", () => {
Expand Down Expand Up @@ -301,7 +313,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", () => {
it("should return a handler that emits events with a proper DataSnapshot", async () => {
const event = {
data: {
data: { foo: "bar" },
Expand All @@ -319,7 +331,11 @@ describe("Database Functions", () => {
expect(data.val()).to.deep.equal({ foo: "bar" });
});

return handler(event.data, event.context);
let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
});

it("Should have params of the correct type", () => {
Expand Down
36 changes: 28 additions & 8 deletions spec/v1/providers/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe("Firestore Functions", () => {
delete process.env.GCLOUD_PROJECT;
});

it('constructs appropriate fields and getters for event.data on "document.write" events', () => {
it('constructs appropriate fields and getters for event.data on "document.write" events', async () => {
const testFunction = firestore.document("path").onWrite((change) => {
expect(change.before.data()).to.deep.equal({
key1: false,
Expand All @@ -246,20 +246,30 @@ describe("Firestore Functions", () => {
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent(createOldValue(), createValue());
return testFunction(event.data, event.context);

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
}).timeout(5000);

it('constructs appropriate fields and getters for event.data on "document.create" events', () => {
it('constructs appropriate fields and getters for event.data on "document.create" events', async () => {
const testFunction = firestore.document("path").onCreate((data) => {
expect(data.data()).to.deep.equal({ key1: true, key2: 123 });
expect(data.get("key1")).to.equal(true);
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent({}, createValue());
return testFunction(event.data, event.context);

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
}).timeout(5000);

it('constructs appropriate fields and getters for event.data on "document.update" events', () => {
it('constructs appropriate fields and getters for event.data on "document.update" events', async () => {
const testFunction = firestore.document("path").onUpdate((change) => {
expect(change.before.data()).to.deep.equal({
key1: false,
Expand All @@ -271,17 +281,27 @@ describe("Firestore Functions", () => {
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent(createOldValue(), createValue());
return testFunction(event.data, event.context);

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
}).timeout(5000);

it('constructs appropriate fields and getters for event.data on "document.delete" events', () => {
it('constructs appropriate fields and getters for event.data on "document.delete" events', async () => {
const testFunction = firestore.document("path").onDelete((data) => {
expect(data.data()).to.deep.equal({ key1: false, key2: 111 });
expect(data.get("key1")).to.equal(false);
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent(createOldValue(), {});
return testFunction(event.data, event.context);

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
}).timeout(5000);
});

Expand Down
45 changes: 43 additions & 2 deletions spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import { runHandler } from "../../helper";
import { MINIMAL_V1_ENDPOINT } from "../../fixtures";
import { CALLABLE_AUTH_HEADER, ORIGINAL_AUTH_HEADER } from "../../../src/common/providers/https";
import { onInit } from "../../../src/v1";

describe("CloudHttpsBuilder", () => {
describe("#onRequest", () => {
Expand Down Expand Up @@ -70,6 +71,26 @@ describe("CloudHttpsBuilder", () => {
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
expect(fn.__endpoint.httpsTrigger.invoker).to.deep.equal(["private"]);
});

it("should call initializer", async () => {
let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
const fn = functions.https.onRequest(() => null);
const req = new MockRequest(
{
data: { foo: "bar" },
},
{
"content-type": "application/json",
}
);
req.method = "POST";
// We don't really have test infrastructure to fake requests. Luckily we
// don't touch much of the request in boilerplate, just trace context.
await fn({headers: []} as any, null as any);
expect(hello).to.equal("world");
});
});
});

Expand Down Expand Up @@ -114,7 +135,7 @@ describe("#onCall", () => {
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
});

it("has a .run method", () => {
it("has a .run method", async () => {
const cf = https.onCall((d, c) => {
return { data: d, context: c };
});
Expand All @@ -127,7 +148,8 @@ describe("#onCall", () => {
token: "token",
},
};
expect(cf.run(data, context)).to.deep.equal({ data, context });

await expect(cf.run(data, context)).to.eventually.deep.equal({ data, context });
});

// Regression test for firebase-functions#947
Expand All @@ -152,6 +174,25 @@ describe("#onCall", () => {
expect(gotData).to.deep.equal({ foo: "bar" });
});

it("should call initializer", async () => {
const func = https.onCall(() => null);
const req = new MockRequest(
{
data: {},
},
{
"content-type": "application/json",
}
);
req.method = "POST";

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await runHandler(func, req as any);
expect(hello).to.equal("world");
});

// Test for firebase-tools#5210
it("should create context.auth for v1 emulated functions", async () => {
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("skipTokenVerification").returns(true);
Expand Down
8 changes: 6 additions & 2 deletions spec/v1/providers/pubsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe("Pubsub Functions", () => {
expect(() => pubsub.topic("bad/topic/format")).to.throw(Error);
});

it("should properly handle a new-style event", () => {
it("should properly handle a new-style event", async () => {
const raw = new Buffer('{"hello":"world"}', "utf8").toString("base64");
const event: Event = {
data: {
Expand Down Expand Up @@ -151,11 +151,15 @@ describe("Pubsub Functions", () => {
};
});

return expect(result(event.data, event.context)).to.eventually.deep.equal({
let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await expect(result(event.data, event.context)).to.eventually.deep.equal({
raw,
json: { hello: "world" },
attributes: { foo: "bar" },
});
expect(hello).equals("world");
});
});

Expand Down
14 changes: 8 additions & 6 deletions spec/v1/providers/remoteConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ describe("RemoteConfig Functions", () => {
delete process.env.GCLOUD_PROJECT;
});

it("should unwrap the version in the event", () => {
return Promise.all([
cloudFunctionUpdate(event.data, event.context).then((data: any) => {
expect(data).to.deep.equal(constructVersion());
}),
]);
it("should unwrap the version in the event", async () => {
let hello;
functions.onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await cloudFunctionUpdate(event.data, event.context).then((data: any) => {
expect(data).to.deep.equal(constructVersion());
});
expect(hello).to.equal("world");
});
});
});
Loading

0 comments on commit 3365a38

Please sign in to comment.