|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import { get_users } from "./get_users"; |
| 4 | +import * as auth from "../../../gcp/auth"; |
| 5 | +import { toContent } from "../../util"; |
| 6 | +import { McpContext } from "../../types"; |
| 7 | + |
| 8 | +describe("get_users tool", () => { |
| 9 | + const projectId = "test-project"; |
| 10 | + const users = [ |
| 11 | + { uid: "uid1", email: "[email protected]", passwordHash: "hash", salt: "salt" }, |
| 12 | + { uid: "uid2", email: "[email protected]", passwordHash: "hash", salt: "salt" }, |
| 13 | + ]; |
| 14 | + const prunedUsers = [ |
| 15 | + { uid: "uid1", email: "[email protected]" }, |
| 16 | + { uid: "uid2", email: "[email protected]" }, |
| 17 | + ]; |
| 18 | + |
| 19 | + let findUserStub: sinon.SinonStub; |
| 20 | + let listUsersStub: sinon.SinonStub; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + findUserStub = sinon.stub(auth, "findUser"); |
| 24 | + listUsersStub = sinon.stub(auth, "listUsers"); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + sinon.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + context("when no identifiers are provided", () => { |
| 32 | + it("should list all users", async () => { |
| 33 | + listUsersStub.resolves(users); |
| 34 | + const result = await get_users.fn({}, { projectId } as McpContext); |
| 35 | + expect(listUsersStub).to.be.calledWith(projectId, 100); |
| 36 | + expect(result).to.deep.equal(toContent(prunedUsers)); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + context("when uids are provided", () => { |
| 41 | + it("should get users by uid", async () => { |
| 42 | + findUserStub.onFirstCall().resolves(users[0]); |
| 43 | + findUserStub.onSecondCall().resolves(users[1]); |
| 44 | + const result = await get_users.fn({ uids: ["uid1", "uid2"] }, { projectId } as McpContext); |
| 45 | + expect(findUserStub).to.be.calledWith(projectId, undefined, undefined, "uid1"); |
| 46 | + expect(findUserStub).to.be.calledWith(projectId, undefined, undefined, "uid2"); |
| 47 | + expect(result).to.deep.equal(toContent(prunedUsers)); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should handle not found users", async () => { |
| 51 | + findUserStub.onFirstCall().resolves(users[0]); |
| 52 | + findUserStub.onSecondCall().rejects(new Error("User not found")); |
| 53 | + const result = await get_users.fn({ uids: ["uid1", "uid2"] }, { projectId } as McpContext); |
| 54 | + expect(findUserStub).to.be.calledWith(projectId, undefined, undefined, "uid1"); |
| 55 | + expect(findUserStub).to.be.calledWith(projectId, undefined, undefined, "uid2"); |
| 56 | + expect(result).to.deep.equal(toContent([prunedUsers[0]])); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + context("when emails are provided", () => { |
| 61 | + it("should get users by email", async () => { |
| 62 | + findUserStub.onFirstCall().resolves(users[0]); |
| 63 | + findUserStub.onSecondCall().resolves(users[1]); |
| 64 | + const result = await get_users.fn({ emails: ["[email protected]", "[email protected]"] }, { |
| 65 | + projectId, |
| 66 | + } as McpContext); |
| 67 | + expect(findUserStub).to.be.calledWith(projectId, "[email protected]", undefined, undefined); |
| 68 | + expect(findUserStub).to.be.calledWith(projectId, "[email protected]", undefined, undefined); |
| 69 | + expect(result).to.deep.equal(toContent(prunedUsers)); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + context("when phone_numbers are provided", () => { |
| 74 | + it("should get users by phone number", async () => { |
| 75 | + findUserStub.onFirstCall().resolves(users[0]); |
| 76 | + findUserStub.onSecondCall().resolves(users[1]); |
| 77 | + const result = await get_users.fn({ phone_numbers: ["+11111111111", "+22222222222"] }, { |
| 78 | + projectId, |
| 79 | + } as McpContext); |
| 80 | + expect(findUserStub).to.be.calledWith(projectId, undefined, "+11111111111", undefined); |
| 81 | + expect(findUserStub).to.be.calledWith(projectId, undefined, "+22222222222", undefined); |
| 82 | + expect(result).to.deep.equal(toContent(prunedUsers)); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments