Skip to content

Commit 2e7c3db

Browse files
authored
feat: auth context support (#227)
* feat: add .idea to .gitignore * feat: Initial implementation * chore: run prettier * feat: types from new interfaces * feat: update firebase-functions to latest version (4.9.0) * feat: fix types from latest functions SDK
1 parent 5410568 commit 2e7c3db

9 files changed

+151
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ lib
55
.vscode
66
*.tgz
77
.tmp
8-
*.log
8+
*.log
9+
.idea

package-lock.json

Lines changed: 8 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"@types/mocha": "^5.2.7",
4848
"chai": "^4.2.0",
4949
"firebase-admin": "^12.0.0",
50-
"firebase-functions": "^4.6.0",
50+
"firebase-functions": "^4.9.0",
5151
"firebase-tools": "^8.9.2",
5252
"mocha": "^6.2.2",
5353
"prettier": "^1.19.1",
@@ -57,7 +57,7 @@
5757
},
5858
"peerDependencies": {
5959
"firebase-admin": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0 || ^12.0.0",
60-
"firebase-functions": ">=4.3.0",
60+
"firebase-functions": ">=4.9.0",
6161
"jest": ">=28.0.0"
6262
},
6363
"engines": {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { MockCloudEventAbstractFactory } from '../../types';
2+
import { CloudEvent, CloudFunction, firestore } from 'firebase-functions/v2';
3+
import { getEventType } from '../helpers';
4+
import { QueryDocumentSnapshot } from 'firebase-admin/firestore';
5+
import { getDocumentSnapshotCloudEventWithAuthContext } from './helpers';
6+
7+
export const firestoreOnDocumentCreatedWithAuthContext: MockCloudEventAbstractFactory<firestore.FirestoreAuthEvent<
8+
QueryDocumentSnapshot
9+
>> = {
10+
generateMock: getDocumentSnapshotCloudEventWithAuthContext,
11+
match(cloudFunction: CloudFunction<CloudEvent<unknown>>): boolean {
12+
return (
13+
getEventType(cloudFunction) ===
14+
'google.cloud.firestore.document.v1.created.withAuthContext'
15+
);
16+
},
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { MockCloudEventAbstractFactory } from '../../types';
2+
import { CloudEvent, CloudFunction, firestore } from 'firebase-functions/v2';
3+
import { getEventType } from '../helpers';
4+
import { QueryDocumentSnapshot } from 'firebase-admin/firestore';
5+
import { getDocumentSnapshotCloudEventWithAuthContext } from './helpers';
6+
7+
export const firestoreOnDocumentDeletedWithAuthContext: MockCloudEventAbstractFactory<firestore.FirestoreAuthEvent<
8+
QueryDocumentSnapshot
9+
>> = {
10+
generateMock: getDocumentSnapshotCloudEventWithAuthContext,
11+
match(cloudFunction: CloudFunction<CloudEvent<unknown>>): boolean {
12+
return (
13+
getEventType(cloudFunction) ===
14+
'google.cloud.firestore.document.v1.deleted.withAuthContext'
15+
);
16+
},
17+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { MockCloudEventAbstractFactory } from '../../types';
2+
import {
3+
Change,
4+
CloudEvent,
5+
CloudFunction,
6+
firestore,
7+
} from 'firebase-functions/v2';
8+
import { getEventType } from '../helpers';
9+
import { QueryDocumentSnapshot } from 'firebase-admin/firestore';
10+
import { getDocumentSnapshotChangeCloudEventWithAuthContext } from './helpers';
11+
12+
export const firestoreOnDocumentUpdatedWithAuthContext: MockCloudEventAbstractFactory<firestore.FirestoreAuthEvent<
13+
Change<QueryDocumentSnapshot>
14+
>> = {
15+
generateMock: getDocumentSnapshotChangeCloudEventWithAuthContext,
16+
match(cloudFunction: CloudFunction<CloudEvent<unknown>>): boolean {
17+
return (
18+
getEventType(cloudFunction) ===
19+
'google.cloud.firestore.document.v1.updated.withAuthContext'
20+
);
21+
},
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { MockCloudEventAbstractFactory } from '../../types';
2+
import {
3+
Change,
4+
CloudEvent,
5+
CloudFunction,
6+
firestore,
7+
} from 'firebase-functions/v2';
8+
import { getEventType } from '../helpers';
9+
import { DocumentSnapshot } from 'firebase-admin/firestore';
10+
import { getDocumentSnapshotChangeCloudEventWithAuthContext } from './helpers';
11+
12+
export const firestoreOnDocumentWrittenWithAuthContext: MockCloudEventAbstractFactory<firestore.FirestoreAuthEvent<
13+
Change<DocumentSnapshot>
14+
>> = {
15+
generateMock: getDocumentSnapshotChangeCloudEventWithAuthContext,
16+
match(cloudFunction: CloudFunction<CloudEvent<unknown>>): boolean {
17+
return (
18+
getEventType(cloudFunction) ===
19+
'google.cloud.firestore.document.v1.written.withAuthContext'
20+
);
21+
},
22+
};

src/cloudevent/mocks/firestore/helpers.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ export function getDocumentSnapshotCloudEvent(
4747
};
4848
}
4949

50+
export function getDocumentSnapshotCloudEventWithAuthContext(
51+
cloudFunction: CloudFunction<firestore.FirestoreAuthEvent<DocumentSnapshot>>,
52+
cloudEventPartial?: DeepPartial<
53+
firestore.FirestoreAuthEvent<DocumentSnapshot | object>
54+
>
55+
) {
56+
const eventWithoutAuthContext = getDocumentSnapshotCloudEvent(
57+
cloudFunction,
58+
cloudEventPartial
59+
);
60+
const authContext: { authId?: string; authType: firestore.AuthType } = {
61+
authType: 'unknown',
62+
};
63+
if (cloudEventPartial?.authId) {
64+
authContext.authId = cloudEventPartial.authId;
65+
}
66+
if (cloudEventPartial?.authType) {
67+
authContext.authType = cloudEventPartial.authType;
68+
}
69+
return {
70+
...eventWithoutAuthContext,
71+
...authContext,
72+
};
73+
}
74+
5075
/** Creates a mock CloudEvent that contains a Change<DocumentSnapshot> as its data. */
5176
export function getDocumentSnapshotChangeCloudEvent(
5277
cloudFunction: CloudFunction<
@@ -82,6 +107,34 @@ export function getDocumentSnapshotChangeCloudEvent(
82107
};
83108
}
84109

110+
export function getDocumentSnapshotChangeCloudEventWithAuthContext(
111+
cloudFunction: CloudFunction<
112+
firestore.FirestoreAuthEvent<Change<DocumentSnapshot>>
113+
>,
114+
cloudEventPartial?: DeepPartial<
115+
firestore.FirestoreAuthEvent<Change<DocumentSnapshot> | ChangeLike>
116+
>
117+
) {
118+
const eventWithoutAuthContext = getDocumentSnapshotChangeCloudEvent(
119+
cloudFunction,
120+
cloudEventPartial
121+
);
122+
const authContext: { authId?: string; authType: firestore.AuthType } = {
123+
authType: 'unknown',
124+
};
125+
if (cloudEventPartial?.authId) {
126+
authContext.authId = cloudEventPartial.authId;
127+
}
128+
if (cloudEventPartial?.authType) {
129+
authContext.authType = cloudEventPartial.authType;
130+
}
131+
132+
return {
133+
...eventWithoutAuthContext,
134+
...authContext,
135+
};
136+
}
137+
85138
/** Finds or provides reasonable defaults for mock FirestoreEvent data. */
86139
function getFirestoreEventFields(
87140
cloudFunction: CloudFunction<

src/cloudevent/mocks/partials.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import {
2727
import { storageV1 } from './storage';
2828
import { remoteConfigOnConfigUpdated } from './remoteconfig/remote-config-on-config-updated';
2929
import { testLabOnTestMatrixCompleted } from './testlab/test-lab-on-test-matrix-completed';
30+
import { firestoreOnDocumentCreatedWithAuthContext } from './firestore/firestore-on-document-created-with-auth-context';
31+
import { firestoreOnDocumentDeletedWithAuthContext } from './firestore/firestore-on-document-deleted-with-auth-context';
32+
import { firestoreOnDocumentUpdatedWithAuthContext } from './firestore/firestore-on-document-updated-with-auth-context';
33+
import { firestoreOnDocumentWrittenWithAuthContext } from './firestore/firestore-on-document-written-with-auth-context';
3034

3135
/**
3236
* Note: Ordering matters. Some MockEventPartials will match more generally
@@ -59,6 +63,10 @@ export const LIST_OF_MOCK_CLOUD_EVENT_PARTIALS: Array<MockCloudEventAbstractFact
5963
firestoreOnDocumentDeleted,
6064
firestoreOnDocumentUpdated,
6165
firestoreOnDocumentWritten,
66+
firestoreOnDocumentCreatedWithAuthContext,
67+
firestoreOnDocumentDeletedWithAuthContext,
68+
firestoreOnDocumentUpdatedWithAuthContext,
69+
firestoreOnDocumentWrittenWithAuthContext,
6270

6371
// CustomEventPublished must be called last
6472
eventarcOnCustomEventPublished,

0 commit comments

Comments
 (0)