-
Notifications
You must be signed in to change notification settings - Fork 88
/
drops_test.go
227 lines (199 loc) · 6.81 KB
/
drops_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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package helix
import (
"context"
"net/http"
"strings"
"testing"
)
func TestGetDropsEntitlements(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
gameID string
respBody string
expectedErrMsg string
}{
{
http.StatusUnauthorized,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "invalid-app-access-token", // invalid app access token
},
"1337",
`{"error":"Unauthorized","status":401,"message":"Must provide valid app token."}`,
"Must provide valid app token.",
},
{
http.StatusForbidden,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "valid-app-access-token", // valid app access token
},
"1337",
`{"error":"Forbidden","status":403,"message":"game not managed by this organization."}`,
"game not managed by this organization.",
},
{
http.StatusOK,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "valid-app-access-token",
},
"33214",
`{"data": [{ "id": "fb78259e-fb81-4d1b-8333-34a06ffc24c0", "benefit_id": "74c52265-e214-48a6-91b9-23b6014e8041", "timestamp": "2019-01-28T04:17:53.325Z", "user_id": "25009227", "game_id": "33214", "fulfillment_status": "CLAIMED", "updated_at": "2019-01-28T04:17:53.325Z" }, { "id": "862750a5-265e-4ab6-9f0a-c64df3d54dd0", "benefit_id": "74c52265-e214-48a6-91b9-23b6014e8041", "timestamp": "2019-01-28T04:16:53.325Z", "user_id": "25009227", "game_id": "33214", "fulfillment_status": "CLAIMED", "updated_at": "2019-01-28T04:17:53.325Z" }, { "id": "d8879baa-3966-4d10-8856-15fdd62cce02", "benefit_id": "cdfdc5c3-65a2-43bc-8767-fde06eb4ab2c", "timestamp": "2019-01-28T04:15:53.325Z", "user_id": "25009227", "game_id": "33214", "fulfillment_status": "FULFILLED", "updated_at": "2019-01-28T04:18:53.325Z" }], "pagination": { "cursor": "eyJiIjpudW..." } }`,
"",
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.GetDropsEntitlements(&GetDropEntitlementsParams{GameID: testCase.gameID})
if err != nil {
t.Error(err)
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
// Test error cases
if resp.StatusCode != http.StatusOK {
if resp.ErrorStatus != testCase.statusCode {
t.Errorf("expected error status to be \"%d\", got \"%d\"", testCase.statusCode, resp.ErrorStatus)
}
if resp.ErrorMessage != testCase.expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", testCase.expectedErrMsg, resp.ErrorMessage)
}
continue
}
// Test success case
if len(resp.Data.Entitlements) < 1 {
t.Errorf("Expected the number of entitlements to be a positive number")
}
if !strings.EqualFold(resp.Data.Entitlements[0].GameID, testCase.gameID) {
t.Errorf("Expected the Entitlement's GameID to be the same as the requested GameID - wanted \"%s\", got \"%s\"",
resp.Data.Entitlements[0].GameID, testCase.gameID)
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.GetDropsEntitlements(&GetDropEntitlementsParams{})
if err == nil {
t.Error("expected error but got nil")
}
if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
t.Error("expected error does match return error")
}
}
func TestUpdateDropsEntitlements(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
requestedIDs []string
fulfillmentStatus string
respBody string
expectedErrMsg string
}{
{
http.StatusUnauthorized,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "invalid-app-access-token", // invalid app access token
},
[]string{},
"FULFILLED",
`{"error":"Unauthorized","status":401,"message":"Must provide valid app token."}`,
"Must provide valid app token.",
},
{
http.StatusOK,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "valid-app-access-token",
},
[]string{
"fb78259e-fb81-4d1b-8333-34a06ffc24c0",
"862750a5-265e-4ab6-9f0a-c64df3d54dd0",
"d8879baa-3966-4d10-8856-15fdd62cce02",
"9a290126-7e3b-4f66-a9ae-551537893b65",
},
"FULFILLED",
`{ "data": [{ "status": "SUCCESS", "ids": ["fb78259e-fb81-4d1b-8333-34a06ffc24c0", "862750a5-265e-4ab6-9f0a-c64df3d54dd0"] }, { "status": "UNAUTHORIZED", "ids": ["d8879baa-3966-4d10-8856-15fdd62cce02"] }, { "status": "UPDATE_FAILED", "ids": ["9a290126-7e3b-4f66-a9ae-551537893b65"] }] }`,
"",
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.UpdateDropsEntitlements(&UpdateDropsEntitlementsParams{
EntitlementIDs: testCase.requestedIDs,
FulfillmentStatus: testCase.fulfillmentStatus,
})
if err != nil {
t.Error(err)
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
// Test error cases
if resp.StatusCode != http.StatusOK {
if resp.ErrorStatus != testCase.statusCode {
t.Errorf("expected error status to be \"%d\", got \"%d\"", testCase.statusCode, resp.ErrorStatus)
}
if resp.ErrorMessage != testCase.expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", testCase.expectedErrMsg, resp.ErrorMessage)
}
continue
}
// Test success case
if len(resp.Data.EntitlementSets) < 1 {
t.Errorf("Expected the number of entitlements sets to be a positive number")
}
// Check all requested ids are in the response
totatReponseIDs := 0
for _, es := range resp.Data.EntitlementSets {
for _, id := range es.IDs {
if !stringInSlice(id, testCase.requestedIDs) {
t.Errorf("Expected entitlement ID %v to be in requested IDs", id)
}
}
totatReponseIDs += len(es.IDs)
}
if totatReponseIDs != len(testCase.requestedIDs) {
t.Errorf("Expected number of entitlement IDs in response to match number of entitlement IDs requested")
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.UpdateDropsEntitlements(&UpdateDropsEntitlementsParams{})
if err == nil {
t.Error("expected error but got nil")
}
if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
t.Error("expected error does match return error")
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}