golift.io/starr@v1.0.0/readarr/command_test.go (about)

     1  package readarr_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/readarr"
    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, readarr.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: []*readarr.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, readarr.APIver, "command"),
    56  			ResponseStatus: http.StatusNotFound,
    57  			ResponseBody:   `{"message": "NotFound"}`,
    58  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
    59  			ExpectedMethod: "GET",
    60  			WithResponse:   []*readarr.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 := readarr.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  
    83  	tests := []*starrtest.MockData{
    84  		{
    85  			Name:           "200",
    86  			ExpectedPath:   path.Join("/", starr.API, readarr.APIver, "command"),
    87  			ResponseStatus: http.StatusOK,
    88  			ResponseBody: `{"id":1234,"name":"SomeCommand","commandName":"SomeCommandName","message":` +
    89  				`"Command Message","priority":"testalert","status":"statusalert","queued":` + string(datejson) +
    90  				`,"started":` + string(datejson) + `,"ended":` + string(datejson) +
    91  				`,"stateChangeTime":` + string(datejson) + `,"lastExecutionTime":` + string(datejson) +
    92  				`,"duration":"woofun","trigger":"someTrigger","sendUpdatesToClient":true,"updateScheduledTask":true` +
    93  				`,"body": {"mapstring": "mapinterface"}` +
    94  				`}`,
    95  			WithError: nil,
    96  			WithRequest: &readarr.CommandRequest{
    97  				Name:    "SomeCommand",
    98  				BookIDs: []int64{1, 3, 7},
    99  			},
   100  			ExpectedRequest: `{"name":"SomeCommand","bookIds":[1,3,7]}` + "\n",
   101  			ExpectedMethod:  "POST",
   102  			WithResponse: &readarr.CommandResponse{
   103  				ID:                  1234,
   104  				Name:                "SomeCommand",
   105  				CommandName:         "SomeCommandName",
   106  				Message:             "Command Message",
   107  				Priority:            "testalert",
   108  				Status:              "statusalert",
   109  				Queued:              somedate,
   110  				Started:             somedate,
   111  				Ended:               somedate,
   112  				StateChangeTime:     somedate,
   113  				LastExecutionTime:   somedate,
   114  				Duration:            "woofun",
   115  				Trigger:             "someTrigger",
   116  				SendUpdatesToClient: true,
   117  				UpdateScheduledTask: true,
   118  				Body:                map[string]interface{}{"mapstring": "mapinterface"},
   119  			},
   120  		},
   121  		{
   122  			Name:            "404",
   123  			ExpectedPath:    path.Join("/", starr.API, readarr.APIver, "command"),
   124  			ResponseStatus:  http.StatusNotFound,
   125  			ResponseBody:    `{"message": "NotFound"}`,
   126  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   127  			ExpectedMethod:  "POST",
   128  			WithResponse:    (*readarr.CommandResponse)(nil),
   129  			WithRequest:     &readarr.CommandRequest{Name: "Something"},
   130  			ExpectedRequest: `{"name":"Something"}` + "\n",
   131  		},
   132  		{
   133  			Name:         "noname", // no name provided? returns empty (non-nil) response.
   134  			WithRequest:  &readarr.CommandRequest{Name: ""},
   135  			WithResponse: &readarr.CommandResponse{},
   136  		},
   137  	}
   138  
   139  	for _, test := range tests {
   140  		test := test
   141  		t.Run(test.Name, func(t *testing.T) {
   142  			t.Parallel()
   143  			mockServer := test.GetMockServer(t)
   144  			client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   145  			output, err := client.SendCommand(test.WithRequest.(*readarr.CommandRequest))
   146  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   147  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   148  		})
   149  	}
   150  }