golift.io/starr@v1.0.0/radarr/mediamanagement_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 mediaManagementBody = `{
    15    "autoUnmonitorPreviouslyDownloadedMovies": false,
    16    "recycleBin": "",
    17    "recycleBinCleanupDays": 7,
    18    "downloadPropersAndRepacks": "preferAndUpgrade",
    19    "createEmptyMovieFolders": false,
    20    "deleteEmptyFolders": false,
    21    "fileDate": "none",
    22    "rescanAfterRefresh": "always",
    23    "autoRenameFolders": false,
    24    "pathsDefaultStatic": false,
    25    "setPermissionsLinux": false,
    26    "chmodFolder": "755",
    27    "chownGroup": "",
    28    "skipFreeSpaceCheckWhenImporting": false,
    29    "minimumFreeSpaceWhenImporting": 100,
    30    "copyUsingHardlinks": true,
    31    "importExtraFiles": false,
    32    "extraFileExtensions": "srt",
    33    "enableMediaInfo": true,
    34    "id": 1
    35  }`
    36  
    37  const mediaManagementRequest = `{"enableMediaInfo":true,"id":0,` +
    38  	`"minimumFreeSpaceWhenImporting":100,"chownGroup":"","recycleBin":""}`
    39  
    40  func TestGetMediaManagement(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	tests := []*starrtest.MockData{
    44  		{
    45  			Name:           "200",
    46  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "mediaManagement"),
    47  			ExpectedMethod: "GET",
    48  			ResponseStatus: 200,
    49  			ResponseBody:   mediaManagementBody,
    50  			WithResponse: &radarr.MediaManagement{
    51  				AutoRenameFolders:                       false,
    52  				AutoUnmonitorPreviouslyDownloadedMovies: false,
    53  				CopyUsingHardlinks:                      true,
    54  				CreateEmptyMovieFolders:                 false,
    55  				DeleteEmptyFolders:                      false,
    56  				EnableMediaInfo:                         true,
    57  				ImportExtraFiles:                        false,
    58  				PathsDefaultStatic:                      false,
    59  				SetPermissionsLinux:                     false,
    60  				SkipFreeSpaceCheckWhenImporting:         false,
    61  				ID:                                      1,
    62  				MinimumFreeSpaceWhenImporting:           100,
    63  				RecycleBinCleanupDays:                   7,
    64  				ChmodFolder:                             "755",
    65  				ChownGroup:                              "",
    66  				DownloadPropersAndRepacks:               "preferAndUpgrade",
    67  				ExtraFileExtensions:                     "srt",
    68  				FileDate:                                "none",
    69  				RecycleBin:                              "",
    70  				RescanAfterRefresh:                      "always",
    71  			},
    72  			WithError: nil,
    73  		},
    74  		{
    75  			Name:           "404",
    76  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "mediaManagement"),
    77  			ExpectedMethod: "GET",
    78  			ResponseStatus: 404,
    79  			ResponseBody:   `{"message": "NotFound"}`,
    80  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
    81  			WithResponse:   (*radarr.MediaManagement)(nil),
    82  		},
    83  	}
    84  
    85  	for _, test := range tests {
    86  		test := test
    87  		t.Run(test.Name, func(t *testing.T) {
    88  			t.Parallel()
    89  			mockServer := test.GetMockServer(t)
    90  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
    91  			output, err := client.GetMediaManagement()
    92  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
    93  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
    94  		})
    95  	}
    96  }
    97  
    98  func TestUpdateMediaManagement(t *testing.T) {
    99  	t.Parallel()
   100  
   101  	tests := []*starrtest.MockData{
   102  		{
   103  			Name:           "202",
   104  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "mediaManagement"),
   105  			ExpectedMethod: "PUT",
   106  			ResponseStatus: 202,
   107  			WithRequest: &radarr.MediaManagement{
   108  				EnableMediaInfo:               true,
   109  				MinimumFreeSpaceWhenImporting: 100,
   110  			},
   111  			ExpectedRequest: mediaManagementRequest + "\n",
   112  			ResponseBody:    mediaManagementBody,
   113  			WithResponse: &radarr.MediaManagement{
   114  				AutoRenameFolders:                       false,
   115  				AutoUnmonitorPreviouslyDownloadedMovies: false,
   116  				CopyUsingHardlinks:                      true,
   117  				CreateEmptyMovieFolders:                 false,
   118  				DeleteEmptyFolders:                      false,
   119  				EnableMediaInfo:                         true,
   120  				ImportExtraFiles:                        false,
   121  				PathsDefaultStatic:                      false,
   122  				SetPermissionsLinux:                     false,
   123  				SkipFreeSpaceCheckWhenImporting:         false,
   124  				ID:                                      1,
   125  				MinimumFreeSpaceWhenImporting:           100,
   126  				RecycleBinCleanupDays:                   7,
   127  				ChmodFolder:                             "755",
   128  				ChownGroup:                              "",
   129  				DownloadPropersAndRepacks:               "preferAndUpgrade",
   130  				ExtraFileExtensions:                     "srt",
   131  				FileDate:                                "none",
   132  				RecycleBin:                              "",
   133  				RescanAfterRefresh:                      "always",
   134  			},
   135  			WithError: nil,
   136  		},
   137  		{
   138  			Name:           "404",
   139  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "mediaManagement"),
   140  			ExpectedMethod: "PUT",
   141  			WithRequest: &radarr.MediaManagement{
   142  				EnableMediaInfo:               true,
   143  				MinimumFreeSpaceWhenImporting: 100,
   144  			},
   145  			ExpectedRequest: mediaManagementRequest + "\n",
   146  			ResponseStatus:  404,
   147  			ResponseBody:    `{"message": "NotFound"}`,
   148  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   149  			WithResponse:    (*radarr.MediaManagement)(nil),
   150  		},
   151  	}
   152  
   153  	for _, test := range tests {
   154  		test := test
   155  		t.Run(test.Name, func(t *testing.T) {
   156  			t.Parallel()
   157  			mockServer := test.GetMockServer(t)
   158  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   159  			output, err := client.UpdateMediaManagement(test.WithRequest.(*radarr.MediaManagement))
   160  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   161  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   162  		})
   163  	}
   164  }