212 lines
8.8 KiB
Go
212 lines
8.8 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const testPasswordHash = "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA"
|
|
|
|
var testRegisteredAt = time.Date(2026, time.October, 5, 12, 0, 0, 0, time.UTC)
|
|
|
|
func testRegistration(t *testing.T, rawEmail string, scenario Scenario) Registration {
|
|
t.Helper()
|
|
email, err := NewEmailAddress(rawEmail)
|
|
if err != nil {
|
|
t.Fatalf("NewEmailAddress() error = %v", err)
|
|
}
|
|
passwordHash, err := NewPasswordHash(testPasswordHash)
|
|
if err != nil {
|
|
t.Fatalf("NewPasswordHash() error = %v", err)
|
|
}
|
|
return Registration{Email: email, PasswordHash: passwordHash, Scenario: scenario, RegisteredAt: testRegisteredAt}
|
|
}
|
|
|
|
func TestParseScenarioAndOnboardingStepAcceptContractValues(t *testing.T) {
|
|
for _, value := range []string{"candidate", "employer"} {
|
|
if scenario, err := ParseScenario(value); err != nil || string(scenario) != value {
|
|
t.Fatalf("ParseScenario(%q) = %q, %v", value, scenario, err)
|
|
}
|
|
}
|
|
for _, value := range []string{
|
|
"candidate_consents", "candidate_profile", "candidate_questionnaire",
|
|
"candidate_assessment", "employer_company", "completed",
|
|
} {
|
|
if step, err := ParseOnboardingStep(value); err != nil || string(step) != value {
|
|
t.Fatalf("ParseOnboardingStep(%q) = %q, %v", value, step, err)
|
|
}
|
|
}
|
|
for _, value := range []string{"", "Candidate", "admin"} {
|
|
if _, err := ParseScenario(value); !errors.Is(err, ErrInvalidUser) {
|
|
t.Fatalf("ParseScenario(%q) error = %v, want ErrInvalidUser", value, err)
|
|
}
|
|
if _, err := ParseOnboardingStep(value); !errors.Is(err, ErrInvalidUser) {
|
|
t.Fatalf("ParseOnboardingStep(%q) error = %v, want ErrInvalidUser", value, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestScenarioOnboardingSteps(t *testing.T) {
|
|
tests := []struct {
|
|
scenario Scenario
|
|
initial OnboardingStep
|
|
allowed []OnboardingStep
|
|
denied []OnboardingStep
|
|
}{
|
|
{
|
|
scenario: ScenarioCandidate,
|
|
initial: OnboardingStepCandidateConsents,
|
|
allowed: []OnboardingStep{
|
|
OnboardingStepCandidateConsents, OnboardingStepCandidateProfile, OnboardingStepCandidateQuestionnaire,
|
|
OnboardingStepCandidateAssessment, OnboardingStepCompleted,
|
|
},
|
|
denied: []OnboardingStep{OnboardingStepEmployerCompany, "unknown"},
|
|
},
|
|
{
|
|
scenario: ScenarioEmployer,
|
|
initial: OnboardingStepEmployerCompany,
|
|
allowed: []OnboardingStep{OnboardingStepEmployerCompany, OnboardingStepCompleted},
|
|
denied: []OnboardingStep{OnboardingStepCandidateConsents, OnboardingStepCandidateAssessment, "unknown"},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
if got := test.scenario.InitialOnboardingStep(); got != test.initial {
|
|
t.Fatalf("%s.InitialOnboardingStep() = %q, want %q", test.scenario, got, test.initial)
|
|
}
|
|
for _, step := range test.allowed {
|
|
if !test.scenario.AllowsStep(step) {
|
|
t.Fatalf("%s.AllowsStep(%q) = false, want true", test.scenario, step)
|
|
}
|
|
}
|
|
for _, step := range test.denied {
|
|
if test.scenario.AllowsStep(step) {
|
|
t.Fatalf("%s.AllowsStep(%q) = true, want false", test.scenario, step)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewPasswordHashAcceptsOnlyArgon2idPHC(t *testing.T) {
|
|
for _, value := range []string{
|
|
testPasswordHash,
|
|
"$argon2id$v=19$m=65536,t=3,p=4$c29tZXNhbHRzb21lc2FsdA$RdescudvJCsgt3ub+b+dWRWJTmaaJObG",
|
|
} {
|
|
if _, err := NewPasswordHash(value); err != nil {
|
|
t.Fatalf("NewPasswordHash(%.30q) error = %v", value, err)
|
|
}
|
|
}
|
|
for name, value := range map[string]string{
|
|
"empty": "",
|
|
"raw password": "correct horse battery staple",
|
|
"bcrypt": "$2b$12$abcdefghijklmnopqrstuuABCDEFGHIJKLMNOPQRSTUVWXYZ01234",
|
|
"argon2i": "$argon2i$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA",
|
|
"prefix only": "$argon2id$",
|
|
"old version": "$argon2id$v=16$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA",
|
|
"missing parameter": "$argon2id$v=19$m=19456,t=2$c2FsdHNhbHRzYWx0$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA",
|
|
"zero memory": "$argon2id$v=19$m=0,t=2,p=1$c2FsdHNhbHRzYWx0$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA",
|
|
"padded salt": "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHQ=$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA",
|
|
"short salt": "$argon2id$v=19$m=19456,t=2,p=1$c2FsdA$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA",
|
|
"short digest": "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$aGFzaA",
|
|
"impossible base64": "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0a$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaA",
|
|
"url-safe alphabet": "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$aGFzaGhhc2hoYXNoaGF_aGhhc2hoYXNoaGFzaA",
|
|
"trailing space": testPasswordHash + " ",
|
|
"trailing newline": testPasswordHash + "\n",
|
|
"extra segment": testPasswordHash + "$extra",
|
|
"too long": "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$" + strings.Repeat("a", maxPasswordHashLength),
|
|
} {
|
|
if _, err := NewPasswordHash(value); !errors.Is(err, ErrInvalidPasswordHash) {
|
|
t.Fatalf("NewPasswordHash(%s) error = %v, want ErrInvalidPasswordHash", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewUserStartsUnverifiedAtTheFirstStep(t *testing.T) {
|
|
registration := testRegistration(t, "Employer@Example.com", ScenarioEmployer)
|
|
user, err := NewUser(registration)
|
|
if err != nil {
|
|
t.Fatalf("NewUser() error = %v", err)
|
|
}
|
|
if user.ID.Version() != 7 {
|
|
t.Fatalf("ID version = %d, want UUIDv7", user.ID.Version())
|
|
}
|
|
if user.EmailVerified() || user.OnboardingStep != OnboardingStepEmployerCompany ||
|
|
user.RegistrationVersion != 1 || user.Version != 1 {
|
|
t.Fatalf("NewUser() = %+v, want an unverified employer at employer_company with versions 1", user)
|
|
}
|
|
if !user.LastRegisteredAt.Equal(testRegisteredAt) || !user.CreatedAt.Equal(testRegisteredAt) || !user.UpdatedAt.Equal(testRegisteredAt) {
|
|
t.Fatalf("NewUser() timestamps = %v, %v, %v, want the registration time", user.LastRegisteredAt, user.CreatedAt, user.UpdatedAt)
|
|
}
|
|
}
|
|
|
|
func TestNewUserRejectsIncompleteRegistration(t *testing.T) {
|
|
valid := testRegistration(t, "candidate@example.com", ScenarioCandidate)
|
|
tests := map[string]func(*Registration){
|
|
"zero email": func(registration *Registration) { registration.Email = EmailAddress{} },
|
|
"zero password hash": func(registration *Registration) { registration.PasswordHash = PasswordHash{} },
|
|
"unknown scenario": func(registration *Registration) { registration.Scenario = "admin" },
|
|
"zero time": func(registration *Registration) { registration.RegisteredAt = time.Time{} },
|
|
}
|
|
for name, mutate := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
registration := valid
|
|
mutate(®istration)
|
|
if _, err := NewUser(registration); !errors.Is(err, ErrInvalidUser) {
|
|
t.Fatalf("NewUser() error = %v, want ErrInvalidUser", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReRegisterReplacesTheRegistrationOfAnUnverifiedAccount(t *testing.T) {
|
|
user, err := NewUser(testRegistration(t, "candidate@example.com", ScenarioCandidate))
|
|
if err != nil {
|
|
t.Fatalf("NewUser() error = %v", err)
|
|
}
|
|
user.OnboardingStep = OnboardingStepCandidateProfile
|
|
again := testRegistration(t, "Candidate@Example.com", ScenarioEmployer)
|
|
again.RegisteredAt = testRegisteredAt.Add(time.Hour)
|
|
|
|
updated, err := user.ReRegister(again)
|
|
if err != nil {
|
|
t.Fatalf("ReRegister() error = %v", err)
|
|
}
|
|
if updated.ID != user.ID || updated.Email.Address() != "Candidate@Example.com" ||
|
|
updated.Scenario != ScenarioEmployer || updated.OnboardingStep != OnboardingStepEmployerCompany ||
|
|
updated.RegistrationVersion != user.RegistrationVersion || updated.Version != user.Version ||
|
|
!updated.LastRegisteredAt.Equal(again.RegisteredAt) || !updated.UpdatedAt.Equal(again.RegisteredAt) ||
|
|
!updated.CreatedAt.Equal(user.CreatedAt) {
|
|
t.Fatalf("ReRegister() = %+v", updated)
|
|
}
|
|
}
|
|
|
|
func TestReRegisterRejectsVerifiedOrDifferentEmail(t *testing.T) {
|
|
user, err := NewUser(testRegistration(t, "candidate@example.com", ScenarioCandidate))
|
|
if err != nil {
|
|
t.Fatalf("NewUser() error = %v", err)
|
|
}
|
|
if _, err := user.ReRegister(testRegistration(t, "other@example.com", ScenarioCandidate)); !errors.Is(err, ErrInvalidUser) {
|
|
t.Fatalf("ReRegister(other email) error = %v, want ErrInvalidUser", err)
|
|
}
|
|
verifiedAt := testRegisteredAt.Add(time.Minute)
|
|
user.EmailVerifiedAt = &verifiedAt
|
|
if _, err := user.ReRegister(testRegistration(t, "candidate@example.com", ScenarioCandidate)); !errors.Is(err, ErrEmailAlreadyVerified) {
|
|
t.Fatalf("ReRegister(verified) error = %v, want ErrEmailAlreadyVerified", err)
|
|
}
|
|
}
|
|
|
|
func TestFormattedUserHidesEmailAndPasswordHash(t *testing.T) {
|
|
user, err := NewUser(testRegistration(t, "Private.Person@Example.com", ScenarioCandidate))
|
|
if err != nil {
|
|
t.Fatalf("NewUser() error = %v", err)
|
|
}
|
|
formatted := fmt.Sprintf("%v %+v %#v", user, user, user)
|
|
for _, secret := range []string{"Private", "private", "example.com", testPasswordHash, "argon2id"} {
|
|
if strings.Contains(formatted, secret) {
|
|
t.Fatalf("formatted user exposes %q: %s", secret, formatted)
|
|
}
|
|
}
|
|
}
|