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