Approval-based snapshot testing library for Go (mirror)
1package shutter_test
2
3import (
4 "testing"
5
6 "github.com/ptdewey/shutter"
7)
8
9func TestIgnoreKeys(t *testing.T) {
10 tests := []struct {
11 name string
12 json string
13 opts []shutter.Option
14 title string
15 }{
16 {
17 name: "multiple_keys",
18 json: `{
19 "id": 1,
20 "name": "John Doe",
21 "password": "secret",
22 "secret": "confidential",
23 "token": "abc123",
24 "email": "john@example.com"
25 }`,
26 opts: []shutter.Option{shutter.IgnoreKey("password", "secret", "token")},
27 title: "Ignore Multiple Keys",
28 },
29 {
30 name: "key_value_pairs",
31 json: `{
32 "username": "john_doe",
33 "password": "secret123",
34 "email": "john@example.com",
35 "api_key": "sk_live_abc123"
36 }`,
37 opts: []shutter.Option{
38 shutter.IgnoreKeyValue("password", "*"),
39 shutter.IgnoreKeyValue("api_key", "*"),
40 },
41 title: "Ignore Password Field",
42 },
43 {
44 name: "arrays",
45 json: `{
46 "users": [
47 {
48 "id": 1,
49 "name": "Alice",
50 "password": "secret1",
51 "email": "alice@example.com"
52 },
53 {
54 "id": 2,
55 "name": "Bob",
56 "password": "secret2",
57 "email": "bob@example.com"
58 }
59 ]
60 }`,
61 opts: []shutter.Option{shutter.IgnoreKey("password")},
62 title: "Ignore in Arrays",
63 },
64 }
65
66 for _, tt := range tests {
67 t.Run(tt.name, func(t *testing.T) {
68 shutter.SnapJSON(t, tt.title, tt.json, tt.opts...)
69 })
70 }
71}
72
73func TestIgnoreSensitiveKeys(t *testing.T) {
74 jsonStr := `{
75 "username": "john_doe",
76 "password": "secret123",
77 "api_key": "sk_live_abc123",
78 "access_token": "token123",
79 "refresh_token": "refresh123",
80 "email": "john@example.com",
81 "name": "John Doe"
82 }`
83
84 shutter.SnapJSON(t, "Ignore Sensitive Keys", jsonStr,
85 shutter.IgnoreSensitive(),
86 )
87}
88
89func TestIgnoreKeyPatterns(t *testing.T) {
90 tests := []struct {
91 name string
92 json string
93 opts []shutter.Option
94 title string
95 }{
96 {
97 name: "prefix_pattern",
98 json: `{
99 "user_id": 1,
100 "user_name": "john",
101 "user_email": "john@example.com",
102 "product_id": 100,
103 "product_name": "Widget"
104 }`,
105 opts: []shutter.Option{shutter.IgnoreKeyMatching(`^user_`)},
106 title: "Ignore Keys Matching Pattern",
107 },
108 {
109 name: "contains_pattern",
110 json: `{
111 "username": "john_doe",
112 "password": "secret",
113 "admin_password": "admin_secret",
114 "user_token": "token123",
115 "email": "john@example.com"
116 }`,
117 opts: []shutter.Option{
118 shutter.IgnoreKeyPattern(`.*password.*`, ""),
119 shutter.IgnoreKeyPattern(`.*token.*`, ""),
120 },
121 title: "Ignore Key Pattern",
122 },
123 }
124
125 for _, tt := range tests {
126 t.Run(tt.name, func(t *testing.T) {
127 shutter.SnapJSON(t, tt.title, tt.json, tt.opts...)
128 })
129 }
130}
131
132func TestIgnoreValues(t *testing.T) {
133 tests := []struct {
134 name string
135 json string
136 opts []shutter.Option
137 title string
138 }{
139 {
140 name: "specific_values",
141 json: `{
142 "status": "pending",
143 "result": "pending",
144 "message": "Processing",
145 "state": "pending"
146 }`,
147 opts: []shutter.Option{shutter.IgnoreValue("pending")},
148 title: "Ignore Specific Values",
149 },
150 {
151 name: "empty_values",
152 json: `{
153 "name": "John Doe",
154 "middle_name": "",
155 "nickname": " ",
156 "email": "john@example.com",
157 "phone": ""
158 }`,
159 opts: []shutter.Option{shutter.IgnoreEmpty()},
160 title: "Ignore Empty Values",
161 },
162 {
163 name: "null_values",
164 json: `{
165 "name": "John Doe",
166 "middle_name": null,
167 "email": "john@example.com",
168 "phone": null,
169 "age": 30
170 }`,
171 opts: []shutter.Option{shutter.IgnoreNull()},
172 title: "Ignore Null Values",
173 },
174 }
175
176 for _, tt := range tests {
177 t.Run(tt.name, func(t *testing.T) {
178 shutter.SnapJSON(t, tt.title, tt.json, tt.opts...)
179 })
180 }
181}
182
183func TestCustomIgnore(t *testing.T) {
184 jsonStr := `{
185 "id": 1,
186 "name": "John Doe",
187 "age": 25,
188 "score": 95,
189 "grade": "A"
190 }`
191
192 shutter.SnapJSON(t, "Custom Ignore Function", jsonStr,
193 shutter.IgnoreWith(func(key, value string) bool {
194 // Ignore numeric values
195 return value == "1" || value == "25" || value == "95"
196 }),
197 )
198}
199
200func TestNestedIgnorePatterns(t *testing.T) {
201 jsonStr := `{
202 "user": {
203 "id": 1,
204 "name": "John Doe",
205 "password": "secret",
206 "email": "john@example.com",
207 "profile": {
208 "bio": "Developer",
209 "api_key": "sk_live_abc123",
210 "website": "https://example.com"
211 }
212 },
213 "admin": {
214 "password": "admin_secret",
215 "token": "admin_token_123"
216 }
217 }`
218
219 shutter.SnapJSON(t, "Nested Ignore Patterns", jsonStr,
220 shutter.IgnoreSensitive(),
221 )
222}
223
224func TestCombinedIgnoreAndScrub(t *testing.T) {
225 jsonStr := `{
226 "user_id": "550e8400-e29b-41d4-a716-446655440000",
227 "name": "John Doe",
228 "email": "john@example.com",
229 "password": "secret123",
230 "created_at": "2023-01-15T10:30:00Z",
231 "api_key": "sk_live_abc123",
232 "ip_address": "192.168.1.1"
233 }`
234
235 shutter.SnapJSON(t, "Combined Ignore and Scrub", jsonStr,
236 // Ignore sensitive keys entirely
237 shutter.IgnoreKey("password", "api_key"),
238 // Scrub dynamic/identifiable data
239 shutter.ScrubUUID(),
240 shutter.ScrubEmail(),
241 shutter.ScrubTimestamp(),
242 shutter.ScrubIP(),
243 )
244}
245
246func TestComplexRealWorldExample(t *testing.T) {
247 jsonStr := `{
248 "request_id": "550e8400-e29b-41d4-a716-446655440000",
249 "timestamp": "2023-11-20T15:30:00Z",
250 "user": {
251 "id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
252 "email": "user@example.com",
253 "name": "John Doe",
254 "password": "hashed_password",
255 "api_key": "sk_live_abc123def456",
256 "ip_address": "192.168.1.1",
257 "created_at": "2023-01-15T10:30:00Z"
258 },
259 "transaction": {
260 "id": "txn_abc123",
261 "amount": 99.99,
262 "currency": "USD",
263 "card_number": "4532-1234-5678-9010",
264 "timestamp": "2023-11-20T15:30:00Z"
265 },
266 "metadata": {
267 "server_ip": "10.0.0.5",
268 "session_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
269 "user_agent": "Mozilla/5.0"
270 }
271 }`
272
273 shutter.SnapJSON(t, "Real World API Response", jsonStr,
274 // Ignore sensitive fields
275 shutter.IgnoreSensitive(),
276 shutter.IgnoreKey("card_number"),
277 // Scrub dynamic/identifiable data
278 shutter.ScrubUUID(),
279 shutter.ScrubEmail(),
280 shutter.ScrubTimestamp(),
281 shutter.ScrubIP(),
282 shutter.ScrubJWT(),
283 )
284}