This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
refresh_token_test.go
115 lines (101 loc) · 3.5 KB
/
refresh_token_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
package moneybutton
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHTTPRefreshAccessToken for mocking requests
type mockHTTPRefreshAccessToken struct{}
// Do is a mock http request
func (m *mockHTTPRefreshAccessToken) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
if req.URL.String() == endpointToken {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"access_token":"eyJ0zXAiOiJKV1QzLCJhbGciOzJIUzI1NiJ9.eyJzdWIiOiIyNjQ1IiwiYXVkIjoiMjhzNzUyZDg0NmRmMjU0YzM3ZWQ1MjBlZTlkYjJjMzMiLCJleHAiOjE2MDzyMzc5MjIsInNjb3BlIjoidXNlcnMucHJvZmlsZXM6cmVhZCBhdXRoLnVzZXJfaWRlbzRpdHk6cmVhZCJ9.bTfOzUvLE5zd2IvRQMXVCJX2kzOB9-44EGLn92UzAMI","token_type":"` + authHeaderBearer + `","expires_in":3600,"scope":"` + PermissionsIdentity + " " + PermissionsProfile + `","refresh_token":"379fc72za81ae1ze2f958399bz2c990350f46034z840584cc5dec63879b8c876"}`)))
}
// Default is valid
return resp, nil
}
func TestClient_RefreshAccessToken(t *testing.T) {
t.Parallel()
t.Run("missing all parameters", func(t *testing.T) {
client := newTestClient(&mockHTTPRefreshAccessToken{})
assert.NotNil(t, client)
tokenResponse, err := client.RefreshAccessToken(
context.Background(),
"",
"",
)
assert.Error(t, err)
assert.Nil(t, tokenResponse)
})
t.Run("missing client id", func(t *testing.T) {
client := newTestClient(&mockHTTPRefreshAccessToken{})
assert.NotNil(t, client)
tokenResponse, err := client.RefreshAccessToken(
context.Background(),
"",
"1234567",
)
assert.Error(t, err)
assert.Nil(t, tokenResponse)
})
t.Run("missing access token", func(t *testing.T) {
client := newTestClient(&mockHTTPRefreshAccessToken{})
assert.NotNil(t, client)
tokenResponse, err := client.RefreshAccessToken(
context.Background(),
"1234567",
"",
)
assert.Error(t, err)
assert.Nil(t, tokenResponse)
})
t.Run("api error response", func(t *testing.T) {
client := newTestClient(&mockHTTPAPIError{})
assert.NotNil(t, client)
tokenResponse, err := client.RefreshAccessToken(
context.Background(),
"1234567",
"1234567",
)
assert.Error(t, err)
assert.Nil(t, tokenResponse)
})
t.Run("http error", func(t *testing.T) {
client := newTestClient(&mockHTTPError{})
assert.NotNil(t, client)
tokenResponse, err := client.RefreshAccessToken(
context.Background(),
"1234567",
"1234567",
)
assert.Error(t, err)
assert.Nil(t, tokenResponse)
})
t.Run("valid response", func(t *testing.T) {
client := newTestClient(&mockHTTPRefreshAccessToken{})
assert.NotNil(t, client)
tokenResponse, err := client.RefreshAccessToken(
context.Background(),
"1234567",
"1234567",
)
assert.NoError(t, err)
assert.NotNil(t, tokenResponse)
assert.Equal(t, "eyJ0zXAiOiJKV1QzLCJhbGciOzJIUzI1NiJ9.eyJzdWIiOiIyNjQ1IiwiYXVkIjoiMjhzNzUyZDg0NmRmMjU0YzM3ZWQ1MjBlZTlkYjJjMzMiLCJleHAiOjE2MDzyMzc5MjIsInNjb3BlIjoidXNlcnMucHJvZmlsZXM6cmVhZCBhdXRoLnVzZXJfaWRlbzRpdHk6cmVhZCJ9.bTfOzUvLE5zd2IvRQMXVCJX2kzOB9-44EGLn92UzAMI", tokenResponse.AccessToken)
assert.Equal(t, uint32(3600), tokenResponse.ExpiresIn)
assert.Equal(t, "379fc72za81ae1ze2f958399bz2c990350f46034z840584cc5dec63879b8c876", tokenResponse.RefreshToken)
assert.Equal(t, PermissionsIdentity+" "+PermissionsProfile, tokenResponse.Scope)
assert.Equal(t, authHeaderBearer, tokenResponse.TokenType)
})
}