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

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