golift.io/starr@v1.0.0/radarr/customformat_test.go (about)

     1  package radarr_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/radarr"
    11  	"golift.io/starr/starrtest"
    12  )
    13  
    14  const customFormatResponseBody = `{
    15      "id": 1,
    16      "name": "test",
    17      "includeCustomFormatWhenRenaming": false,
    18      "specifications": [
    19          {
    20              "name": "Surround Sound",
    21              "implementation": "ReleaseTitleSpecification",
    22              "implementationName": "Release Title",
    23              "infoLink": "https://wiki.servarr.com/radarr/settings#custom-formats-2",
    24              "negate": false,
    25              "required": false,
    26              "fields": [
    27                  {
    28                      "order": 0,
    29                      "name": "value",
    30                      "label": "Regular Expression",
    31                      "helpText": "Custom Format RegEx is Case Insensitive",
    32                      "value": "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
    33                      "type": "textbox",
    34                      "advanced": false
    35                  }
    36              ]
    37          },
    38          {
    39              "name": "Arabic",
    40              "implementation": "LanguageSpecification",
    41              "implementationName": "Language",
    42              "infoLink": "https://wiki.servarr.com/radarr/settings#custom-formats-2",
    43              "negate": false,
    44              "required": false,
    45              "fields": [
    46                  {
    47                      "order": 0,
    48                      "name": "value",
    49                      "label": "Language",
    50                      "value": 31,
    51                      "type": "select",
    52                      "advanced": false,
    53                      "selectOptions": [
    54                          {
    55                              "value": 0,
    56                              "name": "Unknown",
    57                              "order": 0,
    58                              "dividerAfter": true
    59                          },
    60                          {
    61                              "value": 31,
    62                              "name": "Arabic",
    63                              "order": 0,
    64                              "dividerAfter": false
    65                          }
    66                      ]
    67                  }
    68              ]
    69          }
    70      ]
    71  }`
    72  
    73  const addCustomFormat = `{"name":"test","includeCustomFormatWhenRenaming":false,"specifications":` +
    74  	`[{"name":"Surround Sound","implementation":"ReleaseTitleSpecification","negate":false,"required":false,"fields":` +
    75  	`[{"name":"value","value":"DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])"}]},{"name":"Arabic",` +
    76  	`"implementation":"LanguageSpecification","negate":false,"required":false,"fields":[{"name":"value","value":31}]}]}`
    77  
    78  const updateCustomFormat = `{"id":1,"name":"test","includeCustomFormatWhenRenaming":false,"specifications":` +
    79  	`[{"name":"Surround Sound","implementation":"ReleaseTitleSpecification","negate":false,"required":false,"fields":` +
    80  	`[{"name":"value","value":"DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])"}]},{"name":"Arabic",` +
    81  	`"implementation":"LanguageSpecification","negate":false,"required":false,"fields":[{"name":"value","value":31}]}]}`
    82  
    83  func TestGetCustomFormats(t *testing.T) {
    84  	t.Parallel()
    85  
    86  	tests := []*starrtest.MockData{
    87  		{
    88  			Name:            "200",
    89  			ExpectedPath:    path.Join("/", starr.API, radarr.APIver, "customFormat"),
    90  			ExpectedRequest: "",
    91  			ExpectedMethod:  "GET",
    92  			ResponseStatus:  200,
    93  			ResponseBody:    "[" + customFormatResponseBody + "]",
    94  			WithRequest:     nil,
    95  			WithResponse: []*radarr.CustomFormatOutput{
    96  				{
    97  					ID:                    1,
    98  					Name:                  "test",
    99  					IncludeCFWhenRenaming: false,
   100  					Specifications: []*radarr.CustomFormatOutputSpec{
   101  						{
   102  							Name:               "Surround Sound",
   103  							Implementation:     "ReleaseTitleSpecification",
   104  							ImplementationName: "Release Title",
   105  							InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   106  							Negate:             false,
   107  							Required:           false,
   108  							Fields: []*starr.FieldOutput{
   109  								{
   110  									Order:    0,
   111  									Name:     "value",
   112  									Label:    "Regular Expression",
   113  									HelpText: "Custom Format RegEx is Case Insensitive",
   114  									Value:    "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   115  									Type:     "textbox",
   116  									Advanced: false,
   117  								},
   118  							},
   119  						},
   120  						{
   121  							Name:               "Arabic",
   122  							Implementation:     "LanguageSpecification",
   123  							ImplementationName: "Language",
   124  							InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   125  							Negate:             false,
   126  							Required:           false,
   127  							Fields: []*starr.FieldOutput{
   128  								{
   129  									Order: 0,
   130  									Name:  "value",
   131  									Label: "Language",
   132  									// float because of unmarshal.
   133  									Value:    float64(31),
   134  									Type:     "select",
   135  									Advanced: false,
   136  									SelectOptions: []*starr.SelectOption{
   137  										{
   138  											Value:        0,
   139  											Name:         "Unknown",
   140  											Order:        0,
   141  											DividerAfter: true,
   142  										},
   143  										{
   144  											Value:        31,
   145  											Name:         "Arabic",
   146  											Order:        0,
   147  											DividerAfter: false,
   148  										},
   149  									},
   150  								},
   151  							},
   152  						},
   153  					},
   154  				},
   155  			},
   156  			WithError: nil,
   157  		},
   158  		{
   159  			Name:           "404",
   160  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat"),
   161  			ExpectedMethod: "GET",
   162  			ResponseStatus: 404,
   163  			ResponseBody:   `{"message": "NotFound"}`,
   164  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   165  			WithResponse:   ([]*radarr.CustomFormatOutput)(nil),
   166  		},
   167  	}
   168  
   169  	for _, test := range tests {
   170  		test := test
   171  		t.Run(test.Name, func(t *testing.T) {
   172  			t.Parallel()
   173  			mockServer := test.GetMockServer(t)
   174  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   175  			output, err := client.GetCustomFormats()
   176  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   177  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   178  		})
   179  	}
   180  }
   181  
   182  func TestGetCustomFormat(t *testing.T) {
   183  	t.Parallel()
   184  
   185  	tests := []*starrtest.MockData{
   186  		{
   187  			Name:            "200",
   188  			ExpectedPath:    path.Join("/", starr.API, radarr.APIver, "customFormat", "1"),
   189  			ExpectedRequest: "",
   190  			ExpectedMethod:  "GET",
   191  			ResponseStatus:  200,
   192  			ResponseBody:    customFormatResponseBody,
   193  			WithRequest:     nil,
   194  			WithResponse: &radarr.CustomFormatOutput{
   195  				ID:                    1,
   196  				Name:                  "test",
   197  				IncludeCFWhenRenaming: false,
   198  				Specifications: []*radarr.CustomFormatOutputSpec{
   199  					{
   200  						Name:               "Surround Sound",
   201  						Implementation:     "ReleaseTitleSpecification",
   202  						ImplementationName: "Release Title",
   203  						InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   204  						Negate:             false,
   205  						Required:           false,
   206  						Fields: []*starr.FieldOutput{
   207  							{
   208  								Order:    0,
   209  								Name:     "value",
   210  								Label:    "Regular Expression",
   211  								HelpText: "Custom Format RegEx is Case Insensitive",
   212  								Value:    "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   213  								Type:     "textbox",
   214  								Advanced: false,
   215  							},
   216  						},
   217  					},
   218  					{
   219  						Name:               "Arabic",
   220  						Implementation:     "LanguageSpecification",
   221  						ImplementationName: "Language",
   222  						InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   223  						Negate:             false,
   224  						Required:           false,
   225  						Fields: []*starr.FieldOutput{
   226  							{
   227  								Order: 0,
   228  								Name:  "value",
   229  								Label: "Language",
   230  								// float because of unmarshal.
   231  								Value:    float64(31),
   232  								Type:     "select",
   233  								Advanced: false,
   234  								SelectOptions: []*starr.SelectOption{
   235  									{
   236  										Value:        0,
   237  										Name:         "Unknown",
   238  										Order:        0,
   239  										DividerAfter: true,
   240  									},
   241  									{
   242  										Value:        31,
   243  										Name:         "Arabic",
   244  										Order:        0,
   245  										DividerAfter: false,
   246  									},
   247  								},
   248  							},
   249  						},
   250  					},
   251  				},
   252  			},
   253  			WithError: nil,
   254  		},
   255  		{
   256  			Name:           "404",
   257  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat", "1"),
   258  			ExpectedMethod: "GET",
   259  			ResponseStatus: 404,
   260  			ResponseBody:   `{"message": "NotFound"}`,
   261  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   262  			WithResponse:   (*radarr.CustomFormatOutput)(nil),
   263  		},
   264  	}
   265  
   266  	for _, test := range tests {
   267  		test := test
   268  		t.Run(test.Name, func(t *testing.T) {
   269  			t.Parallel()
   270  			mockServer := test.GetMockServer(t)
   271  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   272  			output, err := client.GetCustomFormat(1)
   273  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   274  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   275  		})
   276  	}
   277  }
   278  
   279  func TestAddCustomFormat(t *testing.T) {
   280  	t.Parallel()
   281  
   282  	tests := []*starrtest.MockData{
   283  		{
   284  			Name:           "200",
   285  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat"),
   286  			ExpectedMethod: "POST",
   287  			ResponseStatus: 200,
   288  			WithRequest: &radarr.CustomFormatInput{
   289  				IncludeCFWhenRenaming: false,
   290  				Name:                  "test",
   291  				Specifications: []*radarr.CustomFormatInputSpec{
   292  					{
   293  						Name:           "Surround Sound",
   294  						Implementation: "ReleaseTitleSpecification",
   295  						Negate:         false,
   296  						Required:       false,
   297  						Fields: []*starr.FieldInput{
   298  							{
   299  								Name:  "value",
   300  								Value: "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   301  							},
   302  						},
   303  					},
   304  					{
   305  						Implementation: "LanguageSpecification",
   306  						Negate:         false,
   307  						Required:       false,
   308  						Fields: []*starr.FieldInput{
   309  							{
   310  								Name:  "value",
   311  								Value: 31,
   312  							},
   313  						},
   314  						Name: "Arabic",
   315  					},
   316  				},
   317  			},
   318  			ExpectedRequest: addCustomFormat + "\n",
   319  			ResponseBody:    customFormatResponseBody,
   320  			WithResponse: &radarr.CustomFormatOutput{
   321  				ID:                    1,
   322  				Name:                  "test",
   323  				IncludeCFWhenRenaming: false,
   324  				Specifications: []*radarr.CustomFormatOutputSpec{
   325  					{
   326  						Name:               "Surround Sound",
   327  						Implementation:     "ReleaseTitleSpecification",
   328  						ImplementationName: "Release Title",
   329  						InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   330  						Negate:             false,
   331  						Required:           false,
   332  						Fields: []*starr.FieldOutput{
   333  							{
   334  								Order:    0,
   335  								Name:     "value",
   336  								Label:    "Regular Expression",
   337  								HelpText: "Custom Format RegEx is Case Insensitive",
   338  								Value:    "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   339  								Type:     "textbox",
   340  								Advanced: false,
   341  							},
   342  						},
   343  					},
   344  					{
   345  						Name:               "Arabic",
   346  						Implementation:     "LanguageSpecification",
   347  						ImplementationName: "Language",
   348  						InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   349  						Negate:             false,
   350  						Required:           false,
   351  						Fields: []*starr.FieldOutput{
   352  							{
   353  								Order: 0,
   354  								Name:  "value",
   355  								Label: "Language",
   356  								// float because of unmarshal.
   357  								Value:    float64(31),
   358  								Type:     "select",
   359  								Advanced: false,
   360  								SelectOptions: []*starr.SelectOption{
   361  									{
   362  										Value:        0,
   363  										Name:         "Unknown",
   364  										Order:        0,
   365  										DividerAfter: true,
   366  									},
   367  									{
   368  										Value:        31,
   369  										Name:         "Arabic",
   370  										Order:        0,
   371  										DividerAfter: false,
   372  									},
   373  								},
   374  							},
   375  						},
   376  					},
   377  				},
   378  			},
   379  			WithError: nil,
   380  		},
   381  		{
   382  			Name:           "404",
   383  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat"),
   384  			ExpectedMethod: "POST",
   385  			ResponseStatus: 404,
   386  			WithRequest: &radarr.CustomFormatInput{
   387  				IncludeCFWhenRenaming: false,
   388  				Name:                  "test",
   389  				Specifications: []*radarr.CustomFormatInputSpec{
   390  					{
   391  						Name:           "Surround Sound",
   392  						Implementation: "ReleaseTitleSpecification",
   393  						Negate:         false,
   394  						Required:       false,
   395  						Fields: []*starr.FieldInput{
   396  							{
   397  								Name:  "value",
   398  								Value: "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   399  							},
   400  						},
   401  					},
   402  					{
   403  						Implementation: "LanguageSpecification",
   404  						Negate:         false,
   405  						Required:       false,
   406  						Fields: []*starr.FieldInput{
   407  							{
   408  								Name:  "value",
   409  								Value: 31,
   410  							},
   411  						},
   412  						Name: "Arabic",
   413  					},
   414  				},
   415  			},
   416  			ExpectedRequest: addCustomFormat + "\n",
   417  			ResponseBody:    `{"message": "NotFound"}`,
   418  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   419  			WithResponse:    (*radarr.CustomFormatOutput)(nil),
   420  		},
   421  	}
   422  
   423  	for _, test := range tests {
   424  		test := test
   425  		t.Run(test.Name, func(t *testing.T) {
   426  			t.Parallel()
   427  			mockServer := test.GetMockServer(t)
   428  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   429  			output, err := client.AddCustomFormat(test.WithRequest.(*radarr.CustomFormatInput))
   430  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   431  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   432  		})
   433  	}
   434  }
   435  
   436  func TestUpdateCustomFormat(t *testing.T) {
   437  	t.Parallel()
   438  
   439  	tests := []*starrtest.MockData{
   440  		{
   441  			Name:           "200",
   442  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat", "1"),
   443  			ExpectedMethod: "PUT",
   444  			ResponseStatus: 200,
   445  			WithRequest: &radarr.CustomFormatInput{
   446  				ID:                    1,
   447  				IncludeCFWhenRenaming: false,
   448  				Name:                  "test",
   449  				Specifications: []*radarr.CustomFormatInputSpec{
   450  					{
   451  						Name:           "Surround Sound",
   452  						Implementation: "ReleaseTitleSpecification",
   453  						Negate:         false,
   454  						Required:       false,
   455  						Fields: []*starr.FieldInput{
   456  							{
   457  								Name:  "value",
   458  								Value: "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   459  							},
   460  						},
   461  					},
   462  					{
   463  						Implementation: "LanguageSpecification",
   464  						Negate:         false,
   465  						Required:       false,
   466  						Fields: []*starr.FieldInput{
   467  							{
   468  								Name:  "value",
   469  								Value: 31,
   470  							},
   471  						},
   472  						Name: "Arabic",
   473  					},
   474  				},
   475  			},
   476  			ExpectedRequest: updateCustomFormat + "\n",
   477  			ResponseBody:    customFormatResponseBody,
   478  			WithResponse: &radarr.CustomFormatOutput{
   479  				ID:                    1,
   480  				Name:                  "test",
   481  				IncludeCFWhenRenaming: false,
   482  				Specifications: []*radarr.CustomFormatOutputSpec{
   483  					{
   484  						Name:               "Surround Sound",
   485  						Implementation:     "ReleaseTitleSpecification",
   486  						ImplementationName: "Release Title",
   487  						InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   488  						Negate:             false,
   489  						Required:           false,
   490  						Fields: []*starr.FieldOutput{
   491  							{
   492  								Order:    0,
   493  								Name:     "value",
   494  								Label:    "Regular Expression",
   495  								HelpText: "Custom Format RegEx is Case Insensitive",
   496  								Value:    "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   497  								Type:     "textbox",
   498  								Advanced: false,
   499  							},
   500  						},
   501  					},
   502  					{
   503  						Name:               "Arabic",
   504  						Implementation:     "LanguageSpecification",
   505  						ImplementationName: "Language",
   506  						InfoLink:           "https://wiki.servarr.com/radarr/settings#custom-formats-2",
   507  						Negate:             false,
   508  						Required:           false,
   509  						Fields: []*starr.FieldOutput{
   510  							{
   511  								Order: 0,
   512  								Name:  "value",
   513  								Label: "Language",
   514  								// float because of unmarshal.
   515  								Value:    float64(31),
   516  								Type:     "select",
   517  								Advanced: false,
   518  								SelectOptions: []*starr.SelectOption{
   519  									{
   520  										Value:        0,
   521  										Name:         "Unknown",
   522  										Order:        0,
   523  										DividerAfter: true,
   524  									},
   525  									{
   526  										Value:        31,
   527  										Name:         "Arabic",
   528  										Order:        0,
   529  										DividerAfter: false,
   530  									},
   531  								},
   532  							},
   533  						},
   534  					},
   535  				},
   536  			},
   537  			WithError: nil,
   538  		},
   539  		{
   540  			Name:           "404",
   541  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat", "1"),
   542  			ExpectedMethod: "PUT",
   543  			ResponseStatus: 404,
   544  			WithRequest: &radarr.CustomFormatInput{
   545  				ID:                    1,
   546  				IncludeCFWhenRenaming: false,
   547  				Name:                  "test",
   548  				Specifications: []*radarr.CustomFormatInputSpec{
   549  					{
   550  						Name:           "Surround Sound",
   551  						Implementation: "ReleaseTitleSpecification",
   552  						Negate:         false,
   553  						Required:       false,
   554  						Fields: []*starr.FieldInput{
   555  							{
   556  								Name:  "value",
   557  								Value: "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])",
   558  							},
   559  						},
   560  					},
   561  					{
   562  						Implementation: "LanguageSpecification",
   563  						Negate:         false,
   564  						Required:       false,
   565  						Fields: []*starr.FieldInput{
   566  							{
   567  								Name:  "value",
   568  								Value: 31,
   569  							},
   570  						},
   571  						Name: "Arabic",
   572  					},
   573  				},
   574  			},
   575  			ExpectedRequest: updateCustomFormat + "\n",
   576  			ResponseBody:    `{"message": "NotFound"}`,
   577  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   578  			WithResponse:    (*radarr.CustomFormatOutput)(nil),
   579  		},
   580  	}
   581  
   582  	for _, test := range tests {
   583  		test := test
   584  		t.Run(test.Name, func(t *testing.T) {
   585  			t.Parallel()
   586  			mockServer := test.GetMockServer(t)
   587  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   588  			output, err := client.UpdateCustomFormat(test.WithRequest.(*radarr.CustomFormatInput))
   589  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   590  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   591  		})
   592  	}
   593  }
   594  
   595  func TestDeleteCustomFormat(t *testing.T) {
   596  	t.Parallel()
   597  
   598  	tests := []*starrtest.MockData{
   599  		{
   600  			Name:           "200",
   601  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat", "2"),
   602  			ExpectedMethod: "DELETE",
   603  			WithRequest:    int64(2),
   604  			ResponseStatus: 200,
   605  			ResponseBody:   "{}",
   606  			WithError:      nil,
   607  		},
   608  		{
   609  			Name:           "404",
   610  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "customFormat", "2"),
   611  			ExpectedMethod: "DELETE",
   612  			WithRequest:    int64(2),
   613  			ResponseStatus: 404,
   614  			ResponseBody:   `{"message": "NotFound"}`,
   615  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   616  		},
   617  	}
   618  
   619  	for _, test := range tests {
   620  		test := test
   621  		t.Run(test.Name, func(t *testing.T) {
   622  			t.Parallel()
   623  			mockServer := test.GetMockServer(t)
   624  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   625  			err := client.DeleteCustomFormat(test.WithRequest.(int64))
   626  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   627  		})
   628  	}
   629  }