Approval-based snapshot testing library for Go (mirror)
1package shutter_test
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/ptdewey/shutter"
8)
9
10func TestBuiltInScrubbers(t *testing.T) {
11 // Test all built-in scrubbers in one comprehensive test
12 jsonStr := `{
13 "user_id": "550e8400-e29b-41d4-a716-446655440000",
14 "session_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
15 "email": "user@example.com",
16 "backup_email": "backup.user+tag@subdomain.example.co.uk",
17 "created_at": "2023-01-15T10:30:00Z",
18 "updated_at": "2023-11-20T15:45:30.123Z",
19 "birth_date": "1990-05-15",
20 "us_format_date": "12/25/2023",
21 "unix_created": 1699999999,
22 "unix_updated": 1700000000000,
23 "client_ip": "192.168.1.1",
24 "server_ip": "10.0.0.5",
25 "jwt_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
26 "stripe_key": "sk_live_51HqZ2bKl4FGBMFpLxO0123",
27 "api_key": "api_key_prod_abc123def456",
28 "card_number": "4532-1234-5678-9010",
29 "backup_card": "4532 1234 5678 9010",
30 "name": "John Doe",
31 "message": "Connection from 172.16.0.100"
32 }`
33
34 shutter.SnapJSON(t, "Multiple Scrubbers", jsonStr,
35 shutter.ScrubUUID(),
36 shutter.ScrubEmail(),
37 shutter.ScrubTimestamp(),
38 shutter.ScrubDate(),
39 shutter.ScrubUnixTimestamp(),
40 shutter.ScrubIP(),
41 shutter.ScrubJWT(),
42 shutter.ScrubAPIKey(),
43 shutter.ScrubCreditCard(),
44 )
45}
46
47func TestIndividualScrubbers(t *testing.T) {
48 tests := []struct {
49 name string
50 json string
51 scrubber shutter.Option
52 title string
53 }{
54 {
55 name: "uuid",
56 json: `{
57 "user_id": "550e8400-e29b-41d4-a716-446655440000",
58 "session_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
59 "name": "John Doe"
60 }`,
61 scrubber: shutter.ScrubUUID(),
62 title: "Scrubbed UUIDs",
63 },
64 {
65 name: "timestamps",
66 json: `{
67 "created_at": "2023-01-15T10:30:00Z",
68 "updated_at": "2023-11-20T15:45:30.123Z",
69 "deleted_at": "2023-12-01T08:00:00+05:00",
70 "name": "Test Event"
71 }`,
72 scrubber: shutter.ScrubTimestamp(),
73 title: "Scrubbed Timestamps",
74 },
75 {
76 name: "emails",
77 json: `{
78 "email": "user@example.com",
79 "backup_email": "backup.user+tag@subdomain.example.co.uk",
80 "name": "John Doe"
81 }`,
82 scrubber: shutter.ScrubEmail(),
83 title: "Scrubbed Emails",
84 },
85 {
86 name: "ip_addresses",
87 json: `{
88 "client_ip": "192.168.1.1",
89 "server_ip": "10.0.0.5",
90 "message": "Connection from 172.16.0.100"
91 }`,
92 scrubber: shutter.ScrubIP(),
93 title: "Scrubbed IPs",
94 },
95 {
96 name: "jwts",
97 json: `{
98 "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
99 "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
100 }`,
101 scrubber: shutter.ScrubJWT(),
102 title: "Scrubbed JWTs",
103 },
104 {
105 name: "dates",
106 json: `{
107 "birth_date": "1990-05-15",
108 "hire_date": "2020-01-01",
109 "us_format": "12/25/2023",
110 "name": "John Doe"
111 }`,
112 scrubber: shutter.ScrubDate(),
113 title: "Scrubbed Dates",
114 },
115 {
116 name: "api_keys",
117 json: `{
118 "stripe_key": "sk_live_51HqZ2bKl4FGBMFpLxO0123",
119 "test_key": "pk_test_51HqZ2bKl4FGBMFpLxO0456",
120 "api_key_prod": "api_key_prod_abc123def456",
121 "name": "Test Config"
122 }`,
123 scrubber: shutter.ScrubAPIKey(),
124 title: "Scrubbed API Keys",
125 },
126 {
127 name: "credit_cards",
128 json: `{
129 "card_number": "4532-1234-5678-9010",
130 "backup_card": "4532 1234 5678 9010",
131 "another_card": "4532123456789010",
132 "name": "John Doe"
133 }`,
134 scrubber: shutter.ScrubCreditCard(),
135 title: "Scrubbed Credit Cards",
136 },
137 {
138 name: "unix_timestamps",
139 json: `{
140 "created": 1699999999,
141 "updated": 1700000000000,
142 "deleted": 1700000000,
143 "name": "Test Event"
144 }`,
145 scrubber: shutter.ScrubUnixTimestamp(),
146 title: "Scrubbed Unix Timestamps",
147 },
148 }
149
150 for _, tt := range tests {
151 t.Run(tt.name, func(t *testing.T) {
152 shutter.SnapJSON(t, tt.title, tt.json, tt.scrubber)
153 })
154 }
155}
156
157func TestCustomScrubbers(t *testing.T) {
158 t.Run("regex_scrubber", func(t *testing.T) {
159 jsonStr := `{
160 "api_key": "sk_live_abc123def456",
161 "secret_key": "sk_test_xyz789uvw012",
162 "name": "Test User"
163 }`
164
165 shutter.SnapJSON(t, "Custom Regex Scrubber", jsonStr,
166 shutter.ScrubRegex(`sk_(live|test)_[a-zA-Z0-9]+`, "<API_KEY>"),
167 )
168 })
169
170 t.Run("exact_match_scrubber", func(t *testing.T) {
171 content := "The secret password is 'p@ssw0rd123' and should be hidden."
172
173 shutter.SnapString(t, "Exact Match Scrubber", content,
174 shutter.ScrubExact("p@ssw0rd123", "<PASSWORD>"),
175 )
176 })
177
178 t.Run("custom_function_scrubber", func(t *testing.T) {
179 content := "Hello World! This is a TEST."
180
181 shutter.SnapString(t, "Custom Scrubber", content,
182 shutter.ScrubWith(func(s string) string {
183 return strings.ToLower(s)
184 }),
185 )
186 })
187}
188
189func TestScrubWithSnapFunction(t *testing.T) {
190 data := map[string]any{
191 "user_id": "550e8400-e29b-41d4-a716-446655440000",
192 "email": "user@example.com",
193 "created_at": "2023-01-15T10:30:00Z",
194 "name": "John Doe",
195 }
196
197 shutter.Snap(t, "Scrub With Snap", data,
198 shutter.ScrubUUID(),
199 shutter.ScrubEmail(),
200 shutter.ScrubTimestamp(),
201 )
202}