import { ActorAuthService } from "./actor.auth.service"; import { RoleEnum } from "src/Types&Enums/role.enum"; import { Types } from "mongoose"; const id = "aaaaaaaaaaaaaaaaaaaaaaaa"; const personalRoles = [ RoleEnum.EXPERT, RoleEnum.DAMAGE_EXPERT, RoleEnum.FIELD_EXPERT, RoleEnum.COMPANY, RoleEnum.FILE_MAKER, RoleEnum.FILE_REVIEWER, RoleEnum.CALL_CENTER, ]; function setup(role: RoleEnum, saveDocument = true) { const document: any = { _id: new Types.ObjectId(id), role, firstName: "نام چند بخشی", lastName: "نام خانوادگی چند بخشی", mobile: "09120000000", phone: "02122222222", state: "تهران", city: "تهران", address: "نشانی قبلی", email: "account@example.test", password: "private-password-hash", otp: "private-otp", tokens: { token: "private-token" }, ThirdPartyClaimExpertId: "123", clientKey: new Types.ObjectId("bbbbbbbbbbbbbbbbbbbbbbbb"), }; if (saveDocument) document.save = jest.fn().mockResolvedValue(document); const database = { findOne: jest.fn().mockResolvedValue(document), updateOne: jest .fn() .mockImplementation(async (_filter, update) => Object.assign(document, update.$set), ), }; const client = { findOne: jest .fn() .mockResolvedValue({ clientName: { persian: "بیمه آزمایشی" } }), }; const service = new ActorAuthService( {} as any, {} as any, database as any, database as any, database as any, database as any, database as any, client as any, {} as any, {} as any, database as any, database as any, database as any, database as any, ); const actor = { sub: id, role, username: "stale-username@example.test" }; return { service, document, database, actor }; } describe("self profile across actor roles", () => { it.each(personalRoles)( "saves supported personal fields for %s and returns only the public profile", async (role) => { const { service, document, actor } = setup(role); const result = await service.modifyProfile(actor, { firstName: " نام جدید چند بخشی ", mobile: "۰۹۱۲۳۴۵۶۷۸۹", state: "البرز", city: "کرج", address: "نشانی جدید", }); expect(document.save).toHaveBeenCalledTimes(1); expect(document.mobile).toBe("09123456789"); expect(document.lastName).toBe("نام خانوادگی چند بخشی"); expect(result).toMatchObject({ firstName: "نام جدید چند بخشی", lastName: "نام خانوادگی چند بخشی", fullName: "نام جدید چند بخشی نام خانوادگی چند بخشی", mobile: "09123456789", state: "البرز", city: "کرج", insuActivityCo: "بیمه آزمایشی", }); expect(result.editableFields).toContain("address"); for (const key of [ "password", "otp", "tokens", "clientKey", "ThirdPartyClaimExpertId", ]) expect(result).not.toHaveProperty(key); }, ); it("resolves insurers by their subject rather than a stale or non-email username", async () => { const { service, database, actor } = setup(RoleEnum.COMPANY); await service.getProfiles(actor); expect(database.findOne).toHaveBeenCalledWith({ _id: new Types.ObjectId(id), }); }); it("supports the super-admin's lean record without offering unsupported contact fields", async () => { const { service, database, actor } = setup(RoleEnum.SUPER_ADMIN, false); const result = await service.modifyProfile(actor, { firstName: "نام جدید", }); expect(database.updateOne).toHaveBeenCalledWith( { _id: new Types.ObjectId(id) }, { $set: { firstName: "نام جدید" } }, ); expect(result.editableFields).toEqual(["firstName", "lastName"]); await expect( service.modifyProfile(actor, { mobile: "09123456789" }), ).rejects.toThrow(); }); it.each([ { role: "super_admin" }, { clientKey: id }, { password: "replacement" }, { ThirdPartyClaimExpertId: "666" }, { $set: { firstName: "bad" } }, { city: { $gt: "" } }, { mobile: "invalid" }, { address: null }, ])("rejects invalid/privileged fields before saving: %j", async (body) => { const { service, document, actor } = setup(RoleEnum.FILE_MAKER); await expect(service.modifyProfile(actor, body)).rejects.toThrow(); expect(document.save).not.toHaveBeenCalled(); }); it("does not render undefined names or return data for a missing account", async () => { const { service, database, document, actor } = setup(RoleEnum.SUPER_ADMIN); delete document.firstName; delete document.lastName; expect((await service.getProfiles(actor)).fullName).toBe(document.email); database.findOne.mockResolvedValue(null); await expect(service.getProfiles(actor)).rejects.toThrow( "حساب کاربری یافت نشد", ); }); });