golift.io/starr@v1.0.0/sonarr/importlist_test.go (about) 1 package sonarr_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/sonarr" 11 "golift.io/starr/starrtest" 12 ) 13 14 const importListResponseBody = `{ 15 "enableAutomaticAdd": false, 16 "shouldMonitor": "all", 17 "rootFolderPath": "/config", 18 "qualityProfileId": 1, 19 "seriesType": "standard", 20 "seasonFolder": true, 21 "listType": "plex", 22 "listOrder": 1, 23 "name": "PlexImport", 24 "fields": [ 25 { 26 "order": 0, 27 "name": "accessToken", 28 "label": "Access Token", 29 "type": "textbox", 30 "value": "test", 31 "advanced": false, 32 "hidden": "hidden" 33 }, 34 { 35 "order": 1, 36 "name": "signIn", 37 "label": "Authenticate with Plex.tv", 38 "value": "startOAuth", 39 "type": "oAuth", 40 "advanced": false 41 } 42 ], 43 "implementationName": "Plex Watchlist", 44 "implementation": "PlexImport", 45 "configContract": "PlexListSettings", 46 "infoLink": "https://wiki.servarr.com/sonarr/supported#pleximport", 47 "tags": [], 48 "id": 4 49 }` 50 51 const addImportList = `{"enableAutomaticAdd":false,"seasonFolder":true,"listOrder":0,"qualityProfileId":1,"configContract":"PlexListSettings","implementation":"PlexImport","name":"PlexImport","rootFolderPath":"/config","seriesType":"standard","shouldMonitor":"all","fields":[{"name":"accessToken","value":"test"}]}` 52 53 const updateImportList = `{"enableAutomaticAdd":false,"seasonFolder":true,"listOrder":0,"qualityProfileId":1,"id":4,"configContract":"PlexListSettings","implementation":"PlexImport","name":"PlexImport","rootFolderPath":"/config","seriesType":"standard","shouldMonitor":"all","fields":[{"name":"accessToken","value":"test"}]}` 54 55 func TestGetImportLists(t *testing.T) { 56 t.Parallel() 57 58 tests := []*starrtest.MockData{ 59 { 60 Name: "200", 61 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList"), 62 ExpectedRequest: "", 63 ExpectedMethod: "GET", 64 ResponseStatus: 200, 65 ResponseBody: "[" + importListResponseBody + "]", 66 WithRequest: nil, 67 WithResponse: []*sonarr.ImportListOutput{ 68 { 69 EnableAutomaticAdd: false, 70 ShouldMonitor: "all", 71 RootFolderPath: "/config", 72 QualityProfileID: 1, 73 SeriesType: "standard", 74 SeasonFolder: true, 75 ListType: "plex", 76 ListOrder: 1, 77 Name: "PlexImport", 78 Fields: []*starr.FieldOutput{ 79 { 80 Order: 0, 81 Name: "accessToken", 82 Label: "Access Token", 83 Type: "textbox", 84 Value: "test", 85 Advanced: false, 86 Hidden: "hidden", 87 }, 88 { 89 Order: 1, 90 Name: "signIn", 91 Label: "Authenticate with Plex.tv", 92 Value: "startOAuth", 93 Type: "oAuth", 94 Advanced: false, 95 }, 96 }, 97 ImplementationName: "Plex Watchlist", 98 Implementation: "PlexImport", 99 ConfigContract: "PlexListSettings", 100 InfoLink: "https://wiki.servarr.com/sonarr/supported#pleximport", 101 Tags: []int{}, 102 ID: 4, 103 }, 104 }, 105 WithError: nil, 106 }, 107 { 108 Name: "404", 109 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList"), 110 ExpectedMethod: "GET", 111 ResponseStatus: 404, 112 ResponseBody: `{"message": "NotFound"}`, 113 WithError: &starr.ReqError{Code: http.StatusNotFound}, 114 WithResponse: ([]*sonarr.ImportListOutput)(nil), 115 }, 116 } 117 118 for _, test := range tests { 119 test := test 120 t.Run(test.Name, func(t *testing.T) { 121 t.Parallel() 122 mockServer := test.GetMockServer(t) 123 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 124 output, err := client.GetImportLists() 125 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 126 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 127 }) 128 } 129 } 130 131 func TestGetImportList(t *testing.T) { 132 t.Parallel() 133 134 tests := []*starrtest.MockData{ 135 { 136 Name: "200", 137 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList", "1"), 138 ExpectedRequest: "", 139 ExpectedMethod: "GET", 140 ResponseStatus: 200, 141 ResponseBody: importListResponseBody, 142 WithRequest: nil, 143 WithResponse: &sonarr.ImportListOutput{ 144 EnableAutomaticAdd: false, 145 ShouldMonitor: "all", 146 RootFolderPath: "/config", 147 QualityProfileID: 1, 148 SeriesType: "standard", 149 SeasonFolder: true, 150 ListType: "plex", 151 ListOrder: 1, 152 Name: "PlexImport", 153 Fields: []*starr.FieldOutput{ 154 { 155 Order: 0, 156 Name: "accessToken", 157 Label: "Access Token", 158 Type: "textbox", 159 Value: "test", 160 Advanced: false, 161 Hidden: "hidden", 162 }, 163 { 164 Order: 1, 165 Name: "signIn", 166 Label: "Authenticate with Plex.tv", 167 Value: "startOAuth", 168 Type: "oAuth", 169 Advanced: false, 170 }, 171 }, 172 ImplementationName: "Plex Watchlist", 173 Implementation: "PlexImport", 174 ConfigContract: "PlexListSettings", 175 InfoLink: "https://wiki.servarr.com/sonarr/supported#pleximport", 176 Tags: []int{}, 177 ID: 4, 178 }, 179 WithError: nil, 180 }, 181 { 182 Name: "404", 183 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList", "1"), 184 ExpectedMethod: "GET", 185 ResponseStatus: 404, 186 ResponseBody: `{"message": "NotFound"}`, 187 WithError: &starr.ReqError{Code: http.StatusNotFound}, 188 WithResponse: (*sonarr.ImportListOutput)(nil), 189 }, 190 } 191 192 for _, test := range tests { 193 test := test 194 t.Run(test.Name, func(t *testing.T) { 195 t.Parallel() 196 mockServer := test.GetMockServer(t) 197 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 198 output, err := client.GetImportList(1) 199 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 200 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 201 }) 202 } 203 } 204 205 func TestAddImportList(t *testing.T) { 206 t.Parallel() 207 208 tests := []*starrtest.MockData{ 209 { 210 Name: "200", 211 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList"), 212 ExpectedMethod: "POST", 213 ResponseStatus: 200, 214 WithRequest: &sonarr.ImportListInput{ 215 EnableAutomaticAdd: false, 216 ShouldMonitor: "all", 217 RootFolderPath: "/config", 218 QualityProfileID: 1, 219 SeriesType: "standard", 220 SeasonFolder: true, 221 Name: "PlexImport", 222 Fields: []*starr.FieldInput{ 223 { 224 Name: "accessToken", 225 Value: "test", 226 }, 227 }, 228 Implementation: "PlexImport", 229 ConfigContract: "PlexListSettings", 230 Tags: []int{}, 231 }, 232 ExpectedRequest: addImportList + "\n", 233 ResponseBody: importListResponseBody, 234 WithResponse: &sonarr.ImportListOutput{ 235 EnableAutomaticAdd: false, 236 ShouldMonitor: "all", 237 RootFolderPath: "/config", 238 QualityProfileID: 1, 239 SeriesType: "standard", 240 SeasonFolder: true, 241 ListType: "plex", 242 ListOrder: 1, 243 Name: "PlexImport", 244 Fields: []*starr.FieldOutput{ 245 { 246 Order: 0, 247 Name: "accessToken", 248 Label: "Access Token", 249 Type: "textbox", 250 Value: "test", 251 Advanced: false, 252 Hidden: "hidden", 253 }, 254 { 255 Order: 1, 256 Name: "signIn", 257 Label: "Authenticate with Plex.tv", 258 Value: "startOAuth", 259 Type: "oAuth", 260 Advanced: false, 261 }, 262 }, 263 ImplementationName: "Plex Watchlist", 264 Implementation: "PlexImport", 265 ConfigContract: "PlexListSettings", 266 InfoLink: "https://wiki.servarr.com/sonarr/supported#pleximport", 267 Tags: []int{}, 268 ID: 4, 269 }, 270 WithError: nil, 271 }, 272 { 273 Name: "404", 274 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList"), 275 ExpectedMethod: "POST", 276 ResponseStatus: 404, 277 WithRequest: &sonarr.ImportListInput{ 278 EnableAutomaticAdd: false, 279 ShouldMonitor: "all", 280 RootFolderPath: "/config", 281 QualityProfileID: 1, 282 SeriesType: "standard", 283 SeasonFolder: true, 284 Name: "PlexImport", 285 Fields: []*starr.FieldInput{ 286 { 287 Name: "accessToken", 288 Value: "test", 289 }, 290 }, 291 Implementation: "PlexImport", 292 ConfigContract: "PlexListSettings", 293 Tags: []int{}, 294 }, 295 ExpectedRequest: addImportList + "\n", 296 ResponseBody: `{"message": "NotFound"}`, 297 WithError: &starr.ReqError{Code: http.StatusNotFound}, 298 WithResponse: (*sonarr.ImportListOutput)(nil), 299 }, 300 } 301 302 for _, test := range tests { 303 test := test 304 t.Run(test.Name, func(t *testing.T) { 305 t.Parallel() 306 mockServer := test.GetMockServer(t) 307 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 308 output, err := client.AddImportList(test.WithRequest.(*sonarr.ImportListInput)) 309 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 310 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 311 }) 312 } 313 } 314 315 func TestUpdateImportList(t *testing.T) { 316 t.Parallel() 317 318 tests := []*starrtest.MockData{ 319 { 320 Name: "200", 321 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList", "4?forceSave=false"), 322 ExpectedMethod: "PUT", 323 ResponseStatus: 200, 324 WithRequest: &sonarr.ImportListInput{ 325 EnableAutomaticAdd: false, 326 ShouldMonitor: "all", 327 RootFolderPath: "/config", 328 QualityProfileID: 1, 329 SeriesType: "standard", 330 SeasonFolder: true, 331 Name: "PlexImport", 332 Fields: []*starr.FieldInput{ 333 { 334 Name: "accessToken", 335 Value: "test", 336 }, 337 }, 338 Implementation: "PlexImport", 339 ConfigContract: "PlexListSettings", 340 Tags: []int{}, 341 ID: 4, 342 }, 343 ExpectedRequest: updateImportList + "\n", 344 ResponseBody: importListResponseBody, 345 WithResponse: &sonarr.ImportListOutput{ 346 EnableAutomaticAdd: false, 347 ShouldMonitor: "all", 348 RootFolderPath: "/config", 349 QualityProfileID: 1, 350 SeriesType: "standard", 351 SeasonFolder: true, 352 ListType: "plex", 353 ListOrder: 1, 354 Name: "PlexImport", 355 Fields: []*starr.FieldOutput{ 356 { 357 Order: 0, 358 Name: "accessToken", 359 Label: "Access Token", 360 Type: "textbox", 361 Value: "test", 362 Advanced: false, 363 Hidden: "hidden", 364 }, 365 { 366 Order: 1, 367 Name: "signIn", 368 Label: "Authenticate with Plex.tv", 369 Value: "startOAuth", 370 Type: "oAuth", 371 Advanced: false, 372 }, 373 }, 374 ImplementationName: "Plex Watchlist", 375 Implementation: "PlexImport", 376 ConfigContract: "PlexListSettings", 377 InfoLink: "https://wiki.servarr.com/sonarr/supported#pleximport", 378 Tags: []int{}, 379 ID: 4, 380 }, 381 WithError: nil, 382 }, 383 { 384 Name: "404", 385 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList", "4?forceSave=false"), 386 ExpectedMethod: "PUT", 387 ResponseStatus: 404, 388 WithRequest: &sonarr.ImportListInput{ 389 EnableAutomaticAdd: false, 390 ShouldMonitor: "all", 391 RootFolderPath: "/config", 392 QualityProfileID: 1, 393 SeriesType: "standard", 394 SeasonFolder: true, 395 Name: "PlexImport", 396 Fields: []*starr.FieldInput{ 397 { 398 Name: "accessToken", 399 Value: "test", 400 }, 401 }, 402 Implementation: "PlexImport", 403 ConfigContract: "PlexListSettings", 404 Tags: []int{}, 405 ID: 4, 406 }, 407 ExpectedRequest: updateImportList + "\n", 408 ResponseBody: `{"message": "NotFound"}`, 409 WithError: &starr.ReqError{Code: http.StatusNotFound}, 410 WithResponse: (*sonarr.ImportListOutput)(nil), 411 }, 412 } 413 414 for _, test := range tests { 415 test := test 416 t.Run(test.Name, func(t *testing.T) { 417 t.Parallel() 418 mockServer := test.GetMockServer(t) 419 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 420 output, err := client.UpdateImportList(test.WithRequest.(*sonarr.ImportListInput), false) 421 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 422 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 423 }) 424 } 425 } 426 427 func TestDeleteImportList(t *testing.T) { 428 t.Parallel() 429 430 tests := []*starrtest.MockData{ 431 { 432 Name: "200", 433 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList", "2"), 434 ExpectedMethod: "DELETE", 435 WithRequest: int64(2), 436 ResponseStatus: 200, 437 ResponseBody: "{}", 438 WithError: nil, 439 }, 440 { 441 Name: "404", 442 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "importList", "2"), 443 ExpectedMethod: "DELETE", 444 WithRequest: int64(2), 445 ResponseStatus: 404, 446 ResponseBody: `{"message": "NotFound"}`, 447 WithError: &starr.ReqError{Code: http.StatusNotFound}, 448 }, 449 } 450 451 for _, test := range tests { 452 test := test 453 t.Run(test.Name, func(t *testing.T) { 454 t.Parallel() 455 mockServer := test.GetMockServer(t) 456 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 457 err := client.DeleteImportList(test.WithRequest.(int64)) 458 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 459 }) 460 } 461 }