c668485e6f
cognito and permit shared prod environment support * code complete * user creattion tool test harness
117 lines
4.1 KiB
Go
117 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"queryorchestration/internal/usermanagement"
|
|
)
|
|
|
|
// TestParseFlags_CognitoEnvIDFlagOverridesEnvVar verifies that the -cognito-env-id flag
|
|
// takes precedence over the COGNITO_ENVIRONMENT_ID environment variable. This tests the
|
|
// fallback logic at cognito-permit-sync.go:393-396.
|
|
func TestParseFlags_CognitoEnvIDFlagOverridesEnvVar(t *testing.T) {
|
|
// Set the env var that would normally be the fallback
|
|
t.Setenv(usermanagement.CognitoEnvIDEnvVar, "fromenv")
|
|
|
|
// Reset the global flag.CommandLine to allow re-parsing
|
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
|
|
|
// Simulate command-line args with the flag set
|
|
origArgs := os.Args
|
|
os.Args = []string{"test", "-cognito-env-id=fromflag", "-csv=dummy.csv", "-project=test", "-env=test"}
|
|
defer func() { os.Args = origArgs }()
|
|
|
|
config := parseFlags()
|
|
|
|
assert.Equal(t, "fromflag", config.CognitoEnvironmentID,
|
|
"Flag -cognito-env-id should override COGNITO_ENVIRONMENT_ID env var")
|
|
}
|
|
|
|
// TestParseFlags_CognitoEnvIDFallsBackToEnvVar verifies that when the -cognito-env-id
|
|
// flag is not provided, the COGNITO_ENVIRONMENT_ID env var is used as a fallback.
|
|
func TestParseFlags_CognitoEnvIDFallsBackToEnvVar(t *testing.T) {
|
|
t.Setenv(usermanagement.CognitoEnvIDEnvVar, "fromenv")
|
|
|
|
// Reset the global flag.CommandLine to allow re-parsing
|
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
|
|
|
origArgs := os.Args
|
|
os.Args = []string{"test", "-csv=dummy.csv", "-project=test", "-env=test"}
|
|
defer func() { os.Args = origArgs }()
|
|
|
|
config := parseFlags()
|
|
|
|
assert.Equal(t, "fromenv", config.CognitoEnvironmentID,
|
|
"COGNITO_ENVIRONMENT_ID env var should be used when flag is not set")
|
|
}
|
|
|
|
// TestValidateConfig_InvalidCognitoEnvID verifies that validateConfig returns an error
|
|
// when CognitoEnvironmentID contains invalid characters (e.g., uppercase letters).
|
|
func TestValidateConfig_InvalidCognitoEnvID(t *testing.T) {
|
|
// Create a temp CSV file so file validation passes
|
|
tmpFile, err := os.CreateTemp(t.TempDir(), "test-*.csv")
|
|
require.NoError(t, err)
|
|
defer tmpFile.Close()
|
|
|
|
config := AppConfig{
|
|
PermitAPIKey: "permit_key_test",
|
|
PermitProjectID: "testproject",
|
|
PermitEnvID: "testenv",
|
|
CognitoUserPoolID: "us-east-1_TestPool",
|
|
CognitoRegion: "us-east-1",
|
|
CognitoEnvironmentID: "UPPERCASE", // invalid: must be lowercase
|
|
CSVPath: tmpFile.Name(),
|
|
}
|
|
|
|
err = validateConfig(config)
|
|
assert.Error(t, err, "Should reject uppercase environment ID")
|
|
assert.Contains(t, err.Error(), "invalid cognito environment ID")
|
|
}
|
|
|
|
// TestValidateConfig_ValidCognitoEnvID verifies that validateConfig accepts a valid
|
|
// lowercase environment ID without errors from the env ID validation.
|
|
func TestValidateConfig_ValidCognitoEnvID(t *testing.T) {
|
|
tmpFile, err := os.CreateTemp(t.TempDir(), "test-*.csv")
|
|
require.NoError(t, err)
|
|
defer tmpFile.Close()
|
|
|
|
config := AppConfig{
|
|
PermitAPIKey: "permit_key_test",
|
|
PermitProjectID: "testproject",
|
|
PermitEnvID: "testenv",
|
|
CognitoUserPoolID: "us-east-1_TestPool",
|
|
CognitoRegion: "us-east-1",
|
|
CognitoEnvironmentID: "acmehealth", // valid
|
|
CSVPath: tmpFile.Name(),
|
|
}
|
|
|
|
err = validateConfig(config)
|
|
assert.NoError(t, err, "Should accept valid lowercase environment ID")
|
|
}
|
|
|
|
// TestValidateConfig_EmptyCognitoEnvID verifies that an empty CognitoEnvironmentID
|
|
// passes validation (backward compatibility -- not all deployments use env isolation).
|
|
func TestValidateConfig_EmptyCognitoEnvID(t *testing.T) {
|
|
tmpFile, err := os.CreateTemp(t.TempDir(), "test-*.csv")
|
|
require.NoError(t, err)
|
|
defer tmpFile.Close()
|
|
|
|
config := AppConfig{
|
|
PermitAPIKey: "permit_key_test",
|
|
PermitProjectID: "testproject",
|
|
PermitEnvID: "testenv",
|
|
CognitoUserPoolID: "us-east-1_TestPool",
|
|
CognitoRegion: "us-east-1",
|
|
CognitoEnvironmentID: "", // empty is valid
|
|
CSVPath: tmpFile.Name(),
|
|
}
|
|
|
|
err = validateConfig(config)
|
|
assert.NoError(t, err, "Should accept empty environment ID (backward compat)")
|
|
}
|