golift.io/starr@v1.0.0/readarr/notification_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 notificationResponseBody = `{ 15 "onGrab": false, 16 "onReleaseImport": false, 17 "onUpgrade": true, 18 "onRename": false, 19 "onAuthorDelete": false, 20 "onBookDelete": false, 21 "onBookFileDelete": false, 22 "onBookFileDeleteForUpgrade": false, 23 "onHealthIssue": false, 24 "onDownloadFailure": false, 25 "onImportFailure": false, 26 "onBookRetag": false, 27 "supportsOnGrab": true, 28 "supportsOnReleaseImport": true, 29 "supportsOnUpgrade": true, 30 "supportsOnRename": true, 31 "supportsOnAuthorDelete": true, 32 "supportsOnBookDelete": true, 33 "supportsOnBookFileDelete": true, 34 "supportsOnBookFileDeleteForUpgrade": true, 35 "supportsOnHealthIssue": true, 36 "includeHealthWarnings": false, 37 "supportsOnDownloadFailure": false, 38 "supportsOnImportFailure": false, 39 "supportsOnBookRetag": true, 40 "name": "Test", 41 "fields": [ 42 { 43 "order": 0, 44 "name": "path", 45 "label": "Path", 46 "value": "/scripts/readarr.sh", 47 "type": "filePath", 48 "advanced": false 49 }, 50 { 51 "order": 1, 52 "name": "arguments", 53 "label": "Arguments", 54 "helpText": "Arguments to pass to the script", 55 "type": "textbox", 56 "advanced": false, 57 "hidden": "hiddenIfNotSet" 58 } 59 ], 60 "implementationName": "Custom Script", 61 "implementation": "CustomScript", 62 "configContract": "CustomScriptSettings", 63 "infoLink": "https://wiki.servarr.com/readarr/supported#customscript", 64 "message": { 65 "message": "Testing will execute the script with the EventType set to Test", 66 "type": "warning" 67 }, 68 "tags": [], 69 "id": 3 70 }` 71 72 const addNotification = `{"onUpgrade":true,"name":"Test","implementation":"CustomScript","configContract":` + 73 `"CustomScriptSettings","fields":[{"name":"path","value":"/scripts/readarr.sh"}]}` 74 75 const updateNotification = `{"onUpgrade":true,"id":3,"name":"Test","implementation":"CustomScript","configContract":` + 76 `"CustomScriptSettings","fields":[{"name":"path","value":"/scripts/readarr.sh"}]}` 77 78 func TestGetNotifications(t *testing.T) { 79 t.Parallel() 80 81 tests := []*starrtest.MockData{ 82 { 83 Name: "200", 84 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification"), 85 ExpectedRequest: "", 86 ExpectedMethod: "GET", 87 ResponseStatus: 200, 88 ResponseBody: "[" + notificationResponseBody + "]", 89 WithRequest: nil, 90 WithResponse: []*readarr.NotificationOutput{ 91 { 92 OnUpgrade: true, 93 SupportsOnGrab: true, 94 SupportsOnReleaseImport: true, 95 SupportsOnUpgrade: true, 96 SupportsOnRename: true, 97 SupportsOnAuthorDelete: true, 98 SupportsOnBookDelete: true, 99 SupportsOnBookFileDelete: true, 100 SupportsOnBookFileDeleteForUpgrade: true, 101 SupportsOnDownloadFailure: false, 102 SupportsOnImportFailure: false, 103 SupportsOnBookRetag: true, 104 SupportsOnHealthIssue: true, 105 ID: 3, 106 Name: "Test", 107 ImplementationName: "Custom Script", 108 Implementation: "CustomScript", 109 ConfigContract: "CustomScriptSettings", 110 InfoLink: "https://wiki.servarr.com/readarr/supported#customscript", 111 Tags: []int{}, 112 Fields: []*starr.FieldOutput{ 113 { 114 Order: 0, 115 Name: "path", 116 Label: "Path", 117 Value: "/scripts/readarr.sh", 118 Type: "filePath", 119 Advanced: false, 120 }, 121 { 122 Order: 1, 123 Name: "arguments", 124 Label: "Arguments", 125 HelpText: "Arguments to pass to the script", 126 Hidden: "hiddenIfNotSet", 127 Type: "textbox", 128 Advanced: false, 129 }, 130 }, 131 }, 132 }, 133 WithError: nil, 134 }, 135 { 136 Name: "404", 137 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification"), 138 ExpectedMethod: "GET", 139 ResponseStatus: 404, 140 ResponseBody: `{"message": "NotFound"}`, 141 WithError: &starr.ReqError{Code: http.StatusNotFound}, 142 WithResponse: ([]*readarr.NotificationOutput)(nil), 143 }, 144 } 145 146 for _, test := range tests { 147 test := test 148 t.Run(test.Name, func(t *testing.T) { 149 t.Parallel() 150 mockServer := test.GetMockServer(t) 151 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 152 output, err := client.GetNotifications() 153 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 154 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 155 }) 156 } 157 } 158 159 func TestGetNotification(t *testing.T) { 160 t.Parallel() 161 162 tests := []*starrtest.MockData{ 163 { 164 Name: "200", 165 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification", "1"), 166 ExpectedRequest: "", 167 ExpectedMethod: "GET", 168 ResponseStatus: 200, 169 ResponseBody: notificationResponseBody, 170 WithRequest: nil, 171 WithResponse: &readarr.NotificationOutput{ 172 OnUpgrade: true, 173 SupportsOnGrab: true, 174 SupportsOnReleaseImport: true, 175 SupportsOnUpgrade: true, 176 SupportsOnRename: true, 177 SupportsOnAuthorDelete: true, 178 SupportsOnBookDelete: true, 179 SupportsOnBookFileDelete: true, 180 SupportsOnBookFileDeleteForUpgrade: true, 181 SupportsOnDownloadFailure: false, 182 SupportsOnImportFailure: false, 183 SupportsOnBookRetag: true, 184 SupportsOnHealthIssue: true, 185 ID: 3, 186 Name: "Test", 187 ImplementationName: "Custom Script", 188 Implementation: "CustomScript", 189 ConfigContract: "CustomScriptSettings", 190 InfoLink: "https://wiki.servarr.com/readarr/supported#customscript", 191 Tags: []int{}, 192 Fields: []*starr.FieldOutput{ 193 { 194 Order: 0, 195 Name: "path", 196 Label: "Path", 197 Value: "/scripts/readarr.sh", 198 Type: "filePath", 199 Advanced: false, 200 }, 201 { 202 Order: 1, 203 Name: "arguments", 204 Label: "Arguments", 205 HelpText: "Arguments to pass to the script", 206 Hidden: "hiddenIfNotSet", 207 Type: "textbox", 208 Advanced: false, 209 }, 210 }, 211 }, 212 WithError: nil, 213 }, 214 { 215 Name: "404", 216 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification", "1"), 217 ExpectedMethod: "GET", 218 ResponseStatus: 404, 219 ResponseBody: `{"message": "NotFound"}`, 220 WithError: &starr.ReqError{Code: http.StatusNotFound}, 221 WithResponse: (*readarr.NotificationOutput)(nil), 222 }, 223 } 224 225 for _, test := range tests { 226 test := test 227 t.Run(test.Name, func(t *testing.T) { 228 t.Parallel() 229 mockServer := test.GetMockServer(t) 230 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 231 output, err := client.GetNotification(1) 232 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 233 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 234 }) 235 } 236 } 237 238 func TestAddNotification(t *testing.T) { 239 t.Parallel() 240 241 tests := []*starrtest.MockData{ 242 { 243 Name: "200", 244 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification"), 245 ExpectedMethod: "POST", 246 ResponseStatus: 200, 247 WithRequest: &readarr.NotificationInput{ 248 OnUpgrade: true, 249 Name: "Test", 250 Implementation: "CustomScript", 251 ConfigContract: "CustomScriptSettings", 252 Fields: []*starr.FieldInput{ 253 { 254 Name: "path", 255 Value: "/scripts/readarr.sh", 256 }, 257 }, 258 }, 259 ExpectedRequest: addNotification + "\n", 260 ResponseBody: notificationResponseBody, 261 WithResponse: &readarr.NotificationOutput{ 262 OnUpgrade: true, 263 SupportsOnGrab: true, 264 SupportsOnReleaseImport: true, 265 SupportsOnUpgrade: true, 266 SupportsOnRename: true, 267 SupportsOnAuthorDelete: true, 268 SupportsOnBookDelete: true, 269 SupportsOnBookFileDelete: true, 270 SupportsOnBookFileDeleteForUpgrade: true, 271 SupportsOnDownloadFailure: false, 272 SupportsOnImportFailure: false, 273 SupportsOnBookRetag: true, 274 SupportsOnHealthIssue: true, 275 ID: 3, 276 Name: "Test", 277 ImplementationName: "Custom Script", 278 Implementation: "CustomScript", 279 ConfigContract: "CustomScriptSettings", 280 InfoLink: "https://wiki.servarr.com/readarr/supported#customscript", 281 Tags: []int{}, 282 Fields: []*starr.FieldOutput{ 283 { 284 Order: 0, 285 Name: "path", 286 Label: "Path", 287 Value: "/scripts/readarr.sh", 288 Type: "filePath", 289 Advanced: false, 290 }, 291 { 292 Order: 1, 293 Name: "arguments", 294 Label: "Arguments", 295 HelpText: "Arguments to pass to the script", 296 Hidden: "hiddenIfNotSet", 297 Type: "textbox", 298 Advanced: false, 299 }, 300 }, 301 }, 302 WithError: nil, 303 }, 304 { 305 Name: "404", 306 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification"), 307 ExpectedMethod: "POST", 308 ResponseStatus: 404, 309 WithRequest: &readarr.NotificationInput{ 310 OnUpgrade: true, 311 Name: "Test", 312 Implementation: "CustomScript", 313 ConfigContract: "CustomScriptSettings", 314 Fields: []*starr.FieldInput{ 315 { 316 Name: "path", 317 Value: "/scripts/readarr.sh", 318 }, 319 }, 320 }, 321 ExpectedRequest: addNotification + "\n", 322 ResponseBody: `{"message": "NotFound"}`, 323 WithError: &starr.ReqError{Code: http.StatusNotFound}, 324 WithResponse: (*readarr.NotificationOutput)(nil), 325 }, 326 } 327 328 for _, test := range tests { 329 test := test 330 t.Run(test.Name, func(t *testing.T) { 331 t.Parallel() 332 mockServer := test.GetMockServer(t) 333 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 334 output, err := client.AddNotification(test.WithRequest.(*readarr.NotificationInput)) 335 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 336 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 337 }) 338 } 339 } 340 341 func TestUpdateNotification(t *testing.T) { 342 t.Parallel() 343 344 tests := []*starrtest.MockData{ 345 { 346 Name: "200", 347 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification", "3"), 348 ExpectedMethod: "PUT", 349 ResponseStatus: 200, 350 WithRequest: &readarr.NotificationInput{ 351 OnUpgrade: true, 352 ID: 3, 353 Name: "Test", 354 Implementation: "CustomScript", 355 ConfigContract: "CustomScriptSettings", 356 Fields: []*starr.FieldInput{ 357 { 358 Name: "path", 359 Value: "/scripts/readarr.sh", 360 }, 361 }, 362 }, 363 ExpectedRequest: updateNotification + "\n", 364 ResponseBody: notificationResponseBody, 365 WithResponse: &readarr.NotificationOutput{ 366 OnUpgrade: true, 367 SupportsOnGrab: true, 368 SupportsOnReleaseImport: true, 369 SupportsOnUpgrade: true, 370 SupportsOnRename: true, 371 SupportsOnAuthorDelete: true, 372 SupportsOnBookDelete: true, 373 SupportsOnBookFileDelete: true, 374 SupportsOnBookFileDeleteForUpgrade: true, 375 SupportsOnDownloadFailure: false, 376 SupportsOnImportFailure: false, 377 SupportsOnBookRetag: true, 378 SupportsOnHealthIssue: true, 379 ID: 3, 380 Name: "Test", 381 ImplementationName: "Custom Script", 382 Implementation: "CustomScript", 383 ConfigContract: "CustomScriptSettings", 384 InfoLink: "https://wiki.servarr.com/readarr/supported#customscript", 385 Tags: []int{}, 386 Fields: []*starr.FieldOutput{ 387 { 388 Order: 0, 389 Name: "path", 390 Label: "Path", 391 Value: "/scripts/readarr.sh", 392 Type: "filePath", 393 Advanced: false, 394 }, 395 { 396 Order: 1, 397 Name: "arguments", 398 Label: "Arguments", 399 HelpText: "Arguments to pass to the script", 400 Hidden: "hiddenIfNotSet", 401 Type: "textbox", 402 Advanced: false, 403 }, 404 }, 405 }, 406 WithError: nil, 407 }, 408 { 409 Name: "404", 410 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification", "3"), 411 ExpectedMethod: "PUT", 412 ResponseStatus: 404, 413 WithRequest: &readarr.NotificationInput{ 414 OnUpgrade: true, 415 ID: 3, 416 Name: "Test", 417 Implementation: "CustomScript", 418 ConfigContract: "CustomScriptSettings", 419 Fields: []*starr.FieldInput{ 420 { 421 Name: "path", 422 Value: "/scripts/readarr.sh", 423 }, 424 }, 425 }, 426 ExpectedRequest: updateNotification + "\n", 427 ResponseBody: `{"message": "NotFound"}`, 428 WithError: &starr.ReqError{Code: http.StatusNotFound}, 429 WithResponse: (*readarr.NotificationOutput)(nil), 430 }, 431 } 432 433 for _, test := range tests { 434 test := test 435 t.Run(test.Name, func(t *testing.T) { 436 t.Parallel() 437 mockServer := test.GetMockServer(t) 438 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 439 output, err := client.UpdateNotification(test.WithRequest.(*readarr.NotificationInput)) 440 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 441 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 442 }) 443 } 444 } 445 446 func TestDeleteNotification(t *testing.T) { 447 t.Parallel() 448 449 tests := []*starrtest.MockData{ 450 { 451 Name: "200", 452 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification", "2"), 453 ExpectedMethod: "DELETE", 454 WithRequest: int64(2), 455 ResponseStatus: 200, 456 ResponseBody: "{}", 457 WithError: nil, 458 }, 459 { 460 Name: "404", 461 ExpectedPath: path.Join("/", starr.API, readarr.APIver, "notification", "2"), 462 ExpectedMethod: "DELETE", 463 WithRequest: int64(2), 464 ResponseStatus: 404, 465 ResponseBody: `{"message": "NotFound"}`, 466 WithError: &starr.ReqError{Code: http.StatusNotFound}, 467 }, 468 } 469 470 for _, test := range tests { 471 test := test 472 t.Run(test.Name, func(t *testing.T) { 473 t.Parallel() 474 mockServer := test.GetMockServer(t) 475 client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 476 err := client.DeleteNotification(test.WithRequest.(int64)) 477 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 478 }) 479 } 480 }