diff --git a/pkg/cmd/services.go b/pkg/cmd/services.go index d770863..6761efd 100644 --- a/pkg/cmd/services.go +++ b/pkg/cmd/services.go @@ -129,8 +129,8 @@ var servicesCancelCmd = cli.Command{ Usage: "Cancel a service", ArgsUsage: "", Flags: []cli.Flag{ - &cli.StringFlag{Name: "reason", Usage: "Cancellation reason", Required: true}, - &cli.StringFlag{Name: "reason-detail", Usage: "Cancellation reason detail"}, + &cli.StringFlag{Name: "reason", Usage: "Cancellation reason code (see services cancellation-reasons)", Required: true}, + &cli.StringFlag{Name: "reason-detail", Usage: "Cancellation reason text (required for CANCEL_OTHER)"}, }, Action: handleServicesCancel, HideHelpCommand: true, @@ -146,10 +146,10 @@ func handleServicesCancel(ctx context.Context, cmd *cli.Command) error { return err } payload := map[string]string{ - "reason": cmd.String("reason"), + "reasonCode": cmd.String("reason"), } if d := cmd.String("reason-detail"); d != "" { - payload["reasonDetail"] = d + payload["reason"] = d } body, _ := json.Marshal(payload) _, err = client.PostJSON(ctx, "/services/v1/services/"+args[0]+"/cancel", body) diff --git a/pkg/cmd/services_test.go b/pkg/cmd/services_test.go new file mode 100644 index 0000000..c9fb026 --- /dev/null +++ b/pkg/cmd/services_test.go @@ -0,0 +1,59 @@ +package cmd + +import ( + "encoding/json" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestServicesCancel(t *testing.T) { + for _, test := range []struct { + name string + reason string + detail string + payload map[string]string + }{ + { + name: "predefined reason", + reason: "CANCEL_PRODUCT_SWITCH", + payload: map[string]string{ + "reasonCode": "CANCEL_PRODUCT_SWITCH", + }, + }, + { + name: "other reason with detail", + reason: "CANCEL_OTHER", + detail: "Do not renew at the end of the current contract term.", + payload: map[string]string{ + "reasonCode": "CANCEL_OTHER", + "reason": "Do not renew at the end of the current contract term.", + }, + }, + } { + t.Run(test.name, func(t *testing.T) { + called := false + srv := newTestServer(t, map[string]http.HandlerFunc{ + "POST /services/v1/services/12345/cancel": func(w http.ResponseWriter, r *http.Request) { + called = true + var payload map[string]string + require.NoError(t, json.NewDecoder(r.Body).Decode(&payload)) + assert.Equal(t, test.payload, payload) + w.WriteHeader(http.StatusNoContent) + }, + }) + defer srv.Close() + + args := []string{"services", "cancel", "12345", "--reason", test.reason} + if test.detail != "" { + args = append(args, "--reason-detail", test.detail) + } + _, stderr, err := runCLI(t, srv.URL, args) + require.NoError(t, err) + assert.True(t, called) + assert.Contains(t, stderr, "Cancelled service 12345") + }) + } +}