golift.io/starr@v1.0.0/radarr/indexerconfig_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 indexerConfigBody = `{ 15 "minimumAge": 0, 16 "maximumSize": 0, 17 "retention": 0, 18 "rssSyncInterval": 60, 19 "preferIndexerFlags": false, 20 "availabilityDelay": 0, 21 "allowHardcodedSubs": false, 22 "whitelistedHardcodedSubs": "", 23 "id": 1 24 }` 25 26 const indexerRequest = `{"whitelistedHardcodedSubs":"","id":1,"maximumSize":0,"minimumAge":0,"retention":0,` + 27 `"rssSyncInterval":60,"availabilityDelay":0,"preferIndexerFlags":false,"allowHardcodedSubs":false}` + "\n" 28 29 func TestGetIndexerConfig(t *testing.T) { 30 t.Parallel() 31 32 tests := []*starrtest.MockData{ 33 { 34 Name: "200", 35 ExpectedPath: path.Join("/", starr.API, radarr.APIver, "config", "indexer"), 36 ExpectedMethod: "GET", 37 ResponseStatus: 200, 38 ResponseBody: indexerConfigBody, 39 WithResponse: &radarr.IndexerConfig{ 40 WhitelistedHardcodedSubs: "", 41 ID: 1, 42 MaximumSize: 0, 43 MinimumAge: 0, 44 Retention: 0, 45 RssSyncInterval: 60, 46 AvailabilityDelay: 0, 47 PreferIndexerFlags: false, 48 AllowHardcodedSubs: false, 49 }, 50 WithError: nil, 51 }, 52 { 53 Name: "404", 54 ExpectedPath: path.Join("/", starr.API, radarr.APIver, "config", "indexer"), 55 ExpectedMethod: "GET", 56 ResponseStatus: 404, 57 ResponseBody: `{"message": "NotFound"}`, 58 WithError: &starr.ReqError{Code: http.StatusNotFound}, 59 WithResponse: (*radarr.IndexerConfig)(nil), 60 }, 61 } 62 63 for _, test := range tests { 64 test := test 65 t.Run(test.Name, func(t *testing.T) { 66 t.Parallel() 67 mockServer := test.GetMockServer(t) 68 client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 69 output, err := client.GetIndexerConfig() 70 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 71 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 72 }) 73 } 74 } 75 76 func TestUpdateIndexerConfig(t *testing.T) { 77 t.Parallel() 78 79 tests := []*starrtest.MockData{ 80 { 81 Name: "202", 82 ExpectedPath: path.Join("/", starr.API, radarr.APIver, "config", "indexer", "1"), 83 ExpectedMethod: "PUT", 84 ResponseStatus: 202, 85 WithRequest: &radarr.IndexerConfig{ 86 WhitelistedHardcodedSubs: "", 87 ID: 1, 88 MaximumSize: 0, 89 MinimumAge: 0, 90 Retention: 0, 91 RssSyncInterval: 60, 92 AvailabilityDelay: 0, 93 PreferIndexerFlags: false, 94 AllowHardcodedSubs: false, 95 }, 96 ExpectedRequest: indexerRequest, 97 ResponseBody: indexerConfigBody, 98 WithResponse: &radarr.IndexerConfig{ 99 WhitelistedHardcodedSubs: "", 100 ID: 1, 101 MaximumSize: 0, 102 MinimumAge: 0, 103 Retention: 0, 104 RssSyncInterval: 60, 105 AvailabilityDelay: 0, 106 PreferIndexerFlags: false, 107 AllowHardcodedSubs: false, 108 }, 109 WithError: nil, 110 }, 111 { 112 Name: "404", 113 ExpectedPath: path.Join("/", starr.API, radarr.APIver, "config", "indexer", "1"), 114 ExpectedMethod: "PUT", 115 WithRequest: &radarr.IndexerConfig{ 116 ID: 1, 117 MaximumSize: 0, 118 MinimumAge: 0, 119 Retention: 0, 120 RssSyncInterval: 60, 121 AvailabilityDelay: 0, 122 PreferIndexerFlags: false, 123 AllowHardcodedSubs: false, 124 }, 125 ExpectedRequest: indexerRequest, 126 ResponseStatus: 404, 127 ResponseBody: `{"message": "NotFound"}`, 128 WithError: &starr.ReqError{Code: http.StatusNotFound}, 129 WithResponse: (*radarr.IndexerConfig)(nil), 130 }, 131 } 132 133 for _, test := range tests { 134 test := test 135 t.Run(test.Name, func(t *testing.T) { 136 t.Parallel() 137 mockServer := test.GetMockServer(t) 138 client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 139 output, err := client.UpdateIndexerConfig(test.WithRequest.(*radarr.IndexerConfig)) 140 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 141 assert.EqualValues(t, output, test.WithResponse, "response is not the same as expected") 142 }) 143 } 144 }