import { Types } from "mongoose"; import { partyPersonMatchesUser } from "./iran-mobile"; import { buildBlamePartyAccessOrConditions, mongoUserIdEqualityFilter, } from "./party-access-queries"; describe("party access queries", () => { const userId = new Types.ObjectId(); describe("mongoUserIdEqualityFilter", () => { it("includes both ObjectId and string forms", () => { const filter = mongoUserIdEqualityFilter( "parties.person.userId", String(userId), ); expect(filter).toEqual({ $or: [ { "parties.person.userId": String(userId) }, { "parties.person.userId": userId }, ], }); }); }); describe("buildBlamePartyAccessOrConditions", () => { it("builds userId and phone branches", () => { const or = buildBlamePartyAccessOrConditions({ sub: String(userId), username: "09123456789", }); expect(or).toHaveLength(2); expect(or[0]).toHaveProperty("$or"); expect(or[1]).toEqual({ "parties.person.phoneNumber": { $in: expect.arrayContaining(["09123456789", "9123456789"]), }, }); }); it("includes linked duplicate-account user ids", () => { const linked = new Types.ObjectId(); const or = buildBlamePartyAccessOrConditions( { sub: String(userId), username: "09123456789" }, [String(linked)], ); expect(or[0]).toEqual({ $or: expect.arrayContaining([ { "parties.person.userId": String(userId) }, { "parties.person.userId": userId }, { "parties.person.userId": String(linked) }, { "parties.person.userId": linked }, ]), }); }); }); describe("partyPersonMatchesUser", () => { it("matches party userId stored as ObjectId against string JWT sub", () => { expect( partyPersonMatchesUser( { userId, phoneNumber: "09111111111" }, { sub: String(userId), username: "09999999999" }, ), ).toBe(true); }); it("matches phone variants when userId is not set yet", () => { expect( partyPersonMatchesUser( { phoneNumber: "9123456789" }, { sub: "other", username: "09123456789" }, ), ).toBe(true); }); it("matches linked duplicate-account user ids", () => { const linked = new Types.ObjectId(); expect( partyPersonMatchesUser( { userId: linked, phoneNumber: "09111111111" }, { sub: String(userId), username: "09999999999" }, [String(linked)], ), ).toBe(true); }); }); });