|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { stripIpAddress } from "./sentryBeforeSend"; |
| 3 | + |
| 4 | +describe("stripIpAddress", () => { |
| 5 | + it("removes ip_address from event.user", () => { |
| 6 | + const event = { |
| 7 | + message: "test", |
| 8 | + user: { id: "abc", ip_address: "1.2.3.4" }, |
| 9 | + }; |
| 10 | + const result = stripIpAddress(event); |
| 11 | + expect(result.user).not.toHaveProperty("ip_address"); |
| 12 | + expect(result.user.id).toBe("abc"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("returns the event unchanged when there is no user", () => { |
| 16 | + const event = { message: "test" }; |
| 17 | + const result = stripIpAddress(event); |
| 18 | + expect(result).toBe(event); |
| 19 | + }); |
| 20 | + |
| 21 | + it("handles user object with no ip_address", () => { |
| 22 | + const event = { message: "test", user: { id: "abc" } }; |
| 23 | + const result = stripIpAddress(event); |
| 24 | + expect(result.user.ip_address).toBeUndefined(); |
| 25 | + expect(result.user.id).toBe("abc"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("does not mutate the original event", () => { |
| 29 | + const event = { |
| 30 | + message: "test", |
| 31 | + user: { id: "abc", ip_address: "1.2.3.4" }, |
| 32 | + }; |
| 33 | + stripIpAddress(event); |
| 34 | + expect(event.user.ip_address).toBe("1.2.3.4"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("preserves other event properties", () => { |
| 38 | + const event = { |
| 39 | + message: "test", |
| 40 | + tags: { batchName: "batch1" }, |
| 41 | + user: { id: "abc", ip_address: "1.2.3.4", username: "player1" }, |
| 42 | + }; |
| 43 | + const result = stripIpAddress(event); |
| 44 | + expect(result.message).toBe("test"); |
| 45 | + expect(result.tags).toEqual({ batchName: "batch1" }); |
| 46 | + expect(result.user.username).toBe("player1"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("handles null user gracefully", () => { |
| 50 | + const event = { message: "test", user: null }; |
| 51 | + const result = stripIpAddress(event); |
| 52 | + expect(result).toBe(event); |
| 53 | + }); |
| 54 | +}); |
0 commit comments