forked from customerio/go-customerio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
91 lines (73 loc) · 2.31 KB
/
auth_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
package customerio
import (
"fmt"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
// TestClient_FindRegion will test the method FindRegion()
func TestClient_FindRegion(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("successful response", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
mockFindRegion(http.StatusOK)
var regionInfo *RegionInfo
regionInfo, err = client.FindRegion()
assert.NoError(t, err)
assert.NotNil(t, regionInfo)
assert.Equal(t, "https://track.customer.io", regionInfo.URL)
assert.Equal(t, "us", regionInfo.DataCenter)
assert.Equal(t, uint64(3), regionInfo.EnvironmentID)
})
t.Run("customerIo error", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
mockFindRegion(http.StatusUnprocessableEntity)
var regionInfo *RegionInfo
regionInfo, err = client.FindRegion()
assert.Error(t, err)
assert.Nil(t, regionInfo)
})
}
// TestClient_TestAuth will test the method TestAuth()
func TestClient_TestAuth(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("successful response", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
mockTestAuth(http.StatusOK)
err = client.TestAuth()
assert.NoError(t, err)
})
t.Run("customerIo error", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
mockTestAuth(http.StatusUnauthorized)
err = client.TestAuth()
assert.Error(t, err)
})
}
// mockFindRegion is used for mocking the response
func mockFindRegion(statusCode int) {
httpmock.Reset()
httpmock.RegisterResponder(http.MethodGet, fmt.Sprintf("%sapi/v1/accounts/region", testTrackingAPIURL),
httpmock.NewStringResponder(
statusCode, `{"url": "https://track.customer.io","data_center": "us","environment_id": 3}`,
),
)
}
// mockTestAuth is used for mocking the response
func mockTestAuth(statusCode int) {
httpmock.Reset()
httpmock.RegisterResponder(http.MethodGet, fmt.Sprintf("%sauth", testTrackingAPIURL),
httpmock.NewStringResponder(
statusCode, `{"meta": {"message": "Nice credentials."}}`,
),
)
}