|
| 1 | +package productcore_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/fastly/go-fastly/v9/fastly" |
| 7 | + |
| 8 | + "github.com/fastly/cli/internal/productcore" |
| 9 | + "github.com/fastly/cli/pkg/api" |
| 10 | + "github.com/fastly/cli/pkg/global" |
| 11 | + "github.com/fastly/cli/pkg/testutil" |
| 12 | +) |
| 13 | + |
| 14 | +type TestEnablementInput[O any] struct { |
| 15 | + T *testing.T |
| 16 | + Commands []string |
| 17 | + ProductName string |
| 18 | + Hooks *productcore.EnablementHookFuncs[O] |
| 19 | +} |
| 20 | + |
| 21 | +func TestEnablement[O any](i TestEnablementInput[O]) { |
| 22 | + scenarios := []testutil.CLIScenario{ |
| 23 | + { |
| 24 | + Name: "validate missing Service ID: enable", |
| 25 | + Args: "enable", |
| 26 | + WantError: "error reading service: no service ID found", |
| 27 | + }, |
| 28 | + { |
| 29 | + Name: "validate missing Service ID: disable", |
| 30 | + Args: "enable", |
| 31 | + WantError: "error reading service: no service ID found", |
| 32 | + }, |
| 33 | + { |
| 34 | + Name: "validate missing Service ID: status", |
| 35 | + Args: "enable", |
| 36 | + WantError: "error reading service: no service ID found", |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "validate invalid json/verbose flag combo: enable", |
| 40 | + Args: "enable --service-id 123 --json --verbose", |
| 41 | + WantError: "invalid flag combination, --verbose and --json", |
| 42 | + }, |
| 43 | + { |
| 44 | + Name: "validate invalid json/verbose flag combo: disable", |
| 45 | + Args: "disable --service-id 123 --json --verbose", |
| 46 | + WantError: "invalid flag combination, --verbose and --json", |
| 47 | + }, |
| 48 | + { |
| 49 | + Name: "validate invalid json/verbose flag combo: status", |
| 50 | + Args: "status --service-id 123 --json --verbose", |
| 51 | + WantError: "invalid flag combination, --verbose and --json", |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "validate text output success for enabling product", |
| 55 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 56 | + i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { |
| 57 | + return |
| 58 | + } |
| 59 | + }, |
| 60 | + Args: "enable --service-id 123", |
| 61 | + WantOutput: "SUCCESS: Enabled " + i.ProductName + " on service 123", |
| 62 | + }, |
| 63 | + { |
| 64 | + Name: "validate JSON output success for enabling product", |
| 65 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 66 | + i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { |
| 67 | + return |
| 68 | + } |
| 69 | + }, |
| 70 | + Args: "enable --service-id 123 --json", |
| 71 | + WantOutput: "{\n \"enabled\": true\n}", |
| 72 | + }, |
| 73 | + { |
| 74 | + Name: "validate failure for enabling product", |
| 75 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 76 | + i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { |
| 77 | + err = testutil.Err |
| 78 | + return |
| 79 | + } |
| 80 | + }, |
| 81 | + Args: "enable --service-id 123", |
| 82 | + WantError: "test error", |
| 83 | + }, |
| 84 | + { |
| 85 | + Name: "validate text output success for disabling product", |
| 86 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 87 | + i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { |
| 88 | + return nil |
| 89 | + } |
| 90 | + }, |
| 91 | + Args: "disable --service-id 123", |
| 92 | + WantOutput: "SUCCESS: Disabled " + i.ProductName + " on service 123", |
| 93 | + }, |
| 94 | + { |
| 95 | + Name: "validate JSON output success for disabling product", |
| 96 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 97 | + i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { |
| 98 | + return nil |
| 99 | + } |
| 100 | + }, |
| 101 | + Args: "disable --service-id 123 --json", |
| 102 | + WantOutput: "{\n \"enabled\": false\n}", |
| 103 | + }, |
| 104 | + { |
| 105 | + Name: "validate failure for disabling product", |
| 106 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 107 | + i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { |
| 108 | + return testutil.Err |
| 109 | + } |
| 110 | + }, |
| 111 | + Args: "disable --service-id 123", |
| 112 | + WantError: "test error", |
| 113 | + }, |
| 114 | + { |
| 115 | + Name: "validate text status output for enabled product", |
| 116 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 117 | + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { |
| 118 | + return |
| 119 | + } |
| 120 | + }, |
| 121 | + Args: "status --service-id 123", |
| 122 | + WantOutput: "INFO: " + i.ProductName + " is enabled on service 123", |
| 123 | + }, |
| 124 | + { |
| 125 | + Name: "validate JSON status output for enabled product", |
| 126 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 127 | + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { |
| 128 | + return |
| 129 | + } |
| 130 | + }, |
| 131 | + Args: "status --service-id 123 --json", |
| 132 | + WantOutput: "{\n \"enabled\": true\n}", |
| 133 | + }, |
| 134 | + { |
| 135 | + Name: "validate text status output for disabled product", |
| 136 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 137 | + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { |
| 138 | + // The API returns a 'Bad Request' error when the |
| 139 | + // product has not been enabled on the service |
| 140 | + err = &fastly.HTTPError{StatusCode: 400} |
| 141 | + return |
| 142 | + } |
| 143 | + }, |
| 144 | + Args: "status --service-id 123", |
| 145 | + WantOutput: "INFO: " + i.ProductName + " is disabled on service 123", |
| 146 | + }, |
| 147 | + { |
| 148 | + Name: "validate JSON status output for disabled product", |
| 149 | + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { |
| 150 | + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { |
| 151 | + // The API returns a 'Bad Request' error when the |
| 152 | + // product has not been enabled on the service |
| 153 | + err = &fastly.HTTPError{StatusCode: 400} |
| 154 | + return |
| 155 | + } |
| 156 | + }, |
| 157 | + Args: "status --service-id 123 --json", |
| 158 | + WantOutput: "{\n \"enabled\": false\n}", |
| 159 | + }, |
| 160 | + } |
| 161 | + |
| 162 | + testutil.RunCLIScenarios(i.T, i.Commands, scenarios) |
| 163 | +} |
0 commit comments