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