Skip to content

Commit 9a2cd47

Browse files
authored
More of annotate endpoint property for functions (#1009)
Follows up #999 to annotate each funuctions with `__endpoint` property. Highlight of changes: * Extend unit test coverage for all v1 providers * Add `__endpoint` annotation to v1 task queue functions * Add `__requiredAPIs` annotation to task queue and scheduler functions * Explicitly set `__endpoint` to undefined in the handler namespace * No SDK-level label setting in the __endpoint annotation.
1 parent 5f2fa90 commit 9a2cd47

17 files changed

+711
-301
lines changed

spec/v1/cloud-functions.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('makeCloudFunction', () => {
6767
},
6868
retry: false,
6969
},
70+
labels: {},
7071
});
7172
});
7273

@@ -90,6 +91,7 @@ describe('makeCloudFunction', () => {
9091
},
9192
retry: false,
9293
},
94+
labels: {},
9395
});
9496
});
9597

@@ -121,6 +123,7 @@ describe('makeCloudFunction', () => {
121123
},
122124
retry: false,
123125
},
126+
labels: {},
124127
});
125128
});
126129

@@ -143,6 +146,7 @@ describe('makeCloudFunction', () => {
143146
},
144147
retry: true,
145148
},
149+
labels: {},
146150
});
147151
});
148152

@@ -165,6 +169,7 @@ describe('makeCloudFunction', () => {
165169
expect(cf.__endpoint).to.deep.equal({
166170
platform: 'gcfv1',
167171
scheduleTrigger: schedule,
172+
labels: {},
168173
});
169174
});
170175

spec/v1/providers/analytics.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ describe('Analytics Functions', () => {
5050
expect(fn.__trigger.regions).to.deep.equal(['us-east1']);
5151
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
5252
expect(fn.__trigger.timeout).to.deep.equal('90s');
53+
54+
expect(fn.__endpoint.region).to.deep.equal(['us-east1']);
55+
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
56+
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
5357
});
5458

5559
describe('#onLog', () => {
56-
it('should return a TriggerDefinition with appropriate values', () => {
60+
it('should return a trigger/endpoint with appropriate values', () => {
5761
const cloudFunction = analytics.event('first_open').onLog(() => null);
5862

5963
expect(cloudFunction.__trigger).to.deep.equal({
@@ -64,6 +68,19 @@ describe('Analytics Functions', () => {
6468
service: 'app-measurement.com',
6569
},
6670
});
71+
72+
expect(cloudFunction.__endpoint).to.deep.equal({
73+
platform: 'gcfv1',
74+
eventTrigger: {
75+
eventFilters: {
76+
resource: 'projects/project1/events/first_open',
77+
},
78+
eventType:
79+
'providers/google.firebase.analytics/eventTypes/event.log',
80+
retry: false,
81+
},
82+
labels: {},
83+
});
6784
});
6885
});
6986

@@ -305,11 +322,12 @@ describe('Analytics Functions', () => {
305322

306323
describe('handler namespace', () => {
307324
describe('#onLog', () => {
308-
it('should return an empty trigger', () => {
325+
it('should return an empty trigger/endpoint', () => {
309326
const cloudFunction = functions.handler.analytics.event.onLog(
310327
() => null
311328
);
312329
expect(cloudFunction.__trigger).to.deep.equal({});
330+
expect(cloudFunction.__endpoint).to.undefined;
313331
});
314332

315333
it('should handle an event with the appropriate fields', () => {

spec/v1/providers/auth.spec.ts

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,38 @@ describe('Auth Functions', () => {
5151
};
5252

5353
describe('AuthBuilder', () => {
54+
function expectedTrigger(project: string, eventType: string) {
55+
return {
56+
eventTrigger: {
57+
resource: `projects/${project}`,
58+
eventType: `providers/firebase.auth/eventTypes/${eventType}`,
59+
service: 'firebaseauth.googleapis.com',
60+
},
61+
};
62+
}
63+
64+
function expectedEndpoint(project: string, eventType: string) {
65+
return {
66+
platform: 'gcfv1',
67+
eventTrigger: {
68+
eventFilters: {
69+
resource: `projects/${project}`,
70+
},
71+
eventType: `providers/firebase.auth/eventTypes/${eventType}`,
72+
retry: false,
73+
},
74+
labels: {},
75+
};
76+
}
77+
5478
const handler = (user: firebase.auth.UserRecord) => {
5579
return Promise.resolve();
5680
};
5781

82+
const project = 'project1';
83+
5884
before(() => {
59-
process.env.GCLOUD_PROJECT = 'project1';
85+
process.env.GCLOUD_PROJECT = project;
6086
});
6187

6288
after(() => {
@@ -76,31 +102,37 @@ describe('Auth Functions', () => {
76102
expect(fn.__trigger.regions).to.deep.equal(['us-east1']);
77103
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
78104
expect(fn.__trigger.timeout).to.deep.equal('90s');
105+
106+
expect(fn.__endpoint.region).to.deep.equal(['us-east1']);
107+
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
108+
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
79109
});
80110

81111
describe('#onCreate', () => {
82-
it('should return a TriggerDefinition with appropriate values', () => {
112+
it('should return a trigger/endpoint with appropriate values', () => {
83113
const cloudFunction = auth.user().onCreate(() => null);
84-
expect(cloudFunction.__trigger).to.deep.equal({
85-
eventTrigger: {
86-
eventType: 'providers/firebase.auth/eventTypes/user.create',
87-
resource: 'projects/project1',
88-
service: 'firebaseauth.googleapis.com',
89-
},
90-
});
114+
115+
expect(cloudFunction.__trigger).to.deep.equal(
116+
expectedTrigger(project, 'user.create')
117+
);
118+
119+
expect(cloudFunction.__endpoint).to.deep.equal(
120+
expectedEndpoint(project, 'user.create')
121+
);
91122
});
92123
});
93124

94125
describe('#onDelete', () => {
95-
it('should return a TriggerDefinition with appropriate values', () => {
126+
it('should return a trigger/endpoint with appropriate values', () => {
96127
const cloudFunction = auth.user().onDelete(handler);
97-
expect(cloudFunction.__trigger).to.deep.equal({
98-
eventTrigger: {
99-
eventType: 'providers/firebase.auth/eventTypes/user.delete',
100-
resource: 'projects/project1',
101-
service: 'firebaseauth.googleapis.com',
102-
},
103-
});
128+
129+
expect(cloudFunction.__trigger).to.deep.equal(
130+
expectedTrigger(project, 'user.delete')
131+
);
132+
133+
expect(cloudFunction.__endpoint).to.deep.equal(
134+
expectedEndpoint(project, 'user.delete')
135+
);
104136
});
105137
});
106138

@@ -198,6 +230,11 @@ describe('Auth Functions', () => {
198230
const cloudFunction = functions.handler.auth.user.onCreate(() => null);
199231
expect(cloudFunction.__trigger).to.deep.equal({});
200232
});
233+
234+
it('should return an empty endpoint', () => {
235+
const cloudFunction = functions.handler.auth.user.onCreate(() => null);
236+
expect(cloudFunction.__endpoint).to.be.undefined;
237+
});
201238
});
202239

203240
describe('#onDelete', () => {
@@ -206,13 +243,15 @@ describe('Auth Functions', () => {
206243
);
207244

208245
it('should return an empty trigger', () => {
209-
const handler = (user: firebase.auth.UserRecord) => {
210-
return Promise.resolve();
211-
};
212-
const cloudFunction = functions.handler.auth.user.onDelete(handler);
246+
const cloudFunction = functions.handler.auth.user.onDelete(() => null);
213247
expect(cloudFunction.__trigger).to.deep.equal({});
214248
});
215249

250+
it('should return an empty endpoint', () => {
251+
const cloudFunction = functions.handler.auth.user.onDelete(() => null);
252+
expect(cloudFunction.__endpoint).to.be.undefined;
253+
});
254+
216255
it('should handle wire format as of v5.0.0 of firebase-admin', () => {
217256
return cloudFunctionDelete(event.data, event.context).then(
218257
(data: any) => {
@@ -237,6 +276,10 @@ describe('Auth Functions', () => {
237276
expect(() => auth.user().onCreate(() => null).__trigger).to.throw(Error);
238277
});
239278

279+
it('should throw when endpoint is accessed', () => {
280+
expect(() => auth.user().onCreate(() => null).__endpoint).to.throw(Error);
281+
});
282+
240283
it('should not throw when #run is called', () => {
241284
const cf = auth.user().onCreate(() => null);
242285
expect(cf.run).to.not.throw(Error);

0 commit comments

Comments
 (0)