package util import ( "os" "testing" "time" "github.com/stretchr/testify/require" "gotest.tools/v3/assert" "edge-infra.dev/pkg/edge/iam/config" ) func TestRolesSerialize(t *testing.T) { roles := []string{"SELLING_EXECUTE", "TDM_VIEW"} serializedRoles, _ := Serialize(roles) deserializedRoles, _ := Deserialize(serializedRoles) require.Contains(t, deserializedRoles, roles[0]) require.Contains(t, deserializedRoles, roles[1]) } func TestDeviceLoginAvailable(t *testing.T) { os.Setenv("IAM_MODE", "debug") assert.Equal(t, IsDeviceLoginAvailable(), true) config.ToggleDisruptWAN() assert.Equal(t, IsDeviceLoginAvailable(), false) } // mock time.Now() for testing. func mockNow() time.Time { return time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) } func TestCalculateAge(t *testing.T) { tests := []struct { dob string expected int hasError bool tz string }{ {"1990-01-29", 32, false, "UTC"}, // takes UTC into consideration as the reference is, so it must be 32 as birthday is on Jan 29th. {"1990-01-01", 32, false, "US/Eastern"}, // takes US/East into consideration, so the age would be still be 32 as US wouldn't enter 1-1-2023 yet. {"1990-01-01", 33, false, "Asia/Kolkata"}, // takes Asia/Kolkta into consideration, so the age would be 33 as India should have entered 1-1-2023 by then. {"1990-01-01", 33, false, "UTC"}, // takes UTC into consideration, so the age would be 33 as India should have entered 1-1-2023 by then. {"2023-01-01", 0, false, "UTC"}, // Birthday today {"2025-12-31", 0, true, "UTC"}, // Future date {"invalid-date", 0, true, "UTC"}, // Invalid date format } for _, test := range tests { age, err := CalculateAge(test.dob, mockNow, test.tz) if (err != nil) != test.hasError { t.Errorf("calculateAge(%q) error = %v, wantErr %v", test.dob, err, test.hasError) continue } if age != test.expected { t.Errorf("calculateAge(%q) = %d, want %d", test.dob, age, test.expected) } } }