forked from nicklaw5/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entitlement_grants_test.go
124 lines (109 loc) · 3.73 KB
/
entitlement_grants_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
package helix
import (
"net/http"
"strings"
"testing"
)
func TestCreateEntitlementsUploadURL(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
manifestID string
entitlementType string
respBody string
expectedErrMsg string
}{
{
http.StatusUnauthorized,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "invalid-app-access-token", // invalid app access token
},
"my-manifest-id",
"bulk_drops_grant",
`{"error":"Unauthorized","status":401,"message":"Must provide valid app token."}`,
"Must provide valid app token.",
},
{
http.StatusBadRequest,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "valid-app-access-token",
},
"", // invalid manifest id
"bulk_drops_grant",
`{"error":"Bad Request","status":400,"message":"Missing required parameter \"manifest_id\""}`,
"Missing required parameter \"manifest_id\"",
},
{
http.StatusBadRequest,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "valid-app-access-token",
},
"my-manifest-id",
"invalid_grant", // invalid entitlement type
`{"error":"Bad Request","status":400,"message":"The parameter \"type\" was malformed: value must be one of \"bulk_drops_grant\""}`,
"The parameter \"type\" was malformed: value must be one of \"bulk_drops_grant\"",
},
{
http.StatusOK,
&Options{
ClientID: "valid-client-id",
AppAccessToken: "valid-app-access-token",
},
"my-manifest-id",
"bulk_drops_grant",
`{"data":[{"url": "https://twitch-ds-vhs-drops-granted-uploads-us-west-2-prod.s3-us-west-2.amazonaws.com/valid-client-id/<time>-my-manifest-id.json?X-Amz-Algorithm=AWS4-HMAC-SHA256\u0026X-Amz-Credential=<credential>%2Fus-west-2%2Fs3%2Faws4_request\u0026X-Amz-Date=<date>\u0026X-Amz-Expires=900\u0026X-Amz-Security-Token=<token>\u0026X-Amz-SignedHeaders=host\u0026X-Amz-Signature=<signature>"}]}`,
"",
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.CreateEntitlementsUploadURL(testCase.manifestID, testCase.entitlementType)
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.URLs[0].URL) < 1 {
t.Errorf("Expected URL not to be an empty string, got \"%s\"", resp.Data.URLs[0].URL)
}
if !strings.Contains(resp.Data.URLs[0].URL, testCase.options.ClientID) {
t.Errorf("Expected URL to contain client id \"%s\", got \"%s\"", testCase.options.ClientID, resp.Data.URLs[0].URL)
}
if !strings.Contains(resp.Data.URLs[0].URL, testCase.manifestID) {
t.Errorf("Expected URL to contain manifest id \"%s\", got \"%s\"", testCase.manifestID, resp.Data.URLs[0].URL)
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
}
_, err := c.CreateEntitlementsUploadURL("manifestID", "entitlementType")
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")
}
}