golift.io/starr@v1.0.0/lidarr/remotepathmapping_test.go (about)

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