golift.io/starr@v1.0.0/readarr/downloadclientconfig_test.go (about)

     1  package readarr_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/readarr"
    11  	"golift.io/starr/starrtest"
    12  )
    13  
    14  const downloadClientConfigBody = `{
    15  	"downloadClientWorkingFolders": "_UNPACK_|_FAILED_",
    16  	"enableCompletedDownloadHandling": true,
    17  	"removeCompletedDownloads": false,
    18  	"autoRedownloadFailed": false,
    19  	"removeFailedDownloads": false,
    20  	"id": 1
    21    }`
    22  
    23  const updateDownloadClientConfig = `{"enableCompletedDownloadHandling":true,"autoRedownloadFailed":false,"id":1,` +
    24  	`"downloadClientWorkingFolders":"_UNPACK_|_FAILED_","removeCompletedDownloads":false,"removeFailedDownloads":false}`
    25  
    26  func TestGetDownloadClientConfig(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	tests := []*starrtest.MockData{
    30  		{
    31  			Name:            "200",
    32  			ExpectedPath:    path.Join("/", starr.API, readarr.APIver, "config", "downloadClient"),
    33  			ExpectedRequest: "",
    34  			ExpectedMethod:  "GET",
    35  			ResponseStatus:  200,
    36  			ResponseBody:    downloadClientConfigBody,
    37  			WithRequest:     nil,
    38  			WithResponse: &readarr.DownloadClientConfig{
    39  				EnableCompletedDownloadHandling: true,
    40  				AutoRedownloadFailed:            false,
    41  				RemoveCompletedDownloads:        false,
    42  				RemoveFailedDownloads:           false,
    43  				ID:                              1,
    44  				DownloadClientWorkingFolders:    "_UNPACK_|_FAILED_",
    45  			},
    46  			WithError: nil,
    47  		},
    48  		{
    49  			Name:           "404",
    50  			ExpectedPath:   path.Join("/", starr.API, readarr.APIver, "config", "downloadClient"),
    51  			ExpectedMethod: "GET",
    52  			ResponseStatus: 404,
    53  			ResponseBody:   `{"message": "NotFound"}`,
    54  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
    55  			WithResponse:   (*readarr.DownloadClientConfig)(nil),
    56  		},
    57  	}
    58  
    59  	for _, test := range tests {
    60  		test := test
    61  		t.Run(test.Name, func(t *testing.T) {
    62  			t.Parallel()
    63  			mockServer := test.GetMockServer(t)
    64  			client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
    65  			output, err := client.GetDownloadClientConfig()
    66  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
    67  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
    68  		})
    69  	}
    70  }
    71  
    72  func TestUpdateDownloadClientConfig(t *testing.T) {
    73  	t.Parallel()
    74  
    75  	tests := []*starrtest.MockData{
    76  		{
    77  			Name:           "202",
    78  			ExpectedPath:   path.Join("/", starr.API, readarr.APIver, "config", "downloadClient", "1"),
    79  			ExpectedMethod: "PUT",
    80  			ResponseStatus: 202,
    81  			WithRequest: &readarr.DownloadClientConfig{
    82  				EnableCompletedDownloadHandling: true,
    83  				AutoRedownloadFailed:            false,
    84  				RemoveCompletedDownloads:        false,
    85  				RemoveFailedDownloads:           false,
    86  				ID:                              1,
    87  				DownloadClientWorkingFolders:    "_UNPACK_|_FAILED_",
    88  			},
    89  			ExpectedRequest: updateDownloadClientConfig + "\n",
    90  			ResponseBody:    downloadClientConfigBody,
    91  			WithResponse: &readarr.DownloadClientConfig{
    92  				EnableCompletedDownloadHandling: true,
    93  				AutoRedownloadFailed:            false,
    94  				RemoveCompletedDownloads:        false,
    95  				RemoveFailedDownloads:           false,
    96  				ID:                              1,
    97  				DownloadClientWorkingFolders:    "_UNPACK_|_FAILED_",
    98  			},
    99  			WithError: nil,
   100  		},
   101  		{
   102  			Name:           "404",
   103  			ExpectedPath:   path.Join("/", starr.API, readarr.APIver, "config", "downloadClient", "1"),
   104  			ExpectedMethod: "PUT",
   105  			WithRequest: &readarr.DownloadClientConfig{
   106  				EnableCompletedDownloadHandling: true,
   107  				AutoRedownloadFailed:            false,
   108  				RemoveCompletedDownloads:        false,
   109  				RemoveFailedDownloads:           false,
   110  				ID:                              1,
   111  				DownloadClientWorkingFolders:    "_UNPACK_|_FAILED_",
   112  			},
   113  			ExpectedRequest: updateDownloadClientConfig + "\n",
   114  			ResponseStatus:  404,
   115  			ResponseBody:    `{"message": "NotFound"}`,
   116  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   117  			WithResponse:    (*readarr.DownloadClientConfig)(nil),
   118  		},
   119  	}
   120  
   121  	for _, test := range tests {
   122  		test := test
   123  		t.Run(test.Name, func(t *testing.T) {
   124  			t.Parallel()
   125  			mockServer := test.GetMockServer(t)
   126  			client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   127  			output, err := client.UpdateDownloadClientConfig(test.WithRequest.(*readarr.DownloadClientConfig))
   128  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   129  			assert.EqualValues(t, output, test.WithResponse, "response is not the same as expected")
   130  		})
   131  	}
   132  }