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

     1  package prowlarr_test
     2  
     3  import (
     4  	"net/http"
     5  	"path"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"golift.io/starr"
    11  	"golift.io/starr/prowlarr"
    12  	"golift.io/starr/starrtest"
    13  )
    14  
    15  const (
    16  	indexerResponseBody = `{
    17      "indexerUrls": [
    18          "https://torrentapi.org"
    19      ],
    20      "legacyUrls": [],
    21      "definitionName": "Rarbg",
    22      "description": "RARBG is a Public torrent site for MOVIES / TV / GENERAL",
    23      "language": "en-US",
    24      "encoding": "Unicode (UTF-8)",
    25      "enable": false,
    26      "redirect": false,
    27      "supportsRss": true,
    28      "supportsSearch": true,
    29      "supportsRedirect": false,
    30      "appProfileId": 0,
    31      "protocol": "torrent",
    32      "privacy": "public",
    33      "capabilities": {
    34          "limitsMax": 100,
    35          "limitsDefault": 100,
    36          "categories": [
    37              {
    38                  "id": 2000,
    39                  "name": "Movies",
    40                  "subCategories": [
    41                      {
    42                          "id": 2030,
    43                          "name": "Movies/SD",
    44                          "subCategories": []
    45                      }
    46                  ]
    47              }
    48          ],
    49          "supportsRawSearch": false,
    50          "searchParams": [
    51              "q"
    52          ],
    53          "tvSearchParams": [
    54              "q",
    55              "season",
    56              "ep",
    57              "imdbId",
    58              "tvdbId"
    59          ],
    60          "movieSearchParams": [
    61              "q",
    62              "imdbId",
    63              "tmdbId"
    64          ],
    65          "musicSearchParams": [
    66              "q"
    67          ],
    68          "bookSearchParams": []
    69      },
    70      "priority": 25,
    71      "added": "2019-06-04T01:00:00Z",
    72      "sortName": "nyaa",
    73      "name": "Nyaa",
    74      "fields": [
    75          {
    76              "order": 0,
    77              "name": "baseUrl",
    78              "label": "Base Url",
    79              "helpText": "Select which baseurl Prowlarr will use for requests to the site",
    80              "value": "http://nyaa.si",
    81              "type": "select",
    82              "advanced": false,
    83              "selectOptionsProviderAction": "getUrls"
    84          },
    85          {
    86              "order": 1,
    87              "name": "rankedOnly",
    88              "label": "Ranked Only",
    89              "helpText": "Only include ranked results.",
    90              "value": false,
    91              "type": "checkbox",
    92              "advanced": false
    93          }
    94      ],
    95      "implementationName": "Rarbg",
    96      "implementation": "Rarbg",
    97      "configContract": "RarbgSettings",
    98      "infoLink": "https://wiki.servarr.com/prowlarr/supported-indexers#rarbg",
    99      "tags": [],
   100      "id": 2
   101  }`
   102  	addIndexer = `{"enable":false,"redirect":false,"priority":25,"appProfileId":2,` +
   103  		`"configContract":"RarbgSettings","implementation":"Rarbg","name":"Nyaa","protocol":"torrent",` +
   104  		`"fields":[{"name":"baseUrl","value":"http://nyaa.si"},{"name":"animeStandardFormatSearch","value":false}]}`
   105  	updateIndexer = `{"enable":false,"redirect":false,"priority":25,"id":2,"appProfileId":2,` +
   106  		`"configContract":"RarbgSettings","implementation":"Rarbg","name":"Nyaa","protocol":"torrent",` +
   107  		`"fields":[{"name":"baseUrl","value":"http://nyaa.si"},{"name":"animeStandardFormatSearch","value":false}]}`
   108  )
   109  
   110  var (
   111  	loc, _          = time.LoadLocation("")
   112  	date            = time.Date(2019, 6, 4, 1, 0, 0, 0, loc)
   113  	indexerResponse = prowlarr.IndexerOutput{
   114  		IndexerUrls:      []string{"https://torrentapi.org"},
   115  		LegacyUrls:       []string{},
   116  		DefinitionName:   "Rarbg",
   117  		Description:      "RARBG is a Public torrent site for MOVIES / TV / GENERAL",
   118  		Language:         "en-US",
   119  		Encoding:         "Unicode (UTF-8)",
   120  		Enable:           false,
   121  		Redirect:         false,
   122  		SupportsRss:      true,
   123  		SupportsSearch:   true,
   124  		SupportsRedirect: false,
   125  		AppProfileID:     0,
   126  		Protocol:         "torrent",
   127  		Privacy:          "public",
   128  		Capabilities: &prowlarr.Capabilities{
   129  			LimitsMax:     100,
   130  			LimitsDefault: 100,
   131  			Categories: []*prowlarr.Categories{
   132  				{
   133  					ID:   2000,
   134  					Name: "Movies",
   135  					SubCategories: []*prowlarr.Categories{
   136  						{
   137  							ID:            2030,
   138  							Name:          "Movies/SD",
   139  							SubCategories: []*prowlarr.Categories{},
   140  						},
   141  					},
   142  				},
   143  			},
   144  			SupportsRawSearch: false,
   145  			SearchParams:      []string{"q"},
   146  			TvSearchParams: []string{
   147  				"q",
   148  				"season",
   149  				"ep",
   150  				"imdbId",
   151  				"tvdbId",
   152  			},
   153  			MovieSearchParams: []string{
   154  				"q",
   155  				"imdbId",
   156  				"tmdbId",
   157  			},
   158  			MusicSearchParams: []string{"q"},
   159  			BookSearchParams:  []string{},
   160  		},
   161  		Priority: 25,
   162  		Added:    date,
   163  		SortName: "nyaa",
   164  		Name:     "Nyaa",
   165  		Fields: []*starr.FieldOutput{
   166  			{
   167  				Order:                       0,
   168  				Name:                        "baseUrl",
   169  				Label:                       "Base Url",
   170  				HelpText:                    "Select which baseurl Prowlarr will use for requests to the site",
   171  				Value:                       "http://nyaa.si",
   172  				Type:                        "select",
   173  				Advanced:                    false,
   174  				SelectOptionsProviderAction: "getUrls",
   175  			},
   176  			{
   177  				Order:    1,
   178  				Name:     "rankedOnly",
   179  				Label:    "Ranked Only",
   180  				HelpText: "Only include ranked results.",
   181  				Value:    false,
   182  				Type:     "checkbox",
   183  				Advanced: false,
   184  			},
   185  		},
   186  		ImplementationName: "Rarbg",
   187  		Implementation:     "Rarbg",
   188  		ConfigContract:     "RarbgSettings",
   189  		InfoLink:           "https://wiki.servarr.com/prowlarr/supported-indexers#rarbg",
   190  		Tags:               []int{},
   191  		ID:                 2,
   192  	}
   193  )
   194  
   195  func TestGetIndexers(t *testing.T) {
   196  	t.Parallel()
   197  
   198  	tests := []*starrtest.MockData{
   199  		{
   200  			Name:            "200",
   201  			ExpectedPath:    path.Join("/", starr.API, prowlarr.APIver, "indexer"),
   202  			ExpectedRequest: "",
   203  			ExpectedMethod:  "GET",
   204  			ResponseStatus:  200,
   205  			ResponseBody:    "[" + indexerResponseBody + "]",
   206  			WithRequest:     nil,
   207  			WithResponse: []*prowlarr.IndexerOutput{
   208  				&indexerResponse,
   209  			},
   210  			WithError: nil,
   211  		},
   212  		{
   213  			Name:           "404",
   214  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer"),
   215  			ExpectedMethod: "GET",
   216  			ResponseStatus: 404,
   217  			ResponseBody:   `{"message": "NotFound"}`,
   218  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   219  			WithResponse:   ([]*prowlarr.IndexerOutput)(nil),
   220  		},
   221  	}
   222  
   223  	for _, test := range tests {
   224  		test := test
   225  		t.Run(test.Name, func(t *testing.T) {
   226  			t.Parallel()
   227  			mockServer := test.GetMockServer(t)
   228  			client := prowlarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   229  			output, err := client.GetIndexers()
   230  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   231  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   232  		})
   233  	}
   234  }
   235  
   236  func TestGetIndexer(t *testing.T) {
   237  	t.Parallel()
   238  
   239  	tests := []*starrtest.MockData{
   240  		{
   241  			Name:            "200",
   242  			ExpectedPath:    path.Join("/", starr.API, prowlarr.APIver, "indexer", "2"),
   243  			ExpectedRequest: "",
   244  			ExpectedMethod:  "GET",
   245  			ResponseStatus:  200,
   246  			ResponseBody:    indexerResponseBody,
   247  			WithRequest:     int64(2),
   248  			WithResponse:    &indexerResponse,
   249  			WithError:       nil,
   250  		},
   251  		{
   252  			Name:           "404",
   253  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer", "2"),
   254  			ExpectedMethod: "GET",
   255  			ResponseStatus: 404,
   256  			ResponseBody:   `{"message": "NotFound"}`,
   257  			WithRequest:    int64(2),
   258  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   259  			WithResponse:   (*prowlarr.IndexerOutput)(nil),
   260  		},
   261  	}
   262  
   263  	for _, test := range tests {
   264  		test := test
   265  		t.Run(test.Name, func(t *testing.T) {
   266  			t.Parallel()
   267  			mockServer := test.GetMockServer(t)
   268  			client := prowlarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   269  			output, err := client.GetIndexer(test.WithRequest.(int64))
   270  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   271  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   272  		})
   273  	}
   274  }
   275  
   276  func TestAddIndexer(t *testing.T) {
   277  	t.Parallel()
   278  
   279  	tests := []*starrtest.MockData{
   280  		{
   281  			Name:           "200",
   282  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer"),
   283  			ExpectedMethod: "POST",
   284  			ResponseStatus: 200,
   285  			WithRequest: &prowlarr.IndexerInput{
   286  				Enable:       false,
   287  				Protocol:     "torrent",
   288  				AppProfileID: 2,
   289  				Priority:     25,
   290  				Name:         "Nyaa",
   291  				Fields: []*starr.FieldInput{
   292  					{
   293  						Name:  "baseUrl",
   294  						Value: "http://nyaa.si",
   295  					},
   296  					{
   297  						Name:  "animeStandardFormatSearch",
   298  						Value: false,
   299  					},
   300  				},
   301  				Implementation: "Rarbg",
   302  				ConfigContract: "RarbgSettings",
   303  			},
   304  			ExpectedRequest: addIndexer + "\n",
   305  			ResponseBody:    indexerResponseBody,
   306  			WithResponse:    &indexerResponse,
   307  			WithError:       nil,
   308  		},
   309  		{
   310  			Name:           "404",
   311  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer"),
   312  			ExpectedMethod: "POST",
   313  			ResponseStatus: 404,
   314  			WithRequest: &prowlarr.IndexerInput{
   315  				Enable:       false,
   316  				Protocol:     "torrent",
   317  				AppProfileID: 2,
   318  				Priority:     25,
   319  				Name:         "Nyaa",
   320  				Fields: []*starr.FieldInput{
   321  					{
   322  						Name:  "baseUrl",
   323  						Value: "http://nyaa.si",
   324  					},
   325  					{
   326  						Name:  "animeStandardFormatSearch",
   327  						Value: false,
   328  					},
   329  				},
   330  				Implementation: "Rarbg",
   331  				ConfigContract: "RarbgSettings",
   332  			},
   333  			ExpectedRequest: addIndexer + "\n",
   334  			ResponseBody:    `{"message": "NotFound"}`,
   335  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   336  			WithResponse:    (*prowlarr.IndexerOutput)(nil),
   337  		},
   338  	}
   339  
   340  	for _, test := range tests {
   341  		test := test
   342  		t.Run(test.Name, func(t *testing.T) {
   343  			t.Parallel()
   344  			mockServer := test.GetMockServer(t)
   345  			client := prowlarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   346  			output, err := client.AddIndexer(test.WithRequest.(*prowlarr.IndexerInput))
   347  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   348  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   349  		})
   350  	}
   351  }
   352  
   353  func TestUpdateIndexer(t *testing.T) {
   354  	t.Parallel()
   355  
   356  	tests := []*starrtest.MockData{
   357  		{
   358  			Name:           "200",
   359  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer", "2?forceSave=false"),
   360  			ExpectedMethod: "PUT",
   361  			ResponseStatus: 200,
   362  			WithRequest: &prowlarr.IndexerInput{
   363  				Enable:       false,
   364  				Protocol:     "torrent",
   365  				AppProfileID: 2,
   366  				Priority:     25,
   367  				Name:         "Nyaa",
   368  				Fields: []*starr.FieldInput{
   369  					{
   370  						Name:  "baseUrl",
   371  						Value: "http://nyaa.si",
   372  					},
   373  					{
   374  						Name:  "animeStandardFormatSearch",
   375  						Value: false,
   376  					},
   377  				},
   378  				Implementation: "Rarbg",
   379  				ConfigContract: "RarbgSettings",
   380  				ID:             2,
   381  			},
   382  			ExpectedRequest: updateIndexer + "\n",
   383  			ResponseBody:    indexerResponseBody,
   384  			WithResponse:    &indexerResponse,
   385  			WithError:       nil,
   386  		},
   387  		{
   388  			Name:           "404",
   389  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer", "2?forceSave=false"),
   390  			ExpectedMethod: "PUT",
   391  			ResponseStatus: 404,
   392  			WithRequest: &prowlarr.IndexerInput{
   393  				Enable:       false,
   394  				Protocol:     "torrent",
   395  				AppProfileID: 2,
   396  				Priority:     25,
   397  				Name:         "Nyaa",
   398  				Fields: []*starr.FieldInput{
   399  					{
   400  						Name:  "baseUrl",
   401  						Value: "http://nyaa.si",
   402  					},
   403  					{
   404  						Name:  "animeStandardFormatSearch",
   405  						Value: false,
   406  					},
   407  				},
   408  				Implementation: "Rarbg",
   409  				ConfigContract: "RarbgSettings",
   410  				ID:             2,
   411  			},
   412  			ExpectedRequest: updateIndexer + "\n",
   413  			ResponseBody:    `{"message": "NotFound"}`,
   414  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   415  			WithResponse:    (*prowlarr.IndexerOutput)(nil),
   416  		},
   417  	}
   418  
   419  	for _, test := range tests {
   420  		test := test
   421  		t.Run(test.Name, func(t *testing.T) {
   422  			t.Parallel()
   423  			mockServer := test.GetMockServer(t)
   424  			client := prowlarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   425  			output, err := client.UpdateIndexer(test.WithRequest.(*prowlarr.IndexerInput), false)
   426  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   427  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   428  		})
   429  	}
   430  }
   431  
   432  func TestDeleteIndexer(t *testing.T) {
   433  	t.Parallel()
   434  
   435  	tests := []*starrtest.MockData{
   436  		{
   437  			Name:           "200",
   438  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer", "2"),
   439  			ExpectedMethod: "DELETE",
   440  			WithRequest:    int64(2),
   441  			ResponseStatus: 200,
   442  			ResponseBody:   "{}",
   443  			WithError:      nil,
   444  		},
   445  		{
   446  			Name:           "404",
   447  			ExpectedPath:   path.Join("/", starr.API, prowlarr.APIver, "indexer", "2"),
   448  			ExpectedMethod: "DELETE",
   449  			WithRequest:    int64(2),
   450  			ResponseStatus: 404,
   451  			ResponseBody:   `{"message": "NotFound"}`,
   452  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   453  		},
   454  	}
   455  
   456  	for _, test := range tests {
   457  		test := test
   458  		t.Run(test.Name, func(t *testing.T) {
   459  			t.Parallel()
   460  			mockServer := test.GetMockServer(t)
   461  			client := prowlarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   462  			err := client.DeleteIndexer(test.WithRequest.(int64))
   463  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   464  		})
   465  	}
   466  }