|
1
|
|
|
package client |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/json" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
"testing" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/NdoleStudio/plunk-go/internal/helpers" |
|
10
|
|
|
"github.com/NdoleStudio/plunk-go/internal/stubs" |
|
11
|
|
|
"github.com/stretchr/testify/assert" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
func TestContactService_Create(t *testing.T) { |
|
15
|
|
|
// Arrange |
|
16
|
|
|
apiKey := "test-api-key" |
|
17
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.ContactsCreateResponse()) |
|
18
|
|
|
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey)) |
|
19
|
|
|
|
|
20
|
|
|
// Act |
|
21
|
|
|
contact, response, err := client.Contacts.Create(context.Background(), &ContactCreateRequest{ |
|
22
|
|
|
Email: "[email protected]", |
|
23
|
|
|
Subscribed: true, |
|
24
|
|
|
Data: map[string]any{ |
|
25
|
|
|
"firstName": "John", |
|
26
|
|
|
"lastName": "Doe", |
|
27
|
|
|
"plan": "premium", |
|
28
|
|
|
}, |
|
29
|
|
|
}) |
|
30
|
|
|
|
|
31
|
|
|
// Assert |
|
32
|
|
|
assert.Nil(t, err) |
|
33
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
34
|
|
|
jsonContent, _ := json.Marshal(contact) |
|
35
|
|
|
assert.JSONEq(t, string(stubs.ContactsCreateResponse()), string(jsonContent)) |
|
36
|
|
|
|
|
37
|
|
|
// Teardown |
|
38
|
|
|
server.Close() |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
func TestContactService_CreateWithError(t *testing.T) { |
|
42
|
|
|
// Arrange |
|
43
|
|
|
apiKey := "test-api-key" |
|
44
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
|
45
|
|
|
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey)) |
|
46
|
|
|
|
|
47
|
|
|
// Act |
|
48
|
|
|
_, response, err := client.Contacts.Create(context.Background(), &ContactCreateRequest{ |
|
49
|
|
|
Email: "[email protected]", |
|
50
|
|
|
Subscribed: true, |
|
51
|
|
|
Data: map[string]any{ |
|
52
|
|
|
"firstName": "John", |
|
53
|
|
|
"lastName": "Doe", |
|
54
|
|
|
"plan": "premium", |
|
55
|
|
|
}, |
|
56
|
|
|
}) |
|
57
|
|
|
|
|
58
|
|
|
// Assert |
|
59
|
|
|
assert.NotNil(t, err) |
|
60
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
|
61
|
|
|
|
|
62
|
|
|
// Teardown |
|
63
|
|
|
server.Close() |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
func TestContactService_Delete(t *testing.T) { |
|
67
|
|
|
// Arrange |
|
68
|
|
|
apiKey := "test-api-key" |
|
69
|
|
|
server := helpers.MakeTestServer(http.StatusNoContent, nil) |
|
70
|
|
|
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey)) |
|
71
|
|
|
|
|
72
|
|
|
// Act |
|
73
|
|
|
response, err := client.Contacts.Delete(context.Background(), "contact-id") |
|
74
|
|
|
|
|
75
|
|
|
// Assert |
|
76
|
|
|
assert.Nil(t, err) |
|
77
|
|
|
assert.Equal(t, http.StatusNoContent, response.HTTPResponse.StatusCode) |
|
78
|
|
|
|
|
79
|
|
|
// Teardown |
|
80
|
|
|
server.Close() |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
func TestContactService_DeleteWithError(t *testing.T) { |
|
84
|
|
|
// Arrange |
|
85
|
|
|
apiKey := "test-api-key" |
|
86
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
|
87
|
|
|
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey)) |
|
88
|
|
|
|
|
89
|
|
|
// Act |
|
90
|
|
|
response, err := client.Contacts.Delete(context.Background(), "contact-id") |
|
91
|
|
|
|
|
92
|
|
|
// Assert |
|
93
|
|
|
assert.NotNil(t, err) |
|
94
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
|
95
|
|
|
|
|
96
|
|
|
// Teardown |
|
97
|
|
|
server.Close() |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
func TestContactService_List(t *testing.T) { |
|
101
|
|
|
// Arrange |
|
102
|
|
|
apiKey := "test-api-key" |
|
103
|
|
|
request := new(http.Request) |
|
104
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.ContactsListResponse(), request) |
|
105
|
|
|
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey)) |
|
106
|
|
|
params := map[string]string{ |
|
107
|
|
|
"limit": "10", |
|
108
|
|
|
"cursor": "abc123", |
|
109
|
|
|
"subscribed": "true", |
|
110
|
|
|
"search": "[email protected]", |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Act |
|
114
|
|
|
contact, response, err := client.Contacts.List(context.Background(), params) |
|
115
|
|
|
|
|
116
|
|
|
// Assert |
|
117
|
|
|
assert.Nil(t, err) |
|
118
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
119
|
|
|
jsonContent, _ := json.Marshal(contact) |
|
120
|
|
|
assert.JSONEq(t, string(stubs.ContactsListResponse()), string(jsonContent)) |
|
121
|
|
|
|
|
122
|
|
|
for key, value := range params { |
|
123
|
|
|
assert.Equal(t, value, request.URL.Query().Get(key)) |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Teardown |
|
127
|
|
|
server.Close() |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
func TestContactService_ListWithError(t *testing.T) { |
|
131
|
|
|
// Arrange |
|
132
|
|
|
apiKey := "test-api-key" |
|
133
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
|
134
|
|
|
client := New(WithBaseURL(server.URL), WithSecretKey(apiKey)) |
|
135
|
|
|
|
|
136
|
|
|
// Act |
|
137
|
|
|
_, response, err := client.Contacts.List(context.Background(), map[string]string{}) |
|
138
|
|
|
|
|
139
|
|
|
// Assert |
|
140
|
|
|
assert.NotNil(t, err) |
|
141
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
|
142
|
|
|
|
|
143
|
|
|
// Teardown |
|
144
|
|
|
server.Close() |
|
145
|
|
|
} |
|
146
|
|
|
|