|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import { update_user } from "./update_user"; |
| 4 | +import * as auth from "../../../gcp/auth"; |
| 5 | +import { McpContext } from "../../types"; |
| 6 | +import * as util from "../../util"; |
| 7 | + |
| 8 | +describe("update_user tool", () => { |
| 9 | + const projectId = "test-project"; |
| 10 | + let setCustomClaimsStub: sinon.SinonStub; |
| 11 | + let toggleuserEnablementStub: sinon.SinonStub; |
| 12 | + let mcpErrorStub: sinon.SinonStub; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + setCustomClaimsStub = sinon.stub(auth, "setCustomClaim"); |
| 16 | + toggleuserEnablementStub = sinon.stub(auth, "toggleUserEnablement"); |
| 17 | + mcpErrorStub = sinon.stub(util, "mcpError"); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + sinon.restore(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should disable a user", async () => { |
| 25 | + toggleuserEnablementStub.resolves(true); |
| 26 | + |
| 27 | + const result = await update_user.fn({ uid: "123", disabled: true }, { |
| 28 | + projectId, |
| 29 | + } as McpContext); |
| 30 | + |
| 31 | + expect(result).to.deep.equal({ |
| 32 | + content: [ |
| 33 | + { |
| 34 | + text: "Successfully updated user 123. User disabled.", |
| 35 | + type: "text", |
| 36 | + }, |
| 37 | + ], |
| 38 | + }); |
| 39 | + expect(toggleuserEnablementStub).to.have.been.calledWith(projectId, "123", true); |
| 40 | + expect(setCustomClaimsStub).to.not.have.been.called; |
| 41 | + }); |
| 42 | + |
| 43 | + it("should enable a user", async () => { |
| 44 | + toggleuserEnablementStub.resolves(true); |
| 45 | + |
| 46 | + const result = await update_user.fn({ uid: "123", disabled: false }, { |
| 47 | + projectId, |
| 48 | + } as McpContext); |
| 49 | + |
| 50 | + expect(result).to.deep.equal({ |
| 51 | + content: [ |
| 52 | + { |
| 53 | + text: "Successfully updated user 123. User enabled.", |
| 54 | + type: "text", |
| 55 | + }, |
| 56 | + ], |
| 57 | + }); |
| 58 | + expect(toggleuserEnablementStub).to.have.been.calledWith(projectId, "123", false); |
| 59 | + expect(setCustomClaimsStub).to.not.have.been.called; |
| 60 | + }); |
| 61 | + |
| 62 | + it("should set a custom claim", async () => { |
| 63 | + setCustomClaimsStub.resolves({ uid: "123", customClaims: { admin: true } }); |
| 64 | + |
| 65 | + const result = await update_user.fn( |
| 66 | + { |
| 67 | + uid: "123", |
| 68 | + claim: { key: "admin", value: true }, |
| 69 | + }, |
| 70 | + { |
| 71 | + projectId, |
| 72 | + } as McpContext, |
| 73 | + ); |
| 74 | + |
| 75 | + expect(result).to.deep.equal({ |
| 76 | + content: [ |
| 77 | + { |
| 78 | + text: "Successfully updated user 123. Claim 'admin' set.", |
| 79 | + type: "text", |
| 80 | + }, |
| 81 | + ], |
| 82 | + }); |
| 83 | + expect(setCustomClaimsStub).to.have.been.calledWith(projectId, "123", { admin: true }); |
| 84 | + expect(toggleuserEnablementStub).to.not.have.been.called; |
| 85 | + }); |
| 86 | + |
| 87 | + it("should fail to set a custom claim and disable a user", async () => { |
| 88 | + setCustomClaimsStub.resolves({ uid: "123", customClaims: { admin: true } }); |
| 89 | + toggleuserEnablementStub.resolves(true); |
| 90 | + |
| 91 | + await update_user.fn( |
| 92 | + { |
| 93 | + uid: "123", |
| 94 | + claim: { key: "admin", value: true }, |
| 95 | + disabled: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + projectId, |
| 99 | + } as McpContext, |
| 100 | + ); |
| 101 | + |
| 102 | + expect(mcpErrorStub).to.be.calledWith( |
| 103 | + "Can only enable/disable a user or set a claim, not both.", |
| 104 | + ); |
| 105 | + }); |
| 106 | +}); |
0 commit comments