golift.io/starr@v1.0.0/sonarr/delayprofile_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 ( 15 firstDelayProfile = `{ 16 "enableUsenet": true, 17 "enableTorrent": true, 18 "preferredProtocol": "usenet", 19 "usenetDelay": 0, 20 "torrentDelay": 0, 21 "bypassIfHighestQuality": true, 22 "order": 2147483647, 23 "tags": [], 24 "id": 1 25 }` 26 secondDelayProfile = `{ 27 "enableUsenet": false, 28 "enableTorrent": true, 29 "preferredProtocol": "torrent", 30 "usenetDelay": 0, 31 "torrentDelay": 0, 32 "bypassIfHighestQuality": false, 33 "order": 1, 34 "tags": [11], 35 "id": 10 36 }` 37 delayProfileRequest = `{"enableTorrent":true,"order":1,"tags":[11],"preferredProtocol":"torrent"}` + "\n" 38 ) 39 40 func TestGetDelayProfiles(t *testing.T) { 41 t.Parallel() 42 43 tests := []*starrtest.MockData{ 44 { 45 Name: "200", 46 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile"), 47 ExpectedMethod: "GET", 48 ResponseStatus: 200, 49 ResponseBody: `[` + firstDelayProfile + `,` + secondDelayProfile + `]`, 50 WithResponse: []*sonarr.DelayProfile{ 51 { 52 EnableUsenet: true, 53 EnableTorrent: true, 54 PreferredProtocol: "usenet", 55 UsenetDelay: 0, 56 TorrentDelay: 0, 57 BypassIfHighestQuality: true, 58 Order: 2147483647, 59 Tags: []int{}, 60 ID: 1, 61 }, 62 { 63 EnableUsenet: false, 64 EnableTorrent: true, 65 PreferredProtocol: "torrent", 66 UsenetDelay: 0, 67 TorrentDelay: 0, 68 BypassIfHighestQuality: false, 69 Order: 1, 70 Tags: []int{11}, 71 ID: 10, 72 }, 73 }, 74 WithError: nil, 75 }, 76 { 77 Name: "404", 78 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile"), 79 ExpectedMethod: "GET", 80 ResponseStatus: 404, 81 ResponseBody: `{"message": "NotFound"}`, 82 WithError: &starr.ReqError{Code: http.StatusNotFound}, 83 WithResponse: []*sonarr.DelayProfile(nil), 84 }, 85 } 86 87 for _, test := range tests { 88 test := test 89 t.Run(test.Name, func(t *testing.T) { 90 t.Parallel() 91 mockServer := test.GetMockServer(t) 92 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 93 output, err := client.GetDelayProfiles() 94 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 95 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 96 }) 97 } 98 } 99 100 func TestGetDelayProfile(t *testing.T) { 101 t.Parallel() 102 103 tests := []*starrtest.MockData{ 104 { 105 Name: "200", 106 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile/1"), 107 ExpectedMethod: "GET", 108 ResponseStatus: 200, 109 WithRequest: int64(1), 110 ResponseBody: firstDelayProfile, 111 WithResponse: &sonarr.DelayProfile{ 112 EnableUsenet: true, 113 EnableTorrent: true, 114 PreferredProtocol: "usenet", 115 UsenetDelay: 0, 116 TorrentDelay: 0, 117 BypassIfHighestQuality: true, 118 Order: 2147483647, 119 Tags: []int{}, 120 ID: 1, 121 }, 122 WithError: nil, 123 }, 124 { 125 Name: "404", 126 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile", "1"), 127 ExpectedMethod: "GET", 128 ResponseStatus: 404, 129 WithRequest: int64(1), 130 ResponseBody: `{"message": "NotFound"}`, 131 WithError: &starr.ReqError{Code: http.StatusNotFound}, 132 WithResponse: (*sonarr.DelayProfile)(nil), 133 }, 134 } 135 136 for _, test := range tests { 137 test := test 138 t.Run(test.Name, func(t *testing.T) { 139 t.Parallel() 140 mockServer := test.GetMockServer(t) 141 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 142 output, err := client.GetDelayProfile(test.WithRequest.(int64)) 143 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 144 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 145 }) 146 } 147 } 148 149 func TestAddDelayProfile(t *testing.T) { 150 t.Parallel() 151 152 tests := []*starrtest.MockData{ 153 { 154 Name: "200", 155 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile"), 156 ExpectedMethod: "POST", 157 ResponseStatus: 200, 158 WithRequest: &sonarr.DelayProfile{ 159 EnableUsenet: false, 160 EnableTorrent: true, 161 PreferredProtocol: "torrent", 162 UsenetDelay: 0, 163 TorrentDelay: 0, 164 BypassIfHighestQuality: false, 165 Order: 1, 166 Tags: []int{11}, 167 }, 168 ExpectedRequest: delayProfileRequest, 169 ResponseBody: secondDelayProfile, 170 WithResponse: &sonarr.DelayProfile{ 171 EnableUsenet: false, 172 EnableTorrent: true, 173 PreferredProtocol: "torrent", 174 UsenetDelay: 0, 175 TorrentDelay: 0, 176 BypassIfHighestQuality: false, 177 Order: 1, 178 Tags: []int{11}, 179 ID: 10, 180 }, 181 WithError: nil, 182 }, 183 { 184 Name: "404", 185 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile"), 186 ExpectedMethod: "POST", 187 WithRequest: &sonarr.DelayProfile{ 188 EnableUsenet: false, 189 EnableTorrent: true, 190 PreferredProtocol: "torrent", 191 UsenetDelay: 0, 192 TorrentDelay: 0, 193 BypassIfHighestQuality: false, 194 Order: 1, 195 Tags: []int{11}, 196 }, 197 ExpectedRequest: delayProfileRequest, 198 ResponseStatus: 404, 199 ResponseBody: `{"message": "NotFound"}`, 200 WithError: &starr.ReqError{Code: http.StatusNotFound}, 201 WithResponse: (*sonarr.DelayProfile)(nil), 202 }, 203 } 204 205 for _, test := range tests { 206 test := test 207 t.Run(test.Name, func(t *testing.T) { 208 t.Parallel() 209 mockServer := test.GetMockServer(t) 210 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 211 output, err := client.AddDelayProfile(test.WithRequest.(*sonarr.DelayProfile)) 212 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 213 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 214 }) 215 } 216 } 217 218 func TestUpdateDelayProfile(t *testing.T) { 219 t.Parallel() 220 221 tests := []*starrtest.MockData{ 222 { 223 Name: "200", 224 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile", "10"), 225 ExpectedMethod: "PUT", 226 ResponseStatus: 200, 227 WithRequest: &sonarr.DelayProfile{ 228 EnableTorrent: true, 229 ID: 10, 230 Tags: []int{11}, 231 }, 232 ExpectedRequest: `{"enableTorrent":true,"id":10,"tags":[11]}` + "\n", 233 ResponseBody: secondDelayProfile, 234 WithResponse: &sonarr.DelayProfile{ 235 EnableUsenet: false, 236 EnableTorrent: true, 237 PreferredProtocol: "torrent", 238 UsenetDelay: 0, 239 TorrentDelay: 0, 240 BypassIfHighestQuality: false, 241 Order: 1, 242 Tags: []int{11}, 243 ID: 10, 244 }, 245 WithError: nil, 246 }, 247 { 248 Name: "404", 249 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile", "10"), 250 ExpectedMethod: "PUT", 251 WithRequest: &sonarr.DelayProfile{ 252 EnableTorrent: true, 253 ID: 10, 254 Tags: []int{11}, 255 }, 256 ExpectedRequest: `{"enableTorrent":true,"id":10,"tags":[11]}` + "\n", 257 ResponseStatus: 404, 258 ResponseBody: `{"message": "NotFound"}`, 259 WithError: &starr.ReqError{Code: http.StatusNotFound}, 260 WithResponse: (*sonarr.DelayProfile)(nil), 261 }, 262 } 263 264 for _, test := range tests { 265 test := test 266 t.Run(test.Name, func(t *testing.T) { 267 t.Parallel() 268 mockServer := test.GetMockServer(t) 269 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 270 output, err := client.UpdateDelayProfile(test.WithRequest.(*sonarr.DelayProfile)) 271 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 272 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 273 }) 274 } 275 } 276 277 func TestDeleteDelayProfile(t *testing.T) { 278 t.Parallel() 279 280 tests := []*starrtest.MockData{ 281 { 282 Name: "200", 283 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile", "10"), 284 ExpectedMethod: "DELETE", 285 ResponseStatus: 200, 286 WithRequest: int64(10), 287 ResponseBody: "{}", 288 WithError: nil, 289 }, 290 { 291 Name: "404", 292 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "delayProfile", "10"), 293 ExpectedMethod: "DELETE", 294 WithRequest: int64(10), 295 ResponseStatus: 404, 296 ResponseBody: `{"message": "NotFound"}`, 297 WithError: &starr.ReqError{Code: http.StatusNotFound}, 298 WithResponse: (*sonarr.DelayProfile)(nil), 299 }, 300 } 301 302 for _, test := range tests { 303 test := test 304 t.Run(test.Name, func(t *testing.T) { 305 t.Parallel() 306 mockServer := test.GetMockServer(t) 307 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 308 err := client.DeleteDelayProfile(test.WithRequest.(int64)) 309 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 310 }) 311 } 312 }