golift.io/starr@v1.0.0/sonarr/indexer_test.go (about)

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