golift.io/starr@v1.0.0/radarr/restriction_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 restrictionBody = `{
    15  	"required": "test1",
    16  	"ignored": "test2",
    17  	"tags": [],
    18  	"id": 2
    19    }`
    20  
    21  func TestGetRestrictions(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	tests := []*starrtest.MockData{
    25  		{
    26  			Name:           "200",
    27  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction"),
    28  			ExpectedMethod: "GET",
    29  			ResponseStatus: 200,
    30  			ResponseBody:   "[" + restrictionBody + "]",
    31  			WithResponse: []*radarr.Restriction{
    32  				{
    33  					Tags:     []int{},
    34  					Required: "test1",
    35  					Ignored:  "test2",
    36  					ID:       2,
    37  				},
    38  			},
    39  			WithError: nil,
    40  		},
    41  		{
    42  			Name:           "404",
    43  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction"),
    44  			ExpectedMethod: "GET",
    45  			ResponseStatus: 404,
    46  			ResponseBody:   `{"message": "NotFound"}`,
    47  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
    48  			WithResponse:   []*radarr.Restriction(nil),
    49  		},
    50  	}
    51  
    52  	for _, test := range tests {
    53  		test := test
    54  		t.Run(test.Name, func(t *testing.T) {
    55  			t.Parallel()
    56  			mockServer := test.GetMockServer(t)
    57  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
    58  			output, err := client.GetRestrictions()
    59  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
    60  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
    61  		})
    62  	}
    63  }
    64  
    65  func TestGetRestriction(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	tests := []*starrtest.MockData{
    69  		{
    70  			Name:           "200",
    71  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction", "2"),
    72  			ExpectedMethod: "GET",
    73  			ResponseStatus: 200,
    74  			WithRequest:    int64(2),
    75  			ResponseBody:   restrictionBody,
    76  			WithResponse: &radarr.Restriction{
    77  				Tags:     []int{},
    78  				Required: "test1",
    79  				Ignored:  "test2",
    80  				ID:       2,
    81  			},
    82  			WithError: nil,
    83  		},
    84  		{
    85  			Name:           "404",
    86  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction", "2"),
    87  			ExpectedMethod: "GET",
    88  			ResponseStatus: 404,
    89  			WithRequest:    int64(2),
    90  			ResponseBody:   `{"message": "NotFound"}`,
    91  			WithResponse:   (*radarr.Restriction)(nil),
    92  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
    93  		},
    94  	}
    95  
    96  	for _, test := range tests {
    97  		test := test
    98  		t.Run(test.Name, func(t *testing.T) {
    99  			t.Parallel()
   100  			mockServer := test.GetMockServer(t)
   101  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   102  			output, err := client.GetRestriction(test.WithRequest.(int64))
   103  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   104  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   105  		})
   106  	}
   107  }
   108  
   109  func TestAddRestriction(t *testing.T) {
   110  	t.Parallel()
   111  
   112  	tests := []*starrtest.MockData{
   113  		{
   114  			Name:           "200",
   115  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction"),
   116  			ExpectedMethod: "POST",
   117  			ResponseStatus: 200,
   118  			WithRequest: &radarr.Restriction{
   119  				Required: "test1",
   120  				Ignored:  "test2",
   121  			},
   122  			ExpectedRequest: `{"required":"test1","ignored":"test2"}` + "\n",
   123  			ResponseBody:    restrictionBody,
   124  			WithResponse: &radarr.Restriction{
   125  				Tags:     []int{},
   126  				Required: "test1",
   127  				Ignored:  "test2",
   128  				ID:       2,
   129  			},
   130  			WithError: nil,
   131  		},
   132  		{
   133  			Name:           "404",
   134  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction"),
   135  			ExpectedMethod: "POST",
   136  			ResponseStatus: 404,
   137  			WithRequest: &radarr.Restriction{
   138  				Required: "test1",
   139  				Ignored:  "test2",
   140  			},
   141  			ExpectedRequest: `{"required":"test1","ignored":"test2"}` + "\n",
   142  			ResponseBody:    `{"message": "NotFound"}`,
   143  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   144  			WithResponse:    (*radarr.Restriction)(nil),
   145  		},
   146  	}
   147  
   148  	for _, test := range tests {
   149  		test := test
   150  		t.Run(test.Name, func(t *testing.T) {
   151  			t.Parallel()
   152  			mockServer := test.GetMockServer(t)
   153  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   154  			output, err := client.AddRestriction(test.WithRequest.(*radarr.Restriction))
   155  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   156  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   157  		})
   158  	}
   159  }
   160  
   161  func TestUpdateRestriction(t *testing.T) {
   162  	t.Parallel()
   163  
   164  	tests := []*starrtest.MockData{
   165  		{
   166  			Name:           "200",
   167  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction", "2"),
   168  			ExpectedMethod: "PUT",
   169  			ResponseStatus: 200,
   170  			WithRequest: &radarr.Restriction{
   171  				Required: "test1",
   172  				Ignored:  "test2",
   173  				ID:       2,
   174  			},
   175  			ExpectedRequest: `{"required":"test1","ignored":"test2","id":2}` + "\n",
   176  			ResponseBody:    restrictionBody,
   177  			WithResponse: &radarr.Restriction{
   178  				Tags:     []int{},
   179  				Required: "test1",
   180  				Ignored:  "test2",
   181  				ID:       2,
   182  			},
   183  			WithError: nil,
   184  		},
   185  		{
   186  			Name:           "404",
   187  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction", "2"),
   188  			ExpectedMethod: "PUT",
   189  			ResponseStatus: 404,
   190  			WithRequest: &radarr.Restriction{
   191  				Required: "test1",
   192  				Ignored:  "test2",
   193  				ID:       2,
   194  			},
   195  			ExpectedRequest: `{"required":"test1","ignored":"test2","id":2}` + "\n",
   196  			ResponseBody:    `{"message": "NotFound"}`,
   197  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   198  			WithResponse:    (*radarr.Restriction)(nil),
   199  		},
   200  	}
   201  
   202  	for _, test := range tests {
   203  		test := test
   204  		t.Run(test.Name, func(t *testing.T) {
   205  			t.Parallel()
   206  			mockServer := test.GetMockServer(t)
   207  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   208  			output, err := client.UpdateRestriction(test.WithRequest.(*radarr.Restriction))
   209  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   210  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   211  		})
   212  	}
   213  }
   214  
   215  func TestDeleteRestriction(t *testing.T) {
   216  	t.Parallel()
   217  
   218  	tests := []*starrtest.MockData{
   219  		{
   220  			Name:           "200",
   221  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction", "1"),
   222  			ExpectedMethod: "DELETE",
   223  			WithRequest:    int64(1),
   224  			ResponseStatus: 200,
   225  			ResponseBody:   "{}",
   226  			WithError:      nil,
   227  		},
   228  		{
   229  			Name:           "404",
   230  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "restriction", "1"),
   231  			ExpectedMethod: "DELETE",
   232  			WithRequest:    int64(1),
   233  			ResponseStatus: 404,
   234  			ResponseBody:   `{"message": "NotFound"}`,
   235  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   236  		},
   237  	}
   238  
   239  	for _, test := range tests {
   240  		test := test
   241  		t.Run(test.Name, func(t *testing.T) {
   242  			t.Parallel()
   243  			mockServer := test.GetMockServer(t)
   244  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   245  			err := client.DeleteRestriction(test.WithRequest.(int64))
   246  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   247  		})
   248  	}
   249  }