1
1
package token
2
2
3
3
import (
4
+ "fmt"
4
5
"testing"
5
6
"time"
6
7
@@ -9,51 +10,71 @@ import (
9
10
10
11
func TestNew (t * testing.T ) {
11
12
t .Parallel ()
12
- token := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
13
+ expiration := time .Now ().Add (1 * time .Hour )
14
+ receivedAt := time .Now ()
15
+ ttl := expiration .Unix () - receivedAt .Unix ()
16
+ token := New ("username" , "password" , "rawToken" , expiration , receivedAt , ttl )
13
17
assert .Equal (t , "username" , token .username )
14
18
assert .Equal (t , "password" , token .password )
15
19
assert .Equal (t , "rawToken" , token .rawToken )
16
- assert .Equal (t , int64 (3600 ), token .ttl )
20
+ assert .Equal (t , expiration , token .expiresOn )
21
+ assert .Equal (t , receivedAt , token .receivedAt )
22
+ assert .Equal (t , ttl , token .ttl )
17
23
}
18
24
19
25
func TestBasicAuth (t * testing.T ) {
20
26
t .Parallel ()
21
- token := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
22
- username , password := token .BasicAuth ()
23
- assert .Equal (t , "username" , username )
24
- assert .Equal (t , "password" , password )
27
+ username := "username12"
28
+ password := "password32"
29
+ rawToken := fmt .Sprintf ("%s:%s" , username , password )
30
+ expiration := time .Now ().Add (1 * time .Hour )
31
+ receivedAt := time .Now ()
32
+ ttl := expiration .Unix () - receivedAt .Unix ()
33
+ token := New (username , password , rawToken , expiration , receivedAt , ttl )
34
+ baUsername , baPassword := token .BasicAuth ()
35
+ assert .Equal (t , username , baUsername )
36
+ assert .Equal (t , password , baPassword )
25
37
}
26
38
27
39
func TestRawCredentials (t * testing.T ) {
28
40
t .Parallel ()
29
- token := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
41
+ username := "username12"
42
+ password := "password32"
43
+ rawToken := fmt .Sprintf ("%s:%s" , username , password )
44
+ expiration := time .Now ().Add (1 * time .Hour )
45
+ receivedAt := time .Now ()
46
+ ttl := expiration .Unix () - receivedAt .Unix ()
47
+ token := New (username , password , rawToken , expiration , receivedAt , ttl )
30
48
rawCredentials := token .RawCredentials ()
31
- assert .Equal (t , "rawToken" , rawCredentials )
49
+ assert .Equal (t , rawToken , rawCredentials )
50
+ assert .Contains (t , rawCredentials , username )
51
+ assert .Contains (t , rawCredentials , password )
32
52
}
33
53
34
54
func TestExpirationOn (t * testing.T ) {
35
55
t .Parallel ()
36
- token := New ("username" , "password" , "rawToken" , time .Now ().Add (1 * time .Hour ), time .Now (), 3600 )
56
+ username := "username12"
57
+ password := "password32"
58
+ rawToken := fmt .Sprintf ("%s:%s" , username , password )
59
+ expiration := time .Now ().Add (1 * time .Hour )
60
+ receivedAt := time .Now ()
61
+ ttl := expiration .Unix () - receivedAt .Unix ()
62
+ token := New (username , password , rawToken , expiration , receivedAt , ttl )
37
63
expirationOn := token .ExpirationOn ()
38
64
assert .True (t , expirationOn .After (time .Now ()))
39
- }
40
-
41
- func TestTokenExpiration (t * testing.T ) {
42
- t .Parallel ()
43
- token := New ("username" , "password" , "rawToken" , time .Now ().Add (1 * time .Hour ), time .Now (), 3600 )
44
- assert .True (t , token .ExpirationOn ().After (time .Now ()))
45
-
46
- token .expiresOn = time .Now ().Add (- 1 * time .Hour )
47
- assert .False (t , token .ExpirationOn ().After (time .Now ()))
65
+ assert .Equal (t , expiration , expirationOn )
48
66
}
49
67
50
68
func TestTokenTTL (t * testing.T ) {
51
69
t .Parallel ()
52
- token := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
53
- assert .Equal (t , int64 (3600 ), token .ttl )
54
-
55
- token .ttl = 7200
56
- assert .Equal (t , int64 (7200 ), token .ttl )
70
+ username := "username12"
71
+ password := "password32"
72
+ rawToken := fmt .Sprintf ("%s:%s" , username , password )
73
+ expiration := time .Now ().Add (1 * time .Hour )
74
+ receivedAt := time .Now ()
75
+ ttl := expiration .Unix () - receivedAt .Unix ()
76
+ token := New (username , password , rawToken , expiration , receivedAt , ttl )
77
+ assert .Equal (t , ttl , token .TTL ())
57
78
}
58
79
59
80
func TestCopyToken (t * testing.T ) {
@@ -83,28 +104,6 @@ func TestCopyToken(t *testing.T) {
83
104
assert .NotEqual (t , copiedToken , anotherCopy )
84
105
}
85
106
86
- func TestTokenCompare (t * testing.T ) {
87
- t .Parallel ()
88
- // Create two tokens with the same credentials
89
- token1 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
90
- token2 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
91
- assert .True (t , token1 .compareCredentials (token2 ))
92
- assert .True (t , token1 .compareRawCredentials (token2 ))
93
- assert .True (t , token1 .compareToken (token2 ))
94
-
95
- // Create two tokens with different credentials and different raw credentials
96
- token3 := New ("username" , "differentPassword" , "differentRawToken" , time .Now (), time .Now (), 3600 )
97
- assert .False (t , token1 .compareCredentials (token3 ))
98
- assert .False (t , token1 .compareRawCredentials (token3 ))
99
- assert .False (t , token1 .compareToken (token3 ))
100
-
101
- // Create token with same credentials but different rawCredentials
102
- token4 := New ("username" , "password" , "differentRawToken" , time .Now (), time .Now (), 3600 )
103
- assert .False (t , token1 .compareRawCredentials (token4 ))
104
- assert .False (t , token1 .compareToken (token4 ))
105
- assert .True (t , token1 .compareCredentials (token4 ))
106
- }
107
-
108
107
func TestTokenReceivedAt (t * testing.T ) {
109
108
t .Parallel ()
110
109
// Create a token with a specific receivedAt time
@@ -165,30 +164,3 @@ func BenchmarkCopyToken(b *testing.B) {
165
164
token .Copy ()
166
165
}
167
166
}
168
-
169
- func BenchmarkCompareCredentials (b * testing.B ) {
170
- token1 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
171
- token2 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
172
- b .ResetTimer ()
173
- for i := 0 ; i < b .N ; i ++ {
174
- token1 .compareCredentials (token2 )
175
- }
176
- }
177
-
178
- func BenchmarkCompareRawCredentials (b * testing.B ) {
179
- token1 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
180
- token2 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
181
- b .ResetTimer ()
182
- for i := 0 ; i < b .N ; i ++ {
183
- token1 .compareRawCredentials (token2 )
184
- }
185
- }
186
-
187
- func BenchmarkCompareToken (b * testing.B ) {
188
- token1 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
189
- token2 := New ("username" , "password" , "rawToken" , time .Now (), time .Now (), 3600 )
190
- b .ResetTimer ()
191
- for i := 0 ; i < b .N ; i ++ {
192
- token1 .compareToken (token2 )
193
- }
194
- }
0 commit comments