Files
query-orchestration/cmd/auth_related/user.creation.tool/minimal_csv_operations_test.go
T
Jay Brown 6dccf494f8 Merged in feature/permit-policy-cleanup (pull request #210)
cleanup permit policies and correct documentation

* docs

* policy cleanup

* refactor

* Merge remote-tracking branch 'origin/feature/permit-policy-cleanup' into feature/permit-policy-cleanup

* docs and edits
2026-02-19 20:22:59 +00:00

395 lines
9.4 KiB
Go

//go:build aws
package main
import (
"os"
"testing"
)
const minimalCSVPath = "./temp-minimal-operations.csv"
// Test that the actual CLI operations work with minimal CSV files
func TestMinimalCSVOperations(t *testing.T) {
// Skip test if required environment variables are not set
if !checkRequiredEnvVars(t) {
return
}
// First create some test users that we can later disable/delete
t.Run("CreateUserForMinimalTesting", func(t *testing.T) {
// Create a test user with full data
csvContent := "email,first_name,last_name\n"
csvContent += "minimal-test@gmail.com,Minimal,Test\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create test CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
// Save original os.Args
originalArgs := os.Args
// Set up args for user creation
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
}
// Restore os.Args after test
defer func() {
os.Args = originalArgs
}()
// Reset flags for testing
resetFlags()
// Run the application to create the user
err = run()
if err != nil {
t.Fatalf("Failed to create test user for minimal CSV testing: %v", err)
}
t.Logf("Successfully created test user for minimal CSV operations")
})
// Test disable operation with minimal CSV (only email)
t.Run("DisableUserWithMinimalCSV", func(t *testing.T) {
// Create minimal CSV with only email
csvContent := "email\n"
csvContent += "minimal-test@gmail.com\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create minimal CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
// Save original os.Args
originalArgs := os.Args
// Set up args for user disabling
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
"--disable-users",
}
// Restore os.Args after test
defer func() {
os.Args = originalArgs
}()
// Reset flags for testing
resetFlags()
// Run the application in disable mode
err = run()
if err != nil {
t.Fatalf("Failed to disable user with minimal CSV: %v", err)
}
t.Logf("Successfully disabled user with minimal CSV (email only)")
})
// Test enable operation with minimal CSV (only email)
t.Run("EnableUserWithMinimalCSV", func(t *testing.T) {
// Create minimal CSV with only email
csvContent := "email\n"
csvContent += "minimal-test@gmail.com\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create minimal CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
// Save original os.Args
originalArgs := os.Args
// Set up args for user enabling
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
"--enable-users",
}
// Restore os.Args after test
defer func() {
os.Args = originalArgs
}()
// Reset flags for testing
resetFlags()
// Run the application in enable mode
err = run()
if err != nil {
t.Fatalf("Failed to enable user with minimal CSV: %v", err)
}
t.Logf("Successfully enabled user with minimal CSV (email only)")
})
// Test delete operation with minimal CSV (only email)
t.Run("DeleteUserWithMinimalCSV", func(t *testing.T) {
// Create minimal CSV with only email
csvContent := "email\n"
csvContent += "minimal-test@gmail.com\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create minimal CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
// Save original os.Args
originalArgs := os.Args
// Set up args for user deletion
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
"--delete-users",
}
// Restore os.Args after test
defer func() {
os.Args = originalArgs
}()
// Reset flags for testing
resetFlags()
// Run the application in delete mode
err = run()
if err != nil {
t.Fatalf("Failed to delete user with minimal CSV: %v", err)
}
t.Logf("Successfully deleted user with minimal CSV (email only)")
})
}
// Test that create operations fail with empty names
func TestCreateOperationValidation(t *testing.T) {
// Skip test if required environment variables are not set
if !checkRequiredEnvVars(t) {
return
}
t.Run("CreateFailsWithEmptyFirstName", func(t *testing.T) {
// Create CSV with empty first name
csvContent := "email,first_name,last_name\n"
csvContent += "validation-test@gmail.com,,Test\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create test CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
// Save original os.Args
originalArgs := os.Args
// Set up args for user creation (default mode)
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
}
// Restore os.Args after test
defer func() {
os.Args = originalArgs
}()
// Reset flags for testing
resetFlags()
// Run the application - should fail
err = run()
if err == nil {
t.Fatal("Expected error for empty first_name in create mode, but got none")
}
t.Logf("Create operation correctly failed with empty first_name: %v", err)
})
t.Run("CreateFailsWithEmptyLastName", func(t *testing.T) {
// Create CSV with empty last name
csvContent := "email,first_name,last_name\n"
csvContent += "validation-test@gmail.com,Test,\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create test CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
// Save original os.Args
originalArgs := os.Args
// Set up args for user creation (default mode)
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
}
// Restore os.Args after test
defer func() {
os.Args = originalArgs
}()
// Reset flags for testing
resetFlags()
// Run the application - should fail
err = run()
if err == nil {
t.Fatal("Expected error for empty last_name in create mode, but got none")
}
t.Logf("Create operation correctly failed with empty last_name: %v", err)
})
}
// Test with CSV files that have empty name fields but valid for delete/disable
func TestEmptyNamesForNonCreateOperations(t *testing.T) {
// Skip test if required environment variables are not set
if !checkRequiredEnvVars(t) {
return
}
// First create a test user
t.Run("CreateUserForEmptyNameTesting", func(t *testing.T) {
csvContent := "email,first_name,last_name\n"
csvContent += "empty-names-test@gmail.com,EmptyNames,Test\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create test CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
originalArgs := os.Args
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
}
defer func() { os.Args = originalArgs }()
resetFlags()
err = run()
if err != nil {
t.Fatalf("Failed to create test user: %v", err)
}
t.Logf("Successfully created test user for empty names testing")
})
t.Run("DisableWithEmptyNames", func(t *testing.T) {
// CSV with empty names
csvContent := "email,first_name,last_name\n"
csvContent += "empty-names-test@gmail.com,,\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create test CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
originalArgs := os.Args
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
"--disable-users",
}
defer func() { os.Args = originalArgs }()
resetFlags()
err = run()
if err != nil {
t.Fatalf("Failed to disable user with empty names: %v", err)
}
t.Logf("Successfully disabled user with empty names")
})
t.Run("EnableWithEmptyNames", func(t *testing.T) {
// CSV with empty names
csvContent := "email,first_name,last_name\n"
csvContent += "empty-names-test@gmail.com,,\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create test CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
originalArgs := os.Args
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
"--enable-users",
}
defer func() { os.Args = originalArgs }()
resetFlags()
err = run()
if err != nil {
t.Fatalf("Failed to enable user with empty names: %v", err)
}
t.Logf("Successfully enabled user with empty names")
})
t.Run("DeleteWithEmptyNames", func(t *testing.T) {
// CSV with empty names
csvContent := "email,first_name,last_name\n"
csvContent += "empty-names-test@gmail.com,,\n"
err := os.WriteFile(minimalCSVPath, []byte(csvContent), 0600)
if err != nil {
t.Fatalf("Failed to create test CSV: %v", err)
}
defer os.Remove(minimalCSVPath)
originalArgs := os.Args
os.Args = []string{
"cognito-permit-sync",
"-project", testProjectID,
"-env", testEnvID,
"-csv", minimalCSVPath,
"--delete-users",
}
defer func() { os.Args = originalArgs }()
resetFlags()
err = run()
if err != nil {
t.Fatalf("Failed to delete user with empty names: %v", err)
}
t.Logf("Successfully deleted user with empty names")
})
}