|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import type { Event } from "@sentry/remix"; |
| 3 | +import { tenantContext } from "../app/services/tenantContext.server"; |
| 4 | +import { addTenantContextToEvent } from "../app/utils/sentryTenantContext.server"; |
| 5 | + |
| 6 | +const sample = { |
| 7 | + org: { id: "org_1", slug: "acme" }, |
| 8 | + project: { id: "proj_1", ref: "proj_abc" }, |
| 9 | + environment: { id: "env_1", slug: "prod", type: "PRODUCTION" as const }, |
| 10 | +}; |
| 11 | + |
| 12 | +describe("addTenantContextToEvent", () => { |
| 13 | + it("returns the event unchanged when no ALS context", () => { |
| 14 | + const event: Event = { message: "hi", tags: { existing: "1" } }; |
| 15 | + const out = addTenantContextToEvent(event, {}); |
| 16 | + expect(out).toEqual(event); |
| 17 | + }); |
| 18 | + |
| 19 | + it("stamps user + tags when ALS context is set", () => { |
| 20 | + tenantContext.run(sample, () => { |
| 21 | + const event: Event = { message: "boom", tags: { existing: "1" } }; |
| 22 | + const out = addTenantContextToEvent(event, {}); |
| 23 | + expect(out.user).toEqual({ id: "org_1", username: "acme" }); |
| 24 | + expect(out.tags).toMatchObject({ |
| 25 | + existing: "1", |
| 26 | + org_id: "org_1", |
| 27 | + org_slug: "acme", |
| 28 | + project_id: "proj_1", |
| 29 | + project_ref: "proj_abc", |
| 30 | + environment_id: "env_1", |
| 31 | + env_slug: "prod", |
| 32 | + env_type: "PRODUCTION", |
| 33 | + }); |
| 34 | + expect(out.tags?.impersonating).toBeUndefined(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it("adds impersonating tag when flag set", () => { |
| 39 | + tenantContext.run({ ...sample, impersonating: true }, () => { |
| 40 | + const out = addTenantContextToEvent({}, {}); |
| 41 | + expect(out.tags?.impersonating).toBe("true"); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("preserves prior event.user fields it does not own", () => { |
| 46 | + tenantContext.run(sample, () => { |
| 47 | + const event: Event = { user: { ip_address: "1.2.3.4" } }; |
| 48 | + const out = addTenantContextToEvent(event, {}); |
| 49 | + expect(out.user).toEqual({ ip_address: "1.2.3.4", id: "org_1", username: "acme" }); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments