-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfetcher_test.go
104 lines (85 loc) · 3.05 KB
/
fetcher_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
package auth0
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"strconv"
"time"
"net/http"
"testing"
)
func TestNewClientCredentialsFlowExpectsObjectInitializedSuccessfully(t *testing.T) {
// arrange
clientID := "PV2AvGcMjOFErV6QpaqKnfrUdt8yPuHI"
clientSecret := "9oXvXvWHfQaaAiWr-wBfS5Vtyp3aGyMuwIwqYs2NuRtmV7-1XEXXNJ1ZA97jLo6J"
tokenURL := "https://yourcompany.auth0.com/oauth/token"
grantType := "client_credentials"
// act
f := NewTokenFetcher(nil, tokenURL, clientID, clientSecret)
// assert
a := f.(*tokenFetcher)
assert.Equal(t, clientID, a.clientID, "expected client id to match")
assert.Equal(t, clientSecret, a.clientSecret, "expected client secret to match")
assert.Equal(t, tokenURL, a.tokenURL, "expected tokenURL to match")
assert.Equal(t, grantType, a.grantType, "expected grant type to match")
}
func TestConfirmsTokenNotCaching(t *testing.T) {
i := 1
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"access_token\":\"token"+strconv.Itoa(i)+"\"}")
i++
}))
defer ts.Close()
// arrange
clientID := "PV2AvGcMjOFErV6QpaqKnfrUdt8yPuHI"
clientSecret := "9oXvXvWHfQaaAiWr-wBfS5Vtyp3aGyMuwIwqYs2NuRtmV7-1XEXXNJ1ZA97jLo6J"
// act
f := NewTokenFetcher(ts.Client(), ts.URL, clientID, clientSecret)
token, err := f.Token("audience")
newToken, err := f.NewToken("audience")
// assert
assert.Nil(t, err, "Expected no error")
assert.Equal(t, "token1", token, "Expected 1st token")
assert.Equal(t, "token2", newToken, "Expected 2nd token")
}
func TestConfirmsTokenCaching(t *testing.T) {
i := 1
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expiresAt := time.Now().Add(time.Second * 2).Unix()
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = &jwt.StandardClaims{
ExpiresAt: expiresAt,
}
tokenString, _ := token.SignedString([]byte("secret"))
fmt.Fprintln(w, "{\"access_token\":\""+tokenString+"\"}")
i++
}))
defer ts.Close()
// arrange
clientID := "PV2AvGcMjOFErV6QpaqKnfrUdt8yPuHI"
clientSecret := "9oXvXvWHfQaaAiWr-wBfS5Vtyp3aGyMuwIwqYs2NuRtmV7-1XEXXNJ1ZA97jLo6J"
// act
f := NewTokenFetcher(ts.Client(), ts.URL, clientID, clientSecret)
token, err := f.Token("audience")
token2, err := f.Token("audience")
time.Sleep(3 * time.Second)
token3, err := f.Token("audience")
// assert
assert.Nil(t, err, "Expected no error")
assert.Equal(t, token2, token, "Expected tokens to be equal")
assert.NotEqual(t, token3, token, "Expected new token")
}
// Integration test
func _TestAccessTokenFromGlobalCredentials(t *testing.T) {
// arrange
clientID := "zO2mKgrhEA6kcI23E0lRHutHBd1AX8ht"
clientSecret := "W20FgCL0FZ_ZMFmpdrh13Y49WuflWrJswPqJjDtaXtcaUAYD2x0ETsiPQ1xh8xez"
tokenURL := "https://yourcompany.auth0.com/oauth/token"
// act
f := NewTokenFetcher(http.DefaultClient, tokenURL, clientID, clientSecret)
accessToken, err := f.NewToken("")
// assert
assert.Nil(t, err, "Expected no errors")
assert.NotEmpty(t, accessToken, "Expected a non-empty access token")
}