-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmoderation_test.go
More file actions
195 lines (158 loc) · 6.41 KB
/
moderation_test.go
File metadata and controls
195 lines (158 loc) · 6.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
package gokick_test
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/scorfly/gokick"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBanUserError(t *testing.T) {
t.Run("on new request", func(t *testing.T) {
kickClient, err := gokick.NewClient(&gokick.ClientOptions{UserAccessToken: "access-token"})
require.NoError(t, err)
var ctx context.Context
_, err = kickClient.BanUser(ctx, 1234, 345, nil, nil)
require.EqualError(t, err, "failed to create request: net/http: nil Context")
})
t.Run("timeout", func(t *testing.T) {
kickClient := setupTimeoutMockClient(t)
_, err := kickClient.BanUser(context.Background(), 1234, 345, nil, nil)
require.EqualError(t, err, `failed to make request: Post "https://api.kick.com/public/v1/moderation/bans": context deadline exceeded `+
`(Client.Timeout exceeded while awaiting headers)`)
})
t.Run("unmarshal error response", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `117`)
})
_, err := kickClient.BanUser(context.Background(), 1234, 345, nil, nil)
require.EqualError(t, err, `failed to unmarshal error response (KICK status code: 500 and body "117"): json: cannot unmarshal `+
`number into Go value of type gokick.errorResponse`)
})
t.Run("unmarshal token response", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "117")
})
_, err := kickClient.BanUser(context.Background(), 1234, 345, nil, nil)
assert.EqualError(t, err, `failed to unmarshal response body (KICK status code 200 and body "117"): json: cannot unmarshal `+
`number into Go value of type gokick.successResponse[github.com/scorfly/gokick.BanUserResponse]`)
})
t.Run("reader failure", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "10")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "")
})
_, err := kickClient.BanUser(context.Background(), 1234, 345, nil, nil)
assert.EqualError(t, err, `failed to read response body (KICK status code 500): unexpected EOF`)
})
t.Run("with internal server error", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"message":"internal server error", "data":null}`)
})
_, err := kickClient.BanUser(context.Background(), 1234, 345, nil, nil)
var kickError gokick.Error
require.ErrorAs(t, err, &kickError)
assert.Equal(t, http.StatusInternalServerError, kickError.Code())
assert.Equal(t, "internal server error", kickError.Message())
})
}
func TestBanUserSuccess(t *testing.T) {
testCases := map[string]struct {
reason *string
duration *int
}{
"default": {},
"with reason": {
reason: stringPtr("ban reason"),
},
"with duration": {
duration: intPtr(123),
},
"with reason and duration": {
reason: stringPtr("ban reason"),
duration: intPtr(123),
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{
"message":"success",
"data":{}
}`)
})
_, err := kickClient.BanUser(context.Background(), 1234, 345, tc.duration, tc.reason)
require.NoError(t, err)
})
}
}
func TestUnbanUserError(t *testing.T) {
t.Run("on new request", func(t *testing.T) {
kickClient, err := gokick.NewClient(&gokick.ClientOptions{UserAccessToken: "access-token"})
require.NoError(t, err)
var ctx context.Context
_, err = kickClient.UnbanUser(ctx, 1234, 345)
require.EqualError(t, err, "failed to create request: net/http: nil Context")
})
t.Run("timeout", func(t *testing.T) {
kickClient := setupTimeoutMockClient(t)
_, err := kickClient.UnbanUser(context.Background(), 1234, 345)
require.EqualError(t, err, `failed to make request: Delete "https://api.kick.com/public/v1/moderation/bans": context deadline exceeded `+
`(Client.Timeout exceeded while awaiting headers)`)
})
t.Run("unmarshal error response", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `117`)
})
_, err := kickClient.UnbanUser(context.Background(), 1234, 345)
require.EqualError(t, err, `failed to unmarshal error response (KICK status code: 500 and body "117"): json: cannot unmarshal `+
`number into Go value of type gokick.errorResponse`)
})
t.Run("unmarshal token response", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "117")
})
_, err := kickClient.UnbanUser(context.Background(), 1234, 345)
assert.EqualError(t, err, `failed to unmarshal response body (KICK status code 200 and body "117"): json: cannot unmarshal `+
`number into Go value of type gokick.successResponse[github.com/scorfly/gokick.BanUserResponse]`)
})
t.Run("reader failure", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "10")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "")
})
_, err := kickClient.UnbanUser(context.Background(), 1234, 345)
assert.EqualError(t, err, `failed to read response body (KICK status code 500): unexpected EOF`)
})
t.Run("with internal server error", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"message":"internal server error", "data":null}`)
})
_, err := kickClient.UnbanUser(context.Background(), 1234, 345)
var kickError gokick.Error
require.ErrorAs(t, err, &kickError)
assert.Equal(t, http.StatusInternalServerError, kickError.Code())
assert.Equal(t, "internal server error", kickError.Message())
})
}
func TestUnbanUserSuccess(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{
"message":"success",
"data":{}
}`)
})
_, err := kickClient.UnbanUser(context.Background(), 1234, 345)
require.NoError(t, err)
}