1
|
|
|
package mtnmomo |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"net/http" |
6
|
|
|
"strings" |
7
|
|
|
"testing" |
8
|
|
|
|
9
|
|
|
"github.com/NdoleStudio/mtnmomo-go/internal/stubs" |
10
|
|
|
|
11
|
|
|
"github.com/NdoleStudio/mtnmomo-go/internal/helpers" |
12
|
|
|
|
13
|
|
|
"github.com/google/uuid" |
14
|
|
|
"github.com/stretchr/testify/assert" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
func TestApiUserService_Create(t *testing.T) { |
18
|
|
|
// Setup |
19
|
|
|
t.Parallel() |
20
|
|
|
|
21
|
|
|
// Arrange |
22
|
|
|
requests := make([]*http.Request, 0) |
23
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, [][]byte{nil}, &requests) |
24
|
|
|
client := New(WithBaseURL(server.URL), WithSubscriptionKey(testSubscriptionKey)) |
25
|
|
|
userID := uuid.NewString() |
26
|
|
|
|
27
|
|
|
// Act |
28
|
|
|
apiUser, response, err := client.APIUser.CreateAPIUser(context.Background(), userID, "string") |
29
|
|
|
|
30
|
|
|
// Assert |
31
|
|
|
assert.Nil(t, err) |
32
|
|
|
|
33
|
|
|
assert.GreaterOrEqual(t, 1, len(requests)) |
34
|
|
|
request := requests[0] |
35
|
|
|
|
36
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
37
|
|
|
assert.Equal(t, userID, request.Header.Get("X-Reference-Id")) |
38
|
|
|
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode) |
39
|
|
|
assert.Equal(t, userID, apiUser) |
40
|
|
|
|
41
|
|
|
// Teardown |
42
|
|
|
server.Close() |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
func TestApiUserService_CreateBadRequest(t *testing.T) { |
46
|
|
|
// Setup |
47
|
|
|
t.Parallel() |
48
|
|
|
|
49
|
|
|
// Arrange |
50
|
|
|
server := helpers.MakeTestServer(http.StatusBadRequest, nil) |
51
|
|
|
client := New(WithBaseURL(server.URL)) |
52
|
|
|
|
53
|
|
|
// Act |
54
|
|
|
_, response, err := client.APIUser.CreateAPIUser(context.Background(), "errorID", "string") |
55
|
|
|
|
56
|
|
|
// Assert |
57
|
|
|
assert.NotNil(t, err) |
58
|
|
|
|
59
|
|
|
assert.Equal(t, http.StatusBadRequest, response.HTTPResponse.StatusCode) |
60
|
|
|
|
61
|
|
|
// Teardown |
62
|
|
|
server.Close() |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
func TestApiUserService_CreateAPIKey(t *testing.T) { |
66
|
|
|
// Setup |
67
|
|
|
t.Parallel() |
68
|
|
|
|
69
|
|
|
// Arrange |
70
|
|
|
requests := make([]*http.Request, 0) |
71
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, [][]byte{stubs.APIUserCreateAPIKey()}, &requests) |
72
|
|
|
client := New(WithBaseURL(server.URL), WithSubscriptionKey(testSubscriptionKey)) |
73
|
|
|
userID := uuid.NewString() |
74
|
|
|
|
75
|
|
|
// Act |
76
|
|
|
apiKey, response, err := client.APIUser.CreateAPIKey(context.Background(), userID) |
77
|
|
|
|
78
|
|
|
// Assert |
79
|
|
|
assert.Nil(t, err) |
80
|
|
|
|
81
|
|
|
assert.GreaterOrEqual(t, 1, len(requests)) |
82
|
|
|
request := requests[0] |
83
|
|
|
|
84
|
|
|
assert.True(t, strings.Contains(request.URL.String(), userID)) |
85
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
86
|
|
|
assert.Equal(t, "f1db798c98df4bcf83b538175893bbf0", apiKey) |
87
|
|
|
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode) |
88
|
|
|
|
89
|
|
|
// Teardown |
90
|
|
|
server.Close() |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
func TestApiUserService_Get(t *testing.T) { |
94
|
|
|
// Setup |
95
|
|
|
t.Parallel() |
96
|
|
|
|
97
|
|
|
// Arrange |
98
|
|
|
requests := make([]*http.Request, 0) |
99
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, [][]byte{stubs.APIUserGet()}, &requests) |
100
|
|
|
client := New(WithBaseURL(server.URL), WithSubscriptionKey(testSubscriptionKey)) |
101
|
|
|
userID := uuid.NewString() |
102
|
|
|
|
103
|
|
|
// Act |
104
|
|
|
apiUser, response, err := client.APIUser.Get(context.Background(), userID) |
105
|
|
|
|
106
|
|
|
// Assert |
107
|
|
|
assert.Nil(t, err) |
108
|
|
|
|
109
|
|
|
assert.GreaterOrEqual(t, 1, len(requests)) |
110
|
|
|
request := requests[0] |
111
|
|
|
|
112
|
|
|
assert.True(t, strings.Contains(request.URL.String(), userID)) |
113
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
114
|
|
|
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode) |
115
|
|
|
assert.Equal(t, userID, apiUser.UserID) |
116
|
|
|
assert.Equal(t, "sandbox", apiUser.TargetEnvironment) |
117
|
|
|
assert.Equal(t, "string", apiUser.ProviderCallbackHost) |
118
|
|
|
|
119
|
|
|
// Teardown |
120
|
|
|
server.Close() |
121
|
|
|
} |
122
|
|
|
|