-
Notifications
You must be signed in to change notification settings - Fork 1
/
apikey_test.go
131 lines (124 loc) · 2.86 KB
/
apikey_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
package mfa
import (
"bytes"
"testing"
)
func TestApiKey_IsCorrect(t *testing.T) {
tests := []struct {
name string
HashedSecret string
Given string
want bool
wantErr bool
}{
{
name: "valid secret",
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
Given: "abc123",
want: true,
wantErr: false,
},
{
name: "invalid secret",
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
Given: "123abc",
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &ApiKey{
HashedSecret: tt.HashedSecret,
}
got, err := k.IsCorrect(tt.Given)
if (err != nil) != tt.wantErr {
t.Errorf("IsCorrect() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("IsCorrect() got = %v, want %v", got, tt.want)
}
})
}
}
// TestApiKey_Hash - test that hashed secret can be verified
func TestApiKey_Hash(t *testing.T) {
tests := []struct {
name string
Secret string
wantErr bool
}{
{
name: "matching hash",
Secret: "abc123",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &ApiKey{
Secret: tt.Secret,
}
err := k.Hash()
if (err != nil) != tt.wantErr {
t.Errorf("Hash() error = %v, wantErr %v", err, tt.wantErr)
return
}
if len(k.HashedSecret) == 0 {
t.Error("hashed secret is empty after call to hash")
return
}
valid, err := k.IsCorrect(tt.Secret)
if err != nil {
t.Errorf("hashed password not valid after hashing??? error: %s", err.Error())
return
}
if !valid {
t.Error("hmm, password is not valid but no errors???")
return
}
})
}
}
func TestApiKey_EncryptDecrypt(t *testing.T) {
tests := []struct {
name string
secret string
plaintext []byte
want []byte
wantErr bool
}{
{
name: "test encrypt/decrypt",
secret: "ED86600E-3DBF-4C23-A0DA-9C55D448",
plaintext: []byte("this is a plaintext string to be encrypted"),
want: []byte("this is a plaintext string to be encrypted"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k1 := &ApiKey{
Secret: tt.secret,
}
k2 := &ApiKey{
Secret: tt.secret,
}
encrypted, err := k1.Encrypt(tt.plaintext)
if (err != nil) != tt.wantErr {
t.Errorf("Encrypt() error = %v, wantErr %v", err, tt.wantErr)
return
}
decrypted, err := k2.Decrypt(encrypted)
if (err != nil) != tt.wantErr {
t.Errorf("Decrypt() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !bytes.Equal(tt.plaintext, tt.want) {
t.Errorf("results from decypt do not match expected. Got: %s, wanted: %s", decrypted, tt.want)
return
}
})
}
}