-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth2_test.go
106 lines (93 loc) · 3.36 KB
/
oauth2_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
package oauth2
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)
const (
baseURL = "http://localhost:8888"
)
func TestTokenFetch(t *testing.T) {
//Given
clientID := "testClientID"
clientSecret := "testClientSecret"
respFile, _ := ioutil.ReadFile("./test-fixtures/token_response.json")
resp := tokenResponse{}
req := tokenRequest{}
if err := json.Unmarshal(respFile, &resp); err != nil {
assert.Fail(t, "Can't unmarshal response file")
}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", baseURL+tokenPath,
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
resp, _ := httpmock.NewJsonResponse(200, resp)
return resp, nil
},
)
//when
testConfig := Config{
ClientID: clientID,
ClientSecret: clientSecret,
BaseURL: baseURL}
token, err := testConfig.TokenSource(context.Background(), testHc).Token()
//then
assert.Nil(t, err, "TokenSource should not return an error")
assert.NotNil(t, token, "TokenSource should return a token")
verifyTokenRequest(t, req, clientID, clientSecret)
verifyToken(t, *token, resp)
}
func TestError(t *testing.T) {
respFile, _ := ioutil.ReadFile("./test-fixtures/token_response_err.json")
resp := tokenError{}
if err := json.Unmarshal(respFile, &resp); err != nil {
assert.Fail(t, "Can't unmarshal response file")
}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", baseURL+tokenPath,
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(500, resp)
return resp, nil
},
)
//when
testConfig := Config{
ClientID: "clientID",
ClientSecret: "clientSecret",
BaseURL: baseURL}
_, err := testConfig.TokenSource(context.Background(), testHc).Token()
//then
assert.NotNil(t, err, "TokenSource should return an error")
assert.IsType(t, Error{}, err, "Returned error has proper type")
verifyErrorResponse(t, err.(Error), resp)
}
func verifyToken(t *testing.T, token oauth2.Token, resp tokenResponse) {
assert.Equal(t, resp.AccessToken, token.AccessToken, "AccessToken matches")
assert.Equal(t, resp.RefreshToken, token.RefreshToken, "RefreshToken matches")
assert.Equal(t, resp.TokenType, token.TokenType, "TokenType matches")
respTimeout, err := strconv.Atoi(resp.TokenTimeout)
assert.Nil(t, err, "Error when converting TokenTimeout from the response to int: %v", err)
assert.WithinDuration(t, time.Now().Add(time.Duration(respTimeout)*time.Second), token.Expiry, time.Duration(1)*time.Second, "Token expiry reflects token_timeout from the response")
}
func verifyTokenRequest(t *testing.T, req tokenRequest, clientID string, clientSecret string) {
assert.Equal(t, "client_credentials", req.GrantType, "GrantType matches")
assert.Equal(t, clientID, req.ClientID, "ClientID matches")
assert.Equal(t, clientSecret, req.ClientSecret, "ClientSecret matches")
}
func verifyErrorResponse(t *testing.T, err Error, resp tokenError) {
assert.Equal(t, resp.ErrorCode, err.Code, "Error code matches")
assert.Equal(t, resp.ErrorMessage, err.Message, "Error message matches")
}