github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/products/products_test.go (about) 1 package products_test 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 "github.com/fastly/go-fastly/v9/fastly" 9 10 "github.com/fastly/cli/pkg/app" 11 "github.com/fastly/cli/pkg/global" 12 "github.com/fastly/cli/pkg/mock" 13 "github.com/fastly/cli/pkg/testutil" 14 ) 15 16 func TestProductEnablement(t *testing.T) { 17 args := testutil.Args 18 scenarios := []testutil.TestScenario{ 19 { 20 Name: "validate missing Service ID", 21 Args: args("products"), 22 WantError: "failed to identify Service ID: error reading service: no service ID found", 23 }, 24 { 25 Name: "validate invalid enable/disable flag combo", 26 Args: args("products --enable fanout --disable fanout"), 27 WantError: "invalid flag combination: --enable and --disable", 28 }, 29 { 30 Name: "validate API error for product status", 31 API: mock.API{ 32 GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { 33 return nil, testutil.Err 34 }, 35 }, 36 Args: args("products --service-id 123"), 37 WantOutput: `PRODUCT ENABLED 38 Brotli Compression false 39 Domain Inspector false 40 Fanout false 41 Image Optimizer false 42 Origin Inspector false 43 Web Sockets false 44 `, 45 }, 46 { 47 Name: "validate API success for product status", 48 API: mock.API{ 49 GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { 50 return nil, nil 51 }, 52 }, 53 Args: args("products --service-id 123"), 54 WantOutput: `PRODUCT ENABLED 55 Brotli Compression true 56 Domain Inspector true 57 Fanout true 58 Image Optimizer true 59 Origin Inspector true 60 Web Sockets true 61 `, 62 }, 63 { 64 Name: "validate flag parsing error for enabling product", 65 Args: args("products --service-id 123 --enable foo"), 66 WantError: "error parsing arguments: enum value must be one of brotli_compression,domain_inspector,fanout,image_optimizer,origin_inspector,websockets, got 'foo'", 67 }, 68 { 69 Name: "validate flag parsing error for disabling product", 70 Args: args("products --service-id 123 --disable foo"), 71 WantError: "error parsing arguments: enum value must be one of brotli_compression,domain_inspector,fanout,image_optimizer,origin_inspector,websockets, got 'foo'", 72 }, 73 { 74 Name: "validate success for enabling product", 75 API: mock.API{ 76 EnableProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { 77 return nil, nil 78 }, 79 }, 80 Args: args("products --service-id 123 --enable brotli_compression"), 81 WantOutput: "SUCCESS: Successfully enabled product 'brotli_compression'", 82 }, 83 { 84 Name: "validate success for disabling product", 85 API: mock.API{ 86 DisableProductFn: func(i *fastly.ProductEnablementInput) error { 87 return nil 88 }, 89 }, 90 Args: args("products --service-id 123 --disable brotli_compression"), 91 WantOutput: "SUCCESS: Successfully disabled product 'brotli_compression'", 92 }, 93 { 94 Name: "validate invalid json/verbose flag combo", 95 Args: args("products --service-id 123 --json --verbose"), 96 WantError: "invalid flag combination, --verbose and --json", 97 }, 98 { 99 Name: "validate API error for product status with --json output", 100 API: mock.API{ 101 GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { 102 return nil, testutil.Err 103 }, 104 }, 105 Args: args("products --service-id 123 --json"), 106 WantOutput: `{ 107 "brotli_compression": false, 108 "domain_inspector": false, 109 "fanout": false, 110 "image_optimizer": false, 111 "origin_inspector": false, 112 "websockets": false 113 }`, 114 }, 115 { 116 Name: "validate API success for product status with --json output", 117 API: mock.API{ 118 GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) { 119 return nil, nil 120 }, 121 }, 122 Args: args("products --service-id 123 --json"), 123 WantOutput: `{ 124 "brotli_compression": true, 125 "domain_inspector": true, 126 "fanout": true, 127 "image_optimizer": true, 128 "origin_inspector": true, 129 "websockets": true 130 }`, 131 }, 132 } 133 134 for testcaseIdx := range scenarios { 135 testcase := &scenarios[testcaseIdx] 136 t.Run(testcase.Name, func(t *testing.T) { 137 var stdout bytes.Buffer 138 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 139 opts := testutil.MockGlobalData(testcase.Args, &stdout) 140 opts.APIClientFactory = mock.APIClient(testcase.API) 141 return opts, nil 142 } 143 err := app.Run(testcase.Args, nil) 144 testutil.AssertErrorContains(t, err, testcase.WantError) 145 testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput) 146 }) 147 } 148 }