diff --git a/pkg/cmd/delete/cache_setting/cache_setting.go b/pkg/cmd/delete/cache_setting/cache_setting.go index 304eac4ba..116449003 100644 --- a/pkg/cmd/delete/cache_setting/cache_setting.go +++ b/pkg/cmd/delete/cache_setting/cache_setting.go @@ -9,6 +9,7 @@ import ( msg "github.com/aziontech/azion-cli/messages/cache_setting" api "github.com/aziontech/azion-cli/pkg/api/cache_setting" "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/iostreams" "github.com/aziontech/azion-cli/pkg/logger" "github.com/aziontech/azion-cli/pkg/output" "github.com/aziontech/azion-cli/utils" @@ -21,20 +22,42 @@ var ( cacheSettingsID int64 ) -func NewCmd(f *cmdutil.Factory) *cobra.Command { - cmd := &cobra.Command{ +type DeleteCmd struct { + Io *iostreams.IOStreams + ReadInput func(string) (string, error) + DeleteCache func(context.Context, int64, int64) error + AskInput func(msg string) (string, error) +} + +func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd { + return &DeleteCmd{ + Io: f.IOStreams, + ReadInput: func(prompt string) (string, error) { + return utils.AskInput(prompt) + }, + DeleteCache: func(ctx context.Context, appID int64, cacheID int64) error { + client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) + return client.Delete(ctx, appID, cacheID) + }, + AskInput: utils.AskInput, + } +} + +func NewCobraCmd(delete *DeleteCmd, f *cmdutil.Factory) *cobra.Command { + cobraCmd := &cobra.Command{ Use: msg.Usage, Short: msg.DeleteShortDescription, Long: msg.DeleteLongDescription, SilenceUsage: true, SilenceErrors: true, Example: heredoc.Doc(` - $ azion delete cache-setting --application-id 1673635839 --cache-setting-id 107313 - `), + $ azion delete cache-setting --application-id 1673635839 --cache-setting-id 107313 + `), RunE: func(cmd *cobra.Command, args []string) error { + var err error if !cmd.Flags().Changed("application-id") { - answer, err := utils.AskInput(msg.ListAskInputApplicationID) + answer, err := delete.AskInput(msg.ListAskInputApplicationID) if err != nil { return err } @@ -49,7 +72,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { } if !cmd.Flags().Changed("cache-setting-id") { - answer, err := utils.AskInput(msg.DeleteAskInputCacheID) + answer, err := delete.AskInput(msg.DeleteAskInputCacheID) if err != nil { return err } @@ -67,7 +90,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { ctx := context.Background() - err := client.Delete(ctx, applicationID, cacheSettingsID) + err = client.Delete(ctx, applicationID, cacheSettingsID) if err != nil { return fmt.Errorf(msg.ErrorFailToDelete.Error(), err) } @@ -78,12 +101,16 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { Flags: f.Flags, } return output.Print(&deleteOut) - }, } - cmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.DeleteFlagApplicationID) - cmd.Flags().Int64Var(&cacheSettingsID, "cache-setting-id", 0, msg.DeleteFlagCacheSettingsID) - cmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag) - return cmd + cobraCmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag) + cobraCmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.DeleteFlagApplicationID) + cobraCmd.Flags().Int64Var(&cacheSettingsID, "cache-setting-id", 0, msg.DeleteFlagCacheSettingsID) + + return cobraCmd +} + +func NewCmd(f *cmdutil.Factory) *cobra.Command { + return NewCobraCmd(NewDeleteCmd(f), f) } diff --git a/pkg/cmd/delete/cache_setting/cache_setting_test.go b/pkg/cmd/delete/cache_setting/cache_setting_test.go index 9a14440ad..0ea1f6b1d 100644 --- a/pkg/cmd/delete/cache_setting/cache_setting_test.go +++ b/pkg/cmd/delete/cache_setting/cache_setting_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/aziontech/azion-cli/pkg/logger" + "go.uber.org/zap" "go.uber.org/zap/zapcore" msg "github.com/aziontech/azion-cli/messages/cache_setting" @@ -14,38 +15,103 @@ import ( "github.com/stretchr/testify/require" ) -func TestCreate(t *testing.T) { +func mockAppId(msg string) (string, error) { + return "1673635839", nil +} + +func mockCacheId(msg string) (string, error) { + return "107313", nil +} + +func mockInvalid(msg string) (string, error) { + return "invalid", nil +} + +func TestDeleteWithAskInput(t *testing.T) { logger.New(zapcore.DebugLevel) - t.Run("delete by id", func(t *testing.T) { - mock := &httpmock.Registry{} - - mock.Register( - httpmock.REST("DELETE", "edge_applications/1673635839/cache_settings/107313"), - httpmock.StatusStringResponse(204, ""), - ) - - f, stdout, _ := testutils.NewFactory(mock) - cmd := NewCmd(f) - cmd.SetArgs([]string{"--application-id", "1673635839", "--cache-setting-id", "107313"}) - - _, err := cmd.ExecuteC() - require.NoError(t, err) - assert.Equal(t, fmt.Sprintf(msg.DeleteOutputSuccess, 107313), stdout.String()) - }) - - t.Run("delete that is not found", func(t *testing.T) { - mock := &httpmock.Registry{} - - mock.Register( - httpmock.REST("DELETE", "edge_applications/1234/cache_settings/107313"), - httpmock.StatusStringResponse(404, "Not Found"), - ) - - f, _, _ := testutils.NewFactory(mock) - cmd := NewCmd(f) - cmd.SetArgs([]string{"--application-id", "1234", "--cache-setting-id", "107313"}) - - _, err := cmd.ExecuteC() - require.Error(t, err) - }) + + tests := []struct { + name string + applicationID string + cacheSettingID string + method string + endpoint string + statusCode int + responseBody string + expectedOutput string + expectError bool + mockInputs func(msg string) (string, error) + mockError error + }{ + { + name: "ask for application id success", + cacheSettingID: "107313", + method: "DELETE", + endpoint: "edge_applications/1673635839/cache_settings/107313", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, 107313), + expectError: false, + mockInputs: mockAppId, + mockError: nil, + }, + { + name: "ask for cache setting id success", + applicationID: "1673635839", + method: "DELETE", + endpoint: "edge_applications/1673635839/cache_settings/107313", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, 107313), + expectError: false, + mockInputs: mockCacheId, + mockError: nil, + }, + { + name: "error in input", + cacheSettingID: "107313", + method: "DELETE", + endpoint: "edge_applications/invalid/cache_settings/107313", + statusCode: 400, + responseBody: "Bad Request", + expectedOutput: "", + expectError: true, + mockInputs: mockInvalid, + mockError: fmt.Errorf("invalid argument \"\" for \"--cache-setting-id\" flag: strconv.ParseInt: parsing \"\": invalid syntax"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mock := &httpmock.Registry{} + mock.Register( + httpmock.REST(tt.method, tt.endpoint), + httpmock.StatusStringResponse(tt.statusCode, tt.responseBody), + ) + + f, stdout, _ := testutils.NewFactory(mock) + + deleteCmd := NewDeleteCmd(f) + deleteCmd.AskInput = tt.mockInputs + cobraCmd := NewCobraCmd(deleteCmd, f) + + if tt.applicationID != "" && tt.cacheSettingID != "" { + cobraCmd.SetArgs([]string{"--application-id", tt.applicationID, "--cache-setting-id", tt.cacheSettingID}) + } else if tt.applicationID != "" { + cobraCmd.SetArgs([]string{"--application-id", tt.applicationID}) + } else { + cobraCmd.SetArgs([]string{"--cache-setting-id", tt.cacheSettingID}) + } + + _, err := cobraCmd.ExecuteC() + if tt.expectError { + require.Error(t, err) + logger.Debug("Expected error occurred", zap.Error(err)) + } else { + require.NoError(t, err) + assert.Equal(t, tt.expectedOutput, stdout.String()) + logger.Debug("Expected output", zap.String("output", stdout.String())) + } + }) + } } diff --git a/pkg/cmd/delete/domain/domain.go b/pkg/cmd/delete/domain/domain.go index 5e4dd89d3..b853b2b48 100644 --- a/pkg/cmd/delete/domain/domain.go +++ b/pkg/cmd/delete/domain/domain.go @@ -9,6 +9,7 @@ import ( msg "github.com/aziontech/azion-cli/messages/delete/domain" api "github.com/aziontech/azion-cli/pkg/api/domain" "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/iostreams" "github.com/aziontech/azion-cli/pkg/logger" "github.com/aziontech/azion-cli/pkg/output" "github.com/aziontech/azion-cli/utils" @@ -16,9 +17,31 @@ import ( "go.uber.org/zap" ) -func NewCmd(f *cmdutil.Factory) *cobra.Command { - var domainID int64 - cmd := &cobra.Command{ +var domainID int64 + +type DeleteCmd struct { + Io *iostreams.IOStreams + ReadInput func(string) (string, error) + DeleteDomain func(context.Context, int64) error + AskInput func(string) (string, error) +} + +func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd { + return &DeleteCmd{ + Io: f.IOStreams, + ReadInput: func(prompt string) (string, error) { + return utils.AskInput(prompt) + }, + DeleteDomain: func(ctx context.Context, domainID int64) error { + client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) + return client.Delete(ctx, domainID) + }, + AskInput: utils.AskInput, + } +} + +func NewCobraCmd(delete *DeleteCmd, f *cmdutil.Factory) *cobra.Command { + cobraCmd := &cobra.Command{ Use: msg.Usage, Short: msg.ShortDescription, Long: msg.LongDescription, @@ -26,12 +49,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { SilenceErrors: true, Example: heredoc.Doc(` $ azion delete domain --domain-id 1234 - `), + `), RunE: func(cmd *cobra.Command, args []string) error { + var err error if !cmd.Flags().Changed("domain-id") { - - answer, err := utils.AskInput(msg.AskDeleteInput) + answer, err := delete.AskInput(msg.AskDeleteInput) if err != nil { return err } @@ -49,7 +72,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { ctx := context.Background() - err := client.Delete(ctx, domainID) + err = client.Delete(ctx, domainID) if err != nil { return fmt.Errorf(msg.ErrorFailToDeleteDomain.Error(), err) } @@ -63,7 +86,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { }, } - cmd.Flags().Int64Var(&domainID, "domain-id", 0, msg.FlagId) - cmd.Flags().BoolP("help", "h", false, msg.HelpFlag) - return cmd + cobraCmd.Flags().Int64Var(&domainID, "domain-id", 0, msg.FlagId) + cobraCmd.Flags().BoolP("help", "h", false, msg.HelpFlag) + + return cobraCmd +} + +func NewCmd(f *cmdutil.Factory) *cobra.Command { + return NewCobraCmd(NewDeleteCmd(f), f) } diff --git a/pkg/cmd/delete/domain/domain_test.go b/pkg/cmd/delete/domain/domain_test.go index 03e5c2b22..3afd088f0 100644 --- a/pkg/cmd/delete/domain/domain_test.go +++ b/pkg/cmd/delete/domain/domain_test.go @@ -1,53 +1,146 @@ package domain import ( + "fmt" "testing" "github.com/aziontech/azion-cli/pkg/logger" + "github.com/aziontech/azion-cli/utils" "go.uber.org/zap/zapcore" + msg "github.com/aziontech/azion-cli/messages/delete/domain" "github.com/aziontech/azion-cli/pkg/httpmock" "github.com/aziontech/azion-cli/pkg/testutils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func TestCreate(t *testing.T) { - logger.New(zapcore.DebugLevel) - t.Run("delete domain by id", func(t *testing.T) { - mock := &httpmock.Registry{} - - mock.Register( - httpmock.REST("DELETE", "domains/1234"), - httpmock.StatusStringResponse(204, ""), - ) - - f, stdout, _ := testutils.NewFactory(mock) +func mockDomainID(msg string) (string, error) { + return "1234", nil +} - cmd := NewCmd(f) - cmd.SetArgs([]string{"--domain-id", "1234"}) +func mockInvalid(msg string) (string, error) { + return "invalid", nil +} - _, err := cmd.ExecuteC() - require.NoError(t, err) +func mockParseError(msg string) (string, error) { + return "invalid", utils.ErrorParseResponse +} - assert.Equal(t, "Domain 1234 was successfully deleted", stdout.String()) - }) +func TestDeleteWithAskInput(t *testing.T) { + logger.New(zapcore.DebugLevel) - t.Run("delete domain - not found", func(t *testing.T) { - mock := &httpmock.Registry{} + tests := []struct { + name string + domainID string + method string + endpoint string + statusCode int + responseBody string + expectedOutput string + expectError bool + mockInputs func(string) (string, error) + mockError error + }{ + { + name: "delete domain by id", + domainID: "1234", + method: "DELETE", + endpoint: "domains/1234", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.OutputSuccess, 1234), + expectError: false, + mockInputs: mockDomainID, + mockError: nil, + }, + { + name: "delete domain - not found", + domainID: "1234", + method: "DELETE", + endpoint: "domains/1234", + statusCode: 404, + responseBody: "Not Found", + expectedOutput: "", + expectError: true, + mockInputs: mockDomainID, + mockError: fmt.Errorf("Failed to parse your response. Check your response and try again. If the error persists, contact Azion support"), + }, + { + name: "error in input", + domainID: "1234", + method: "DELETE", + endpoint: "domains/invalid", + statusCode: 400, + responseBody: "Bad Request", + expectedOutput: "", + expectError: true, + mockInputs: mockInvalid, + mockError: fmt.Errorf("invalid argument \"\" for \"--domain-id\" flag: strconv.ParseInt: parsing \"\": invalid syntax"), + }, + { + name: "ask for domain id success", + domainID: "", + method: "DELETE", + endpoint: "domains/1234", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.OutputSuccess, 1234), + expectError: false, + mockInputs: mockDomainID, + mockError: nil, + }, + { + name: "ask for domain id conversion failure", + domainID: "", + method: "", + endpoint: "", + statusCode: 0, + responseBody: "", + expectedOutput: "", + expectError: true, + mockInputs: mockInvalid, + mockError: fmt.Errorf(msg.ErrorConvertId.Error()), + }, + { + name: "error - parse answer", + domainID: "", + method: "", + endpoint: "", + statusCode: 0, + responseBody: "", + expectedOutput: "", + expectError: true, + mockInputs: mockParseError, + mockError: fmt.Errorf(utils.ErrorParseResponse.Error()), + }, + } - mock.Register( - httpmock.REST("DELETE", "domains/1234"), - httpmock.StatusStringResponse(404, "Not Found"), - ) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mock := &httpmock.Registry{} + mock.Register( + httpmock.REST(tt.method, tt.endpoint), + httpmock.StatusStringResponse(tt.statusCode, tt.responseBody), + ) - f, _, _ := testutils.NewFactory(mock) + f, stdout, _ := testutils.NewFactory(mock) - cmd := NewCmd(f) + deleteCmd := NewDeleteCmd(f) + deleteCmd.AskInput = tt.mockInputs + cobraCmd := NewCobraCmd(deleteCmd, f) - cmd.SetArgs([]string{"--domain-id", "1234"}) + if tt.domainID != "" { + cobraCmd.SetArgs([]string{"--domain-id", tt.domainID}) + } - _, err := cmd.ExecuteC() - require.Error(t, err) - }) + _, err := cobraCmd.ExecuteC() + if tt.expectError { + require.Error(t, err) + } else { + require.NoError(t, err) + assert.Equal(t, tt.expectedOutput, stdout.String()) + } + }) + } } diff --git a/pkg/cmd/delete/edge_function/edge_function.go b/pkg/cmd/delete/edge_function/edge_function.go index 0db196094..61fc94d13 100644 --- a/pkg/cmd/delete/edge_function/edge_function.go +++ b/pkg/cmd/delete/edge_function/edge_function.go @@ -9,6 +9,7 @@ import ( msg "github.com/aziontech/azion-cli/messages/edge_function" api "github.com/aziontech/azion-cli/pkg/api/edge_function" "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/iostreams" "github.com/aziontech/azion-cli/pkg/logger" "github.com/aziontech/azion-cli/pkg/output" "github.com/aziontech/azion-cli/utils" @@ -16,20 +17,44 @@ import ( "go.uber.org/zap" ) -func NewCmd(f *cmdutil.Factory) *cobra.Command { - var function_id int64 - cmd := &cobra.Command{ +var functionID int64 + +type DeleteCmd struct { + Io *iostreams.IOStreams + ReadInput func(string) (string, error) + DeleteFunction func(context.Context, int64) error + AskInput func(string) (string, error) +} + +func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd { + return &DeleteCmd{ + Io: f.IOStreams, + ReadInput: func(prompt string) (string, error) { + return utils.AskInput(prompt) + }, + DeleteFunction: func(ctx context.Context, functionID int64) error { + client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) + return client.Delete(ctx, functionID) + }, + AskInput: utils.AskInput, + } +} + +func NewCobraCmd(delete *DeleteCmd, f *cmdutil.Factory) *cobra.Command { + cobraCmd := &cobra.Command{ Use: msg.Usage, Short: msg.DeleteShortDescription, Long: msg.DeleteLongDescription, SilenceUsage: true, SilenceErrors: true, Example: heredoc.Doc(` - $ azion delete edge-function --function-id 1234 - `), + $ azion delete edge-function --function-id 1234 + `), RunE: func(cmd *cobra.Command, args []string) error { + var err error + if !cmd.Flags().Changed("function-id") { - answer, err := utils.AskInput(msg.AskEdgeFunctionID) + answer, err := delete.AskInput(msg.AskEdgeFunctionID) if err != nil { return err } @@ -40,20 +65,20 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { return msg.ErrorConvertIdFunction } - function_id = num + functionID = num } client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) ctx := context.Background() - err := client.Delete(ctx, function_id) + err = client.Delete(ctx, functionID) if err != nil { return fmt.Errorf(msg.ErrorFailToDeleteFunction.Error(), err) } deleteOut := output.GeneralOutput{ - Msg: fmt.Sprintf(msg.DeleteOutputSuccess, function_id), + Msg: fmt.Sprintf(msg.DeleteOutputSuccess, functionID), Out: f.IOStreams.Out, Flags: f.Flags, } @@ -61,8 +86,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { }, } - cmd.Flags().Int64Var(&function_id, "function-id", 0, msg.FlagID) - cmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag) + cobraCmd.Flags().Int64Var(&functionID, "function-id", 0, msg.FlagID) + cobraCmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag) - return cmd + return cobraCmd +} + +func NewCmd(f *cmdutil.Factory) *cobra.Command { + return NewCobraCmd(NewDeleteCmd(f), f) } diff --git a/pkg/cmd/delete/edge_function/edge_function_test.go b/pkg/cmd/delete/edge_function/edge_function_test.go index 88ec8c21a..ac32d28dd 100644 --- a/pkg/cmd/delete/edge_function/edge_function_test.go +++ b/pkg/cmd/delete/edge_function/edge_function_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/aziontech/azion-cli/pkg/logger" + "github.com/aziontech/azion-cli/utils" "go.uber.org/zap/zapcore" msg "github.com/aziontech/azion-cli/messages/edge_function" @@ -14,42 +15,132 @@ import ( "github.com/stretchr/testify/require" ) -func TestCreate(t *testing.T) { - logger.New(zapcore.DebugLevel) - t.Run("delete function by id", func(t *testing.T) { - mock := &httpmock.Registry{} - - mock.Register( - httpmock.REST("DELETE", "edge_functions/1234"), - httpmock.StatusStringResponse(204, ""), - ) - - f, stdout, _ := testutils.NewFactory(mock) +func mockFunctionID(msg string) (string, error) { + return "1234", nil +} - cmd := NewCmd(f) - cmd.SetArgs([]string{"--function-id", "1234"}) +func mockInvalidFunctionID(msg string) (string, error) { + return "invalid", nil +} - _, err := cmd.ExecuteC() - require.NoError(t, err) +func mockParseErrorFunctionID(msg string) (string, error) { + return "invalid", utils.ErrorParseResponse +} - assert.Equal(t, fmt.Sprintf(msg.DeleteOutputSuccess, 1234), stdout.String()) - }) +func TestDeleteWithAskInput(t *testing.T) { + logger.New(zapcore.DebugLevel) - t.Run("delete function that is not found", func(t *testing.T) { - mock := &httpmock.Registry{} + tests := []struct { + name string + functionID string + method string + endpoint string + statusCode int + responseBody string + expectedOutput string + expectError bool + mockInputs func(string) (string, error) + mockError error + }{ + { + name: "delete function by id", + functionID: "1234", + method: "DELETE", + endpoint: "edge_functions/1234", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, 1234), + expectError: false, + mockInputs: mockFunctionID, + mockError: nil, + }, + { + name: "delete function - not found", + functionID: "1234", + method: "DELETE", + endpoint: "edge_functions/1234", + statusCode: 404, + responseBody: "Not Found", + expectedOutput: "", + expectError: true, + mockInputs: mockFunctionID, + mockError: fmt.Errorf("Failed to parse your response. Check your response and try again. If the error persists, contact Azion support"), + }, + { + name: "error in input", + functionID: "1234", + method: "DELETE", + endpoint: "edge_functions/invalid", + statusCode: 400, + responseBody: "Bad Request", + expectedOutput: "", + expectError: true, + mockInputs: mockInvalidFunctionID, + mockError: fmt.Errorf("invalid argument \"\" for \"--function-id\" flag: strconv.ParseInt: parsing \"\": invalid syntax"), + }, + { + name: "ask for function id success", + functionID: "", + method: "DELETE", + endpoint: "edge_functions/1234", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, 1234), + expectError: false, + mockInputs: mockFunctionID, + mockError: nil, + }, + { + name: "ask for function id conversion failure", + functionID: "", + method: "", + endpoint: "", + statusCode: 0, + responseBody: "", + expectedOutput: "", + expectError: true, + mockInputs: mockInvalidFunctionID, + mockError: fmt.Errorf(msg.ErrorConvertIdFunction.Error()), + }, + { + name: "error - parse answer", + functionID: "", + method: "", + endpoint: "", + statusCode: 0, + responseBody: "", + expectedOutput: "", + expectError: true, + mockInputs: mockParseErrorFunctionID, + mockError: fmt.Errorf(utils.ErrorParseResponse.Error()), + }, + } - mock.Register( - httpmock.REST("DELETE", "edge_functions/1234"), - httpmock.StatusStringResponse(404, "Not Found"), - ) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mock := &httpmock.Registry{} + mock.Register( + httpmock.REST(tt.method, tt.endpoint), + httpmock.StatusStringResponse(tt.statusCode, tt.responseBody), + ) - f, _, _ := testutils.NewFactory(mock) + f, stdout, _ := testutils.NewFactory(mock) - cmd := NewCmd(f) + deleteCmd := NewDeleteCmd(f) + deleteCmd.AskInput = tt.mockInputs + cobraCmd := NewCobraCmd(deleteCmd, f) - cmd.SetArgs([]string{"--function-id", "1234"}) + if tt.functionID != "" { + cobraCmd.SetArgs([]string{"--function-id", tt.functionID}) + } - _, err := cmd.ExecuteC() - require.Error(t, err) - }) + _, err := cobraCmd.ExecuteC() + if tt.expectError { + require.Error(t, err) + } else { + require.NoError(t, err) + assert.Equal(t, tt.expectedOutput, stdout.String()) + } + }) + } } diff --git a/pkg/cmd/delete/origin/origin.go b/pkg/cmd/delete/origin/origin.go index e28c400af..0d52f089a 100644 --- a/pkg/cmd/delete/origin/origin.go +++ b/pkg/cmd/delete/origin/origin.go @@ -9,6 +9,7 @@ import ( msg "github.com/aziontech/azion-cli/messages/origin" api "github.com/aziontech/azion-cli/pkg/api/origin" "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/iostreams" "github.com/aziontech/azion-cli/pkg/logger" "github.com/aziontech/azion-cli/pkg/output" "github.com/aziontech/azion-cli/utils" @@ -16,10 +17,34 @@ import ( "go.uber.org/zap" ) -func NewCmd(f *cmdutil.Factory) *cobra.Command { - var applicationID int64 - var originKey string - cmd := &cobra.Command{ +var ( + applicationID int64 + originKey string +) + +type DeleteCmd struct { + Io *iostreams.IOStreams + ReadInput func(string) (string, error) + DeleteOrigins func(context.Context, int64, string) error + AskInput func(string) (string, error) +} + +func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd { + return &DeleteCmd{ + Io: f.IOStreams, + ReadInput: func(prompt string) (string, error) { + return utils.AskInput(prompt) + }, + DeleteOrigins: func(ctx context.Context, appID int64, key string) error { + client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) + return client.DeleteOrigins(ctx, appID, key) + }, + AskInput: utils.AskInput, + } +} + +func NewCobraCmd(delete *DeleteCmd, f *cmdutil.Factory) *cobra.Command { + cobraCmd := &cobra.Command{ Use: msg.Usage, Short: msg.DeleteShortDescription, Long: msg.DeleteLongDescription, @@ -28,10 +53,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { Example: heredoc.Doc(` $ azion delete origin --application-id 1673635839 --origin-key 03a6e7bf-8e26-49c7-a66e-ab8eaa425086 $ azion delete origin - `), + `), RunE: func(cmd *cobra.Command, args []string) error { + var err error + if !cmd.Flags().Changed("application-id") { - answer, err := utils.AskInput(msg.DeleteAskInputApp) + answer, err := delete.AskInput(msg.DeleteAskInputApp) if err != nil { return err } @@ -44,7 +71,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { } if !cmd.Flags().Changed("origin-key") { - answer, err := utils.AskInput(msg.DeleteAskInputOri) + answer, err := delete.AskInput(msg.DeleteAskInputOri) if err != nil { return err } @@ -52,9 +79,8 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { } ctx := context.Background() - client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) - err := client.DeleteOrigins(ctx, applicationID, originKey) + err = delete.DeleteOrigins(ctx, applicationID, originKey) if err != nil { return fmt.Errorf(msg.ErrorFailToDelete.Error(), err) } @@ -68,8 +94,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command { }, } - cmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.FlagEdgeApplicationID) - cmd.Flags().StringVar(&originKey, "origin-key", "", msg.FlagOriginKey) - cmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag) - return cmd + cobraCmd.Flags().Int64Var(&applicationID, "application-id", 0, msg.FlagEdgeApplicationID) + cobraCmd.Flags().StringVar(&originKey, "origin-key", "", msg.FlagOriginKey) + cobraCmd.Flags().BoolP("help", "h", false, msg.DeleteHelpFlag) + return cobraCmd +} + +func NewCmd(f *cmdutil.Factory) *cobra.Command { + return NewCobraCmd(NewDeleteCmd(f), f) } diff --git a/pkg/cmd/delete/origin/origin_test.go b/pkg/cmd/delete/origin/origin_test.go index f68e020f4..1acab12cc 100644 --- a/pkg/cmd/delete/origin/origin_test.go +++ b/pkg/cmd/delete/origin/origin_test.go @@ -4,49 +4,121 @@ import ( "fmt" "testing" - "github.com/aziontech/azion-cli/pkg/logger" - "go.uber.org/zap/zapcore" - msg "github.com/aziontech/azion-cli/messages/origin" "github.com/aziontech/azion-cli/pkg/httpmock" + "github.com/aziontech/azion-cli/pkg/logger" "github.com/aziontech/azion-cli/pkg/testutils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) -func TestCreate(t *testing.T) { +func mockAppID(msg string) (string, error) { + return "1673635839", nil +} + +func mockOriginKey(msg string) (string, error) { + return "58755fef-e830-4ea4-b9e0-6481f1ef496d", nil +} + +func mockInvalid(msg string) (string, error) { + return "invalid", nil +} + +func TestDeleteWithAskInput(t *testing.T) { logger.New(zapcore.DebugLevel) - t.Run("delete origin by key", func(t *testing.T) { - mock := &httpmock.Registry{} - mock.Register( - httpmock.REST("DELETE", "edge_applications/1673635839/origins/58755fef-e830-4ea4-b9e0-6481f1ef496d"), - httpmock.StatusStringResponse(204, ""), - ) + tests := []struct { + name string + applicationID string + originKey string + method string + endpoint string + statusCode int + responseBody string + expectedOutput string + expectError bool + mockInputs func(msg string) (string, error) + mockError error + }{ + { + name: "ask for application id success", + applicationID: "1673635839", + originKey: "58755fef-e830-4ea4-b9e0-6481f1ef496d", + method: "DELETE", + endpoint: "edge_applications/1673635839/origins/58755fef-e830-4ea4-b9e0-6481f1ef496d", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, "58755fef-e830-4ea4-b9e0-6481f1ef496d"), + expectError: false, + mockInputs: mockAppID, + mockError: nil, + }, + { + name: "ask for origin key success", + applicationID: "1673635839", + method: "DELETE", + endpoint: "edge_applications/1673635839/origins/58755fef-e830-4ea4-b9e0-6481f1ef496d", + statusCode: 204, + responseBody: "", + expectedOutput: fmt.Sprintf(msg.DeleteOutputSuccess, "58755fef-e830-4ea4-b9e0-6481f1ef496d"), + expectError: false, + mockInputs: mockOriginKey, + mockError: nil, + }, + { + name: "error in input", + originKey: "58755fef-e830-4ea4-b9e0-6481f1ef496d", + method: "DELETE", + endpoint: "edge_applications/invalid/origins/58755fef-e830-4ea4-b9e0-6481f1ef496d", + statusCode: 400, + responseBody: "Bad Request", + expectedOutput: "", + expectError: true, + mockInputs: mockInvalid, + mockError: fmt.Errorf("invalid argument \"\" for \"--application-id\" flag: strconv.ParseInt: parsing \"\": invalid syntax"), + }, + } - f, stdout, _ := testutils.NewFactory(mock) - cmd := NewCmd(f) - cmd.SetArgs([]string{"--application-id", "1673635839", "--origin-key", "58755fef-e830-4ea4-b9e0-6481f1ef496d"}) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mock := &httpmock.Registry{} + mock.Register( + httpmock.REST(tt.method, tt.endpoint), + httpmock.StatusStringResponse(tt.statusCode, tt.responseBody), + ) - _, err := cmd.ExecuteC() - require.NoError(t, err) + f, stdout, _ := testutils.NewFactory(mock) - assert.Equal(t, fmt.Sprintf(msg.DeleteOutputSuccess, "58755fef-e830-4ea4-b9e0-6481f1ef496d"), stdout.String()) - }) + // Create your delete command instance (adjust as per your package structure) + deleteCmd := NewDeleteCmd(f) + deleteCmd.AskInput = tt.mockInputs - t.Run("delete domain - not found", func(t *testing.T) { - mock := &httpmock.Registry{} + // Create your Cobra command instance (adjust as per your package structure) + cobraCmd := NewCobraCmd(deleteCmd, f) - mock.Register( - httpmock.REST("DELETE", "edge_applications/1673635839/origins/58755fef-e830-4ea4-b9e0-6481f1ef496d"), - httpmock.StatusStringResponse(404, "Not Found"), - ) + // Set command line arguments based on test case + if tt.applicationID != "" && tt.originKey != "" { + cobraCmd.SetArgs([]string{"--application-id", tt.applicationID, "--origin-key", tt.originKey}) + } else if tt.applicationID != "" { + cobraCmd.SetArgs([]string{"--application-id", tt.applicationID}) + } else { + cobraCmd.SetArgs([]string{"--origin-key", tt.originKey}) + } - f, _, _ := testutils.NewFactory(mock) - cmd := NewCmd(f) - cmd.SetArgs([]string{"--application-id", "1673635839", "--origin-key", "58755fef-e830-4ea4-b9e0-6481f1ef496d"}) + // Execute the command + _, err := cobraCmd.ExecuteC() - _, err := cmd.ExecuteC() - require.Error(t, err) - }) + // Validate the results + if tt.expectError { + require.Error(t, err) + logger.Debug("Expected error occurred", zap.Error(err)) + } else { + require.NoError(t, err) + assert.Equal(t, tt.expectedOutput, stdout.String()) + logger.Debug("Expected output", zap.String("output", stdout.String())) + } + }) + } }