From b79ce8a8807ec6471c2950d2dd6b661999ea63c1 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:59:13 +0200 Subject: [PATCH] Feat: Cli integration tests -- login --- cli/test/login_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 cli/test/login_test.go diff --git a/cli/test/login_test.go b/cli/test/login_test.go new file mode 100644 index 0000000000..1d598153a0 --- /dev/null +++ b/cli/test/login_test.go @@ -0,0 +1,43 @@ +package tests + +import ( + "bytes" + "fmt" + "regexp" + "testing" + + "github.com/Infisical/infisical-merge/packages/cmd" + "github.com/stretchr/testify/assert" +) + +func UALoginCmd(t *testing.T) { + jwtPattern := `^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$` + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + errorOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(errorOutput) + + args := []string{ + "login", + } + + args = append(args, fmt.Sprintf("--method=%s", "universal-auth")) + args = append(args, fmt.Sprintf("--client-id=%s", creds.ClientID)) + args = append(args, fmt.Sprintf("--client-secret=%s", creds.ClientSecret)) + + rootCommand.SetArgs(args) + rootCommand.Execute() + + token := commandOutput.String() + + // We do a match and compare it against true, instead of using assert.Regexp. + // If the assertion fails, we would be able to see the potential token that was generated in the output console, which would be bad if running in a CI/CD pipeline. + match, err := regexp.MatchString(jwtPattern, token) + assert.Nil(t, err) + assert.True(t, match, "The token does not match the pattern") + + creds.UAAccessToken = token +}