-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbreach_test.go
706 lines (671 loc) Β· 27.7 KB
/
breach_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// SPDX-FileCopyrightText: Winni Neessen <[email protected]> et al
//
// SPDX-License-Identifier: MIT
package hibp
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
const (
// ServerResponseBreachesAllTruncatedUnverified represents the file path to a test dataset of all
// truncated unverified breaches.
ServerResponseBreachesAllTruncatedUnverified = "testdata/breach-all-truncated-unverified.txt"
// ServerResponseBreachesAllNonTruncatedUnverified represents the file path to a test dataset of all
// non-truncated unverified breaches.
ServerResponseBreachesAllNonTruncatedUnverified = "testdata/breach-all-notruncate-unverified.txt"
// ServerResponseBreachesAllNonTruncatedVerifiedOnly represents the file path to a test dataset of all
// non-truncated verified breaches.
ServerResponseBreachesAllNonTruncatedVerifiedOnly = "testdata/breach-all-notruncate-verifiedonly.txt"
// ServerResponseBreachesAllTruncatedVerifiedOnly represents the file path to a test dataset of all
// truncated verified breaches.
ServerResponseBreachesAllTruncatedVerifiedOnly = "testdata/breach-all-truncated-verifiedonly.txt"
// ServerResponseBreachesBrokenJSON represents the file path to a test dataset with broken or invalid
// JSON data.
ServerResponseBreachesBrokenJSON = "testdata/breach-broken-json.txt"
// ServerResponseBreachesDomainTruncatedUnverified represents a file path template for truncated unverified
// domain breach data.
ServerResponseBreachesDomainTruncatedUnverified = "testdata/breachdomain-%s-truncated-unverified.txt"
// ServerResponseBreachesDomainNonTruncatedUnverified represents the path for a non-truncated, unverified
// domain breach file.
ServerResponseBreachesDomainNonTruncatedUnverified = "testdata/breachdomain-%s-notruncate-unverified.txt"
// ServerResponseBreachesDomainNonTruncatedVerifiedOnly represents a file path for non-truncated, verified-only
// breach data by domain.
ServerResponseBreachesDomainNonTruncatedVerifiedOnly = "testdata/breachdomain-%s-notruncate-verifiedonly.txt"
// ServerResponseBreachesDomainTruncatedVerifiedOnly represents the file path for a specific truncated,
// verified-only breach response.
ServerResponseBreachesDomainTruncatedVerifiedOnly = "testdata/breachdomain-%s-truncated-verifiedonly.txt"
// ServerResponseBreachByName represents the file path format for breach data identified by name
ServerResponseBreachByName = "testdata/breachbyname-%s.txt"
// ServerResponseBreachLatestBreach is the file path containing the test data for the latest breach response.
ServerResponseBreachLatestBreach = "testdata/breach-latestbreach.txt"
// ServerResponseBreachLatestBreachBroken represents the path to a test file containing broken data for the
// latest breach endpoint.
ServerResponseBreachLatestBreachBroken = "testdata/breach-latestbreach-broken.txt"
// ServerResponseDataClasses represents the file path for mock server response data containing data classes.
ServerResponseDataClasses = "testdata/dataclasses.txt"
// ServerResponseDataClassesBroken represents the file path to test data simulating a broken dataclasses response.
ServerResponseDataClassesBroken = "testdata/dataclasses-broken.txt"
// ServerResponseBreachAccount represents the test data file path for simulating a breached account response.
ServerResponseBreachAccount = "testdata/breachaccount-%s.txt"
// ServerResponseBreachAccountBroken represents the file path for a broken breach account server response
// mock data.
ServerResponseBreachAccountBroken = "testdata/breachaccount-%s-broken.txt"
// ServerResponseBreachSubscribedDomains represents the path to the mock data file for subscribed domains
// breach responses.
ServerResponseBreachSubscribedDomains = "testdata/breach-subscribeddomains.txt"
// ServerResponseBreachSubscribedDomainsBroken represents a file path to test data for broken subscribed
// domains breaches.
ServerResponseBreachSubscribedDomainsBroken = "testdata/breach-subscribeddomains-broken.txt"
// ServerResponseBreachedDomain represents the file path to mock server response data for a breached domain.
ServerResponseBreachedDomain = "testdata/breacheddomain.txt"
// ServerResponseBreachedDomainBroken represents the file path for a test dataset with a broken breached
// domain response.
ServerResponseBreachedDomainBroken = "testdata/breacheddomain-broken.txt"
)
func TestBreachAPI_Breaches(t *testing.T) {
t.Run("return all breaches, truncated, including unverified", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachesAllTruncatedUnverified))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches()
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if len(breaches) != 871 {
t.Errorf("expected 871 breaches, got %d", len(breaches))
}
})
t.Run("return all breaches, truncated, including unverified, nil options", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachesAllTruncatedUnverified))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(nil)
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if len(breaches) != 871 {
t.Errorf("expected 871 breaches, got %d", len(breaches))
}
})
t.Run("return all breaches, non-truncated, including unverified", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachesAllNonTruncatedUnverified))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(WithoutTruncate())
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if len(breaches) != 871 {
t.Errorf("expected 871 breaches, got %d", len(breaches))
}
})
t.Run("return all breaches, non-truncated, exclude unverified", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachesAllNonTruncatedVerifiedOnly))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(WithoutTruncate(), WithoutUnverified())
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if len(breaches) != 871 {
t.Errorf("expected 871 breaches, got %d", len(breaches))
}
})
t.Run("return all breaches, truncated, exclude unverified", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachesAllTruncatedVerifiedOnly))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(WithoutTruncate(), WithoutUnverified())
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if len(breaches) != 871 {
t.Errorf("expected 871 breaches, got %d", len(breaches))
}
})
t.Run("return all breaches on broken JSON should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachesBrokenJSON))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.Breaches()
if err == nil {
t.Error("expected error, got nil")
}
})
t.Run("return all breaches succeeds on rate limit", func(t *testing.T) {
run := 0
server := httptest.NewServer(newTestRetryHandler(t, &run, true))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithRateLimitSleep(), WithLogger(newTestLogger(t)))
_, _, err := hc.BreachAPI.Breaches()
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
})
t.Run("return all breaches fails on rate limit", func(t *testing.T) {
run := 0
server := httptest.NewServer(newTestRetryHandler(t, &run, true))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithLogger(newTestLogger(t)))
_, hr, err := hc.BreachAPI.Breaches()
if err == nil {
t.Error("expected request to fail due to rate limiting")
}
if hr == nil {
t.Fatal("expected HTTP response to be returned")
}
if hr.StatusCode != http.StatusTooManyRequests {
t.Errorf("expected HTTP status code to be %d, got %d", http.StatusTooManyRequests, hr.StatusCode)
}
})
}
func TestBreachAPI_Breaches_using_WithDomain(t *testing.T) {
tests := []struct {
name string
breached bool
verified bool
}{
{"adobe.com", true, true},
{"parapa.mail.ru", true, true},
{"xiaomi.cn", true, false},
{"example.com", false, false},
}
for _, tt := range tests {
t.Run("get breaches for "+tt.name+", truncated, including unverified", func(t *testing.T) {
response := fmt.Sprintf(ServerResponseBreachesDomainTruncatedUnverified, tt.name)
server := httptest.NewServer(newTestFileHandler(t, response))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(WithDomain(tt.name))
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if tt.breached && len(breaches) == 0 {
t.Errorf("expected breaches for domain %q, got none", tt.name)
}
if !tt.breached && len(breaches) > 0 {
t.Errorf("expected no breaches for domain %q, got %d", tt.name, len(breaches))
}
if len(breaches) > 0 && breaches[0].Domain != tt.name {
t.Errorf("expected breaches for domain %q, got %q", tt.name, breaches[0].Domain)
}
if len(breaches) > 0 && tt.breached && !breaches[0].Present() {
t.Errorf("expected breaches for domain %q to be returned by API, got %t", tt.name, breaches[0].Present())
}
})
t.Run("get breaches for "+tt.name+", non-truncated, including unverified", func(t *testing.T) {
response := fmt.Sprintf(ServerResponseBreachesDomainNonTruncatedUnverified, tt.name)
server := httptest.NewServer(newTestFileHandler(t, response))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(WithDomain(tt.name))
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if tt.breached && len(breaches) == 0 {
t.Errorf("expected breaches for domain %q, got none", tt.name)
}
if !tt.breached && len(breaches) > 0 {
t.Errorf("expected no breaches for domain %q, got %d", tt.name, len(breaches))
}
if len(breaches) > 0 && breaches[0].Domain != tt.name {
t.Errorf("expected breaches for domain %q, got %q", tt.name, breaches[0].Domain)
}
if len(breaches) > 0 && tt.breached && !breaches[0].Present() {
t.Errorf("expected breaches for domain %q to be returned by API, got %t", tt.name, breaches[0].Present())
}
})
t.Run("get breaches for "+tt.name+", non-truncated, excluding unverified", func(t *testing.T) {
response := fmt.Sprintf(ServerResponseBreachesDomainNonTruncatedVerifiedOnly, tt.name)
server := httptest.NewServer(newTestFileHandler(t, response))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(WithDomain(tt.name))
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if tt.breached && tt.verified && len(breaches) == 0 {
t.Errorf("expected breaches for domain %q, got none", tt.name)
}
if !tt.breached && len(breaches) > 0 {
t.Errorf("expected no breaches for domain %q, got %d", tt.name, len(breaches))
}
if len(breaches) > 0 && breaches[0].Domain != tt.name {
t.Errorf("expected breaches for domain %q, got %q", tt.name, breaches[0].Domain)
}
if len(breaches) > 0 && tt.breached && !breaches[0].Present() {
t.Errorf("expected breaches for domain %q to be returned by API, got %t", tt.name, breaches[0].Present())
}
})
t.Run("get breaches for "+tt.name+", truncated, excluding unverified", func(t *testing.T) {
response := fmt.Sprintf(ServerResponseBreachesDomainTruncatedVerifiedOnly, tt.name)
server := httptest.NewServer(newTestFileHandler(t, response))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breaches, _, err := hc.BreachAPI.Breaches(WithDomain(tt.name))
if err != nil {
t.Errorf("failed to get breaches: %s", err)
}
if tt.breached && tt.verified && len(breaches) == 0 {
t.Errorf("expected breaches for domain %q, got none", tt.name)
}
if !tt.breached && len(breaches) > 0 {
t.Errorf("expected no breaches for domain %q, got %d", tt.name, len(breaches))
}
if len(breaches) > 0 && breaches[0].Domain != tt.name {
t.Errorf("expected breaches for domain %q, got %q", tt.name, breaches[0].Domain)
}
if len(breaches) > 0 && tt.breached && !breaches[0].Present() {
t.Errorf("expected breaches for domain %q to be returned by API, got %t", tt.name, breaches[0].Present())
}
})
}
}
func TestBreachAPI_BreachByName(t *testing.T) {
tests := []struct {
name string
verified bool
}{
{"Adobe", true},
{"Parapa", true},
{"Xiaomi", false},
}
for _, tt := range tests {
t.Run("get breach by name for "+tt.name, func(t *testing.T) {
response := fmt.Sprintf(ServerResponseBreachByName, strings.ToLower(tt.name))
server := httptest.NewServer(newTestFileHandler(t, response))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breach, _, err := hc.BreachAPI.BreachByName(tt.name)
if err != nil {
t.Errorf("failed to get breach: %s", err)
}
if breach.Name != tt.name {
t.Errorf("expected breach for name %q, got %q", tt.name, breach.Name)
}
if tt.verified && breach.IsVerified == false {
t.Errorf("expected breach for name %q to be verified, got %t", tt.name, breach.IsVerified)
}
})
}
t.Run("get breach with empty name should fail", func(t *testing.T) {
response := fmt.Sprintf(ServerResponseBreachByName, "adobe")
server := httptest.NewServer(newTestFileHandler(t, response))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.BreachByName("")
if err == nil {
t.Errorf("expected to fail with empty name")
}
if !errors.Is(err, ErrNoName) {
t.Errorf("expected to error to be: %s, got: %s", ErrNoName, err)
}
})
t.Run("get breach should fail on HTTP error", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusInternalServerError))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.BreachByName("Adobe")
if err == nil {
t.Errorf("expected to fail on HTTP error")
}
})
t.Run("get breach with broken JSON should fail", func(t *testing.T) {
response := fmt.Sprintf(ServerResponseBreachByName, "brokenjson")
server := httptest.NewServer(newTestFileHandler(t, response))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.BreachByName("brokenjson")
if err == nil {
t.Errorf("expected to fail with empty name")
}
})
t.Run("get breach for example.com should leave found-tag false", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusNotFound))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breach, _, err := hc.BreachAPI.BreachByName("Example")
if err == nil {
t.Errorf("expected to fail on HTTP error")
}
if breach.Present() {
t.Errorf("expected breach to not be found")
}
})
}
func TestBreachAPI_LatestBreach(t *testing.T) {
t.Run("get latest breach", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachLatestBreach))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
breach, _, err := hc.BreachAPI.LatestBreach()
if err != nil {
t.Errorf("failed to get latest breach: %s", err)
}
if breach.Name != "AlienStealerLogs" {
t.Errorf("expected latest breach to be AlienStealerLogs, got %s", breach.Name)
}
if !breach.IsVerified {
t.Errorf("expected latest breach to be verified, got %t", breach.IsVerified)
}
})
t.Run("get latest breach should fail on HTTP error", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusInternalServerError))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.LatestBreach()
if err == nil {
t.Errorf("expected to fail on HTTP error")
}
})
t.Run("get latest breach with broken JSON should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachLatestBreachBroken))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.LatestBreach()
if err == nil {
t.Errorf("expected to fail with empty name")
}
})
}
func TestBreachAPI_DataClasses(t *testing.T) {
t.Run("data classes a returned successfully", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseDataClasses))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
classes, _, err := hc.BreachAPI.DataClasses()
if err != nil {
t.Errorf("failed to get data classes: %s", err)
}
if len(classes) != 148 {
t.Errorf("expected %d data class, got %d", 148, len(classes))
}
})
t.Run("data classes should fail on HTTP error", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusInternalServerError))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.DataClasses()
if err == nil {
t.Errorf("expected to fail on HTTP error")
}
})
t.Run("data classes with broken JSON should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseDataClassesBroken))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.DataClasses()
if err == nil {
t.Errorf("expected to fail on broken JSON")
}
})
}
func TestBreachAPI_BreachedAccount(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
t.Run("breached account successfully returns data", func(t *testing.T) {
email := "[email protected]"
resp := fmt.Sprintf(ServerResponseBreachAccount, email)
server := httptest.NewServer(newTestFileHandler(t, resp))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
breaches, _, err := hc.BreachAPI.BreachedAccount("[email protected]")
if err != nil {
t.Errorf("failed to get breached account: %s", err)
}
if len(breaches) != 5 {
t.Errorf("expected %d breaches, got %d", 5, len(breaches))
}
})
t.Run("breached account with empty account id should fail", func(t *testing.T) {
email := "[email protected]"
resp := fmt.Sprintf(ServerResponseBreachAccount, email)
server := httptest.NewServer(newTestFileHandler(t, resp))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.BreachAPI.BreachedAccount("")
if err == nil {
t.Error("expected to fail with empty account id")
}
if !errors.Is(err, ErrNoAccountID) {
t.Errorf("expected to error to be: %s, got: %s", ErrNoAccountID, err)
}
})
t.Run("account request with no findings empty breaches list", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusNotFound))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
breach, _, err := hc.BreachAPI.BreachedAccount("[email protected]")
if err != nil {
t.Errorf("failed to get breached account: %s", err)
}
if len(breach) != 0 {
t.Errorf("expected %d breaches, got %d", 0, len(breach))
}
})
t.Run("account request fails on HTTP error", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusInternalServerError))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.BreachAPI.BreachedAccount("[email protected]")
if err == nil {
t.Error("expected to fail on HTTP error")
}
})
t.Run("account request fails on broken JSON", func(t *testing.T) {
email := "[email protected]"
resp := fmt.Sprintf(ServerResponseBreachAccountBroken, email)
server := httptest.NewServer(newTestFileHandler(t, resp))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.BreachAPI.BreachedAccount("[email protected]")
if err == nil {
t.Error("expected to fail on broken JSON")
}
})
t.Run("account request without API key should fail", func(t *testing.T) {
email := "[email protected]"
resp := fmt.Sprintf(ServerResponseBreachAccount, email)
server := httptest.NewServer(newTestFileHandler(t, resp))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.BreachedAccount("[email protected]")
if err == nil {
t.Error("expected to fail without API key")
}
if !errors.Is(err, ErrMethodRequiresAPIKey) {
t.Errorf("expected to error to be: %s, got: %s", ErrMethodRequiresAPIKey, err)
}
})
}
func TestBreachAPI_SubscribedDomains(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
t.Run("subscribed domains successfully returns data", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachSubscribedDomains))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
domains, _, err := hc.BreachAPI.SubscribedDomains()
if err != nil {
t.Fatalf("failed to get subscribed domains: %s", err)
}
if len(domains) != 1 {
t.Errorf("expected %d subscribed domains, got %d", 1, len(domains))
}
domain := domains[0]
if domain.PwnCount.IsNil() {
t.Errorf("expected pwn count to be set, got nil")
}
if domain.PwnCountExcludingSpamLists.NotNil() {
t.Errorf("expected pwn count excluding spam lists to be nil, got %d",
domain.PwnCountExcludingSpamLists.Value())
}
if domain.PwnCountExcludingSpamListsAtLastSubscriptionRenewal.IsNil() {
t.Errorf("expected pwn count excluding spam lists at last subscription renewal to be set, got nil")
}
})
t.Run("subscribed domains should fail on HTTP error", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusInternalServerError))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.BreachAPI.SubscribedDomains()
if err == nil {
t.Errorf("expected to fail on HTTP error")
}
})
t.Run("subscribed domains with broken JSON should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachSubscribedDomainsBroken))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.BreachAPI.SubscribedDomains()
if err == nil {
t.Errorf("expected to fail on broken JSON")
}
})
t.Run("subscribed domains without API key should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachSubscribedDomains))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.SubscribedDomains()
if err == nil {
t.Errorf("expected subscribed domains to fail without API key")
}
if !errors.Is(err, ErrMethodRequiresAPIKey) {
t.Errorf("expected to error to be: %s, got: %s", ErrMethodRequiresAPIKey, err)
}
})
}
func TestBreachAPI_BreachedDomain(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
t.Run("breached domain successfully returns data", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachedDomain))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
accounts, _, err := hc.BreachAPI.BreachedDomain("domain.tld")
if err != nil {
t.Errorf("failed to get breached domains: %s", err)
}
if len(accounts) != 6 {
t.Errorf("expected %d breached accounts, got %d", 6, len(accounts))
}
})
t.Run("breached domain with no breaches returns empty list", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusNotFound))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
accounts, _, err := hc.BreachAPI.BreachedDomain("domain.tld")
if err != nil {
t.Errorf("failed to get breached domains %s", err)
}
if len(accounts) != 0 {
t.Errorf("expected %d breached accounts, got %d", 0, len(accounts))
}
})
t.Run("breached domain with HTTP error should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusInternalServerError))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.BreachAPI.BreachedDomain("domain.tld")
if err == nil {
t.Errorf("expected to fail on HTTP error")
}
})
t.Run("breached domain with broken JSON should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachedDomainBroken))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.BreachAPI.BreachedDomain("domain.tld")
if err == nil {
t.Errorf("expected to fail on broken JSON")
}
})
t.Run("breached domain without API key should fail", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseBreachedDomain))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.BreachAPI.BreachedDomain("domain.tld")
if err == nil {
t.Errorf("expected to fail on missing API key")
}
if !errors.Is(err, ErrMethodRequiresAPIKey) {
t.Errorf("expected to error to be: %s, got: %s", ErrMethodRequiresAPIKey, err)
}
})
}
// ExampleBreachAPI_Breaches_getAllBreaches is a code example to show how to fetch all breaches from the
// HIBP breaches API
func ExampleBreachAPI_Breaches_getAllBreaches() {
hc := New()
bl, _, err := hc.BreachAPI.Breaches()
if err != nil {
panic(err)
}
if len(bl) != 0 {
for _, b := range bl {
fmt.Printf("Found breach:\n\tName: %s\n\tDomain: %s\n\tBreach date: %s\n\n",
b.Name, b.Domain, b.BreachDate.Format("Mon, 2. January 2006"))
}
}
}
// ExampleBreachAPI_Breaches_getAllBreachesNoUnverified is a code example to show how to fetch all breaches from the
// HIBP breaches API but ignoring unverified breaches
func ExampleBreachAPI_Breaches_getAllBreachesNoUnverified() {
hc := New()
bl, _, err := hc.BreachAPI.Breaches()
if err != nil {
panic(err)
}
if len(bl) != 0 {
fmt.Printf("Found %d breaches total.\n", len(bl))
}
bl, _, err = hc.BreachAPI.Breaches(WithoutUnverified())
if err != nil {
panic(err)
}
if len(bl) != 0 {
fmt.Printf("Found %d verified breaches total.\n", len(bl))
}
}
// ExampleBreachAPI_BreachByName is a code example to show how to fetch a specific breach
// from the HIBP breaches API using the BreachByName method
func ExampleBreachAPI_BreachByName() {
hc := New()
bd, _, err := hc.BreachAPI.BreachByName("Adobe")
if err != nil {
panic(err)
}
if bd.Present() {
fmt.Println("Details of the 'Adobe' breach:")
fmt.Printf("\tDomain: %s\n", bd.Domain)
fmt.Printf("\tBreach date: %s\n", bd.BreachDate.Format("2006-01-02"))
fmt.Printf("\tAdded to HIBP: %s\n", bd.AddedDate.String())
}
}
// ExampleBreachAPI_BreachedAccount is a code example to show how to fetch a list of breaches
// for a specific site/account from the HIBP breaches API using the BreachedAccount method
func ExampleBreachAPI_BreachedAccount() {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
panic("A API key is required for this API")
}
hc := New(WithAPIKey(apiKey))
bd, _, err := hc.BreachAPI.BreachedAccount("[email protected]")
if err != nil {
panic(err)
}
for _, b := range bd {
fmt.Printf("Your account was part of the %q breach\n", b.Name)
}
}