6dccf494f8
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
420 lines
11 KiB
Go
420 lines
11 KiB
Go
//go:build aws
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const nameValidationTestCSVPath = "./temp-name-validation.csv"
|
|
|
|
func TestNameValidationForCreateMode(t *testing.T) {
|
|
t.Run("CreateMode_MissingFirstName", func(t *testing.T) {
|
|
// CSV with missing first name
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,,Smith\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
_, err = readCSV(nameValidationTestCSVPath, config)
|
|
if err == nil {
|
|
t.Fatal("Expected error for missing first_name in create mode, but got none")
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "first_name cannot be empty for user creation") {
|
|
t.Fatalf("Expected error about missing first_name, got: %v", err)
|
|
}
|
|
|
|
t.Logf("Correctly detected missing first_name: %v", err)
|
|
})
|
|
|
|
t.Run("CreateMode_MissingLastName", func(t *testing.T) {
|
|
// CSV with missing last name
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,John,\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
_, err = readCSV(nameValidationTestCSVPath, config)
|
|
if err == nil {
|
|
t.Fatal("Expected error for missing last_name in create mode, but got none")
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "last_name cannot be empty for user creation") {
|
|
t.Fatalf("Expected error about missing last_name, got: %v", err)
|
|
}
|
|
|
|
t.Logf("Correctly detected missing last_name: %v", err)
|
|
})
|
|
|
|
t.Run("CreateMode_MissingBothNames", func(t *testing.T) {
|
|
// CSV with missing both names
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,,\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
_, err = readCSV(nameValidationTestCSVPath, config)
|
|
if err == nil {
|
|
t.Fatal("Expected error for missing names in create mode, but got none")
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "first_name cannot be empty for user creation") {
|
|
t.Fatalf("Expected error about missing first_name, got: %v", err)
|
|
}
|
|
|
|
t.Logf("Correctly detected missing first_name: %v", err)
|
|
})
|
|
|
|
t.Run("CreateMode_ValidNames", func(t *testing.T) {
|
|
// CSV with valid names
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,John,Smith\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for valid names in create mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
user := users[0]
|
|
if user["email"] != "test@example.com" {
|
|
t.Fatalf("Expected email 'test@example.com', got '%s'", user["email"])
|
|
}
|
|
if user["first_name"] != "John" {
|
|
t.Fatalf("Expected first_name 'John', got '%s'", user["first_name"])
|
|
}
|
|
if user["last_name"] != "Smith" {
|
|
t.Fatalf("Expected last_name 'Smith', got '%s'", user["last_name"])
|
|
}
|
|
|
|
t.Logf("Valid names accepted correctly for create mode")
|
|
})
|
|
}
|
|
|
|
func TestNameValidationForDeleteMode(t *testing.T) {
|
|
t.Run("DeleteMode_OnlyEmail", func(t *testing.T) {
|
|
// CSV with only email (minimal CSV for delete)
|
|
csvContent := "email\n"
|
|
csvContent += "test@example.com\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: true,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for email-only CSV in delete mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
user := users[0]
|
|
if user["email"] != "test@example.com" {
|
|
t.Fatalf("Expected email 'test@example.com', got '%s'", user["email"])
|
|
}
|
|
if user["first_name"] != "" {
|
|
t.Fatalf("Expected empty first_name, got '%s'", user["first_name"])
|
|
}
|
|
if user["last_name"] != "" {
|
|
t.Fatalf("Expected empty last_name, got '%s'", user["last_name"])
|
|
}
|
|
|
|
t.Logf("Email-only CSV accepted correctly for delete mode")
|
|
})
|
|
|
|
t.Run("DeleteMode_EmptyNames", func(t *testing.T) {
|
|
// CSV with empty names (should be allowed for delete)
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,,\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: true,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for empty names in delete mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
user := users[0]
|
|
if user["email"] != "test@example.com" {
|
|
t.Fatalf("Expected email 'test@example.com', got '%s'", user["email"])
|
|
}
|
|
|
|
t.Logf("Empty names accepted correctly for delete mode")
|
|
})
|
|
}
|
|
|
|
func TestNameValidationForDisableMode(t *testing.T) {
|
|
t.Run("DisableMode_OnlyEmail", func(t *testing.T) {
|
|
// CSV with only email
|
|
csvContent := "email\n"
|
|
csvContent += "test@example.com\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: true,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for email-only CSV in disable mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
user := users[0]
|
|
if user["email"] != "test@example.com" {
|
|
t.Fatalf("Expected email 'test@example.com', got '%s'", user["email"])
|
|
}
|
|
|
|
t.Logf("Email-only CSV accepted correctly for disable mode")
|
|
})
|
|
|
|
t.Run("DisableMode_EmptyNames", func(t *testing.T) {
|
|
// CSV with empty names
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,,\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: true,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for empty names in disable mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
t.Logf("Empty names accepted correctly for disable mode")
|
|
})
|
|
}
|
|
|
|
func TestNameValidationForEnableMode(t *testing.T) {
|
|
t.Run("EnableMode_OnlyEmail", func(t *testing.T) {
|
|
// CSV with only email
|
|
csvContent := "email\n"
|
|
csvContent += "test@example.com\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: false,
|
|
EnableUsers: true,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for email-only CSV in enable mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
user := users[0]
|
|
if user["email"] != "test@example.com" {
|
|
t.Fatalf("Expected email 'test@example.com', got '%s'", user["email"])
|
|
}
|
|
|
|
t.Logf("Email-only CSV accepted correctly for enable mode")
|
|
})
|
|
|
|
t.Run("EnableMode_EmptyNames", func(t *testing.T) {
|
|
// CSV with empty names
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,,\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: false,
|
|
EnableUsers: true,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for empty names in enable mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
t.Logf("Empty names accepted correctly for enable mode")
|
|
})
|
|
}
|
|
|
|
// Test edge case where CSV has inconsistent field counts
|
|
func TestNameValidationEdgeCases(t *testing.T) {
|
|
t.Run("CreateMode_InsufficientFields", func(t *testing.T) {
|
|
// CSV with insufficient fields for create mode
|
|
csvContent := "email,first_name,last_name\n"
|
|
csvContent += "test@example.com,John\n" // Missing last_name field
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: false,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
_, err = readCSV(nameValidationTestCSVPath, config)
|
|
if err == nil {
|
|
t.Fatal("Expected error for insufficient fields in create mode, but got none")
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "must have at least 3 fields") {
|
|
t.Fatalf("Expected error about insufficient fields, got: %v", err)
|
|
}
|
|
|
|
t.Logf("Correctly detected insufficient fields: %v", err)
|
|
})
|
|
|
|
t.Run("DeleteMode_OnlyEmailField", func(t *testing.T) {
|
|
// CSV with just email field for delete mode
|
|
csvContent := "email\n"
|
|
csvContent += "test@example.com\n"
|
|
|
|
err := os.WriteFile(nameValidationTestCSVPath, []byte(csvContent), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test CSV: %v", err)
|
|
}
|
|
defer os.Remove(nameValidationTestCSVPath)
|
|
|
|
config := AppConfig{
|
|
CSVPath: nameValidationTestCSVPath,
|
|
DeleteUsers: true,
|
|
DisableUsers: false,
|
|
EnableUsers: false,
|
|
}
|
|
|
|
users, err := readCSV(nameValidationTestCSVPath, config)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error for email-only CSV in delete mode, got: %v", err)
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
t.Fatalf("Expected 1 user, got %d", len(users))
|
|
}
|
|
|
|
t.Logf("Single email field correctly handled for delete mode")
|
|
})
|
|
}
|