-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_properties_test.go
More file actions
205 lines (188 loc) · 5.41 KB
/
Copy pathcontact_properties_test.go
File metadata and controls
205 lines (188 loc) · 5.41 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package loops
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestListContactProperties(t *testing.T) {
tests := []struct {
name string
customOnly bool
statusCode int
body string
wantAPIErr *APIError
wantErrMsg string
wantCount int
wantQuery string
}{
{
name: "success all",
customOnly: false,
statusCode: http.StatusOK,
body: `[{"key":"firstName","label":"First name","type":"string"},{"key":"score","label":"Score","type":"number"}]`,
wantCount: 2,
},
{
name: "success custom",
customOnly: true,
statusCode: http.StatusOK,
body: `[{"key":"score","label":"Score","type":"number"}]`,
wantCount: 1,
wantQuery: "list=custom",
},
{
name: "empty",
customOnly: false,
statusCode: http.StatusOK,
body: `[]`,
wantCount: 0,
},
{
name: "unauthorized",
customOnly: false,
statusCode: http.StatusUnauthorized,
body: `{"error":"Invalid API key"}`,
wantAPIErr: &APIError{StatusCode: http.StatusUnauthorized, Message: "Invalid API key"},
},
{
name: "invalid json",
customOnly: false,
statusCode: http.StatusOK,
body: `not json`,
wantErrMsg: "failed to decode response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotQuery string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotQuery = r.URL.RawQuery
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
props, err := client.ListContactProperties(tt.customOnly)
if tt.wantQuery != "" && gotQuery != tt.wantQuery {
t.Errorf("query = %q, want %q", gotQuery, tt.wantQuery)
}
if tt.wantAPIErr != nil {
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected *APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != tt.wantAPIErr.StatusCode {
t.Errorf("StatusCode = %d, want %d", apiErr.StatusCode, tt.wantAPIErr.StatusCode)
}
if tt.wantAPIErr.Message != "" && apiErr.Message != tt.wantAPIErr.Message {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantAPIErr.Message)
}
return
}
if tt.wantErrMsg != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErrMsg)
}
if !strings.Contains(err.Error(), tt.wantErrMsg) {
t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErrMsg)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(props) != tt.wantCount {
t.Errorf("len(props) = %d, want %d", len(props), tt.wantCount)
}
})
}
}
func TestCreateContactProperty(t *testing.T) {
tests := []struct {
name string
statusCode int
body string
wantAPIErr *APIError
wantBody map[string]string
}{
{
name: "success",
statusCode: http.StatusOK,
body: `{"success":true}`,
wantBody: map[string]string{"name": "age", "type": "number"},
},
{
name: "failure",
statusCode: http.StatusBadRequest,
body: `{"message":"Property already exists"}`,
wantAPIErr: &APIError{StatusCode: http.StatusBadRequest, Message: "Property already exists"},
},
{
name: "unauthorized",
statusCode: http.StatusUnauthorized,
body: `{"error":"Invalid API key"}`,
wantAPIErr: &APIError{StatusCode: http.StatusUnauthorized, Message: "Invalid API key"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotBody map[string]string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewDecoder(r.Body).Decode(&gotBody)
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
err := client.CreateContactProperty("age", "number")
if tt.wantBody != nil {
for k, v := range tt.wantBody {
if gotBody[k] != v {
t.Errorf("body[%q] = %q, want %q", k, gotBody[k], v)
}
}
}
if tt.wantAPIErr != nil {
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected *APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != tt.wantAPIErr.StatusCode {
t.Errorf("StatusCode = %d, want %d", apiErr.StatusCode, tt.wantAPIErr.StatusCode)
}
if tt.wantAPIErr.Message != "" && apiErr.Message != tt.wantAPIErr.Message {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantAPIErr.Message)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
}
func TestListContactProperties_ResponseData(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`[{"key":"favoriteColor","label":"Favorite color","type":"string"}]`))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
props, err := client.ListContactProperties(false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
p := props[0]
if p.Key != "favoriteColor" {
t.Errorf("Key = %q, want %q", p.Key, "favoriteColor")
}
if p.Label != "Favorite color" {
t.Errorf("Label = %q, want %q", p.Label, "Favorite color")
}
if p.Type != "string" {
t.Errorf("Type = %q, want %q", p.Type, "string")
}
}