golift.io/starr@v1.0.0/lidarr/downloadclient_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 downloadClientResponseBody = `{
    15      "enable": true,
    16      "protocol": "torrent",
    17      "priority": 1,
    18      "removeCompletedDownloads": false,
    19      "removeFailedDownloads": false,
    20      "name": "Transmission",
    21      "fields": [
    22          {
    23              "order": 0,
    24              "name": "host",
    25              "label": "Host",
    26              "value": "transmission",
    27              "type": "textbox",
    28              "advanced": false
    29          },
    30          {
    31              "order": 1,
    32              "name": "port",
    33              "label": "Port",
    34              "value": 9091,
    35              "type": "textbox",
    36              "advanced": false
    37          },
    38          {
    39              "order": 2,
    40              "name": "useSsl",
    41              "label": "Use SSL",
    42              "helpText": "Use secure connection when connecting to Transmission",
    43              "value": false,
    44              "type": "checkbox",
    45              "advanced": false
    46          }
    47      ],
    48      "implementationName": "Transmission",
    49      "implementation": "Transmission",
    50      "configContract": "TransmissionSettings",
    51      "infoLink": "https://wiki.servarr.com/lidarr/supported#transmission",
    52      "tags": [],
    53      "id": 3
    54  }`
    55  
    56  const addDownloadClient = `{"enable":true,"removeCompletedDownloads":false,"removeFailedDownloads":false,` +
    57  	`"priority":1,"configContract":"TransmissionSettings","implementation":"Transmission","name":"Transmission",` +
    58  	`"protocol":"torrent","tags":null,"fields":[{"name":"host","value":"transmission"},` +
    59  	`{"name":"port","value":9091},{"name":"useSSL","value":false}]}`
    60  
    61  const updateDownloadClient = `{"enable":true,"removeCompletedDownloads":false,"removeFailedDownloads":false,` +
    62  	`"priority":1,"id":3,"configContract":"TransmissionSettings","implementation":"Transmission","name":"Transmission",` +
    63  	`"protocol":"torrent","tags":null,"fields":[{"name":"host","value":"transmission"},` +
    64  	`{"name":"port","value":9091},{"name":"useSSL","value":false}]}`
    65  
    66  func TestGetDownloadClients(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	tests := []*starrtest.MockData{
    70  		{
    71  			Name:            "200",
    72  			ExpectedPath:    path.Join("/", starr.API, lidarr.APIver, "downloadClient"),
    73  			ExpectedRequest: "",
    74  			ExpectedMethod:  "GET",
    75  			ResponseStatus:  200,
    76  			ResponseBody:    "[" + downloadClientResponseBody + "]",
    77  			WithRequest:     nil,
    78  			WithResponse: []*lidarr.DownloadClientOutput{
    79  				{
    80  					Enable:             true,
    81  					Priority:           1,
    82  					ID:                 3,
    83  					ConfigContract:     "TransmissionSettings",
    84  					Implementation:     "Transmission",
    85  					ImplementationName: "Transmission",
    86  					InfoLink:           "https://wiki.servarr.com/lidarr/supported#transmission",
    87  					Name:               "Transmission",
    88  					Protocol:           "torrent",
    89  					Fields: []*starr.FieldOutput{
    90  						{
    91  							Order:    0,
    92  							Name:     "host",
    93  							Label:    "Host",
    94  							Value:    "transmission",
    95  							Type:     "textbox",
    96  							Advanced: false,
    97  						},
    98  						{
    99  							Order:    1,
   100  							Name:     "port",
   101  							Label:    "Port",
   102  							Value:    float64(9091),
   103  							Type:     "textbox",
   104  							Advanced: false,
   105  						},
   106  						{
   107  							Order:    2,
   108  							Name:     "useSsl",
   109  							Label:    "Use SSL",
   110  							HelpText: "Use secure connection when connecting to Transmission",
   111  							Value:    false,
   112  							Type:     "checkbox",
   113  							Advanced: false,
   114  						},
   115  					},
   116  					Tags: []int{},
   117  				},
   118  			},
   119  			WithError: nil,
   120  		},
   121  		{
   122  			Name:           "404",
   123  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient"),
   124  			ExpectedMethod: "GET",
   125  			ResponseStatus: 404,
   126  			ResponseBody:   `{"message": "NotFound"}`,
   127  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   128  			WithResponse:   ([]*lidarr.DownloadClientOutput)(nil),
   129  		},
   130  	}
   131  
   132  	for _, test := range tests {
   133  		test := test
   134  		t.Run(test.Name, func(t *testing.T) {
   135  			t.Parallel()
   136  			mockServer := test.GetMockServer(t)
   137  			client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   138  			output, err := client.GetDownloadClients()
   139  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   140  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   141  		})
   142  	}
   143  }
   144  
   145  func TestGetDownloadClient(t *testing.T) {
   146  	t.Parallel()
   147  
   148  	tests := []*starrtest.MockData{
   149  		{
   150  			Name:            "200",
   151  			ExpectedPath:    path.Join("/", starr.API, lidarr.APIver, "downloadClient", "1"),
   152  			ExpectedRequest: "",
   153  			ExpectedMethod:  "GET",
   154  			ResponseStatus:  200,
   155  			ResponseBody:    downloadClientResponseBody,
   156  			WithRequest:     nil,
   157  			WithResponse: &lidarr.DownloadClientOutput{
   158  				Enable:             true,
   159  				Priority:           1,
   160  				ID:                 3,
   161  				ConfigContract:     "TransmissionSettings",
   162  				Implementation:     "Transmission",
   163  				ImplementationName: "Transmission",
   164  				InfoLink:           "https://wiki.servarr.com/lidarr/supported#transmission",
   165  				Name:               "Transmission",
   166  				Protocol:           "torrent",
   167  				Fields: []*starr.FieldOutput{
   168  					{
   169  						Order:    0,
   170  						Name:     "host",
   171  						Label:    "Host",
   172  						Value:    "transmission",
   173  						Type:     "textbox",
   174  						Advanced: false,
   175  					},
   176  					{
   177  						Order:    1,
   178  						Name:     "port",
   179  						Label:    "Port",
   180  						Value:    float64(9091),
   181  						Type:     "textbox",
   182  						Advanced: false,
   183  					},
   184  					{
   185  						Order:    2,
   186  						Name:     "useSsl",
   187  						Label:    "Use SSL",
   188  						HelpText: "Use secure connection when connecting to Transmission",
   189  						Value:    false,
   190  						Type:     "checkbox",
   191  						Advanced: false,
   192  					},
   193  				},
   194  				Tags: []int{},
   195  			},
   196  			WithError: nil,
   197  		},
   198  		{
   199  			Name:           "404",
   200  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient", "1"),
   201  			ExpectedMethod: "GET",
   202  			ResponseStatus: 404,
   203  			ResponseBody:   `{"message": "NotFound"}`,
   204  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   205  			WithResponse:   (*lidarr.DownloadClientOutput)(nil),
   206  		},
   207  	}
   208  
   209  	for _, test := range tests {
   210  		test := test
   211  		t.Run(test.Name, func(t *testing.T) {
   212  			t.Parallel()
   213  			mockServer := test.GetMockServer(t)
   214  			client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   215  			output, err := client.GetDownloadClient(1)
   216  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   217  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   218  		})
   219  	}
   220  }
   221  
   222  func TestAddDownloadClient(t *testing.T) {
   223  	t.Parallel()
   224  
   225  	tests := []*starrtest.MockData{
   226  		{
   227  			Name:           "200",
   228  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient"),
   229  			ExpectedMethod: "POST",
   230  			ResponseStatus: 200,
   231  			WithRequest: &lidarr.DownloadClientInput{
   232  				Enable:                   true,
   233  				RemoveCompletedDownloads: false,
   234  				RemoveFailedDownloads:    false,
   235  				Priority:                 1,
   236  				ConfigContract:           "TransmissionSettings",
   237  				Implementation:           "Transmission",
   238  				Name:                     "Transmission",
   239  				Protocol:                 "torrent",
   240  				Fields: []*starr.FieldInput{
   241  					{
   242  						Name:  "host",
   243  						Value: "transmission",
   244  					},
   245  					{
   246  						Name:  "port",
   247  						Value: 9091,
   248  					},
   249  					{
   250  						Name:  "useSSL",
   251  						Value: false,
   252  					},
   253  				},
   254  			},
   255  			ExpectedRequest: addDownloadClient + "\n",
   256  			ResponseBody:    downloadClientResponseBody,
   257  			WithResponse: &lidarr.DownloadClientOutput{
   258  				Enable:             true,
   259  				Priority:           1,
   260  				ID:                 3,
   261  				ConfigContract:     "TransmissionSettings",
   262  				Implementation:     "Transmission",
   263  				ImplementationName: "Transmission",
   264  				InfoLink:           "https://wiki.servarr.com/lidarr/supported#transmission",
   265  				Name:               "Transmission",
   266  				Protocol:           "torrent",
   267  				Fields: []*starr.FieldOutput{
   268  					{
   269  						Order:    0,
   270  						Name:     "host",
   271  						Label:    "Host",
   272  						Value:    "transmission",
   273  						Type:     "textbox",
   274  						Advanced: false,
   275  					},
   276  					{
   277  						Order:    1,
   278  						Name:     "port",
   279  						Label:    "Port",
   280  						Value:    float64(9091),
   281  						Type:     "textbox",
   282  						Advanced: false,
   283  					},
   284  					{
   285  						Order:    2,
   286  						Name:     "useSsl",
   287  						Label:    "Use SSL",
   288  						HelpText: "Use secure connection when connecting to Transmission",
   289  						Value:    false,
   290  						Type:     "checkbox",
   291  						Advanced: false,
   292  					},
   293  				},
   294  				Tags: []int{},
   295  			},
   296  			WithError: nil,
   297  		},
   298  		{
   299  			Name:           "404",
   300  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient"),
   301  			ExpectedMethod: "POST",
   302  			ResponseStatus: 404,
   303  			WithRequest: &lidarr.DownloadClientInput{
   304  				Enable:                   true,
   305  				RemoveCompletedDownloads: false,
   306  				RemoveFailedDownloads:    false,
   307  				Priority:                 1,
   308  				ConfigContract:           "TransmissionSettings",
   309  				Implementation:           "Transmission",
   310  				Name:                     "Transmission",
   311  				Protocol:                 "torrent",
   312  				Fields: []*starr.FieldInput{
   313  					{
   314  						Name:  "host",
   315  						Value: "transmission",
   316  					},
   317  					{
   318  						Name:  "port",
   319  						Value: 9091,
   320  					},
   321  					{
   322  						Name:  "useSSL",
   323  						Value: false,
   324  					},
   325  				},
   326  			},
   327  			ExpectedRequest: addDownloadClient + "\n",
   328  			ResponseBody:    `{"message": "NotFound"}`,
   329  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   330  			WithResponse:    (*lidarr.DownloadClientOutput)(nil),
   331  		},
   332  	}
   333  
   334  	for _, test := range tests {
   335  		test := test
   336  		t.Run(test.Name, func(t *testing.T) {
   337  			t.Parallel()
   338  			mockServer := test.GetMockServer(t)
   339  			client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   340  			output, err := client.AddDownloadClient(test.WithRequest.(*lidarr.DownloadClientInput))
   341  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   342  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   343  		})
   344  	}
   345  }
   346  
   347  func TestUpdateDownloadClient(t *testing.T) {
   348  	t.Parallel()
   349  
   350  	tests := []*starrtest.MockData{
   351  		{
   352  			Name:           "200",
   353  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient", "3?forceSave=false"),
   354  			ExpectedMethod: "PUT",
   355  			ResponseStatus: 200,
   356  			WithRequest: &lidarr.DownloadClientInput{
   357  				Enable:                   true,
   358  				RemoveCompletedDownloads: false,
   359  				RemoveFailedDownloads:    false,
   360  				Priority:                 1,
   361  				ConfigContract:           "TransmissionSettings",
   362  				Implementation:           "Transmission",
   363  				Name:                     "Transmission",
   364  				Protocol:                 "torrent",
   365  				Fields: []*starr.FieldInput{
   366  					{
   367  						Name:  "host",
   368  						Value: "transmission",
   369  					},
   370  					{
   371  						Name:  "port",
   372  						Value: 9091,
   373  					},
   374  					{
   375  						Name:  "useSSL",
   376  						Value: false,
   377  					},
   378  				},
   379  				ID: 3,
   380  			},
   381  			ExpectedRequest: updateDownloadClient + "\n",
   382  			ResponseBody:    downloadClientResponseBody,
   383  			WithResponse: &lidarr.DownloadClientOutput{
   384  				Enable:             true,
   385  				Priority:           1,
   386  				ID:                 3,
   387  				ConfigContract:     "TransmissionSettings",
   388  				Implementation:     "Transmission",
   389  				ImplementationName: "Transmission",
   390  				InfoLink:           "https://wiki.servarr.com/lidarr/supported#transmission",
   391  				Name:               "Transmission",
   392  				Protocol:           "torrent",
   393  				Fields: []*starr.FieldOutput{
   394  					{
   395  						Order:    0,
   396  						Name:     "host",
   397  						Label:    "Host",
   398  						Value:    "transmission",
   399  						Type:     "textbox",
   400  						Advanced: false,
   401  					},
   402  					{
   403  						Order:    1,
   404  						Name:     "port",
   405  						Label:    "Port",
   406  						Value:    float64(9091),
   407  						Type:     "textbox",
   408  						Advanced: false,
   409  					},
   410  					{
   411  						Order:    2,
   412  						Name:     "useSsl",
   413  						Label:    "Use SSL",
   414  						HelpText: "Use secure connection when connecting to Transmission",
   415  						Value:    false,
   416  						Type:     "checkbox",
   417  						Advanced: false,
   418  					},
   419  				},
   420  				Tags: []int{},
   421  			},
   422  			WithError: nil,
   423  		},
   424  		{
   425  			Name:           "404",
   426  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient", "3?forceSave=false"),
   427  			ExpectedMethod: "PUT",
   428  			ResponseStatus: 404,
   429  			WithRequest: &lidarr.DownloadClientInput{
   430  				Enable:                   true,
   431  				RemoveCompletedDownloads: false,
   432  				RemoveFailedDownloads:    false,
   433  				Priority:                 1,
   434  				ConfigContract:           "TransmissionSettings",
   435  				Implementation:           "Transmission",
   436  				Name:                     "Transmission",
   437  				Protocol:                 "torrent",
   438  				Fields: []*starr.FieldInput{
   439  					{
   440  						Name:  "host",
   441  						Value: "transmission",
   442  					},
   443  					{
   444  						Name:  "port",
   445  						Value: 9091,
   446  					},
   447  					{
   448  						Name:  "useSSL",
   449  						Value: false,
   450  					},
   451  				},
   452  				ID: 3,
   453  			},
   454  			ExpectedRequest: updateDownloadClient + "\n",
   455  			ResponseBody:    `{"message": "NotFound"}`,
   456  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   457  			WithResponse:    (*lidarr.DownloadClientOutput)(nil),
   458  		},
   459  	}
   460  
   461  	for _, test := range tests {
   462  		test := test
   463  		t.Run(test.Name, func(t *testing.T) {
   464  			t.Parallel()
   465  			mockServer := test.GetMockServer(t)
   466  			client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   467  			output, err := client.UpdateDownloadClient(test.WithRequest.(*lidarr.DownloadClientInput), false)
   468  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   469  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   470  		})
   471  	}
   472  }
   473  
   474  func TestDeleteDownloadClient(t *testing.T) {
   475  	t.Parallel()
   476  
   477  	tests := []*starrtest.MockData{
   478  		{
   479  			Name:           "200",
   480  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient", "2"),
   481  			ExpectedMethod: "DELETE",
   482  			WithRequest:    int64(2),
   483  			ResponseStatus: 200,
   484  			ResponseBody:   "{}",
   485  			WithError:      nil,
   486  		},
   487  		{
   488  			Name:           "404",
   489  			ExpectedPath:   path.Join("/", starr.API, lidarr.APIver, "downloadClient", "2"),
   490  			ExpectedMethod: "DELETE",
   491  			WithRequest:    int64(2),
   492  			ResponseStatus: 404,
   493  			ResponseBody:   `{"message": "NotFound"}`,
   494  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   495  		},
   496  	}
   497  
   498  	for _, test := range tests {
   499  		test := test
   500  		t.Run(test.Name, func(t *testing.T) {
   501  			t.Parallel()
   502  			mockServer := test.GetMockServer(t)
   503  			client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   504  			err := client.DeleteDownloadClient(test.WithRequest.(int64))
   505  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   506  		})
   507  	}
   508  }