generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapiuser_service_test.go
121 lines (90 loc) · 3.23 KB
/
apiuser_service_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
116
117
118
119
120
121
package mtnmomo
import (
"context"
"net/http"
"strings"
"testing"
"github.com/NdoleStudio/mtnmomo-go/internal/stubs"
"github.com/NdoleStudio/mtnmomo-go/internal/helpers"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestApiUserService_Create(t *testing.T) {
// Setup
t.Parallel()
// Arrange
requests := make([]*http.Request, 0)
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, [][]byte{nil}, &requests)
client := New(WithBaseURL(server.URL), WithSubscriptionKey(testSubscriptionKey))
userID := uuid.NewString()
// Act
apiUser, response, err := client.APIUser.CreateAPIUser(context.Background(), userID, "string")
// Assert
assert.Nil(t, err)
assert.GreaterOrEqual(t, 1, len(requests))
request := requests[0]
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey))
assert.Equal(t, userID, request.Header.Get("X-Reference-Id"))
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode)
assert.Equal(t, userID, apiUser)
// Teardown
server.Close()
}
func TestApiUserService_CreateBadRequest(t *testing.T) {
// Setup
t.Parallel()
// Arrange
server := helpers.MakeTestServer(http.StatusBadRequest, nil)
client := New(WithBaseURL(server.URL))
// Act
_, response, err := client.APIUser.CreateAPIUser(context.Background(), "errorID", "string")
// Assert
assert.NotNil(t, err)
assert.Equal(t, http.StatusBadRequest, response.HTTPResponse.StatusCode)
// Teardown
server.Close()
}
func TestApiUserService_CreateAPIKey(t *testing.T) {
// Setup
t.Parallel()
// Arrange
requests := make([]*http.Request, 0)
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, [][]byte{stubs.APIUserCreateAPIKey()}, &requests)
client := New(WithBaseURL(server.URL), WithSubscriptionKey(testSubscriptionKey))
userID := uuid.NewString()
// Act
apiKey, response, err := client.APIUser.CreateAPIKey(context.Background(), userID)
// Assert
assert.Nil(t, err)
assert.GreaterOrEqual(t, 1, len(requests))
request := requests[0]
assert.True(t, strings.Contains(request.URL.String(), userID))
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey))
assert.Equal(t, "f1db798c98df4bcf83b538175893bbf0", apiKey)
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode)
// Teardown
server.Close()
}
func TestApiUserService_Get(t *testing.T) {
// Setup
t.Parallel()
// Arrange
requests := make([]*http.Request, 0)
server := helpers.MakeRequestCapturingTestServer(http.StatusCreated, [][]byte{stubs.APIUserGet()}, &requests)
client := New(WithBaseURL(server.URL), WithSubscriptionKey(testSubscriptionKey))
userID := uuid.NewString()
// Act
apiUser, response, err := client.APIUser.Get(context.Background(), userID)
// Assert
assert.Nil(t, err)
assert.GreaterOrEqual(t, 1, len(requests))
request := requests[0]
assert.True(t, strings.Contains(request.URL.String(), userID))
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey))
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode)
assert.Equal(t, userID, apiUser.UserID)
assert.Equal(t, "sandbox", apiUser.TargetEnvironment)
assert.Equal(t, "string", apiUser.ProviderCallbackHost)
// Teardown
server.Close()
}