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