forked from Yara724/api
145 lines
4.5 KiB
TypeScript
145 lines
4.5 KiB
TypeScript
import { ClaimRequestManagementService } from "src/claim-request-management/claim-request-management.service";
|
|
import { fanavaranSignatureRevision } from "./fanavaran-expert-signature";
|
|
|
|
describe("Fanavaran expert signature gate", () => {
|
|
const actor = {
|
|
sub: "expert-1",
|
|
role: "damage_expert",
|
|
fullName: "کارشناس نمونه",
|
|
};
|
|
const image =
|
|
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+j3ZkAAAAASUVORK5CYII=";
|
|
let service: any;
|
|
let claim: any;
|
|
let old: string | undefined;
|
|
|
|
beforeEach(() => {
|
|
old = process.env.FANAVARAN_REQUIRE_EXPERT_SIGNATURE;
|
|
process.env.FANAVARAN_REQUIRE_EXPERT_SIGNATURE = "true";
|
|
claim = {
|
|
_id: "claim-1",
|
|
vehicle: { carName: "پژو" },
|
|
updatedAt: new Date(),
|
|
evaluation: {},
|
|
};
|
|
service = Object.create(ClaimRequestManagementService.prototype);
|
|
service.claimCaseDbService = {
|
|
findById: jest.fn(async () => claim),
|
|
findWithFanavaranSignature: jest.fn(async () => claim),
|
|
findOneAndUpdate: jest.fn(async (_query, update) => {
|
|
Object.assign(claim, update.$set);
|
|
return claim;
|
|
}),
|
|
};
|
|
service.completeFanavaranExpertisePrerequisites = jest.fn();
|
|
service.resolveFanavaranClaimProduct = jest.fn(async () => "third-party");
|
|
service.buildFanavaranExpertisePayload = jest.fn(async () => ({
|
|
payload: {},
|
|
warnings: [],
|
|
infoWarnings: [],
|
|
}));
|
|
service.assertFanavaranExpertisePayloadReady = jest.fn();
|
|
service.executeFanavaranExpertiseSubmit = jest.fn(async () => ({
|
|
Id: 123,
|
|
}));
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (old === undefined)
|
|
delete process.env.FANAVARAN_REQUIRE_EXPERT_SIGNATURE;
|
|
else process.env.FANAVARAN_REQUIRE_EXPERT_SIGNATURE = old;
|
|
});
|
|
|
|
it("requires a signature before running prerequisites and leaves automatic final submission pending", async () => {
|
|
await expect(
|
|
service.submitFanavaranExpertiseV2(
|
|
"claim-1",
|
|
"parsian",
|
|
undefined,
|
|
actor,
|
|
),
|
|
).rejects.toMatchObject({
|
|
response: { code: "FANAVARAN_EXPERT_SIGNATURE_REQUIRED" },
|
|
});
|
|
await expect(
|
|
service.autoSubmitFanavaranExpertiseOnExpertReply("claim-1"),
|
|
).resolves.toMatchObject({ skipped: true });
|
|
expect(
|
|
service.completeFanavaranExpertisePrerequisites,
|
|
).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("stores the authenticated signer's signature, rejects stale or mismatched signatures and accepts retry", async () => {
|
|
const revision = fanavaranSignatureRevision(claim);
|
|
await expect(
|
|
service.saveFanavaranExpertSignature("claim-1", actor, {
|
|
revision,
|
|
image: "invalid",
|
|
}),
|
|
).rejects.toMatchObject({ status: 400 });
|
|
await expect(
|
|
service.saveFanavaranExpertSignature("claim-1", actor, {
|
|
revision: "stale",
|
|
image,
|
|
}),
|
|
).rejects.toMatchObject({ status: 409 });
|
|
await expect(
|
|
service.saveFanavaranExpertSignature("claim-1", actor, {
|
|
revision,
|
|
image,
|
|
}),
|
|
).resolves.toMatchObject({ signed: true });
|
|
expect(claim.fanavaranExpertSignature).toMatchObject({
|
|
actorId: actor.sub,
|
|
actorRole: actor.role,
|
|
revision,
|
|
image,
|
|
});
|
|
await expect(
|
|
service.submitFanavaranExpertiseV2("claim-1", "parsian", undefined, {
|
|
...actor,
|
|
sub: "other",
|
|
}),
|
|
).rejects.toMatchObject({ status: 409 });
|
|
await expect(
|
|
service.submitFanavaranExpertiseV2(
|
|
"claim-1",
|
|
"parsian",
|
|
{ ClaimExpertId: 12 },
|
|
actor,
|
|
),
|
|
).rejects.toMatchObject({ status: 400 });
|
|
expect(
|
|
service.completeFanavaranExpertisePrerequisites,
|
|
).not.toHaveBeenCalled();
|
|
claim.fanavaranSync = { baseClaim: { status: "success" } };
|
|
await expect(
|
|
service.submitFanavaranExpertiseV2(
|
|
"claim-1",
|
|
"parsian",
|
|
undefined,
|
|
actor,
|
|
),
|
|
).resolves.toEqual({ Id: 123 });
|
|
claim.evaluation.priceDrop = { total: 123 };
|
|
await expect(
|
|
service.submitFanavaranExpertiseV2(
|
|
"claim-1",
|
|
"parsian",
|
|
undefined,
|
|
actor,
|
|
),
|
|
).rejects.toMatchObject({ status: 409 });
|
|
});
|
|
|
|
it("retains unsigned submission when the deployment disables the feature", async () => {
|
|
process.env.FANAVARAN_REQUIRE_EXPERT_SIGNATURE = "false";
|
|
await expect(
|
|
service.submitFanavaranExpertiseV2("claim-1", "parsian"),
|
|
).resolves.toEqual({ Id: 123 });
|
|
expect(
|
|
service.claimCaseDbService.findWithFanavaranSignature,
|
|
).not.toHaveBeenCalled();
|
|
});
|
|
});
|