|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "context" |
5 | 6 | "encoding/base64" |
6 | 7 | "errors" |
7 | 8 | "os" |
8 | 9 | "path/filepath" |
| 10 | + "strings" |
9 | 11 | "testing" |
10 | 12 |
|
11 | 13 | "github.com/stretchr/testify/assert" |
@@ -398,3 +400,41 @@ func TestGcloudDeleteKeyCommand(t *testing.T) { |
398 | 400 | "gcloud iam service-accounts keys delete abc123 --iam-account=sa@proj.iam.gserviceaccount.com", |
399 | 401 | gcloudDeleteKeyCommand(testMintedKeyName)) |
400 | 402 | } |
| 403 | + |
| 404 | +// --- gcpStepCreateServiceAccount --- |
| 405 | + |
| 406 | +func TestGCPStepCreateServiceAccount_SkipUsesOperatorEmail(t *testing.T) { |
| 407 | + const existing = "existing-sa@my-proj.iam.gserviceaccount.com" |
| 408 | + reader := bufio.NewReader(strings.NewReader("s\n " + existing + " \n")) |
| 409 | + |
| 410 | + email, err := gcpStepCreateServiceAccount(context.Background(), reader, "my-proj") |
| 411 | + |
| 412 | + require.NoError(t, err) |
| 413 | + assert.Equal(t, existing, email) |
| 414 | + assert.NotEqual(t, "cudly-service-account@my-proj.iam.gserviceaccount.com", email) |
| 415 | +} |
| 416 | + |
| 417 | +func TestGCPStepCreateServiceAccount_UnknownOptionPromptsForEmail(t *testing.T) { |
| 418 | + const existing = "other@my-proj.iam.gserviceaccount.com" |
| 419 | + reader := bufio.NewReader(strings.NewReader("x\n" + existing + "\n")) |
| 420 | + |
| 421 | + email, err := gcpStepCreateServiceAccount(context.Background(), reader, "my-proj") |
| 422 | + |
| 423 | + require.NoError(t, err) |
| 424 | + assert.Equal(t, existing, email) |
| 425 | +} |
| 426 | + |
| 427 | +func TestGCPStepCreateServiceAccount_SkipRejectsInvalidEmail(t *testing.T) { |
| 428 | + for _, in := range []string{"s\n\n", "s\nnot-an-email\n", "s\nuser@example.com\n", "s\n@x.gserviceaccount.com\n", "s\na@b@c.gserviceaccount.com\n", "s\na@.gserviceaccount.com\n", "s\n ", "s\na\x00@x.gserviceaccount.com\n", "s\n"} { |
| 429 | + email, err := gcpStepCreateServiceAccount(context.Background(), bufio.NewReader(strings.NewReader(in)), "my-proj") |
| 430 | + require.Error(t, err, "input %q", in) |
| 431 | + assert.Empty(t, email) |
| 432 | + } |
| 433 | +} |
| 434 | + |
| 435 | +func TestGCPStepCreateServiceAccount_SkipAcceptsUppercaseEmail(t *testing.T) { |
| 436 | + const existing = "Existing-SA@My-Proj.IAM.GServiceAccount.com" |
| 437 | + email, err := gcpStepCreateServiceAccount(context.Background(), bufio.NewReader(strings.NewReader("s\n"+existing+"\n")), "my-proj") |
| 438 | + require.NoError(t, err) |
| 439 | + assert.Equal(t, strings.ToLower(existing), email) |
| 440 | +} |
0 commit comments