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