golift.io/starr@v1.0.0/radarr/qualityprofile_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 (
    15  	qualityProfileResponse = `{
    16  		"name": "test",
    17  		"upgradeAllowed": false,
    18  		"cutoff": 1003,
    19  		"items": [
    20  		  {
    21  			"name": "WEB 2160p",
    22  			"items": [
    23  				{
    24  					"quality": {
    25  					  "id": 18,
    26  					  "name": "WEBDL-2160p",
    27  					  "source": "webdl",
    28  					  "resolution": 2160,
    29  					  "modifier": "none"
    30  					},
    31  					"allowed": true
    32  				  },
    33  				  {
    34  					"quality": {
    35  					  "id": 17,
    36  					  "name": "WEBRip-2160p",
    37  					  "source": "webrip",
    38  					  "resolution": 2160,
    39  					  "modifier": "none"
    40  					},
    41  					"allowed": true
    42  				}
    43  			],
    44  			"allowed": true,
    45  			"id": 1003
    46  		  }
    47  		],
    48  		"minFormatScore": 0,
    49  		"cutoffFormatScore": 0,
    50  		"formatItems": [],
    51  		"language": {
    52  		  "id": 1,
    53  		  "name": "English"
    54  		},
    55  		"id": 7
    56  	  }`
    57  
    58  	addQualityProfileRequest = `{"name":"test","upgradeAllowed":false,"cutoff":1003,"items":[{"name":"WEB 2160p",` +
    59  		`"id":1003,"items":[{"quality":{"id":18,"name":"WEBDL-2160p","source":"webdl","resolution":2160,"modifier":"none"},` +
    60  		`"allowed":true},{"quality":{"id":17,"name":"WEBRip-2160p","source":"webrip","resolution":2160,"modifier":"none"},` +
    61  		`"allowed":true}],"allowed":true}],"minFormatScore":0,"cutoffFormatScore":0,"formatItems":null,` +
    62  		`"language":{"id":1,"name":"English"}}` + "\n"
    63  	updateQualityProfileRequest = `{"id":7,"name":"test","upgradeAllowed":false,"cutoff":1003,"items":` +
    64  		`[{"name":"WEB 2160p","id":1003,"items":[{"quality":{"id":18,"name":"WEBDL-2160p","source":"webdl",` +
    65  		`"resolution":2160,"modifier":"none"},"allowed":true},{"quality":{"id":17,"name":"WEBRip-2160p","source":"webrip",` +
    66  		`"resolution":2160,"modifier":"none"},"allowed":true}],"allowed":true}],"minFormatScore":0,` +
    67  		`"cutoffFormatScore":0,"formatItems":null,"language":{"id":1,"name":"English"}}` + "\n"
    68  )
    69  
    70  func TestGetQualityProfiles(t *testing.T) {
    71  	t.Parallel()
    72  
    73  	tests := []*starrtest.MockData{
    74  		{
    75  			Name:           "200",
    76  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile"),
    77  			ExpectedMethod: "GET",
    78  			ResponseStatus: 200,
    79  			ResponseBody:   `[` + qualityProfileResponse + `]`,
    80  			WithResponse: []*radarr.QualityProfile{
    81  				{
    82  					ID:             7,
    83  					Name:           "test",
    84  					UpgradeAllowed: false,
    85  					Cutoff:         1003,
    86  					FormatItems:    []*starr.FormatItem{},
    87  					Qualities: []*starr.Quality{
    88  						{
    89  							Name: "WEB 2160p",
    90  							ID:   1003,
    91  							Items: []*starr.Quality{
    92  								{
    93  									Allowed: true,
    94  									Quality: &starr.BaseQuality{
    95  										ID:         18,
    96  										Name:       "WEBDL-2160p",
    97  										Source:     "webdl",
    98  										Resolution: 2160,
    99  										Modifier:   "none",
   100  									},
   101  								},
   102  								{
   103  									Allowed: true,
   104  									Quality: &starr.BaseQuality{
   105  										ID:         17,
   106  										Name:       "WEBRip-2160p",
   107  										Source:     "webrip",
   108  										Resolution: 2160,
   109  										Modifier:   "none",
   110  									},
   111  								},
   112  							},
   113  							Allowed: true,
   114  						},
   115  					},
   116  					MinFormatScore:    0,
   117  					CutoffFormatScore: 0,
   118  					Language: &starr.Value{
   119  						ID:   1,
   120  						Name: "English",
   121  					},
   122  				},
   123  			},
   124  			WithError: nil,
   125  		},
   126  		{
   127  			Name:           "404",
   128  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile"),
   129  			ExpectedMethod: "GET",
   130  			ResponseStatus: 404,
   131  			ResponseBody:   `{"message": "NotFound"}`,
   132  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   133  			WithResponse:   []*radarr.QualityProfile(nil),
   134  		},
   135  	}
   136  
   137  	for _, test := range tests {
   138  		test := test
   139  		t.Run(test.Name, func(t *testing.T) {
   140  			t.Parallel()
   141  			mockServer := test.GetMockServer(t)
   142  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   143  			output, err := client.GetQualityProfiles()
   144  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   145  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   146  		})
   147  	}
   148  }
   149  
   150  func TestGetQualityProfile(t *testing.T) {
   151  	t.Parallel()
   152  
   153  	tests := []*starrtest.MockData{
   154  		{
   155  			Name:           "200",
   156  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile", "7"),
   157  			ExpectedMethod: "GET",
   158  			ResponseStatus: 200,
   159  			WithRequest:    int64(7),
   160  			ResponseBody:   qualityProfileResponse,
   161  			WithResponse: &radarr.QualityProfile{
   162  				ID:             7,
   163  				Name:           "test",
   164  				UpgradeAllowed: false,
   165  				Cutoff:         1003,
   166  				FormatItems:    []*starr.FormatItem{},
   167  				Qualities: []*starr.Quality{
   168  					{
   169  						Name: "WEB 2160p",
   170  						ID:   1003,
   171  						Items: []*starr.Quality{
   172  							{
   173  								Allowed: true,
   174  								Quality: &starr.BaseQuality{
   175  									ID:         18,
   176  									Name:       "WEBDL-2160p",
   177  									Source:     "webdl",
   178  									Resolution: 2160,
   179  									Modifier:   "none",
   180  								},
   181  							},
   182  							{
   183  								Allowed: true,
   184  								Quality: &starr.BaseQuality{
   185  									ID:         17,
   186  									Name:       "WEBRip-2160p",
   187  									Source:     "webrip",
   188  									Resolution: 2160,
   189  									Modifier:   "none",
   190  								},
   191  							},
   192  						},
   193  						Allowed: true,
   194  					},
   195  				},
   196  				MinFormatScore:    0,
   197  				CutoffFormatScore: 0,
   198  				Language: &starr.Value{
   199  					ID:   1,
   200  					Name: "English",
   201  				},
   202  			},
   203  			WithError: nil,
   204  		},
   205  		{
   206  			Name:           "404",
   207  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile", "1"),
   208  			ExpectedMethod: "GET",
   209  			ResponseStatus: 404,
   210  			WithRequest:    int64(1),
   211  			ResponseBody:   `{"message": "NotFound"}`,
   212  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   213  			WithResponse:   (*radarr.QualityProfile)(nil),
   214  		},
   215  	}
   216  
   217  	for _, test := range tests {
   218  		test := test
   219  		t.Run(test.Name, func(t *testing.T) {
   220  			t.Parallel()
   221  			mockServer := test.GetMockServer(t)
   222  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   223  			output, err := client.GetQualityProfile(test.WithRequest.(int64))
   224  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   225  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   226  		})
   227  	}
   228  }
   229  
   230  func TestAddQualityProfile(t *testing.T) {
   231  	t.Parallel()
   232  
   233  	tests := []*starrtest.MockData{
   234  		{
   235  			Name:           "200",
   236  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile"),
   237  			ExpectedMethod: "POST",
   238  			ResponseStatus: 200,
   239  			WithRequest: &radarr.QualityProfile{
   240  				Name:   "test",
   241  				Cutoff: 1003,
   242  				Qualities: []*starr.Quality{
   243  					{
   244  						Name: "WEB 2160p",
   245  						ID:   1003,
   246  						Items: []*starr.Quality{
   247  							{
   248  								Allowed: true,
   249  								Quality: &starr.BaseQuality{
   250  									ID:         18,
   251  									Name:       "WEBDL-2160p",
   252  									Source:     "webdl",
   253  									Resolution: 2160,
   254  									Modifier:   "none",
   255  								},
   256  							},
   257  							{
   258  								Allowed: true,
   259  								Quality: &starr.BaseQuality{
   260  									ID:         17,
   261  									Name:       "WEBRip-2160p",
   262  									Source:     "webrip",
   263  									Resolution: 2160,
   264  									Modifier:   "none",
   265  								},
   266  							},
   267  						},
   268  						Allowed: true,
   269  					},
   270  				},
   271  				MinFormatScore:    0,
   272  				CutoffFormatScore: 0,
   273  				Language: &starr.Value{
   274  					ID:   1,
   275  					Name: "English",
   276  				},
   277  			},
   278  			ExpectedRequest: addQualityProfileRequest,
   279  			ResponseBody:    qualityProfileResponse,
   280  			WithResponse: &radarr.QualityProfile{
   281  				ID:             7,
   282  				Name:           "test",
   283  				UpgradeAllowed: false,
   284  				Cutoff:         1003,
   285  				FormatItems:    []*starr.FormatItem{},
   286  				Qualities: []*starr.Quality{
   287  					{
   288  						Name: "WEB 2160p",
   289  						ID:   1003,
   290  						Items: []*starr.Quality{
   291  							{
   292  								Allowed: true,
   293  								Quality: &starr.BaseQuality{
   294  									ID:         18,
   295  									Name:       "WEBDL-2160p",
   296  									Source:     "webdl",
   297  									Resolution: 2160,
   298  									Modifier:   "none",
   299  								},
   300  							},
   301  							{
   302  								Allowed: true,
   303  								Quality: &starr.BaseQuality{
   304  									ID:         17,
   305  									Name:       "WEBRip-2160p",
   306  									Source:     "webrip",
   307  									Resolution: 2160,
   308  									Modifier:   "none",
   309  								},
   310  							},
   311  						},
   312  						Allowed: true,
   313  					},
   314  				},
   315  				MinFormatScore:    0,
   316  				CutoffFormatScore: 0,
   317  				Language: &starr.Value{
   318  					ID:   1,
   319  					Name: "English",
   320  				},
   321  			},
   322  			WithError: nil,
   323  		},
   324  		{
   325  			Name:           "404",
   326  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile"),
   327  			ExpectedMethod: "POST",
   328  			WithRequest: &radarr.QualityProfile{
   329  				Name:   "test",
   330  				Cutoff: 1003,
   331  				Qualities: []*starr.Quality{
   332  					{
   333  						Name: "WEB 2160p",
   334  						ID:   1003,
   335  						Items: []*starr.Quality{
   336  							{
   337  								Allowed: true,
   338  								Quality: &starr.BaseQuality{
   339  									ID:         18,
   340  									Name:       "WEBDL-2160p",
   341  									Source:     "webdl",
   342  									Resolution: 2160,
   343  									Modifier:   "none",
   344  								},
   345  							},
   346  							{
   347  								Allowed: true,
   348  								Quality: &starr.BaseQuality{
   349  									ID:         17,
   350  									Name:       "WEBRip-2160p",
   351  									Source:     "webrip",
   352  									Resolution: 2160,
   353  									Modifier:   "none",
   354  								},
   355  							},
   356  						},
   357  						Allowed: true,
   358  					},
   359  				},
   360  				MinFormatScore:    0,
   361  				CutoffFormatScore: 0,
   362  				Language: &starr.Value{
   363  					ID:   1,
   364  					Name: "English",
   365  				},
   366  			},
   367  			ExpectedRequest: addQualityProfileRequest,
   368  			ResponseStatus:  404,
   369  			ResponseBody:    `{"message": "NotFound"}`,
   370  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   371  			WithResponse:    (*radarr.QualityProfile)(nil),
   372  		},
   373  	}
   374  
   375  	for _, test := range tests {
   376  		test := test
   377  		t.Run(test.Name, func(t *testing.T) {
   378  			t.Parallel()
   379  			mockServer := test.GetMockServer(t)
   380  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   381  			output, err := client.AddQualityProfile(test.WithRequest.(*radarr.QualityProfile))
   382  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   383  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   384  		})
   385  	}
   386  }
   387  
   388  func TestUpdateQualityProfile(t *testing.T) {
   389  	t.Parallel()
   390  
   391  	tests := []*starrtest.MockData{
   392  		{
   393  			Name:           "200",
   394  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile", "7"),
   395  			ExpectedMethod: "PUT",
   396  			ResponseStatus: 200,
   397  			WithRequest: &radarr.QualityProfile{
   398  				Name:   "test",
   399  				Cutoff: 1003,
   400  				Qualities: []*starr.Quality{
   401  					{
   402  						Name: "WEB 2160p",
   403  						ID:   1003,
   404  						Items: []*starr.Quality{
   405  							{
   406  								Allowed: true,
   407  								Quality: &starr.BaseQuality{
   408  									ID:         18,
   409  									Name:       "WEBDL-2160p",
   410  									Source:     "webdl",
   411  									Resolution: 2160,
   412  									Modifier:   "none",
   413  								},
   414  							},
   415  							{
   416  								Allowed: true,
   417  								Quality: &starr.BaseQuality{
   418  									ID:         17,
   419  									Name:       "WEBRip-2160p",
   420  									Source:     "webrip",
   421  									Resolution: 2160,
   422  									Modifier:   "none",
   423  								},
   424  							},
   425  						},
   426  						Allowed: true,
   427  					},
   428  				},
   429  				MinFormatScore:    0,
   430  				CutoffFormatScore: 0,
   431  				Language: &starr.Value{
   432  					ID:   1,
   433  					Name: "English",
   434  				},
   435  				ID: 7,
   436  			},
   437  			ExpectedRequest: updateQualityProfileRequest,
   438  			ResponseBody:    qualityProfileResponse,
   439  			WithResponse: &radarr.QualityProfile{
   440  				ID:             7,
   441  				Name:           "test",
   442  				UpgradeAllowed: false,
   443  				Cutoff:         1003,
   444  				FormatItems:    []*starr.FormatItem{},
   445  				Qualities: []*starr.Quality{
   446  					{
   447  						Name: "WEB 2160p",
   448  						ID:   1003,
   449  						Items: []*starr.Quality{
   450  							{
   451  								Allowed: true,
   452  								Quality: &starr.BaseQuality{
   453  									ID:         18,
   454  									Name:       "WEBDL-2160p",
   455  									Source:     "webdl",
   456  									Resolution: 2160,
   457  									Modifier:   "none",
   458  								},
   459  							},
   460  							{
   461  								Allowed: true,
   462  								Quality: &starr.BaseQuality{
   463  									ID:         17,
   464  									Name:       "WEBRip-2160p",
   465  									Source:     "webrip",
   466  									Resolution: 2160,
   467  									Modifier:   "none",
   468  								},
   469  							},
   470  						},
   471  						Allowed: true,
   472  					},
   473  				},
   474  				MinFormatScore:    0,
   475  				CutoffFormatScore: 0,
   476  				Language: &starr.Value{
   477  					ID:   1,
   478  					Name: "English",
   479  				},
   480  			},
   481  			WithError: nil,
   482  		},
   483  		{
   484  			Name:           "404",
   485  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile", "7"),
   486  			ExpectedMethod: "PUT",
   487  			WithRequest: &radarr.QualityProfile{
   488  				Name:   "test",
   489  				Cutoff: 1003,
   490  				Qualities: []*starr.Quality{
   491  					{
   492  						Name: "WEB 2160p",
   493  						ID:   1003,
   494  						Items: []*starr.Quality{
   495  							{
   496  								Allowed: true,
   497  								Quality: &starr.BaseQuality{
   498  									ID:         18,
   499  									Name:       "WEBDL-2160p",
   500  									Source:     "webdl",
   501  									Resolution: 2160,
   502  									Modifier:   "none",
   503  								},
   504  							},
   505  							{
   506  								Allowed: true,
   507  								Quality: &starr.BaseQuality{
   508  									ID:         17,
   509  									Name:       "WEBRip-2160p",
   510  									Source:     "webrip",
   511  									Resolution: 2160,
   512  									Modifier:   "none",
   513  								},
   514  							},
   515  						},
   516  						Allowed: true,
   517  					},
   518  				},
   519  				MinFormatScore:    0,
   520  				CutoffFormatScore: 0,
   521  				Language: &starr.Value{
   522  					ID:   1,
   523  					Name: "English",
   524  				},
   525  				ID: 7,
   526  			},
   527  			ExpectedRequest: updateQualityProfileRequest,
   528  			ResponseStatus:  404,
   529  			ResponseBody:    `{"message": "NotFound"}`,
   530  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   531  			WithResponse:    (*radarr.QualityProfile)(nil),
   532  		},
   533  	}
   534  
   535  	for _, test := range tests {
   536  		test := test
   537  		t.Run(test.Name, func(t *testing.T) {
   538  			t.Parallel()
   539  			mockServer := test.GetMockServer(t)
   540  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   541  			output, err := client.UpdateQualityProfile(test.WithRequest.(*radarr.QualityProfile))
   542  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   543  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   544  		})
   545  	}
   546  }
   547  
   548  func TestDeleteQualityProfile(t *testing.T) {
   549  	t.Parallel()
   550  
   551  	tests := []*starrtest.MockData{
   552  		{
   553  			Name:           "200",
   554  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile", "10"),
   555  			ExpectedMethod: "DELETE",
   556  			ResponseStatus: 200,
   557  			WithRequest:    int64(10),
   558  			ResponseBody:   "{}",
   559  			WithError:      nil,
   560  		},
   561  		{
   562  			Name:           "404",
   563  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "qualityProfile", "10"),
   564  			ExpectedMethod: "DELETE",
   565  			WithRequest:    int64(10),
   566  			ResponseStatus: 404,
   567  			ResponseBody:   `{"message": "NotFound"}`,
   568  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   569  			WithResponse:   (*radarr.QualityProfile)(nil),
   570  		},
   571  	}
   572  
   573  	for _, test := range tests {
   574  		test := test
   575  		t.Run(test.Name, func(t *testing.T) {
   576  			t.Parallel()
   577  			mockServer := test.GetMockServer(t)
   578  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   579  			err := client.DeleteQualityProfile(test.WithRequest.(int64))
   580  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   581  		})
   582  	}
   583  }