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
|
|
|
request := http.Request{} |
23
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, nil, &request) |
24
|
|
|
key := "subscriptionKey" |
25
|
|
|
client := New(WithBaseURL(server.URL), WithSubscriptionKey(key)) |
26
|
|
|
userID := uuid.NewString() |
27
|
|
|
|
28
|
|
|
// Act |
29
|
|
|
apiUser, response, err := client.APIUser.CreateAPIUser(context.Background(), userID, "string") |
30
|
|
|
|
31
|
|
|
// Assert |
32
|
|
|
assert.Nil(t, err) |
33
|
|
|
|
34
|
|
|
assert.Equal(t, key, request.Header.Get(subscriptionKeyHeaderKey)) |
35
|
|
|
assert.Equal(t, userID, request.Header.Get("X-Reference-Id")) |
36
|
|
|
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode) |
37
|
|
|
assert.Equal(t, userID, apiUser) |
38
|
|
|
|
39
|
|
|
// Teardown |
40
|
|
|
server.Close() |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
func TestApiUserService_CreateBadRequest(t *testing.T) { |
44
|
|
|
// Setup |
45
|
|
|
t.Parallel() |
46
|
|
|
|
47
|
|
|
// Arrange |
48
|
|
|
server := helpers.MakeTestServer(http.StatusBadRequest, nil) |
49
|
|
|
client := New(WithBaseURL(server.URL)) |
50
|
|
|
|
51
|
|
|
// Act |
52
|
|
|
_, response, err := client.APIUser.CreateAPIUser(context.Background(), "errorID", "string") |
53
|
|
|
|
54
|
|
|
// Assert |
55
|
|
|
assert.NotNil(t, err) |
56
|
|
|
|
57
|
|
|
assert.Equal(t, http.StatusBadRequest, response.HTTPResponse.StatusCode) |
58
|
|
|
|
59
|
|
|
// Teardown |
60
|
|
|
server.Close() |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
func TestApiUserService_CreateAPIKey(t *testing.T) { |
64
|
|
|
// Setup |
65
|
|
|
t.Parallel() |
66
|
|
|
|
67
|
|
|
// Arrange |
68
|
|
|
request := http.Request{} |
69
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, stubs.APIUserCreateAPIKey(), &request) |
70
|
|
|
key := "subscriptionKey" |
71
|
|
|
client := New(WithBaseURL(server.URL), WithSubscriptionKey(key)) |
72
|
|
|
userID := uuid.NewString() |
73
|
|
|
|
74
|
|
|
// Act |
75
|
|
|
apiKey, response, err := client.APIUser.CreateAPIKey(context.Background(), userID) |
76
|
|
|
|
77
|
|
|
// Assert |
78
|
|
|
assert.Nil(t, err) |
79
|
|
|
|
80
|
|
|
assert.True(t, strings.Contains(request.URL.String(), userID)) |
81
|
|
|
assert.Equal(t, key, request.Header.Get(subscriptionKeyHeaderKey)) |
82
|
|
|
assert.Equal(t, "f1db798c98df4bcf83b538175893bbf0", apiKey) |
83
|
|
|
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode) |
84
|
|
|
|
85
|
|
|
// Teardown |
86
|
|
|
server.Close() |
87
|
|
|
} |
88
|
|
|
|