golift.io/starr@v1.0.0/radarr/notification_test.go (about)

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