golift.io/starr@v1.0.0/readarr/downloadclient_test.go (about) 1 package readarr_test 2 3 import ( 4 "net/http" 5 "path" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "golift.io/starr" 10 "golift.io/starr/readarr" 11 "golift.io/starr/starrtest" 12 ) 13 14 const downloadClientResponseBody = `{ 15 "enable": true, 16 "protocol": "torrent", 17 "priority": 1, 18 "name": "Transmission", 19 "fields": [ 20 { 21 "order": 0, 22 "name": "host", 23 "label": "Host", 24 "value": "transmission", 25 "type": "textbox", 26 "advanced": false 27 }, 28 { 29 "order": 1, 30 "name": "port", 31 "label": "Port", 32 "value": 9091, 33 "type": "textbox", 34 "advanced": false 35 }, 36 { 37 "order": 2, 38 "name": "useSsl", 39 "label": "Use SSL", 40 "helpText": "Use secure connection when connecting to Transmission", 41 "value": false, 42 "type": "checkbox", 43 "advanced": false 44 } 45 ], 46 "implementationName": "Transmission", 47 "implementation": "Transmission", 48 "configContract": "TransmissionSettings", 49 "infoLink": "https://wiki.servarr.com/readarr/supported#transmission", 50 "tags": [], 51 "id": 3 52 }` 53 54 const addDownloadClient = `{"enable":true,"priority":1,"configContract":"TransmissionSettings","implementation":"Transmission","implementationName":"","name":"Transmission","protocol":"torrent","tags":null,"fields":[{"name":"host","value":"transmission"},{"name":"port","value":9091},{"name":"useSSL","value":false}]}` 55 56 const updateDownloadClient = `{"enable":true,"priority":1,"id":3,"configContract":"TransmissionSettings","implementation":"Transmission","implementationName":"","name":"Transmission","protocol":"torrent","tags":null,"fields":[{"name":"host","value":"transmission"},{"name":"port","value":9091},{"name":"useSSL","value":false}]}` 57 58 func TestGetDownloadClients(t *testing.T) { 59 t.Parallel() 60 61 tests := []*starrtest.MockData{ 62 { 63 Name: "200", 64 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient"), 65 ExpectedRequest: "", 66 ExpectedMethod: "GET", 67 ResponseStatus: 200, 68 ResponseBody: "[" + downloadClientResponseBody + "]", 69 WithRequest: nil, 70 WithResponse: []*readarr.DownloadClientOutput{ 71 { 72 Enable: true, 73 Priority: 1, 74 ID: 3, 75 ConfigContract: "TransmissionSettings", 76 Implementation: "Transmission", 77 ImplementationName: "Transmission", 78 InfoLink: "https://wiki.servarr.com/readarr/supported#transmission", 79 Name: "Transmission", 80 Protocol: "torrent", 81 Fields: []*starr.FieldOutput{ 82 { 83 Order: 0, 84 Name: "host", 85 Label: "Host", 86 Value: "transmission", 87 Type: "textbox", 88 Advanced: false, 89 }, 90 { 91 Order: 1, 92 Name: "port", 93 Label: "Port", 94 Value: float64(9091), 95 Type: "textbox", 96 Advanced: false, 97 }, 98 { 99 Order: 2, 100 Name: "useSsl", 101 Label: "Use SSL", 102 HelpText: "Use secure connection when connecting to Transmission", 103 Value: false, 104 Type: "checkbox", 105 Advanced: false, 106 }, 107 }, 108 Tags: []int{}, 109 }, 110 }, 111 WithError: nil, 112 }, 113 { 114 Name: "404", 115 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient"), 116 ExpectedMethod: "GET", 117 ResponseStatus: 404, 118 ResponseBody: `{"message": "NotFound"}`, 119 WithError: &starr.ReqError{Code: http.StatusNotFound}, 120 WithResponse: ([]*readarr.DownloadClientOutput)(nil), 121 }, 122 } 123 124 for _, test := range tests { 125 test := test 126 t.Run(test.Name, func(t *testing.T) { 127 t.Parallel() 128 mockServer := test.GetMockServer(t) 129 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 130 output, err := client.GetDownloadClients() 131 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 132 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 133 }) 134 } 135 } 136 137 func TestGetDownloadClient(t *testing.T) { 138 t.Parallel() 139 140 tests := []*starrtest.MockData{ 141 { 142 Name: "200", 143 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient", "1"), 144 ExpectedRequest: "", 145 ExpectedMethod: "GET", 146 ResponseStatus: 200, 147 ResponseBody: downloadClientResponseBody, 148 WithRequest: nil, 149 WithResponse: &readarr.DownloadClientOutput{ 150 Enable: true, 151 Priority: 1, 152 ID: 3, 153 ConfigContract: "TransmissionSettings", 154 Implementation: "Transmission", 155 ImplementationName: "Transmission", 156 InfoLink: "https://wiki.servarr.com/readarr/supported#transmission", 157 Name: "Transmission", 158 Protocol: "torrent", 159 Fields: []*starr.FieldOutput{ 160 { 161 Order: 0, 162 Name: "host", 163 Label: "Host", 164 Value: "transmission", 165 Type: "textbox", 166 Advanced: false, 167 }, 168 { 169 Order: 1, 170 Name: "port", 171 Label: "Port", 172 Value: float64(9091), 173 Type: "textbox", 174 Advanced: false, 175 }, 176 { 177 Order: 2, 178 Name: "useSsl", 179 Label: "Use SSL", 180 HelpText: "Use secure connection when connecting to Transmission", 181 Value: false, 182 Type: "checkbox", 183 Advanced: false, 184 }, 185 }, 186 Tags: []int{}, 187 }, 188 WithError: nil, 189 }, 190 { 191 Name: "404", 192 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient", "1"), 193 ExpectedMethod: "GET", 194 ResponseStatus: 404, 195 ResponseBody: `{"message": "NotFound"}`, 196 WithError: &starr.ReqError{Code: http.StatusNotFound}, 197 WithResponse: (*readarr.DownloadClientOutput)(nil), 198 }, 199 } 200 201 for _, test := range tests { 202 test := test 203 t.Run(test.Name, func(t *testing.T) { 204 t.Parallel() 205 mockServer := test.GetMockServer(t) 206 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 207 output, err := client.GetDownloadClient(1) 208 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 209 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 210 }) 211 } 212 } 213 214 func TestAddDownloadClient(t *testing.T) { 215 t.Parallel() 216 217 tests := []*starrtest.MockData{ 218 { 219 Name: "200", 220 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient"), 221 ExpectedMethod: "POST", 222 ResponseStatus: 200, 223 WithRequest: &readarr.DownloadClientInput{ 224 Enable: true, 225 Priority: 1, 226 ConfigContract: "TransmissionSettings", 227 Implementation: "Transmission", 228 Name: "Transmission", 229 Protocol: "torrent", 230 Fields: []*starr.FieldInput{ 231 { 232 Name: "host", 233 Value: "transmission", 234 }, 235 { 236 Name: "port", 237 Value: 9091, 238 }, 239 { 240 Name: "useSSL", 241 Value: false, 242 }, 243 }, 244 }, 245 ExpectedRequest: addDownloadClient + "\n", 246 ResponseBody: downloadClientResponseBody, 247 WithResponse: &readarr.DownloadClientOutput{ 248 Enable: true, 249 Priority: 1, 250 ID: 3, 251 ConfigContract: "TransmissionSettings", 252 Implementation: "Transmission", 253 ImplementationName: "Transmission", 254 InfoLink: "https://wiki.servarr.com/readarr/supported#transmission", 255 Name: "Transmission", 256 Protocol: "torrent", 257 Fields: []*starr.FieldOutput{ 258 { 259 Order: 0, 260 Name: "host", 261 Label: "Host", 262 Value: "transmission", 263 Type: "textbox", 264 Advanced: false, 265 }, 266 { 267 Order: 1, 268 Name: "port", 269 Label: "Port", 270 Value: float64(9091), 271 Type: "textbox", 272 Advanced: false, 273 }, 274 { 275 Order: 2, 276 Name: "useSsl", 277 Label: "Use SSL", 278 HelpText: "Use secure connection when connecting to Transmission", 279 Value: false, 280 Type: "checkbox", 281 Advanced: false, 282 }, 283 }, 284 Tags: []int{}, 285 }, 286 WithError: nil, 287 }, 288 { 289 Name: "404", 290 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient"), 291 ExpectedMethod: "POST", 292 ResponseStatus: 404, 293 WithRequest: &readarr.DownloadClientInput{ 294 Enable: true, 295 Priority: 1, 296 ConfigContract: "TransmissionSettings", 297 Implementation: "Transmission", 298 Name: "Transmission", 299 Protocol: "torrent", 300 Fields: []*starr.FieldInput{ 301 { 302 Name: "host", 303 Value: "transmission", 304 }, 305 { 306 Name: "port", 307 Value: 9091, 308 }, 309 { 310 Name: "useSSL", 311 Value: false, 312 }, 313 }, 314 }, 315 ExpectedRequest: addDownloadClient + "\n", 316 ResponseBody: `{"message": "NotFound"}`, 317 WithError: &starr.ReqError{Code: http.StatusNotFound}, 318 WithResponse: (*readarr.DownloadClientOutput)(nil), 319 }, 320 } 321 322 for _, test := range tests { 323 test := test 324 t.Run(test.Name, func(t *testing.T) { 325 t.Parallel() 326 mockServer := test.GetMockServer(t) 327 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 328 output, err := client.AddDownloadClient(test.WithRequest.(*readarr.DownloadClientInput)) 329 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 330 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 331 }) 332 } 333 } 334 335 func TestUpdateDownloadClient(t *testing.T) { 336 t.Parallel() 337 338 tests := []*starrtest.MockData{ 339 { 340 Name: "200", 341 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient", "3?forceSave=false"), 342 ExpectedMethod: "PUT", 343 ResponseStatus: 200, 344 WithRequest: &readarr.DownloadClientInput{ 345 Enable: true, 346 Priority: 1, 347 ConfigContract: "TransmissionSettings", 348 Implementation: "Transmission", 349 Name: "Transmission", 350 Protocol: "torrent", 351 Fields: []*starr.FieldInput{ 352 { 353 Name: "host", 354 Value: "transmission", 355 }, 356 { 357 Name: "port", 358 Value: 9091, 359 }, 360 { 361 Name: "useSSL", 362 Value: false, 363 }, 364 }, 365 ID: 3, 366 }, 367 ExpectedRequest: updateDownloadClient + "\n", 368 ResponseBody: downloadClientResponseBody, 369 WithResponse: &readarr.DownloadClientOutput{ 370 Enable: true, 371 Priority: 1, 372 ID: 3, 373 ConfigContract: "TransmissionSettings", 374 Implementation: "Transmission", 375 ImplementationName: "Transmission", 376 InfoLink: "https://wiki.servarr.com/readarr/supported#transmission", 377 Name: "Transmission", 378 Protocol: "torrent", 379 Fields: []*starr.FieldOutput{ 380 { 381 Order: 0, 382 Name: "host", 383 Label: "Host", 384 Value: "transmission", 385 Type: "textbox", 386 Advanced: false, 387 }, 388 { 389 Order: 1, 390 Name: "port", 391 Label: "Port", 392 Value: float64(9091), 393 Type: "textbox", 394 Advanced: false, 395 }, 396 { 397 Order: 2, 398 Name: "useSsl", 399 Label: "Use SSL", 400 HelpText: "Use secure connection when connecting to Transmission", 401 Value: false, 402 Type: "checkbox", 403 Advanced: false, 404 }, 405 }, 406 Tags: []int{}, 407 }, 408 WithError: nil, 409 }, 410 { 411 Name: "404", 412 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient", "3?forceSave=false"), 413 ExpectedMethod: "PUT", 414 ResponseStatus: 404, 415 WithRequest: &readarr.DownloadClientInput{ 416 Enable: true, 417 Priority: 1, 418 ConfigContract: "TransmissionSettings", 419 Implementation: "Transmission", 420 Name: "Transmission", 421 Protocol: "torrent", 422 Fields: []*starr.FieldInput{ 423 { 424 Name: "host", 425 Value: "transmission", 426 }, 427 { 428 Name: "port", 429 Value: 9091, 430 }, 431 { 432 Name: "useSSL", 433 Value: false, 434 }, 435 }, 436 ID: 3, 437 }, 438 ExpectedRequest: updateDownloadClient + "\n", 439 ResponseBody: `{"message": "NotFound"}`, 440 WithError: &starr.ReqError{Code: http.StatusNotFound}, 441 WithResponse: (*readarr.DownloadClientOutput)(nil), 442 }, 443 } 444 445 for _, test := range tests { 446 test := test 447 t.Run(test.Name, func(t *testing.T) { 448 t.Parallel() 449 mockServer := test.GetMockServer(t) 450 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 451 output, err := client.UpdateDownloadClient(test.WithRequest.(*readarr.DownloadClientInput), false) 452 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 453 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 454 }) 455 } 456 } 457 458 func TestDeleteDownloadClient(t *testing.T) { 459 t.Parallel() 460 461 tests := []*starrtest.MockData{ 462 { 463 Name: "200", 464 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient", "2"), 465 ExpectedMethod: "DELETE", 466 WithRequest: int64(2), 467 ResponseStatus: 200, 468 ResponseBody: "{}", 469 WithError: nil, 470 }, 471 { 472 Name: "404", 473 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "downloadClient", "2"), 474 ExpectedMethod: "DELETE", 475 WithRequest: int64(2), 476 ResponseStatus: 404, 477 ResponseBody: `{"message": "NotFound"}`, 478 WithError: &starr.ReqError{Code: http.StatusNotFound}, 479 }, 480 } 481 482 for _, test := range tests { 483 test := test 484 t.Run(test.Name, func(t *testing.T) { 485 t.Parallel() 486 mockServer := test.GetMockServer(t) 487 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 488 err := client.DeleteDownloadClient(test.WithRequest.(int64)) 489 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 490 }) 491 } 492 }