github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/serviceauth/service_test.go (about) 1 package serviceauth_test 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 "strings" 8 "testing" 9 10 "github.com/fastly/go-fastly/v9/fastly" 11 12 "github.com/fastly/cli/pkg/app" 13 "github.com/fastly/cli/pkg/global" 14 "github.com/fastly/cli/pkg/mock" 15 "github.com/fastly/cli/pkg/testutil" 16 ) 17 18 func TestServiceAuthCreate(t *testing.T) { 19 args := testutil.Args 20 scenarios := []struct { 21 args []string 22 api mock.API 23 wantError string 24 wantOutput string 25 }{ 26 { 27 args: args("service-auth create"), 28 wantError: "error parsing arguments: required flag --user-id not provided", 29 }, 30 { 31 args: args("service-auth create --user-id 123 --service-id 123"), 32 api: mock.API{CreateServiceAuthorizationFn: createServiceAuthError}, 33 wantError: errTest.Error(), 34 }, 35 { 36 args: args("service-auth create --user-id 123 --service-id 123"), 37 api: mock.API{CreateServiceAuthorizationFn: createServiceAuthOK}, 38 wantOutput: "Created service authorization 12345", 39 }, 40 } 41 for testcaseIdx := range scenarios { 42 testcase := &scenarios[testcaseIdx] 43 t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { 44 var stdout bytes.Buffer 45 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 46 opts := testutil.MockGlobalData(testcase.args, &stdout) 47 opts.APIClientFactory = mock.APIClient(testcase.api) 48 return opts, nil 49 } 50 err := app.Run(testcase.args, nil) 51 testutil.AssertErrorContains(t, err, testcase.wantError) 52 testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) 53 }) 54 } 55 } 56 57 func TestServiceAuthList(t *testing.T) { 58 args := testutil.Args 59 scenarios := []struct { 60 args []string 61 api mock.API 62 wantError string 63 wantOutput string 64 }{ 65 { 66 args: args("service-auth list --verbose --json"), 67 wantError: "invalid flag combination, --verbose and --json", 68 }, 69 { 70 args: args("service-auth list"), 71 api: mock.API{ListServiceAuthorizationsFn: listServiceAuthError}, 72 wantError: errTest.Error(), 73 }, 74 { 75 args: args("service-auth list"), 76 api: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, 77 wantOutput: "AUTH ID USER ID SERVICE ID PERMISSION\n123 456 789 read_only\n", 78 }, 79 { 80 args: args("service-auth list --json"), 81 api: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, 82 wantOutput: `{ 83 "Info": { 84 "links": {}, 85 "meta": {} 86 }, 87 "Items": [ 88 { 89 "CreatedAt": null, 90 "DeletedAt": null, 91 "ID": "123", 92 "Permission": "read_only", 93 "Service": { 94 "ID": "789" 95 }, 96 "UpdatedAt": null, 97 "User": { 98 "ID": "456" 99 } 100 } 101 ] 102 }`, 103 }, 104 { 105 args: args("service-auth list --verbose"), 106 api: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, 107 wantOutput: "Fastly API endpoint: https://api.fastly.com\nFastly API token provided via config file (profile: user)\n\nAuth ID: 123\nUser ID: 456\nService ID: 789\nPermission: read_only\n", 108 }, 109 } 110 for testcaseIdx := range scenarios { 111 testcase := &scenarios[testcaseIdx] 112 t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { 113 var stdout bytes.Buffer 114 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 115 opts := testutil.MockGlobalData(testcase.args, &stdout) 116 opts.APIClientFactory = mock.APIClient(testcase.api) 117 return opts, nil 118 } 119 err := app.Run(testcase.args, nil) 120 t.Log(stdout.String()) 121 testutil.AssertErrorContains(t, err, testcase.wantError) 122 testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) 123 }) 124 } 125 } 126 127 func TestServiceAuthDescribe(t *testing.T) { 128 args := testutil.Args 129 scenarios := []struct { 130 args []string 131 api mock.API 132 wantError string 133 wantOutput string 134 }{ 135 { 136 args: args("service-auth describe"), 137 wantError: "error parsing arguments: required flag --id not provided", 138 }, 139 { 140 args: args("service-auth describe --id 123 --verbose --json"), 141 wantError: "invalid flag combination, --verbose and --json", 142 }, 143 { 144 args: args("service-auth describe --id 123"), 145 api: mock.API{GetServiceAuthorizationFn: describeServiceAuthError}, 146 wantError: errTest.Error(), 147 }, 148 { 149 args: args("service-auth describe --id 123"), 150 api: mock.API{GetServiceAuthorizationFn: describeServiceAuthOK}, 151 wantOutput: "Auth ID: 12345\nUser ID: 456\nService ID: 789\nPermission: read_only\n", 152 }, 153 { 154 args: args("service-auth describe --id 123 --json"), 155 api: mock.API{GetServiceAuthorizationFn: describeServiceAuthOK}, 156 wantOutput: `{ 157 "CreatedAt": null, 158 "DeletedAt": null, 159 "ID": "12345", 160 "Permission": "read_only", 161 "Service": { 162 "ID": "789" 163 }, 164 "UpdatedAt": null, 165 "User": { 166 "ID": "456" 167 } 168 }`, 169 }, 170 } 171 for testcaseIdx := range scenarios { 172 testcase := &scenarios[testcaseIdx] 173 t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { 174 var stdout bytes.Buffer 175 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 176 opts := testutil.MockGlobalData(testcase.args, &stdout) 177 opts.APIClientFactory = mock.APIClient(testcase.api) 178 return opts, nil 179 } 180 err := app.Run(testcase.args, nil) 181 t.Log(stdout.String()) 182 testutil.AssertErrorContains(t, err, testcase.wantError) 183 testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) 184 }) 185 } 186 } 187 188 func TestServiceAuthUpdate(t *testing.T) { 189 args := testutil.Args 190 scenarios := []struct { 191 args []string 192 api mock.API 193 wantError string 194 wantOutput string 195 }{ 196 { 197 args: args("service-auth update --permission full"), 198 wantError: "error parsing arguments: required flag --id not provided", 199 }, 200 { 201 args: args("service-auth update --id 123"), 202 wantError: "error parsing arguments: required flag --permission not provided", 203 }, 204 { 205 args: args("service-auth update --id 123 --permission full"), 206 api: mock.API{UpdateServiceAuthorizationFn: updateServiceAuthError}, 207 wantError: errTest.Error(), 208 }, 209 { 210 args: args("service-auth update --id 123 --permission full"), 211 api: mock.API{UpdateServiceAuthorizationFn: updateServiceAuthOK}, 212 wantOutput: "Updated service authorization 123", 213 }, 214 } 215 for testcaseIdx := range scenarios { 216 testcase := &scenarios[testcaseIdx] 217 t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { 218 var stdout bytes.Buffer 219 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 220 opts := testutil.MockGlobalData(testcase.args, &stdout) 221 opts.APIClientFactory = mock.APIClient(testcase.api) 222 return opts, nil 223 } 224 err := app.Run(testcase.args, nil) 225 testutil.AssertErrorContains(t, err, testcase.wantError) 226 testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) 227 }) 228 } 229 } 230 231 func TestServiceAuthDelete(t *testing.T) { 232 args := testutil.Args 233 scenarios := []struct { 234 args []string 235 api mock.API 236 wantError string 237 wantOutput string 238 }{ 239 { 240 args: args("service-auth delete"), 241 wantError: "error parsing arguments: required flag --id not provided", 242 }, 243 { 244 args: args("service-auth delete --id 123"), 245 api: mock.API{DeleteServiceAuthorizationFn: deleteServiceAuthError}, 246 wantError: errTest.Error(), 247 }, 248 { 249 args: args("service-auth delete --id 123"), 250 api: mock.API{DeleteServiceAuthorizationFn: deleteServiceAuthOK}, 251 wantOutput: "Deleted service authorization 123", 252 }, 253 } 254 for testcaseIdx := range scenarios { 255 testcase := &scenarios[testcaseIdx] 256 t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { 257 var stdout bytes.Buffer 258 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 259 opts := testutil.MockGlobalData(testcase.args, &stdout) 260 opts.APIClientFactory = mock.APIClient(testcase.api) 261 return opts, nil 262 } 263 err := app.Run(testcase.args, nil) 264 testutil.AssertErrorContains(t, err, testcase.wantError) 265 testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) 266 }) 267 } 268 } 269 270 var errTest = errors.New("fixture error") 271 272 func createServiceAuthError(*fastly.CreateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error) { 273 return nil, errTest 274 } 275 276 func createServiceAuthOK(_ *fastly.CreateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error) { 277 return &fastly.ServiceAuthorization{ 278 ID: "12345", 279 }, nil 280 } 281 282 func listServiceAuthError(*fastly.ListServiceAuthorizationsInput) (*fastly.ServiceAuthorizations, error) { 283 return nil, errTest 284 } 285 286 func listServiceAuthOK(_ *fastly.ListServiceAuthorizationsInput) (*fastly.ServiceAuthorizations, error) { 287 return &fastly.ServiceAuthorizations{ 288 Items: []*fastly.ServiceAuthorization{ 289 { 290 ID: "123", 291 User: &fastly.SAUser{ 292 ID: "456", 293 }, 294 Service: &fastly.SAService{ 295 ID: "789", 296 }, 297 Permission: "read_only", 298 }, 299 }, 300 }, nil 301 } 302 303 func describeServiceAuthError(*fastly.GetServiceAuthorizationInput) (*fastly.ServiceAuthorization, error) { 304 return nil, errTest 305 } 306 307 func describeServiceAuthOK(_ *fastly.GetServiceAuthorizationInput) (*fastly.ServiceAuthorization, error) { 308 return &fastly.ServiceAuthorization{ 309 ID: "12345", 310 User: &fastly.SAUser{ 311 ID: "456", 312 }, 313 Service: &fastly.SAService{ 314 ID: "789", 315 }, 316 Permission: "read_only", 317 }, nil 318 } 319 320 func updateServiceAuthError(*fastly.UpdateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error) { 321 return nil, errTest 322 } 323 324 func updateServiceAuthOK(_ *fastly.UpdateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error) { 325 return &fastly.ServiceAuthorization{ 326 ID: "12345", 327 }, nil 328 } 329 330 func deleteServiceAuthError(*fastly.DeleteServiceAuthorizationInput) error { 331 return errTest 332 } 333 334 func deleteServiceAuthOK(_ *fastly.DeleteServiceAuthorizationInput) error { 335 return nil 336 }