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