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