golift.io/starr@v1.0.0/sonarr/rootfolder_test.go (about)

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