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