|
| 1 | +package bot_management_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/fastly/go-fastly/v9/fastly" |
| 7 | + "github.com/fastly/go-fastly/v9/fastly/products/bot_management" |
| 8 | + |
| 9 | + "github.com/fastly/cli/pkg/api" |
| 10 | + root "github.com/fastly/cli/pkg/commands/product" |
| 11 | + sub "github.com/fastly/cli/pkg/commands/product/bot_management" |
| 12 | + "github.com/fastly/cli/pkg/global" |
| 13 | + "github.com/fastly/cli/pkg/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func TestProductEnablement(t *testing.T) { |
| 17 | + scenarios := []testutil.CLIScenario{ |
| 18 | + { |
| 19 | + Name: "validate missing Service ID: enable", |
| 20 | + Args: "enable", |
| 21 | + WantError: "error reading service: no service ID found", |
| 22 | + }, |
| 23 | + { |
| 24 | + Name: "validate missing Service ID: disable", |
| 25 | + Args: "enable", |
| 26 | + WantError: "error reading service: no service ID found", |
| 27 | + }, |
| 28 | + { |
| 29 | + Name: "validate missing Service ID: status", |
| 30 | + Args: "enable", |
| 31 | + WantError: "error reading service: no service ID found", |
| 32 | + }, |
| 33 | + { |
| 34 | + Name: "validate invalid json/verbose flag combo: status", |
| 35 | + Args: "status --service-id 123 --json --verbose", |
| 36 | + WantError: "invalid flag combination, --verbose and --json", |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "validate success for enabling product", |
| 40 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 41 | + sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { |
| 42 | + return nil, nil |
| 43 | + } |
| 44 | + }, |
| 45 | + Args: "enable --service-id 123", |
| 46 | + WantOutput: "SUCCESS: Enabled " + bot_management.ProductName + " on service 123", |
| 47 | + }, |
| 48 | + { |
| 49 | + Name: "validate failure for enabling product", |
| 50 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 51 | + sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { |
| 52 | + return nil, testutil.Err |
| 53 | + } |
| 54 | + }, |
| 55 | + Args: "enable --service-id 123", |
| 56 | + WantError: "test error", |
| 57 | + }, |
| 58 | + { |
| 59 | + Name: "validate success for disabling product", |
| 60 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 61 | + sub.DisableFn = func(_ api.Interface, _ string) error { |
| 62 | + return nil |
| 63 | + } |
| 64 | + }, |
| 65 | + Args: "disable --service-id 123", |
| 66 | + WantOutput: "SUCCESS: Disabled " + bot_management.ProductName + " on service 123", |
| 67 | + }, |
| 68 | + { |
| 69 | + Name: "validate failure for disabling product", |
| 70 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 71 | + sub.DisableFn = func(_ api.Interface, _ string) error { |
| 72 | + return testutil.Err |
| 73 | + } |
| 74 | + }, |
| 75 | + Args: "disable --service-id 123", |
| 76 | + WantError: "test error", |
| 77 | + }, |
| 78 | + { |
| 79 | + Name: "validate regular status output for enabled product", |
| 80 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 81 | + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { |
| 82 | + return nil, nil |
| 83 | + } |
| 84 | + }, |
| 85 | + Args: "status --service-id 123", |
| 86 | + WantOutput: "INFO: " + bot_management.ProductName + " is enabled on service 123", |
| 87 | + }, |
| 88 | + { |
| 89 | + Name: "validate JSON status output for enabled product", |
| 90 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 91 | + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { |
| 92 | + return nil, nil |
| 93 | + } |
| 94 | + }, |
| 95 | + Args: "status --service-id 123 --json", |
| 96 | + WantOutput: "{\n \"enabled\": true\n}", |
| 97 | + }, |
| 98 | + { |
| 99 | + Name: "validate regular status output for disabled product", |
| 100 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 101 | + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { |
| 102 | + // The API returns a 'Bad Request' error when the |
| 103 | + // product has not been enabled on the service |
| 104 | + return nil, &fastly.HTTPError{StatusCode: 400} |
| 105 | + } |
| 106 | + }, |
| 107 | + Args: "status --service-id 123", |
| 108 | + WantOutput: "INFO: " + bot_management.ProductName + " is disabled on service 123", |
| 109 | + }, |
| 110 | + { |
| 111 | + Name: "validate JSON status output for disabled product", |
| 112 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 113 | + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { |
| 114 | + // The API returns a 'Bad Request' error when the |
| 115 | + // product has not been enabled on the service |
| 116 | + return nil, &fastly.HTTPError{StatusCode: 400} |
| 117 | + } |
| 118 | + }, |
| 119 | + Args: "status --service-id 123 --json", |
| 120 | + WantOutput: "{\n \"enabled\": false\n}", |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName}, scenarios) |
| 125 | +} |
0 commit comments