Skip to content

Commit 154200d

Browse files
committed
MOAR GENERICS
1 parent 85c1b49 commit 154200d

File tree

3 files changed

+171
-114
lines changed

3 files changed

+171
-114
lines changed

internal/productcore/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type EnablementHookFuncs[O any] struct {
5353
}
5454

5555
// ConfigurationHookFuncs is a structure of dependency-injection
56-
// points by unit tests to provide mock behaviors
56+
// points used by unit tests to provide mock behaviors
5757
type ConfigurationHookFuncs[O, I any] struct {
5858
GetConfigurationFunc func(api.Interface, string) (O, error)
5959
UpdateConfigurationFunc func(api.Interface, string, I) (O, error)
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
}

pkg/commands/product/botmanagement/product_test.go

Lines changed: 7 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3,123 +3,17 @@ package botmanagement_test
33
import (
44
"testing"
55

6-
"github.com/fastly/go-fastly/v9/fastly"
76
"github.com/fastly/go-fastly/v9/fastly/products/botmanagement"
8-
9-
"github.com/fastly/cli/pkg/api"
7+
"github.com/fastly/cli/internal/productcore_test"
108
root "github.com/fastly/cli/pkg/commands/product"
119
sub "github.com/fastly/cli/pkg/commands/product/botmanagement"
12-
"github.com/fastly/cli/pkg/global"
13-
"github.com/fastly/cli/pkg/testutil"
1410
)
1511

1612
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.EnablementHooks.EnableFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) {
42-
return nil, nil
43-
}
44-
},
45-
Args: "enable --service-id 123",
46-
WantOutput: "SUCCESS: Enabled " + botmanagement.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.EnablementHooks.EnableFunc = func(_ api.Interface, _ string) (*botmanagement.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.EnablementHooks.DisableFunc = func(_ api.Interface, _ string) error {
62-
return nil
63-
}
64-
},
65-
Args: "disable --service-id 123",
66-
WantOutput: "SUCCESS: Disabled " + botmanagement.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.EnablementHooks.DisableFunc = 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.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) {
82-
return nil, nil
83-
}
84-
},
85-
Args: "status --service-id 123",
86-
WantOutput: "INFO: " + botmanagement.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.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.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.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.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: " + botmanagement.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.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.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)
13+
productcore_test.TestEnablement(productcore_test.TestEnablementInput[*botmanagement.EnableOutput]{
14+
T: t,
15+
Commands: []string{root.CommandName, sub.CommandName},
16+
ProductName: botmanagement.ProductName,
17+
Hooks: &sub.EnablementHooks,
18+
})
12519
}

0 commit comments

Comments
 (0)