golift.io/starr@v1.0.0/sonarr/command_test.go (about) 1 package sonarr_test 2 3 import ( 4 "net/http" 5 "path" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 "golift.io/starr" 11 "golift.io/starr/sonarr" 12 "golift.io/starr/starrtest" 13 ) 14 15 func TestGetCommands(t *testing.T) { 16 t.Parallel() 17 18 somedate := time.Now().Add(-36 * time.Hour).Round(time.Millisecond).UTC() 19 datejson, _ := somedate.MarshalJSON() 20 21 tests := []*starrtest.MockData{ 22 { 23 Name: "200", 24 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "command"), 25 ResponseStatus: http.StatusOK, 26 ResponseBody: `[{"id":1234,"name":"SomeCommand","commandName":"SomeCommandName","message":` + 27 `"Command Message","priority":"testalert","status":"statusalert","queued":` + string(datejson) + 28 `,"started":` + string(datejson) + `,"ended":` + string(datejson) + 29 `,"stateChangeTime":` + string(datejson) + `,"lastExecutionTime":` + string(datejson) + 30 `,"duration":"woofun","trigger":"someTrigger","sendUpdatesToClient":true,"updateScheduledTask":true` + 31 `,"body": {"mapstring": "mapinterface"}` + 32 `}]`, 33 WithError: nil, 34 ExpectedMethod: "GET", 35 WithResponse: []*sonarr.CommandResponse{{ 36 ID: 1234, 37 Name: "SomeCommand", 38 CommandName: "SomeCommandName", 39 Message: "Command Message", 40 Priority: "testalert", 41 Status: "statusalert", 42 Queued: somedate, 43 Started: somedate, 44 Ended: somedate, 45 StateChangeTime: somedate, 46 LastExecutionTime: somedate, 47 Duration: "woofun", 48 Trigger: "someTrigger", 49 SendUpdatesToClient: true, 50 UpdateScheduledTask: true, 51 Body: map[string]interface{}{"mapstring": "mapinterface"}, 52 }}, 53 }, 54 { 55 Name: "404", 56 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "command"), 57 ResponseStatus: http.StatusNotFound, 58 ResponseBody: `{"message": "NotFound"}`, 59 WithError: &starr.ReqError{Code: http.StatusNotFound}, 60 ExpectedMethod: "GET", 61 WithResponse: []*sonarr.CommandResponse(nil), 62 }, 63 } 64 65 for _, test := range tests { 66 test := test 67 t.Run(test.Name, func(t *testing.T) { 68 t.Parallel() 69 mockServer := test.GetMockServer(t) 70 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 71 output, err := client.GetCommands() 72 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 73 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 74 }) 75 } 76 } 77 78 func TestSendCommand(t *testing.T) { 79 t.Parallel() 80 81 somedate := time.Now().Add(-36 * time.Hour).Round(time.Millisecond).UTC() 82 datejson, _ := somedate.MarshalJSON() 83 84 tests := []*starrtest.MockData{ 85 { 86 Name: "200", 87 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "command"), 88 ResponseStatus: http.StatusOK, 89 ResponseBody: `{"id":1234,"name":"SomeCommand","commandName":"SomeCommandName","message":` + 90 `"Command Message","priority":"testalert","status":"statusalert","queued":` + string(datejson) + 91 `,"started":` + string(datejson) + `,"ended":` + string(datejson) + 92 `,"stateChangeTime":` + string(datejson) + `,"lastExecutionTime":` + string(datejson) + 93 `,"duration":"woofun","trigger":"someTrigger","sendUpdatesToClient":true,"updateScheduledTask":true` + 94 `,"body": {"mapstring": "mapinterface"}` + 95 `}`, 96 WithError: nil, 97 WithRequest: &sonarr.CommandRequest{ 98 Name: "SomeCommand", 99 SeriesIDs: []int64{1, 3, 7}, 100 }, 101 ExpectedRequest: `{"name":"SomeCommand","seriesIds":[1,3,7]}` + "\n", 102 ExpectedMethod: "POST", 103 WithResponse: &sonarr.CommandResponse{ 104 ID: 1234, 105 Name: "SomeCommand", 106 CommandName: "SomeCommandName", 107 Message: "Command Message", 108 Priority: "testalert", 109 Status: "statusalert", 110 Queued: somedate, 111 Started: somedate, 112 Ended: somedate, 113 StateChangeTime: somedate, 114 LastExecutionTime: somedate, 115 Duration: "woofun", 116 Trigger: "someTrigger", 117 SendUpdatesToClient: true, 118 UpdateScheduledTask: true, 119 Body: map[string]interface{}{"mapstring": "mapinterface"}, 120 }, 121 }, 122 { 123 Name: "404", 124 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "command"), 125 ResponseStatus: http.StatusNotFound, 126 ResponseBody: `{"message": "NotFound"}`, 127 WithError: &starr.ReqError{Code: http.StatusNotFound}, 128 ExpectedMethod: "POST", 129 WithResponse: (*sonarr.CommandResponse)(nil), 130 WithRequest: &sonarr.CommandRequest{Name: "Something"}, 131 ExpectedRequest: `{"name":"Something"}` + "\n", 132 }, 133 { 134 Name: "noname", // no name provided? returns empty (non-nil) response. 135 WithRequest: &sonarr.CommandRequest{Name: ""}, 136 WithResponse: &sonarr.CommandResponse{}, 137 }, 138 } 139 140 for _, test := range tests { 141 test := test 142 t.Run(test.Name, func(t *testing.T) { 143 t.Parallel() 144 mockServer := test.GetMockServer(t) 145 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 146 output, err := client.SendCommand(test.WithRequest.(*sonarr.CommandRequest)) 147 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 148 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 149 }) 150 } 151 } 152 153 func TestGetCommandStatus(t *testing.T) { 154 t.Parallel() 155 156 somedate := time.Now().Add(-36 * time.Hour).Round(time.Millisecond).UTC() 157 datejson, _ := somedate.MarshalJSON() 158 159 tests := []*starrtest.MockData{ 160 { 161 Name: "200", 162 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "command", "146"), 163 ResponseStatus: http.StatusOK, 164 ResponseBody: `{"id":1234,"name":"SomeCommand","commandName":"SomeCommandName","message":` + 165 `"Command Message","priority":"testalert","status":"statusalert","queued":` + string(datejson) + 166 `,"started":` + string(datejson) + `,"ended":` + string(datejson) + 167 `,"stateChangeTime":` + string(datejson) + `,"lastExecutionTime":` + string(datejson) + 168 `,"duration":"woofun","trigger":"someTrigger","sendUpdatesToClient":true,"updateScheduledTask":true` + 169 `,"body": {"mapstring": "mapinterface"}}`, 170 WithError: nil, 171 ExpectedMethod: "GET", 172 WithRequest: int64(146), 173 WithResponse: &sonarr.CommandResponse{ 174 ID: 1234, 175 Name: "SomeCommand", 176 CommandName: "SomeCommandName", 177 Message: "Command Message", 178 Priority: "testalert", 179 Status: "statusalert", 180 Queued: somedate, 181 Started: somedate, 182 Ended: somedate, 183 StateChangeTime: somedate, 184 LastExecutionTime: somedate, 185 Duration: "woofun", 186 Trigger: "someTrigger", 187 SendUpdatesToClient: true, 188 UpdateScheduledTask: true, 189 Body: map[string]interface{}{"mapstring": "mapinterface"}, 190 }, 191 }, 192 { 193 Name: "404", 194 ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "command", "123"), 195 ResponseStatus: http.StatusNotFound, 196 WithRequest: int64(123), 197 ResponseBody: `{"message": "NotFound"}`, 198 WithError: &starr.ReqError{Code: http.StatusNotFound}, 199 ExpectedMethod: "GET", 200 WithResponse: (*sonarr.CommandResponse)(nil), 201 }, 202 { 203 Name: "command0", // command zero returns empty (non-nil) response. 204 WithRequest: int64(0), 205 WithError: nil, 206 ExpectedMethod: "GET", 207 WithResponse: &sonarr.CommandResponse{}, 208 }, 209 } 210 211 for _, test := range tests { 212 test := test 213 t.Run(test.Name, func(t *testing.T) { 214 t.Parallel() 215 mockServer := test.GetMockServer(t) 216 client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 217 output, err := client.GetCommandStatus(test.WithRequest.(int64)) 218 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 219 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 220 }) 221 } 222 }