Skip to content

Commit d3672b6

Browse files
committed
add token tests
1 parent 0bee01a commit d3672b6

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ replace github.com/redis/go-redis/v9 => /Users/nedyalko.dyakov/go-redis/
77
require (
88
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.2
99
github.com/redis/go-redis/v9 v9.7.1
10+
github.com/stretchr/testify v1.10.0
1011
)
1112

1213
require (
13-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
gopkg.in/yaml.v3 v3.0.1 // indirect
17+
)
18+
19+
require (
20+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0
1421
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
1522
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1
16-
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
23+
github.com/golang-jwt/jwt/v5 v5.2.1
1724
github.com/google/uuid v1.6.0 // indirect
1825
github.com/kylelemons/godebug v1.1.0 // indirect
1926
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 h1:8BKxhZZLX/W
1010
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
1111
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
1212
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
13+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1314
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
1415
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
1516
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
1617
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1718
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1819
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6 h1:IsMZxCuZqKuao2vNdfD82fjjgPLfyHLpR41Z88viRWs=
20+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
21+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1922
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
2023
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
2124
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
2225
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
2326
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
27+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
28+
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
2429
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
30+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2531
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
2632
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
2733
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
@@ -31,4 +37,7 @@ golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
3137
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3238
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
3339
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
40+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
3442
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
43+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

token_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ var defaultRetryableFunc = func(err error) bool {
382382
return true
383383
}
384384

385-
if ok := errors.As(err, netErr); ok {
385+
if ok := errors.As(err, &netErr); ok {
386386
return netErr.Timeout()
387387
}
388388
return false

token_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package entraid
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewToken(t *testing.T) {
11+
token := NewToken("username", "password", "rawToken", time.Now(), time.Now(), 3600)
12+
assert.Equal(t, "username", token.username)
13+
assert.Equal(t, "password", token.password)
14+
assert.Equal(t, "rawToken", token.rawToken)
15+
assert.Equal(t, int64(3600), token.ttl)
16+
}
17+
18+
func TestBasicAuth(t *testing.T) {
19+
token := NewToken("username", "password", "rawToken", time.Now(), time.Now(), 3600)
20+
username, password := token.BasicAuth()
21+
assert.Equal(t, "username", username)
22+
assert.Equal(t, "password", password)
23+
}
24+
25+
func TestRawCredentials(t *testing.T) {
26+
token := NewToken("username", "password", "rawToken", time.Now(), time.Now(), 3600)
27+
rawCredentials := token.RawCredentials()
28+
assert.Equal(t, "rawToken", rawCredentials)
29+
}
30+
31+
func TestExpirationOn(t *testing.T) {
32+
token := NewToken("username", "password", "rawToken", time.Now().Add(1*time.Hour), time.Now(), 3600)
33+
expirationOn := token.ExpirationOn()
34+
assert.True(t, expirationOn.After(time.Now()))
35+
}
36+
37+
func TestTokenExpiration(t *testing.T) {
38+
token := NewToken("username", "password", "rawToken", time.Now().Add(1*time.Hour), time.Now(), 3600)
39+
assert.True(t, token.ExpirationOn().After(time.Now()))
40+
41+
token.expiresOn = time.Now().Add(-1 * time.Hour)
42+
assert.False(t, token.ExpirationOn().After(time.Now()))
43+
}
44+
45+
func TestTokenReceivedAt(t *testing.T) {
46+
token := NewToken("username", "password", "rawToken", time.Now(), time.Now().Add(1*time.Hour), 3600)
47+
assert.True(t, token.receivedAt.After(time.Now().Add(-1*time.Hour)))
48+
assert.True(t, token.receivedAt.Before(time.Now().Add(1*time.Hour)))
49+
}
50+
51+
func TestTokenTTL(t *testing.T) {
52+
token := NewToken("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)
57+
}
58+
59+
func TestCopyToken(t *testing.T) {
60+
token := NewToken("username", "password", "rawToken", time.Now(), time.Now(), 3600)
61+
copiedToken := copyToken(token)
62+
63+
assert.Equal(t, token.username, copiedToken.username)
64+
assert.Equal(t, token.password, copiedToken.password)
65+
assert.Equal(t, token.rawToken, copiedToken.rawToken)
66+
assert.Equal(t, token.ttl, copiedToken.ttl)
67+
assert.Equal(t, token.expiresOn, copiedToken.expiresOn)
68+
assert.Equal(t, token.receivedAt, copiedToken.receivedAt)
69+
70+
// change the copied token
71+
copiedToken.expiresOn = time.Now().Add(-1 * time.Hour)
72+
assert.NotEqual(t, token.expiresOn, copiedToken.expiresOn)
73+
}
74+
75+
func TestTokenCompare(t *testing.T) {
76+
// Create two tokens with the same credentials
77+
token1 := NewToken("username", "password", "rawToken", time.Now(), time.Now(), 3600)
78+
token2 := NewToken("username", "password", "rawToken", time.Now(), time.Now(), 3600)
79+
assert.True(t, token1.compareCredentials(token2))
80+
assert.True(t, token1.compareRawCredentials(token2))
81+
assert.True(t, token1.compareToken(token2))
82+
83+
// Create two tokens with different credentials and different raw credentials
84+
token3 := NewToken("username", "differentPassword", "differentRawToken", time.Now(), time.Now(), 3600)
85+
assert.False(t, token1.compareCredentials(token3))
86+
assert.False(t, token1.compareRawCredentials(token3))
87+
assert.False(t, token1.compareToken(token3))
88+
89+
// Create token with same credentials but different rawCredentials
90+
token4 := NewToken("username", "password", "differentRawToken", time.Now(), time.Now(), 3600)
91+
assert.False(t, token1.compareRawCredentials(token4))
92+
assert.False(t, token1.compareToken(token4))
93+
assert.True(t, token1.compareCredentials(token4))
94+
}

0 commit comments

Comments
 (0)