Skip to content

More of annotate endpoint property for functions #1009

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

Merged
merged 9 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions spec/v1/providers/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ describe('Analytics Functions', () => {
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);
});

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

expect(cloudFunction.__trigger).to.deep.equal({
Expand All @@ -64,6 +68,18 @@ describe('Analytics Functions', () => {
service: 'app-measurement.com',
},
});

expect(cloudFunction.__endpoint).to.deep.equal({
platform: 'gcfv1',
eventTrigger: {
eventFilters: {
resource: 'projects/project1/events/first_open',
},
eventType:
'providers/google.firebase.analytics/eventTypes/event.log',
retry: false,
},
});
});
});

Expand Down Expand Up @@ -305,11 +321,12 @@ describe('Analytics Functions', () => {

describe('handler namespace', () => {
describe('#onLog', () => {
it('should return an empty trigger', () => {
it('should return an empty trigger/endpoint', () => {
const cloudFunction = functions.handler.analytics.event.onLog(
() => null
);
expect(cloudFunction.__trigger).to.deep.equal({});
expect(cloudFunction.__endpoint).to.deep.equal({});
});

it('should handle an event with the appropriate fields', () => {
Expand Down
77 changes: 59 additions & 18 deletions spec/v1/providers/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,37 @@ 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 {
platform: 'gcfv1',
eventTrigger: {
eventFilters: {
resource: `projects/${project}`,
},
eventType: `providers/firebase.auth/eventTypes/${eventType}`,
retry: false,
},
};
}

const handler = (user: firebase.auth.UserRecord) => {
return Promise.resolve();
};

const project = 'project1';

before(() => {
process.env.GCLOUD_PROJECT = 'project1';
process.env.GCLOUD_PROJECT = project;
});

after(() => {
Expand All @@ -76,31 +101,37 @@ describe('Auth Functions', () => {
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);
});

describe('#onCreate', () => {
it('should return a TriggerDefinition with appropriate values', () => {
it('should return a trigger/endpoint with appropriate values', () => {
const cloudFunction = auth.user().onCreate(() => null);
expect(cloudFunction.__trigger).to.deep.equal({
eventTrigger: {
eventType: 'providers/firebase.auth/eventTypes/user.create',
resource: 'projects/project1',
service: 'firebaseauth.googleapis.com',
},
});

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

expect(cloudFunction.__endpoint).to.deep.equal(
expectedEndpoint(project, 'user.create')
);
});
});

describe('#onDelete', () => {
it('should return a TriggerDefinition with appropriate values', () => {
it('should return a trigger/endpoint with appropriate values', () => {
const cloudFunction = auth.user().onDelete(handler);
expect(cloudFunction.__trigger).to.deep.equal({
eventTrigger: {
eventType: 'providers/firebase.auth/eventTypes/user.delete',
resource: 'projects/project1',
service: 'firebaseauth.googleapis.com',
},
});

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

expect(cloudFunction.__endpoint).to.deep.equal(
expectedEndpoint(project, 'user.delete')
);
});
});

Expand Down Expand Up @@ -198,19 +229,25 @@ describe('Auth Functions', () => {
const cloudFunction = functions.handler.auth.user.onCreate(() => null);
expect(cloudFunction.__trigger).to.deep.equal({});
});

it('should return an empty endpoint', () => {
const cloudFunction = functions.handler.auth.user.onCreate(() => null);
expect(cloudFunction.__endpoint).to.deep.equal({});
});
});

describe('#onDelete', () => {
const cloudFunctionDelete: CloudFunction<firebase.auth.UserRecord> = functions.handler.auth.user.onDelete(
(data: firebase.auth.UserRecord) => data
);

it('should return an empty trigger', () => {
it('should return an empty trigger/endpoint', () => {
const handler = (user: firebase.auth.UserRecord) => {
return Promise.resolve();
};
const cloudFunction = functions.handler.auth.user.onDelete(handler);
expect(cloudFunction.__trigger).to.deep.equal({});
expect(cloudFunction.__endpoint).to.deep.equal({});
});

it('should handle wire format as of v5.0.0 of firebase-admin', () => {
Expand All @@ -237,6 +274,10 @@ describe('Auth Functions', () => {
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);
});

it('should not throw when #run is called', () => {
const cf = auth.user().onCreate(() => null);
expect(cf.run).to.not.throw(Error);
Expand Down
Loading