github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/custom/privatekey/privatekey_test.go (about) 1 package privatekey_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 const ( 17 mockFieldValue = "example" 18 mockKeyLength = 123 19 mockResponseID = "123" 20 validateAPIError = "validate API error" 21 validateAPISuccess = "validate API success" 22 validateMissingIDFlag = "validate missing --id flag" 23 ) 24 25 func TestTLSCustomPrivateKeyCreate(t *testing.T) { 26 args := testutil.Args 27 scenarios := []testutil.TestScenario{ 28 { 29 Name: "validate missing --key flag", 30 Args: args("tls-custom private-key create --name example"), 31 WantError: "required flag --key not provided", 32 }, 33 { 34 Name: "validate missing --name flag", 35 Args: args("tls-custom private-key create --key example"), 36 WantError: "required flag --name not provided", 37 }, 38 { 39 Name: validateAPIError, 40 API: mock.API{ 41 CreatePrivateKeyFn: func(i *fastly.CreatePrivateKeyInput) (*fastly.PrivateKey, error) { 42 return nil, testutil.Err 43 }, 44 }, 45 Args: args("tls-custom private-key create --key example --name example"), 46 WantError: testutil.Err.Error(), 47 }, 48 { 49 Name: validateAPISuccess, 50 API: mock.API{ 51 CreatePrivateKeyFn: func(i *fastly.CreatePrivateKeyInput) (*fastly.PrivateKey, error) { 52 return &fastly.PrivateKey{ 53 ID: mockResponseID, 54 Name: i.Name, 55 }, nil 56 }, 57 }, 58 Args: args("tls-custom private-key create --key example --name example"), 59 WantOutput: "Created TLS Private Key 'example'", 60 }, 61 } 62 63 for testcaseIdx := range scenarios { 64 testcase := &scenarios[testcaseIdx] 65 t.Run(testcase.Name, func(t *testing.T) { 66 var stdout bytes.Buffer 67 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 68 opts := testutil.MockGlobalData(testcase.Args, &stdout) 69 opts.APIClientFactory = mock.APIClient(testcase.API) 70 return opts, nil 71 } 72 err := app.Run(testcase.Args, nil) 73 testutil.AssertErrorContains(t, err, testcase.WantError) 74 testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput) 75 }) 76 } 77 } 78 79 func TestTLSCustomPrivateKeyDelete(t *testing.T) { 80 args := testutil.Args 81 scenarios := []testutil.TestScenario{ 82 { 83 Name: validateMissingIDFlag, 84 Args: args("tls-custom private-key delete"), 85 WantError: "error parsing arguments: required flag --id not provided", 86 }, 87 { 88 Name: validateAPIError, 89 API: mock.API{ 90 DeletePrivateKeyFn: func(_ *fastly.DeletePrivateKeyInput) error { 91 return testutil.Err 92 }, 93 }, 94 Args: args("tls-custom private-key delete --id example"), 95 WantError: testutil.Err.Error(), 96 }, 97 { 98 Name: validateAPISuccess, 99 API: mock.API{ 100 DeletePrivateKeyFn: func(_ *fastly.DeletePrivateKeyInput) error { 101 return nil 102 }, 103 }, 104 Args: args("tls-custom private-key delete --id example"), 105 WantOutput: "Deleted TLS Private Key 'example'", 106 }, 107 } 108 109 for testcaseIdx := range scenarios { 110 testcase := &scenarios[testcaseIdx] 111 t.Run(testcase.Name, func(t *testing.T) { 112 var stdout bytes.Buffer 113 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 114 opts := testutil.MockGlobalData(testcase.Args, &stdout) 115 opts.APIClientFactory = mock.APIClient(testcase.API) 116 return opts, nil 117 } 118 err := app.Run(testcase.Args, nil) 119 testutil.AssertErrorContains(t, err, testcase.WantError) 120 testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput) 121 }) 122 } 123 } 124 125 func TestTLSCustomPrivateKeyDescribe(t *testing.T) { 126 args := testutil.Args 127 scenarios := []testutil.TestScenario{ 128 { 129 Name: validateMissingIDFlag, 130 Args: args("tls-custom private-key describe"), 131 WantError: "error parsing arguments: required flag --id not provided", 132 }, 133 { 134 Name: validateAPIError, 135 API: mock.API{ 136 GetPrivateKeyFn: func(_ *fastly.GetPrivateKeyInput) (*fastly.PrivateKey, error) { 137 return nil, testutil.Err 138 }, 139 }, 140 Args: args("tls-custom private-key describe --id example"), 141 WantError: testutil.Err.Error(), 142 }, 143 { 144 Name: validateAPISuccess, 145 API: mock.API{ 146 GetPrivateKeyFn: func(_ *fastly.GetPrivateKeyInput) (*fastly.PrivateKey, error) { 147 t := testutil.Date 148 return &fastly.PrivateKey{ 149 ID: mockResponseID, 150 Name: mockFieldValue, 151 KeyLength: mockKeyLength, 152 KeyType: mockFieldValue, 153 PublicKeySHA1: mockFieldValue, 154 CreatedAt: &t, 155 }, nil 156 }, 157 }, 158 Args: args("tls-custom private-key describe --id example"), 159 WantOutput: "\nID: " + mockResponseID + "\nName: example\nKey Length: 123\nKey Type: example\nPublic Key SHA1: example\nCreated at: 2021-06-15 23:00:00 +0000 UTC\nReplace: false\n", 160 }, 161 } 162 163 for testcaseIdx := range scenarios { 164 testcase := &scenarios[testcaseIdx] 165 t.Run(testcase.Name, func(t *testing.T) { 166 var stdout bytes.Buffer 167 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 168 opts := testutil.MockGlobalData(testcase.Args, &stdout) 169 opts.APIClientFactory = mock.APIClient(testcase.API) 170 return opts, nil 171 } 172 err := app.Run(testcase.Args, nil) 173 testutil.AssertErrorContains(t, err, testcase.WantError) 174 testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput) 175 }) 176 } 177 } 178 179 func TestTLSCustomPrivateKeyList(t *testing.T) { 180 args := testutil.Args 181 scenarios := []testutil.TestScenario{ 182 { 183 Name: validateAPIError, 184 API: mock.API{ 185 ListPrivateKeysFn: func(_ *fastly.ListPrivateKeysInput) ([]*fastly.PrivateKey, error) { 186 return nil, testutil.Err 187 }, 188 }, 189 Args: args("tls-custom private-key list"), 190 WantError: testutil.Err.Error(), 191 }, 192 { 193 Name: validateAPISuccess, 194 API: mock.API{ 195 ListPrivateKeysFn: func(_ *fastly.ListPrivateKeysInput) ([]*fastly.PrivateKey, error) { 196 t := testutil.Date 197 return []*fastly.PrivateKey{ 198 { 199 ID: mockResponseID, 200 Name: mockFieldValue, 201 KeyLength: mockKeyLength, 202 KeyType: mockFieldValue, 203 PublicKeySHA1: mockFieldValue, 204 CreatedAt: &t, 205 }, 206 }, nil 207 }, 208 }, 209 Args: args("tls-custom private-key list --verbose"), 210 WantOutput: "\nID: " + mockResponseID + "\nName: example\nKey Length: 123\nKey Type: example\nPublic Key SHA1: example\nCreated at: 2021-06-15 23:00:00 +0000 UTC\nReplace: false\n", 211 }, 212 } 213 214 for testcaseIdx := range scenarios { 215 testcase := &scenarios[testcaseIdx] 216 t.Run(testcase.Name, func(t *testing.T) { 217 var stdout bytes.Buffer 218 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 219 opts := testutil.MockGlobalData(testcase.Args, &stdout) 220 opts.APIClientFactory = mock.APIClient(testcase.API) 221 return opts, nil 222 } 223 err := app.Run(testcase.Args, nil) 224 testutil.AssertErrorContains(t, err, testcase.WantError) 225 testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput) 226 }) 227 } 228 }