mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-05 20:26:44 +00:00
Fix: Refactor teests to use cupaloy
This commit is contained in:
+73
-74
@@ -1,99 +1,98 @@
|
|||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/Infisical/infisical-merge/packages/cmd"
|
"github.com/bradleyjkemp/cupaloy/v2"
|
||||||
"github.com/Infisical/infisical-merge/packages/models"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListSecretsWithImportsAndRecursive(t *testing.T, authToken string, projectId string, envSlug string) {
|
func TestServiceToken_SecretsGetWithImportsAndRecursiveCmd(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
rootCommand := cmd.NewRootCmd()
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent")
|
||||||
|
|
||||||
commandOutput := new(bytes.Buffer)
|
fmt.Printf("output: %v\n", output)
|
||||||
rootCommand.SetOut(commandOutput)
|
|
||||||
rootCommand.SetErr(commandOutput)
|
|
||||||
|
|
||||||
args := []string{
|
|
||||||
"secrets",
|
|
||||||
}
|
|
||||||
args = append(args, fmt.Sprintf("--token=%s", authToken))
|
|
||||||
args = append(args, fmt.Sprintf("--projectId=%s", projectId))
|
|
||||||
args = append(args, fmt.Sprintf("--env=%s", envSlug))
|
|
||||||
args = append(args, "--include-imports=true")
|
|
||||||
args = append(args, "--recursive=true")
|
|
||||||
|
|
||||||
rootCommand.SetArgs(args)
|
|
||||||
rootCommand.Execute()
|
|
||||||
|
|
||||||
var secrets []models.SingleEnvironmentVariable
|
|
||||||
|
|
||||||
err := json.Unmarshal(commandOutput.Bytes(), &secrets)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error: %v", err)
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(secrets) == 0 {
|
// Use cupaloy to snapshot test the output
|
||||||
t.Errorf("No secrets found")
|
err = cupaloy.Snapshot(output)
|
||||||
}
|
if err != nil {
|
||||||
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
secretKeys := []string{}
|
|
||||||
secretValues := []string{}
|
|
||||||
|
|
||||||
for _, secret := range secrets {
|
|
||||||
secretKeys = append(secretKeys, secret.Key)
|
|
||||||
secretValues = append(secretValues, secret.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Secrets can have different order and potentially more secrets. but the secrets should at least contain the above secrets.
|
|
||||||
for _, key := range ALL_SECRET_KEYS {
|
|
||||||
assert.Contains(t, secretKeys, key)
|
|
||||||
}
|
|
||||||
for _, value := range ALL_SECRET_VALUES {
|
|
||||||
assert.Contains(t, secretValues, value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSecretsByNames(t *testing.T, authToken string, projectId string, envSlug string) {
|
func TestServiceToken_SecretsGetWithoutImportsAndWithoutRecursiveCmd(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
rootCommand := cmd.NewRootCmd()
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--include-imports=false", "--silent")
|
||||||
|
|
||||||
commandOutput := new(bytes.Buffer)
|
fmt.Printf("output: %v\n", output)
|
||||||
rootCommand.SetOut(commandOutput)
|
|
||||||
rootCommand.SetErr(commandOutput)
|
|
||||||
|
|
||||||
args := []string{
|
|
||||||
"secrets",
|
|
||||||
"get",
|
|
||||||
}
|
|
||||||
|
|
||||||
args = append(args, ALL_SECRET_KEYS...)
|
|
||||||
args = append(args, fmt.Sprintf("--token=%s", authToken))
|
|
||||||
args = append(args, fmt.Sprintf("--projectId=%s", projectId))
|
|
||||||
args = append(args, fmt.Sprintf("--env=%s", envSlug))
|
|
||||||
|
|
||||||
rootCommand.SetArgs(args)
|
|
||||||
rootCommand.Execute()
|
|
||||||
|
|
||||||
var secrets []models.SingleEnvironmentVariable
|
|
||||||
|
|
||||||
err := json.Unmarshal(commandOutput.Bytes(), &secrets)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error: %v", err)
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Len(t, secrets, len(ALL_SECRETS))
|
// Use cupaloy to snapshot test the output
|
||||||
|
err = cupaloy.Snapshot(output)
|
||||||
for _, secret := range secrets {
|
if err != nil {
|
||||||
assert.Contains(t, ALL_SECRET_KEYS, secret.Key)
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
|
|
||||||
if secret.Key == "FOLDER-SECRET-1" {
|
|
||||||
assert.Equal(t, secret.Value, "*not found*") // Should not be found because recursive isn't enabled in this test, and the default path is "/"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUniversalAuth_SecretsGetWithImportsAndRecursiveCmd(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
MachineIdentityLoginCmd(t)
|
||||||
|
|
||||||
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent")
|
||||||
|
|
||||||
|
fmt.Printf("output: %v\n", output)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use cupaloy to snapshot test the output
|
||||||
|
err = cupaloy.Snapshot(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUniversalAuth_SecretsGetWithoutImportsAndWithoutRecursiveCmd(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
MachineIdentityLoginCmd(t)
|
||||||
|
|
||||||
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--include-imports=false", "--silent")
|
||||||
|
|
||||||
|
fmt.Printf("output: %v\n", output)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use cupaloy to snapshot test the output
|
||||||
|
err = cupaloy.Snapshot(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUniversalAuth_SecretsGetWrongEnvironment(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
MachineIdentityLoginCmd(t)
|
||||||
|
|
||||||
|
output, _ := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", "invalid-env", "--recursive", "--silent")
|
||||||
|
|
||||||
|
fmt.Printf("output: %v\n", output)
|
||||||
|
|
||||||
|
// Use cupaloy to snapshot test the output
|
||||||
|
err := cupaloy.Snapshot(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user