forked from nicklaw5/helix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension_pubsub_test.go
144 lines (127 loc) · 3.5 KB
/
extension_pubsub_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
package helix
import (
"net/http"
"strings"
"testing"
)
func TestExtensionFormPubSubPerms(t *testing.T) {
t.Parallel()
c := newMockClient(&Options{}, newMockHandler(http.StatusOK, "", nil))
perms := c.FormGlobalSendPubSubPermissions()
if len(perms.Send) == 0 {
t.Errorf(
"invalid pubsub permission type, expected 1 got %d",
len(perms.Send),
)
}
if len(perms.Send) > 1 && perms.Send[0] != ExtensionPubSubGlobalPublish {
t.Errorf(
"invalid pubsub permission type, expected %s got %s",
ExtensionPubSubGlobalPublish.String(),
perms.Send[0],
)
}
perms = c.FormBroadcastSendPubSubPermissions()
if len(perms.Send) == 0 {
t.Errorf(
"invalid pubsub permission type, expected 1 got %d",
len(perms.Send),
)
}
if len(perms.Send) > 1 && perms.Send[0] != ExtensionPubSubBroadcastPublish {
t.Errorf(
"invalid pubsub permission type, expected %s got %s",
ExtensionPubSubBroadcastPublish.String(),
perms.Send[0],
)
}
userId := "100249558"
perms = c.FormWhisperSendPubSubPermissions(userId)
if len(perms.Send) == 0 {
t.Errorf(
"invalid pubsub permission type, expected 1 got %d",
len(perms.Send),
)
}
if len(perms.Send) > 1 && strings.Contains(perms.Send[0].String(), ("whisper-"+userId)) {
t.Errorf(
"invalid whisper pubsub permission type, does not contain whisper-%s",
userId,
)
}
perms = c.FormGenericPubSubPermissions()
if len(perms.Send) == 0 {
t.Errorf(
"invalid pubsub permission type, expected 1 got %d",
len(perms.Send),
)
}
if len(perms.Send) > 1 && perms.Send[0] != ExtensionPubSubGenericPublish {
t.Errorf(
"invalid pubsub permission type, expected %s got %s",
ExtensionPubSubBroadcastPublish.String(),
perms.Send[0],
)
}
}
func TestExtensionSendPubSubMessage(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
params *ExtensionSendPubSubMessageParams
respBody string
validationErr string
}{
{
http.StatusUnauthorized,
&Options{ClientID: "my-client-id"},
&ExtensionSendPubSubMessageParams{},
`{"error":"Unauthorized","status":401,"message":"JWT token is missing"}`,
"",
},
{
http.StatusNoContent,
&Options{
ClientID: "my-client-id",
ExtensionOpts: ExtensionOptions{
Secret: "my-ext-secret",
OwnerUserID: "ext-owner-id",
},
},
&ExtensionSendPubSubMessageParams{
BroadcasterID: "100249558",
Message: "{}",
Target: []ExtensionPubSubPublishType{ExtensionPubSubBroadcastPublish},
},
"",
"",
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.SendExtensionPubSubMessage(testCase.params)
if err != nil {
if err.Error() == testCase.validationErr {
continue
}
t.Error(err)
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
if resp.StatusCode == http.StatusUnauthorized {
if resp.Error != "Unauthorized" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Unauthorized", resp.Error)
}
if resp.ErrorStatus != http.StatusUnauthorized {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusUnauthorized, resp.ErrorStatus)
}
expectedErrMsg := "JWT token is missing"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
}
}