onasty/internal/hasher/sha256_test.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com refactor: use models in verification tokes repo (#113)..., 1 year ago
ss2316544@gmail.com refactor: use models in verification tokes repo (#113)..., 1 year ago
| 1 | package hasher |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/require" |
| 7 | ) |
| 8 | |
| 9 | func TestSHA256Hasher_Hash(t *testing.T) { |
| 10 | hasher := NewSHA256Hasher("salt") |
| 11 | |
| 12 | hashed, err := hasher.Hash("qwerty123") |
| 13 | require.NoError(t, err) |
| 14 | require.NotEmpty(t, hashed) |
| 15 | } |
| 16 | |
| 17 | func TestSHA256Hasher_Compared(t *testing.T) { |
| 18 | hasher := NewSHA256Hasher("salt") |
| 19 | input := "qwerty123" |
| 20 | |
| 21 | t.Run("valid", func(t *testing.T) { |
| 22 | hashed, err := hasher.Hash(input) |
| 23 | require.NoError(t, err) |
| 24 | require.NotEmpty(t, hashed) |
| 25 | |
| 26 | err = hasher.Compare(hashed, input) |
| 27 | require.NoError(t, err) |
| 28 | }) |
| 29 | |
| 30 | t.Run("mismatch", func(t *testing.T) { |
| 31 | hashed, err := hasher.Hash(input + "4") |
| 32 | require.NoError(t, err) |
| 33 | require.NotEmpty(t, hashed) |
| 34 | |
| 35 | err = hasher.Compare(hashed, input) |
| 36 | require.ErrorIs(t, err, ErrMismatchedHashes) |
| 37 | }) |
| 38 | } |