diff --git a/pkg/commands/products/products_test.go b/pkg/commands/products/products_test.go index 8b55bd12f..02f2d5930 100644 --- a/pkg/commands/products/products_test.go +++ b/pkg/commands/products/products_test.go @@ -11,30 +11,94 @@ import ( "github.com/fastly/cli/pkg/testutil" ) -func TestAllDatacenters(t *testing.T) { - var stdout bytes.Buffer - args := testutil.Args("pops --token 123") - api := mock.API{ - AllDatacentersFn: func() ([]fastly.Datacenter, error) { - return []fastly.Datacenter{ - { - Name: "Foobar", - Code: "FBR", - Group: "Bar", - Shield: "Baz", - Coordinates: fastly.Coordinates{ - Latitude: 1, - Longtitude: 2, - X: 3, - Y: 4, - }, +func TestProductEnablement(t *testing.T) { + args := testutil.Args + scenarios := []testutil.TestScenario{ + { + Name: "validate missing Service ID", + Args: args("products"), + WantError: "failed to identify Service ID: error reading service: no service ID found", + }, + { + Name: "validate invalid flag combo", + Args: args("products --enable fanout --disable fanout"), + WantError: "invalid flag combination: --enable and --disable", + }, + { + Name: "validate API error for product status", + API: mock.API{ + GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { + return nil, testutil.Err + }, + }, + Args: args("products --service-id 123"), + WantOutput: `PRODUCT ENABLED +Brotli Compression false +Domain Inspector false +Fanout false +Image Optimizer false +Origin Inspector false +Web Sockets false +`, + }, + { + Name: "validate API success for product status", + API: mock.API{ + GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { + return nil, nil + }, + }, + Args: args("products --service-id 123"), + WantOutput: `PRODUCT ENABLED +Brotli Compression true +Domain Inspector true +Fanout true +Image Optimizer true +Origin Inspector true +Web Sockets true +`, + }, + { + Name: "validate flag parsing error for enabling product", + Args: args("products --service-id 123 --enable foo"), + WantError: "error parsing arguments: enum value must be one of brotli_compression,domain_inspector,fanout,image_optimizer,origin_inspector,websockets, got 'foo'", + }, + { + Name: "validate flag parsing error for disabling product", + Args: args("products --service-id 123 --disable foo"), + WantError: "error parsing arguments: enum value must be one of brotli_compression,domain_inspector,fanout,image_optimizer,origin_inspector,websockets, got 'foo'", + }, + { + Name: "validate success for enabling product", + API: mock.API{ + EnableProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { + return nil, nil + }, + }, + Args: args("products --service-id 123 --enable brotli_compression"), + WantOutput: "SUCCESS: Successfully enabled product 'brotli_compression'", + }, + { + Name: "validate success for disabling product", + API: mock.API{ + DisableProductFn: func(i *fastly.ProductEnablementInput) error { + return nil }, - }, nil + }, + Args: args("products --service-id 123 --disable brotli_compression"), + WantOutput: "SUCCESS: Successfully disabled product 'brotli_compression'", }, } - opts := testutil.NewRunOpts(args, &stdout) - opts.APIClient = mock.APIClient(api) - err := app.Run(opts) - testutil.AssertNoError(t, err) - testutil.AssertString(t, "\nNAME CODE GROUP SHIELD COORDINATES\nFoobar FBR Bar Baz {Latitude:1 Longtitude:2 X:3 Y:4}\n", stdout.String()) + + for testcaseIdx := range scenarios { + testcase := &scenarios[testcaseIdx] + t.Run(testcase.Name, func(t *testing.T) { + var stdout bytes.Buffer + opts := testutil.NewRunOpts(testcase.Args, &stdout) + opts.APIClient = mock.APIClient(testcase.API) + err := app.Run(opts) + testutil.AssertErrorContains(t, err, testcase.WantError) + testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput) + }) + } } diff --git a/pkg/commands/products/root.go b/pkg/commands/products/root.go index f414fb130..578b61313 100644 --- a/pkg/commands/products/root.go +++ b/pkg/commands/products/root.go @@ -93,9 +93,6 @@ func (c *RootCommand) Exec(_ io.Reader, out io.Writer) error { return nil } - // NOTE: The API returns a 400 if a product is not enabled. - // The API client returns an error if a non-2xx is returned from the API. - var brotliEnabled, diEnabled, fanoutEnabled, ioEnabled, oiEnabled, wsEnabled bool if _, err = ac.GetProduct(&fastly.ProductEnablementInput{