|
1
|
|
|
package ynote |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"net/http" |
|
6
|
|
|
"testing" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/NdoleStudio/ynote-go/internal/helpers" |
|
9
|
|
|
"github.com/NdoleStudio/ynote-go/internal/stubs" |
|
10
|
|
|
"github.com/stretchr/testify/assert" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
const ( |
|
14
|
|
|
testClientID = "test-clientID" |
|
15
|
|
|
testClientSecret = "test-clientSecret" |
|
16
|
|
|
) |
|
17
|
|
|
|
|
18
|
|
|
func TestClient_Token(t *testing.T) { |
|
19
|
|
|
// Setup |
|
20
|
|
|
t.Parallel() |
|
21
|
|
|
|
|
22
|
|
|
// Arrange |
|
23
|
|
|
requests := make([]http.Request, 0) |
|
24
|
|
|
server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK}, [][]byte{stubs.TokenResponse()}, &requests) |
|
25
|
|
|
|
|
26
|
|
|
client := New( |
|
27
|
|
|
WithTokenURL(server.URL), |
|
28
|
|
|
WithClientID(testClientID), |
|
29
|
|
|
WithClientSecret(testClientSecret), |
|
30
|
|
|
) |
|
31
|
|
|
|
|
32
|
|
|
// Act |
|
33
|
|
|
accessToken, response, err := client.AccessToken(context.Background()) |
|
34
|
|
|
|
|
35
|
|
|
// Assert |
|
36
|
|
|
assert.Nil(t, err) |
|
37
|
|
|
|
|
38
|
|
|
assert.Equal(t, 1, len(requests)) |
|
39
|
|
|
|
|
40
|
|
|
request := requests[0] |
|
41
|
|
|
actualUsername, actualPassword, ok := request.BasicAuth() |
|
42
|
|
|
assert.True(t, ok) |
|
43
|
|
|
|
|
44
|
|
|
assert.Equal(t, testClientID, actualUsername) |
|
45
|
|
|
assert.Equal(t, testClientSecret, actualPassword) |
|
46
|
|
|
assert.Equal(t, "/oauth2/token", request.URL.Path) |
|
47
|
|
|
assert.Equal(t, "application/x-www-form-urlencoded", request.Header.Get("Content-Type")) |
|
48
|
|
|
|
|
49
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
50
|
|
|
assert.Equal(t, int64(2496), accessToken.ExpiresIn) |
|
51
|
|
|
assert.Equal(t, "Bearer", accessToken.TokenType) |
|
52
|
|
|
assert.Equal(t, "19077204-9d0a-31fa-85cf-xxxxxxxxxx", accessToken.AccessToken) |
|
53
|
|
|
|
|
54
|
|
|
// Teardown |
|
55
|
|
|
server.Close() |
|
56
|
|
|
} |
|
57
|
|
|
|