You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pwn_test.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pwn
  4. import (
  5. "math/rand"
  6. "net/http"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/h2non/gock"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. var client = New(WithHTTP(&http.Client{
  14. Timeout: time.Second * 2,
  15. }))
  16. func TestMain(m *testing.M) {
  17. rand.Seed(time.Now().Unix())
  18. os.Exit(m.Run())
  19. }
  20. func TestPassword(t *testing.T) {
  21. defer gock.Off()
  22. gock.New("https://api.pwnedpasswords.com").Get("/range/5c1d8").Times(1).Reply(200).BodyString("EAF2F254732680E8AC339B84F3266ECCBB5:1\r\nFC446EB88938834178CB9322C1EE273C2A7:2")
  23. gock.New("https://api.pwnedpasswords.com").Get("/range/ba189").Times(1).Reply(200).BodyString("FD4CB34F0378BCB15D23F6FFD28F0775C9E:3\r\nFDF342FCD8C3611DAE4D76E8A992A3E4169:4")
  24. count, err := client.CheckPassword("", false)
  25. assert.ErrorIs(t, err, ErrEmptyPassword, "blank input should return ErrEmptyPassword")
  26. assert.Equal(t, -1, count)
  27. count, err = client.CheckPassword("pwned", true)
  28. assert.NoError(t, err)
  29. assert.Equal(t, 1, count)
  30. count, err = client.CheckPassword("notpwned", false)
  31. assert.NoError(t, err)
  32. assert.Equal(t, 0, count)
  33. }