-
Notifications
You must be signed in to change notification settings - Fork 2
/
dummy_test.go
294 lines (247 loc) · 7.77 KB
/
dummy_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package radarr
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"strings"
)
type hTTPClient struct{}
var (
dummyHTTPClient *hTTPClient = &hTTPClient{}
dummyURL string = "https://radarr.dummy"
dummyAPIKey string = "dummy-api-key"
)
// Read the given .json file
func helperLoadBytes(name string) []byte {
path := filepath.Join("testdata", name+".json")
bytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return bytes
}
func dummyMoviesResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("multiple_movies"))),
}
}
func dummyMovieResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("movie"))),
}
}
func dummyUpcomingWithBothFilterResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("one_movie_in_array"))),
}
}
func dummyEmptyListResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBufferString(`[]`))}
}
func dummyNotFoundResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusNotFound,
Status: http.StatusText(http.StatusNotFound),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("not_found"))),
}
}
func dummyHistoryResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("history"))),
}
}
func dummyInvalidJSONResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBufferString("foo"))}
}
func dummyEmptyJSONObject() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`))}
}
func invalidIMDBIDResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Status: http.StatusText(http.StatusInternalServerError),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("invalid_imdb_id"))),
}
}
func invalidTMDBIDResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Status: http.StatusText(http.StatusInternalServerError),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("invalid_tmdb_id"))),
}
}
func dummySystemStatusResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("status"))),
}
}
func dummyDiskspaceResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("diskspace"))),
}
}
func dummyExcludedMovies() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBuffer(helperLoadBytes("excluded_movie"))),
}
}
var dummyStartDate string = "2019-11-19T23:00:00Z"
var dummyEndDate string = "2019-11-20T23:00:00Z"
// Do mocked http client Do function
func (c *hTTPClient) Do(req *http.Request) (*http.Response, error) {
// Query params
params := req.URL.Query()
if req.Method == http.MethodGet {
// GET /history
if strings.Contains(req.URL.String(), fmt.Sprintf("%s/api%s", dummyURL, "/history")) {
page := params.Get("page")
pageSize := params.Get("pageSize")
// Return one record on page 1
if page == "1" && pageSize == "50" {
return dummyHistoryResponse(), nil
}
// Return bad JSON for page 3
if page == "3" && pageSize == "50" {
return dummyInvalidJSONResponse(), nil
}
// Return error for page 4
if page == "4" && pageSize == "50" {
return nil, errors.New("Oooops")
}
}
// GET /calendar
if strings.Contains(req.URL.String(), fmt.Sprintf("%s/api%s", dummyURL, "/calendar")) {
start := params.Get("start")
end := params.Get("end")
// Upcoming movies without filters. Return 0 movies
if start == "" && end == "" {
return dummyEmptyListResponse(), nil
}
// Upcoming movies with start filter. Returns 2 movies
if start == dummyStartDate && end == "" {
return dummyMoviesResponse(), nil
}
// Upcoming movies with end filter. Returns 0 movies
if start == "" && end == dummyEndDate {
return dummyEmptyListResponse(), nil
}
// Upcoming movies with start filter and end filter. Return 1 movies
if start == dummyStartDate && end == dummyEndDate {
return dummyUpcomingWithBothFilterResponse(), nil
}
}
// GET /movie/lookup/imdb
if strings.Contains(req.URL.String(), fmt.Sprintf("%s/api%s", dummyURL, "/movie/lookup/imdb")) {
imdbID := params.Get("imdbId")
switch imdbID {
case "tt3778644":
return dummyMovieResponse(), nil
case "1":
return invalidIMDBIDResponse(), nil
}
}
// GET /movie/lookup/tmdb
if strings.Contains(req.URL.String(), fmt.Sprintf("%s/api%s", dummyURL, "/movie/lookup/tmdb")) {
tmdbID := params.Get("tmdbId")
switch tmdbID {
case "348350":
return dummyMovieResponse(), nil
case "1":
return invalidTMDBIDResponse(), nil
}
}
// GET /movie/lookup
if strings.Contains(req.URL.String(), fmt.Sprintf("%s/api%s", dummyURL, "/movie/lookup")) {
term := params.Get("term")
switch term {
case "star wars":
return dummyMoviesResponse(), nil
case "this film does not exist either":
return dummyEmptyListResponse(), nil
}
}
// Else, return 404
return dummyNotFoundResponse(), nil
}
if req.Method == http.MethodDelete {
// Delete movie
if strings.Contains(req.URL.String(), fmt.Sprintf("%s/api%s/%d", dummyURL, "/movie", 217)) {
deleteFiles := params.Get("deleteFiles")
addExclusion := params.Get("addExclusion")
// without parameters
if addExclusion == "" && deleteFiles == "" {
return dummyEmptyJSONObject(), nil
}
// With all false
if addExclusion == "false" && deleteFiles == "false" {
return dummyEmptyJSONObject(), nil
}
// With all true
if addExclusion == "true" && deleteFiles == "true" {
return dummyEmptyJSONObject(), nil
}
// With addExclusion=true and deleteFiles=false
if addExclusion == "true" && deleteFiles == "false" {
return dummyEmptyJSONObject(), nil
}
// With addExclusion=false and deleteFiles=true
if addExclusion == "false" && deleteFiles == "true" {
return dummyEmptyJSONObject(), nil
}
}
return dummyNotFoundResponse(), nil
}
// Bad method, return error
return nil, &url.Error{
Op: req.Method,
URL: req.URL.String(),
Err: errors.New("Ooops"),
}
}
// Get Mock .Get() http client method
func (c *hTTPClient) Get(targetURL string) (resp *http.Response, err error) {
switch targetURL {
case fmt.Sprintf("%s/api%s/%d", dummyURL, "/movie", 217):
// Get one movie
return dummyMovieResponse(), nil
case fmt.Sprintf("%s/api%s", dummyURL, "/movie"):
// List of movies
return dummyMoviesResponse(), nil
case fmt.Sprintf("%s/api%s", dummyURL, "/system/status"):
return dummySystemStatusResponse(), nil
case fmt.Sprintf("%s/api%s", dummyURL, "/diskspace"):
return dummyDiskspaceResponse(), nil
case fmt.Sprintf("%s/api%s", dummyURL, "/exclusions"):
return dummyExcludedMovies(), nil
default:
// Defaulting to 404
return dummyNotFoundResponse(), nil
}
}