Skip to content

Commit 53f7204

Browse files
Add new 2nd gen Firestore auth context triggers (#1519)
* add changelog * update docstrings * export authtype and add comment * separate out event type with auth context * Ugly type safety (#1548) * consolidate make firestore event fns and simplify typings --------- Co-authored-by: Brian Li <[email protected]> * add oninit tests --------- Co-authored-by: Thomas Bouldin <[email protected]>
1 parent 0f1e8f3 commit 53f7204

File tree

4 files changed

+570
-28
lines changed

4 files changed

+570
-28
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add new 2nd gen Firestore auth context triggers. (#1519)

package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/v2/providers/firestore.spec.ts

+294
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ function makeEvent(data?: any): firestore.RawFirestoreEvent {
8484
} as firestore.RawFirestoreEvent;
8585
}
8686

87+
function makeAuthEvent(data?: any): firestore.RawFirestoreAuthEvent {
88+
return {
89+
...eventBase,
90+
data,
91+
authid: "userId",
92+
authtype: "unknown",
93+
} as firestore.RawFirestoreAuthEvent;
94+
}
95+
8796
const createdData = {
8897
value: {
8998
fields: {
@@ -511,6 +520,262 @@ describe("firestore", () => {
511520
});
512521
});
513522

523+
describe("onDocumentWrittenWithAuthContext", () => {
524+
it("should create a func", () => {
525+
const expectedEp = makeExpectedEp(
526+
firestore.writtenEventWithAuthContextType,
527+
{
528+
database: "(default)",
529+
namespace: "(default)",
530+
},
531+
{
532+
document: "foo/{bar}",
533+
}
534+
);
535+
536+
const func = firestore.onDocumentWrittenWithAuthContext("foo/{bar}", () => 2);
537+
538+
expect(func.run(true as any)).to.eq(2);
539+
expect(func.__endpoint).to.deep.eq(expectedEp);
540+
});
541+
542+
it("should create a func with opts", () => {
543+
const expectedEp = makeExpectedEp(
544+
firestore.writtenEventWithAuthContextType,
545+
{
546+
database: "my-db",
547+
namespace: "my-ns",
548+
},
549+
{
550+
document: "foo/{bar}",
551+
}
552+
);
553+
expectedEp["region"] = ["us-central1"];
554+
555+
const func = firestore.onDocumentWrittenWithAuthContext(
556+
{
557+
region: "us-central1",
558+
document: "foo/{bar}",
559+
database: "my-db",
560+
namespace: "my-ns",
561+
},
562+
() => 2
563+
);
564+
565+
expect(func.run(true as any)).to.eq(2);
566+
expect(func.__endpoint).to.deep.eq(expectedEp);
567+
});
568+
569+
it("calls init function", async () => {
570+
const event: firestore.RawFirestoreEvent = {
571+
...eventBase,
572+
datacontenttype: "application/json",
573+
data: {
574+
oldValue: null,
575+
value: null,
576+
},
577+
};
578+
579+
let hello;
580+
onInit(() => (hello = "world"));
581+
expect(hello).to.be.undefined;
582+
await firestore.onDocumentWrittenWithAuthContext("path", () => null)(event);
583+
expect(hello).to.equal("world");
584+
});
585+
});
586+
587+
describe("onDocumentCreatedWithAuthContext", () => {
588+
it("should create a func", () => {
589+
const expectedEp = makeExpectedEp(
590+
firestore.createdEventWithAuthContextType,
591+
{
592+
database: "(default)",
593+
namespace: "(default)",
594+
},
595+
{
596+
document: "foo/{bar}",
597+
}
598+
);
599+
600+
const func = firestore.onDocumentCreatedWithAuthContext("foo/{bar}", () => 2);
601+
602+
expect(func.run(true as any)).to.eq(2);
603+
expect(func.__endpoint).to.deep.eq(expectedEp);
604+
});
605+
606+
it("should create a func with opts", () => {
607+
const expectedEp = makeExpectedEp(
608+
firestore.createdEventWithAuthContextType,
609+
{
610+
database: "my-db",
611+
namespace: "my-ns",
612+
},
613+
{
614+
document: "foo/{bar}",
615+
}
616+
);
617+
expectedEp["region"] = ["us-central1"];
618+
619+
const func = firestore.onDocumentCreatedWithAuthContext(
620+
{
621+
region: "us-central1",
622+
document: "foo/{bar}",
623+
database: "my-db",
624+
namespace: "my-ns",
625+
},
626+
() => 2
627+
);
628+
629+
expect(func.run(true as any)).to.eq(2);
630+
expect(func.__endpoint).to.deep.eq(expectedEp);
631+
});
632+
633+
it("calls init function", async () => {
634+
const event: firestore.RawFirestoreEvent = {
635+
...eventBase,
636+
datacontenttype: "application/json",
637+
data: {
638+
oldValue: null,
639+
value: null,
640+
},
641+
};
642+
643+
let hello;
644+
onInit(() => (hello = "world"));
645+
expect(hello).to.be.undefined;
646+
await firestore.onDocumentCreatedWithAuthContext("path", () => null)(event);
647+
expect(hello).to.equal("world");
648+
});
649+
});
650+
651+
describe("onDocumentUpdatedWithAuthContext", () => {
652+
it("should create a func", () => {
653+
const expectedEp = makeExpectedEp(
654+
firestore.updatedEventWithAuthContextType,
655+
{
656+
database: "(default)",
657+
namespace: "(default)",
658+
},
659+
{
660+
document: "foo/{bar}",
661+
}
662+
);
663+
664+
const func = firestore.onDocumentUpdatedWithAuthContext("foo/{bar}", () => 2);
665+
666+
expect(func.run(true as any)).to.eq(2);
667+
expect(func.__endpoint).to.deep.eq(expectedEp);
668+
});
669+
670+
it("should create a func with opts", () => {
671+
const expectedEp = makeExpectedEp(
672+
firestore.updatedEventWithAuthContextType,
673+
{
674+
database: "my-db",
675+
namespace: "my-ns",
676+
},
677+
{
678+
document: "foo/{bar}",
679+
}
680+
);
681+
expectedEp["region"] = ["us-central1"];
682+
683+
const func = firestore.onDocumentUpdatedWithAuthContext(
684+
{
685+
region: "us-central1",
686+
document: "foo/{bar}",
687+
database: "my-db",
688+
namespace: "my-ns",
689+
},
690+
() => 2
691+
);
692+
693+
expect(func.run(true as any)).to.eq(2);
694+
expect(func.__endpoint).to.deep.eq(expectedEp);
695+
});
696+
697+
it("calls init function", async () => {
698+
const event: firestore.RawFirestoreEvent = {
699+
...eventBase,
700+
datacontenttype: "application/json",
701+
data: {
702+
oldValue: null,
703+
value: null,
704+
},
705+
};
706+
707+
let hello;
708+
onInit(() => (hello = "world"));
709+
expect(hello).to.be.undefined;
710+
await firestore.onDocumentUpdatedWithAuthContext("path", () => null)(event);
711+
expect(hello).to.equal("world");
712+
});
713+
});
714+
715+
describe("onDocumentDeletedWithAuthContext", () => {
716+
it("should create a func", () => {
717+
const expectedEp = makeExpectedEp(
718+
firestore.deletedEventWithAuthContextType,
719+
{
720+
database: "(default)",
721+
namespace: "(default)",
722+
},
723+
{
724+
document: "foo/{bar}",
725+
}
726+
);
727+
728+
const func = firestore.onDocumentDeletedWithAuthContext("foo/{bar}", () => 2);
729+
730+
expect(func.run(true as any)).to.eq(2);
731+
expect(func.__endpoint).to.deep.eq(expectedEp);
732+
});
733+
734+
it("should create a func with opts", () => {
735+
const expectedEp = makeExpectedEp(
736+
firestore.deletedEventWithAuthContextType,
737+
{
738+
database: "my-db",
739+
namespace: "my-ns",
740+
},
741+
{
742+
document: "foo/{bar}",
743+
}
744+
);
745+
expectedEp["region"] = ["us-central1"];
746+
747+
const func = firestore.onDocumentDeletedWithAuthContext(
748+
{
749+
region: "us-central1",
750+
document: "foo/{bar}",
751+
database: "my-db",
752+
namespace: "my-ns",
753+
},
754+
() => 2
755+
);
756+
757+
expect(func.run(true as any)).to.eq(2);
758+
expect(func.__endpoint).to.deep.eq(expectedEp);
759+
});
760+
761+
it("calls init function", async () => {
762+
const event: firestore.RawFirestoreEvent = {
763+
...eventBase,
764+
datacontenttype: "application/json",
765+
data: {
766+
oldValue: null,
767+
value: null,
768+
},
769+
};
770+
771+
let hello;
772+
onInit(() => (hello = "world"));
773+
expect(hello).to.be.undefined;
774+
await firestore.onDocumentDeletedWithAuthContext("path", () => null)(event);
775+
expect(hello).to.equal("world");
776+
});
777+
});
778+
514779
describe("getOpts", () => {
515780
it("should handle document string", () => {
516781
const { document, database, namespace, opts } = firestore.getOpts("foo/{bar}");
@@ -720,6 +985,26 @@ describe("firestore", () => {
720985

721986
expect(event.data.data()).to.deep.eq({ hello: "delete world" });
722987
});
988+
989+
it("should make event from a created event with auth context", () => {
990+
const event = firestore.makeFirestoreEvent(
991+
firestore.createdEventWithAuthContextType,
992+
makeAuthEvent(makeEncodedProtobuf(createdProto)),
993+
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
994+
);
995+
996+
expect(event.data.data()).to.deep.eq({ hello: "create world" });
997+
});
998+
999+
it("should include auth fields if provided in raw event", () => {
1000+
const event = firestore.makeFirestoreEvent(
1001+
firestore.createdEventWithAuthContextType,
1002+
makeAuthEvent(makeEncodedProtobuf(createdProto)),
1003+
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
1004+
);
1005+
1006+
expect(event).to.include({ authId: "userId", authType: "unknown" });
1007+
});
7231008
});
7241009

7251010
describe("makeChangedFirestoreEvent", () => {
@@ -753,6 +1038,15 @@ describe("firestore", () => {
7531038
});
7541039
});
7551040

1041+
it("should include auth fields if provided in raw event", () => {
1042+
const event = firestore.makeChangedFirestoreEvent(
1043+
makeAuthEvent(makeEncodedProtobuf(writtenProto)),
1044+
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
1045+
);
1046+
1047+
expect(event).to.include({ authId: "userId", authType: "unknown" });
1048+
});
1049+
7561050
describe("makeEndpoint", () => {
7571051
it("should make an endpoint with a document path pattern", () => {
7581052
const expectedEp = makeExpectedEp(

0 commit comments

Comments
 (0)