Files
Jay Brown c668485e6f Merged in feature/cognito-shared-environments (pull request #211)
cognito and permit shared prod environment support

* code complete

* user creattion tool

test harness
2026-02-26 12:33:35 +00:00

111 lines
3.7 KiB
Go

package usermanagement
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestValidateCognitoEnvironmentID_ValidValues tests that valid environment IDs
// pass validation without error.
func TestValidateCognitoEnvironmentID_ValidValues(t *testing.T) {
tests := []struct {
name string
input string
}{
{"simple lowercase", "acme"},
{"alphanumeric with digit", "client1"},
{"single character", "a"},
{"underscore in middle", "acme_health"},
{"multiple underscores separated", "a_b_c"},
{"empty string means not set", ""},
{"two characters", "ab"},
{"ends with digit", "env9"},
{"all lowercase letters", "abcdefghij"},
{"mixed alpha and digits", "abc123def456"},
{"underscore between digits", "a1_b2"},
{"max length 50 chars", "a" + strings.Repeat("b", 48) + "c"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateCognitoEnvironmentID(tt.input)
assert.NoError(t, err, "expected %q to be valid", tt.input)
})
}
}
// TestValidateCognitoEnvironmentID_InvalidValues tests that invalid environment IDs
// are rejected with an error.
func TestValidateCognitoEnvironmentID_InvalidValues(t *testing.T) {
tests := []struct {
name string
input string
}{
{"starts with digit", "1acme"},
{"uppercase letters", "Acme"},
{"all uppercase", "ACME"},
{"mixed case", "acmeHealth"},
{"special char hyphen", "acme-health"},
{"special char dot", "acme.health"},
{"special char at", "acme@health"},
{"special char space", "acme health"},
{"starts with underscore", "_acme"},
{"ends with underscore", "acme_"},
{"consecutive underscores", "acme__health"},
{"only underscore", "_"},
{"only underscores", "__"},
{"exceeds max length", "a" + strings.Repeat("b", 50)},
{"contains space in middle", "acme health"},
{"tab character", "acme\thealth"},
{"starts with digit and underscore", "1_acme"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateCognitoEnvironmentID(tt.input)
assert.Error(t, err, "expected %q to be invalid", tt.input)
})
}
}
// TestCognitoEnvIDConstants verifies the exported constants have the expected values.
func TestCognitoEnvIDConstants(t *testing.T) {
assert.Equal(t, 50, CognitoEnvIDMaxLen, "max length constant should be 50")
assert.Equal(t, "custom:environment_id", CognitoEnvIDAttrName, "attribute name should match Cognito custom attribute convention")
assert.Equal(t, "COGNITO_ENVIRONMENT_ID", CognitoEnvIDEnvVar, "env var name should match expected convention")
}
// TestValidateCognitoEnvironmentID_ExactMaxLength verifies that a string at exactly
// the max length is accepted, but one character over is rejected.
func TestValidateCognitoEnvironmentID_ExactMaxLength(t *testing.T) {
// Exactly 50 characters: starts with 'a', 48 middle chars, ends with 'z'
exactMax := "a" + strings.Repeat("b", 48) + "z"
require.Equal(t, 50, len(exactMax))
err := ValidateCognitoEnvironmentID(exactMax)
assert.NoError(t, err, "exactly 50 chars should be valid")
// 51 characters: should fail
overMax := exactMax + "x"
require.Equal(t, 51, len(overMax))
err = ValidateCognitoEnvironmentID(overMax)
assert.Error(t, err, "51 chars should be invalid")
}
// TestValidateCognitoEnvironmentID_ErrorMessages verifies that error messages
// are descriptive enough to help users fix invalid values.
func TestValidateCognitoEnvironmentID_ErrorMessages(t *testing.T) {
err := ValidateCognitoEnvironmentID("ACME")
require.Error(t, err)
// Error message should mention what went wrong
assert.NotEmpty(t, err.Error())
err = ValidateCognitoEnvironmentID("a" + strings.Repeat("b", 50))
require.Error(t, err)
assert.NotEmpty(t, err.Error())
}