forked from nicklaw5/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrops_test.go
106 lines (92 loc) · 3.21 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
package helix
import (
"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", // invalid 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" }, { "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" }, { "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" }] }`,
"",
},
}
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,
}
_, 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")
}
}