golift.io/starr@v1.0.0/lidarr/command_test.go (about) 1 package lidarr_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/lidarr" 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 tests := []*starrtest.MockData{ 21 { 22 Name: "200", 23 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "command"), 24 ResponseStatus: http.StatusOK, 25 ResponseBody: `[{"id":1234,"name":"SomeCommand","commandName":"SomeCommandName","message":` + 26 `"Command Message","priority":"testalert","status":"statusalert","queued":` + string(datejson) + 27 `,"started":` + string(datejson) + `,"ended":` + string(datejson) + 28 `,"stateChangeTime":` + string(datejson) + `,"lastExecutionTime":` + string(datejson) + 29 `,"duration":"woofun","trigger":"someTrigger","sendUpdatesToClient":true,"updateScheduledTask":true` + 30 `,"body": {"mapstring": "mapinterface"}` + 31 `}]`, 32 WithError: nil, 33 ExpectedMethod: "GET", 34 WithResponse: []*lidarr.CommandResponse{{ 35 ID: 1234, 36 Name: "SomeCommand", 37 CommandName: "SomeCommandName", 38 Message: "Command Message", 39 Priority: "testalert", 40 Status: "statusalert", 41 Queued: somedate, 42 Started: somedate, 43 Ended: somedate, 44 StateChangeTime: somedate, 45 LastExecutionTime: somedate, 46 Duration: "woofun", 47 Trigger: "someTrigger", 48 SendUpdatesToClient: true, 49 UpdateScheduledTask: true, 50 Body: map[string]interface{}{"mapstring": "mapinterface"}, 51 }}, 52 }, 53 { 54 Name: "404", 55 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "command"), 56 ResponseStatus: http.StatusNotFound, 57 ResponseBody: `{"message": "NotFound"}`, 58 WithError: &starr.ReqError{Code: http.StatusNotFound}, 59 ExpectedMethod: "GET", 60 WithResponse: []*lidarr.CommandResponse(nil), 61 }, 62 } 63 64 for _, test := range tests { 65 test := test 66 t.Run(test.Name, func(t *testing.T) { 67 t.Parallel() 68 mockServer := test.GetMockServer(t) 69 client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 70 output, err := client.GetCommands() 71 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 72 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 73 }) 74 } 75 } 76 77 func TestSendCommand(t *testing.T) { 78 t.Parallel() 79 80 somedate := time.Now().Add(-36 * time.Hour).Round(time.Millisecond).UTC() 81 datejson, _ := somedate.MarshalJSON() 82 tests := []*starrtest.MockData{ 83 { 84 Name: "200", 85 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "command"), 86 ResponseStatus: http.StatusOK, 87 ResponseBody: `{"id":1234,"name":"SomeCommand","commandName":"SomeCommandName","message":` + 88 `"Command Message","priority":"testalert","status":"statusalert","queued":` + string(datejson) + 89 `,"started":` + string(datejson) + `,"ended":` + string(datejson) + 90 `,"stateChangeTime":` + string(datejson) + `,"lastExecutionTime":` + string(datejson) + 91 `,"duration":"woofun","trigger":"someTrigger","sendUpdatesToClient":true,"updateScheduledTask":true` + 92 `,"body": {"mapstring": "mapinterface"}` + 93 `}`, 94 WithError: nil, 95 WithRequest: &lidarr.CommandRequest{ 96 Name: "SomeCommand", 97 AlbumIDs: []int64{1, 3, 7}, 98 }, 99 ExpectedRequest: `{"name":"SomeCommand","albumIds":[1,3,7]}` + "\n", 100 ExpectedMethod: "POST", 101 WithResponse: &lidarr.CommandResponse{ 102 ID: 1234, 103 Name: "SomeCommand", 104 CommandName: "SomeCommandName", 105 Message: "Command Message", 106 Priority: "testalert", 107 Status: "statusalert", 108 Queued: somedate, 109 Started: somedate, 110 Ended: somedate, 111 StateChangeTime: somedate, 112 LastExecutionTime: somedate, 113 Duration: "woofun", 114 Trigger: "someTrigger", 115 SendUpdatesToClient: true, 116 UpdateScheduledTask: true, 117 Body: map[string]interface{}{"mapstring": "mapinterface"}, 118 }, 119 }, 120 { 121 Name: "404", 122 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "command"), 123 ResponseStatus: http.StatusNotFound, 124 ResponseBody: `{"message": "NotFound"}`, 125 WithError: &starr.ReqError{Code: http.StatusNotFound}, 126 ExpectedMethod: "POST", 127 WithResponse: (*lidarr.CommandResponse)(nil), // completely nil (typed) response. 128 WithRequest: &lidarr.CommandRequest{Name: "Something"}, 129 ExpectedRequest: `{"name":"Something"}` + "\n", 130 }, 131 { 132 Name: "noname", // no name provided? returns empty (non-nil) response. 133 WithRequest: &lidarr.CommandRequest{Name: ""}, 134 WithResponse: &lidarr.CommandResponse{}, 135 }, 136 } 137 138 for _, test := range tests { 139 test := test 140 t.Run(test.Name, func(t *testing.T) { 141 t.Parallel() 142 mockServer := test.GetMockServer(t) 143 client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 144 output, err := client.SendCommand(test.WithRequest.(*lidarr.CommandRequest)) 145 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 146 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 147 }) 148 } 149 }