@ogcio/o11y-sdk-react
Version:
Opentelemetry standard instrumentation SDK for React based project
71 lines (59 loc) • 2.13 kB
text/typescript
import { TransportItemType } from "@grafana/faro-web-sdk";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { _cleanStringPII } from "../../lib/internals/pii-detection";
// Mock faro.api.pushMeasurement
vi.mock("@grafana/faro-web-sdk", async () => {
const actual = await vi.importActual<any>("@grafana/faro-web-sdk");
return {
...actual,
faro: {
api: {
pushMeasurement: vi.fn(),
},
},
};
});
import { faro } from "@grafana/faro-web-sdk";
describe("_cleanStringPII", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("should redact email and call pushMeasurement", () => {
const input = "Contact me at alice@example.com.";
const result = _cleanStringPII(input, TransportItemType.LOG);
expect(result).toBe("Contact me at [REDACTED EMAIL].");
expect(faro.api.pushMeasurement).toHaveBeenCalledOnce();
expect(faro.api.pushMeasurement).toHaveBeenCalledWith(
{
type: "faro_o11y_pii_redaction",
values: { redacted: 1 },
},
{
context: {
pii_type: "email",
redaction_source: TransportItemType.LOG,
pii_email_domain: "example.com",
pii_format: "string",
},
},
);
});
it("should decode and redact URI-encoded emails", () => {
const encoded = "mailto%3Ajohn.doe%40gmail.com";
const result = _cleanStringPII(encoded, TransportItemType.EXCEPTION);
expect(result).toBe("mailto:[REDACTED EMAIL]");
expect(faro.api.pushMeasurement).toHaveBeenCalledOnce();
});
it("should return same string when no email present", () => {
const input = "Just a normal message.";
const result = _cleanStringPII(input, TransportItemType.LOG);
expect(result).toBe(input);
expect(faro.api.pushMeasurement).not.toHaveBeenCalled();
});
it("should return same string when decodeURIComponent fail", () => {
const input = "mailto%john.doe%4gmail.com";
const result = _cleanStringPII(input, TransportItemType.LOG);
expect(result).toBe(input);
expect(faro.api.pushMeasurement).not.toHaveBeenCalled();
});
});