//nolint:dupl package delete import ( "context" "encoding/json" "strings" "testing" "time" "github.com/stretchr/testify/require" "edge-infra.dev/pkg/edge/api/fake" "edge-infra.dev/pkg/edge/api/graph/model" "edge-infra.dev/pkg/edge/edgecli" "edge-infra.dev/pkg/edge/edgecli/flagutil" "edge-infra.dev/pkg/lib/cli/rags" ) type DeleteOperatorInterventionCommandResponse struct { DeleteOperatorInterventionCommand model.DeleteOperatorInterventionCommandResponse `json:"deleteOperatorInterventionCommand"` } // Mocks the DeleteOperatorInterventionCommandResponse. Returns an error // response when the command `bad-command` is sent, otherwise returns a // non-error response func DeleteOperatorInterventionCommand(body []byte) (interface{}, bool, error) { if !strings.Contains(string(body), "deleteOperatorInterventionCommand(command: $command)") { return nil, false, nil } // This callback should handle the response var ok = true var m struct { Query string Variables struct { Command struct { Name string } } } err := json.Unmarshal(body, &m) if err != nil { return DeleteOperatorInterventionCommandResponse{}, ok, nil } badCommand := "bad-command" var errors []*model.OperatorInterventionErrorResponse if m.Variables.Command.Name == "bad-command" { errors = []*model.OperatorInterventionErrorResponse{{ Type: model.OperatorInterventionErrorTypeUnknownCommand, Command: &badCommand, }} } return DeleteOperatorInterventionCommandResponse{ DeleteOperatorInterventionCommand: model.DeleteOperatorInterventionCommandResponse{ Errors: errors, }, }, ok, nil } func TestDeleteCommand(t *testing.T) { t.Parallel() edgeAPIMockSvr := fake.GetMockAPIServer(DeleteOperatorInterventionCommand) edgeAPIMockURL := edgeAPIMockSvr.URL + "/api/v2" t.Cleanup(func() { edgeAPIMockSvr.Close() }) tests := map[string]struct { flagsFunc func(*rags.RagSet) error expError require.ErrorAssertionFunc }{ "No Flags": { flagsFunc: func(*rags.RagSet) error { return nil }, expError: require.Error, }, "Empty Flag": { flagsFunc: func(c *rags.RagSet) error { return flagutil.SetFlag(c, flagutil.OICommand, "") }, expError: require.Error, }, "Flag": { flagsFunc: func(c *rags.RagSet) error { return flagutil.SetFlag(c, flagutil.OICommand, "abcd") }, expError: require.NoError, }, "Bad Command": { flagsFunc: func(c *rags.RagSet) error { return flagutil.SetFlag(c, flagutil.OICommand, "bad-command") }, expError: require.Error, }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() // Set a dummy token in a fake banner context so that we bypass the // ValidateConnectionFlags check which would otherwise do a login // mutation to the api server, something which is not supported by // the fake server future := time.Now().Add(time.Hour * 24) testConfig := edgecli.Config{ CurrentBannerContext: "fakeBanner", BannerContexts: map[string]*edgecli.BannerContext{ "fakeBanner": { TokenTime: future.Format(time.RFC3339), Token: "fakeToken", Endpoint: edgeAPIMockURL, }, }, } cmd := NewDeleteCommand(&testConfig) cmd.Command() // Required to initialise cmd.Rags require.NoError(t, tc.flagsFunc(cmd.Rags)) err := cmd.Command().Exec(context.Background(), []string{}) tc.expError(t, err) }) } }