Skip to content

Commit

Permalink
test(product_enablement): add tests for products command
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Oct 9, 2023
1 parent 63e66a6 commit 27901b8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 26 deletions.
110 changes: 87 additions & 23 deletions pkg/commands/products/products_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
3 changes: 0 additions & 3 deletions pkg/commands/products/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 27901b8

Please sign in to comment.