pcmt/modules/hibp/hibp_test.go

91 lines
1.6 KiB
Go
Raw Normal View History

// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: AGPL-3.0-only
package hibp
import (
"io"
"net/http"
"os"
"os/signal"
"sync"
"testing"
"time"
)
const hibpTestDomain = "@hibp-integration-tests.com"
func init() {
os.Setenv("GO_ENV_TESTING", "1")
prepScheduler() // purposely ignoring the output values.
}
func prepScheduler() (*chan os.Signal, *chan error, *sync.WaitGroup) {
quit := make(chan os.Signal, 1)
errCh := make(chan error)
wg := &sync.WaitGroup{}
signal.Notify(quit, os.Interrupt)
schedInitNapDuration = 0 * time.Millisecond
go func() {
wg.Add(1) // nolint:staticcheck
go RunReqScheduler(quit, errCh, wg) //nolint:wsl
}()
// give the scheduler some time to start up.
time.Sleep(200 * time.Millisecond)
return &quit, &errCh, wg
}
func TestMultipleBreaches(t *testing.T) {
a := "multiple-breaches"
u := api + "/breachedaccount/" + a + hibpTestDomain
req, err := http.NewRequest("GET", u, nil)
if err != nil {
t.Error(err)
}
respCh, errCh := rChans()
setUA(req)
scheduleReq(req, &respCh, &errCh)
resp := <-respCh
err = <-errCh
defer resp.Body.Close()
if err != nil {
t.Error(err)
}
t.Logf("%+v\n", resp)
body, _ := io.ReadAll(resp.Body)
b := string(body)
t.Log(b)
if resp.StatusCode != 200 {
if apiKey == "" {
if resp.StatusCode == 401 {
t.Logf("apiKey is empty, expected a 401")
} else {
t.Errorf("apiKey is empty, expected 401, got: %q", resp.Status)
}
} else {
t.Errorf("wanted 200, got: %q", resp.Status)
}
}
}
func TestGetAllBreaches(t *testing.T) {
_, err := GetAllBreaches()
if err != nil {
t.Errorf("error: %q", err)
}
}