From 85f024c814cca3ef2939215cb6e3aa74cfb5a581 Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Wed, 1 May 2024 01:45:24 +0800 Subject: [PATCH] test: added scripting for user login --- cli/go.mod | 1 + cli/go.sum | 2 + ...stUserAuth_SecretsGetAllWithoutConnection} | 0 cli/test/helper.go | 4 + cli/test/login_test.go | 78 +++++++++++++++++++ cli/test/secrets_test.go | 9 ++- 6 files changed, 91 insertions(+), 3 deletions(-) rename cli/test/.snapshots/{test-TestUserAuth_SecretsGetAllWithoutConnection => test-testUserAuth_SecretsGetAllWithoutConnection} (100%) diff --git a/cli/go.mod b/cli/go.mod index 833745effc..52c7375332 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -31,6 +31,7 @@ require ( github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect github.com/bradleyjkemp/cupaloy/v2 v2.8.0 // indirect github.com/chzyer/readline v1.5.1 // indirect + github.com/creack/pty v1.1.21 // indirect github.com/danieljoos/wincred v1.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dvsekhvalnov/jose2go v1.5.0 // indirect diff --git a/cli/go.sum b/cli/go.sum index 3535791366..ff3030a9cc 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -74,6 +74,8 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0= +github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE= github.com/danieljoos/wincred v1.2.0/go.mod h1:FzQLLMKBFdvu+osBrnFODiv32YGwCfx0SkRa/eYHgec= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/cli/test/.snapshots/test-TestUserAuth_SecretsGetAllWithoutConnection b/cli/test/.snapshots/test-testUserAuth_SecretsGetAllWithoutConnection similarity index 100% rename from cli/test/.snapshots/test-TestUserAuth_SecretsGetAllWithoutConnection rename to cli/test/.snapshots/test-testUserAuth_SecretsGetAllWithoutConnection diff --git a/cli/test/helper.go b/cli/test/helper.go index 4704733687..b773d138e5 100644 --- a/cli/test/helper.go +++ b/cli/test/helper.go @@ -23,6 +23,8 @@ type Credentials struct { ServiceToken string ProjectID string EnvSlug string + UserEmail string + UserPassword string } var creds = Credentials{ @@ -32,6 +34,8 @@ var creds = Credentials{ ServiceToken: os.Getenv("CLI_TESTS_SERVICE_TOKEN"), ProjectID: os.Getenv("CLI_TESTS_PROJECT_ID"), EnvSlug: os.Getenv("CLI_TESTS_ENV_SLUG"), + UserEmail: os.Getenv("CLI_TESTS_USER_EMAIL"), + UserPassword: os.Getenv("CLI_TESTS_USER_PASSWORD"), } func ExecuteCliCommand(command string, args ...string) (string, error) { diff --git a/cli/test/login_test.go b/cli/test/login_test.go index 0f45914132..6857c00fba 100644 --- a/cli/test/login_test.go +++ b/cli/test/login_test.go @@ -1,11 +1,89 @@ package tests import ( + "fmt" + "os/exec" + "strings" "testing" + "github.com/creack/pty" "github.com/stretchr/testify/assert" ) +func UserLoginCmd(t *testing.T) { + SetupCli(t) + + // set vault to file because CI has no keyring + vaultCmd := exec.Command(FORMATTED_CLI_NAME, "vault", "set", "file") + _, err := vaultCmd.Output() + if err != nil { + t.Fatalf("error setting vault: %v", err) + } + + // Start programmatic interaction with CLI + c := exec.Command(FORMATTED_CLI_NAME, "login", "--interactive") + ptmx, err := pty.Start(c) + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + defer func() { _ = ptmx.Close() }() + + stepChan := make(chan int, 10) + + go func() { + buf := make([]byte, 1024) + step := -1 + for { + n, err := ptmx.Read(buf) + if n > 0 { + terminalOut := string(buf) + if strings.Contains(terminalOut, "Add a new account") && step < 0 { + step += 1 + stepChan <- step + } else if strings.Contains(terminalOut, "Infisical Cloud") && step < 1 { + step += 1; + stepChan <- step + } else if strings.Contains(terminalOut, "Email") && step < 2 { + step += 1; + stepChan <- step + } else if strings.Contains(terminalOut, "Password") && step < 3 { + step += 1; + stepChan <- step + } else if strings.Contains(terminalOut, "Infisical organization") && step < 4 { + step += 1; + stepChan <- step + } else if strings.Contains(terminalOut, "Enter passphrase") && step < 5 { + step += 1; + stepChan <- step + } + } + if err != nil { + close(stepChan) + return + } + fmt.Print(string(buf[:n])) + } + }() + + for i := range stepChan { + switch i { + case 0: + ptmx.Write([]byte("\n")) + case 1: + ptmx.Write([]byte("\n")) + case 2: + ptmx.Write([]byte(creds.UserEmail)) + ptmx.Write([]byte("\n")) + case 3: + ptmx.Write([]byte(creds.UserPassword)) + ptmx.Write([]byte("\n")) + case 4: + ptmx.Write([]byte("\n")) + } + } + +} + func MachineIdentityLoginCmd(t *testing.T) { SetupCli(t) diff --git a/cli/test/secrets_test.go b/cli/test/secrets_test.go index b9c8fa85d9..294434f5f3 100644 --- a/cli/test/secrets_test.go +++ b/cli/test/secrets_test.go @@ -90,6 +90,8 @@ func TestUniversalAuth_SecretsGetWrongEnvironment(t *testing.T) { func TestUserAuth_SecretsGetAll(t *testing.T) { SetupCli(t) + UserLoginCmd(t); + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--include-imports=false", "--silent") if err != nil { t.Fatalf("error running CLI command: %v", err) @@ -100,11 +102,12 @@ func TestUserAuth_SecretsGetAll(t *testing.T) { if err != nil { t.Fatalf("snapshot failed: %v", err) } + + // intentionally invoked this here because it should directly follow secretsGetAll + testUserAuth_SecretsGetAllWithoutConnection(t) } -func TestUserAuth_SecretsGetAllWithoutConnection(t *testing.T) { - SetupCli(t) - +func testUserAuth_SecretsGetAllWithoutConnection(t *testing.T) { originalConfigFile, err := util.GetConfigFile() if err != nil { t.Fatalf("error getting config file")