Skip to content

Commit

Permalink
Add new 2nd gen Firestore auth context triggers (#1519)
Browse files Browse the repository at this point in the history
* 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 <blidd@google.com>

* add oninit tests

---------

Co-authored-by: Thomas Bouldin <inlined@users.noreply.github.com>
  • Loading branch information
blidd-google and inlined committed Apr 4, 2024
1 parent 0f1e8f3 commit 53f7204
Show file tree
Hide file tree
Showing 4 changed files with 570 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -0,0 +1 @@
- Add new 2nd gen Firestore auth context triggers. (#1519)
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

294 changes: 294 additions & 0 deletions spec/v2/providers/firestore.spec.ts
Expand Up @@ -84,6 +84,15 @@ function makeEvent(data?: any): firestore.RawFirestoreEvent {
} as firestore.RawFirestoreEvent;
}

function makeAuthEvent(data?: any): firestore.RawFirestoreAuthEvent {
return {
...eventBase,
data,
authid: "userId",
authtype: "unknown",
} as firestore.RawFirestoreAuthEvent;
}

const createdData = {
value: {
fields: {
Expand Down Expand Up @@ -511,6 +520,262 @@ describe("firestore", () => {
});
});

describe("onDocumentWrittenWithAuthContext", () => {
it("should create a func", () => {
const expectedEp = makeExpectedEp(
firestore.writtenEventWithAuthContextType,
{
database: "(default)",
namespace: "(default)",
},
{
document: "foo/{bar}",
}
);

const func = firestore.onDocumentWrittenWithAuthContext("foo/{bar}", () => 2);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("should create a func with opts", () => {
const expectedEp = makeExpectedEp(
firestore.writtenEventWithAuthContextType,
{
database: "my-db",
namespace: "my-ns",
},
{
document: "foo/{bar}",
}
);
expectedEp["region"] = ["us-central1"];

const func = firestore.onDocumentWrittenWithAuthContext(
{
region: "us-central1",
document: "foo/{bar}",
database: "my-db",
namespace: "my-ns",
},
() => 2
);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("calls init function", async () => {
const event: firestore.RawFirestoreEvent = {
...eventBase,
datacontenttype: "application/json",
data: {
oldValue: null,
value: null,
},
};

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await firestore.onDocumentWrittenWithAuthContext("path", () => null)(event);
expect(hello).to.equal("world");
});
});

describe("onDocumentCreatedWithAuthContext", () => {
it("should create a func", () => {
const expectedEp = makeExpectedEp(
firestore.createdEventWithAuthContextType,
{
database: "(default)",
namespace: "(default)",
},
{
document: "foo/{bar}",
}
);

const func = firestore.onDocumentCreatedWithAuthContext("foo/{bar}", () => 2);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("should create a func with opts", () => {
const expectedEp = makeExpectedEp(
firestore.createdEventWithAuthContextType,
{
database: "my-db",
namespace: "my-ns",
},
{
document: "foo/{bar}",
}
);
expectedEp["region"] = ["us-central1"];

const func = firestore.onDocumentCreatedWithAuthContext(
{
region: "us-central1",
document: "foo/{bar}",
database: "my-db",
namespace: "my-ns",
},
() => 2
);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("calls init function", async () => {
const event: firestore.RawFirestoreEvent = {
...eventBase,
datacontenttype: "application/json",
data: {
oldValue: null,
value: null,
},
};

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await firestore.onDocumentCreatedWithAuthContext("path", () => null)(event);
expect(hello).to.equal("world");
});
});

describe("onDocumentUpdatedWithAuthContext", () => {
it("should create a func", () => {
const expectedEp = makeExpectedEp(
firestore.updatedEventWithAuthContextType,
{
database: "(default)",
namespace: "(default)",
},
{
document: "foo/{bar}",
}
);

const func = firestore.onDocumentUpdatedWithAuthContext("foo/{bar}", () => 2);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("should create a func with opts", () => {
const expectedEp = makeExpectedEp(
firestore.updatedEventWithAuthContextType,
{
database: "my-db",
namespace: "my-ns",
},
{
document: "foo/{bar}",
}
);
expectedEp["region"] = ["us-central1"];

const func = firestore.onDocumentUpdatedWithAuthContext(
{
region: "us-central1",
document: "foo/{bar}",
database: "my-db",
namespace: "my-ns",
},
() => 2
);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("calls init function", async () => {
const event: firestore.RawFirestoreEvent = {
...eventBase,
datacontenttype: "application/json",
data: {
oldValue: null,
value: null,
},
};

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await firestore.onDocumentUpdatedWithAuthContext("path", () => null)(event);
expect(hello).to.equal("world");
});
});

describe("onDocumentDeletedWithAuthContext", () => {
it("should create a func", () => {
const expectedEp = makeExpectedEp(
firestore.deletedEventWithAuthContextType,
{
database: "(default)",
namespace: "(default)",
},
{
document: "foo/{bar}",
}
);

const func = firestore.onDocumentDeletedWithAuthContext("foo/{bar}", () => 2);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("should create a func with opts", () => {
const expectedEp = makeExpectedEp(
firestore.deletedEventWithAuthContextType,
{
database: "my-db",
namespace: "my-ns",
},
{
document: "foo/{bar}",
}
);
expectedEp["region"] = ["us-central1"];

const func = firestore.onDocumentDeletedWithAuthContext(
{
region: "us-central1",
document: "foo/{bar}",
database: "my-db",
namespace: "my-ns",
},
() => 2
);

expect(func.run(true as any)).to.eq(2);
expect(func.__endpoint).to.deep.eq(expectedEp);
});

it("calls init function", async () => {
const event: firestore.RawFirestoreEvent = {
...eventBase,
datacontenttype: "application/json",
data: {
oldValue: null,
value: null,
},
};

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await firestore.onDocumentDeletedWithAuthContext("path", () => null)(event);
expect(hello).to.equal("world");
});
});

describe("getOpts", () => {
it("should handle document string", () => {
const { document, database, namespace, opts } = firestore.getOpts("foo/{bar}");
Expand Down Expand Up @@ -720,6 +985,26 @@ describe("firestore", () => {

expect(event.data.data()).to.deep.eq({ hello: "delete world" });
});

it("should make event from a created event with auth context", () => {
const event = firestore.makeFirestoreEvent(
firestore.createdEventWithAuthContextType,
makeAuthEvent(makeEncodedProtobuf(createdProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event.data.data()).to.deep.eq({ hello: "create world" });
});

it("should include auth fields if provided in raw event", () => {
const event = firestore.makeFirestoreEvent(
firestore.createdEventWithAuthContextType,
makeAuthEvent(makeEncodedProtobuf(createdProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event).to.include({ authId: "userId", authType: "unknown" });
});
});

describe("makeChangedFirestoreEvent", () => {
Expand Down Expand Up @@ -753,6 +1038,15 @@ describe("firestore", () => {
});
});

it("should include auth fields if provided in raw event", () => {
const event = firestore.makeChangedFirestoreEvent(
makeAuthEvent(makeEncodedProtobuf(writtenProto)),
firestore.makeParams("foo/fGRodw71mHutZ4wGDuT8", new PathPattern("foo/{bar}"))
);

expect(event).to.include({ authId: "userId", authType: "unknown" });
});

describe("makeEndpoint", () => {
it("should make an endpoint with a document path pattern", () => {
const expectedEp = makeExpectedEp(
Expand Down

0 comments on commit 53f7204

Please sign in to comment.