Skip to content

Add new 2nd gen Firestore auth context triggers #1519

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 12 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading