feat(identity): confirm the email address with a six-digit code from the message

This commit is contained in:
shira committed 2026-10-10 00:52:46 +03:00
1 parent c94434cd09
commit d365a05c96
87 files changed
+4764 -224

No files matched your search

@@ -12,6 +12,7 @@ import (
"strings"
"testing"
"testing/fstest"
"time"
"lincodex/backend/internal/domain"
)
@@ -50,6 +51,21 @@ func testToken(t *testing.T) domain.SecretToken {
return token
}
// testCode is a fixed synthetic code. It is not a credential.
const (
syntheticCode = "482915"
testCodeLifetime = 30 * time.Minute
)
func testCode(t *testing.T) domain.VerificationCode {
t.Helper()
code, err := domain.ParseVerificationCode(syntheticCode)
if err != nil {
t.Fatalf("ParseVerificationCode() error = %v", err)
}
return code
}
func hostileNotification() NotificationData {
return NotificationData{Subject: hostileSubject, Body: hostileBody, LinkPath: "/candidate/invitations"}
}
@@ -58,7 +74,7 @@ func hostileNotification() NotificationData {
func renderAll(t *testing.T, renderer *Renderer) map[Kind]RenderedMail {
t.Helper()
token := testToken(t)
verification, err := renderer.RenderVerification(VerificationData{Token: token, TokenLifetime: 24})
verification, err := renderer.RenderVerification(VerificationData{Token: token, TokenLifetime: 24, Code: testCode(t), CodeLifetime: testCodeLifetime})
if err != nil {
t.Fatalf("RenderVerification() error = %v", err)
}
@@ -102,7 +118,7 @@ func assertGolden(t *testing.T, path, got string) {
func TestRenderingCarriesKindAndVersion(t *testing.T) {
want := map[Kind]TemplateVersion{
KindVerification: "verification.v1",
KindVerification: "verification.v2",
KindPasswordReset: "password_reset.v1",
KindNotification: "notification.v1",
}
@@ -146,7 +162,7 @@ func TestLoopbackOriginKeepsPort(t *testing.T) {
if err != nil {
t.Fatalf("NewRenderer() error = %v", err)
}
mail, err := renderer.RenderVerification(VerificationData{Token: testToken(t), TokenLifetime: 24})
mail, err := renderer.RenderVerification(VerificationData{Token: testToken(t), TokenLifetime: 24, Code: testCode(t), CodeLifetime: testCodeLifetime})
if err != nil {
t.Fatalf("RenderVerification() error = %v", err)
}
@@ -251,18 +267,29 @@ func TestNotificationRejectsLinksOutsideTheApp(t *testing.T) {
func TestRejectsInvalidData(t *testing.T) {
renderer := newTestRenderer(t)
token := testToken(t)
code := testCode(t)
tokenCases := map[string]VerificationData{
"missing token": {TokenLifetime: 24},
"zero lifetime": {Token: token},
"negative": {Token: token, TokenLifetime: -1},
"lifetime too big": {Token: token, TokenLifetime: maxTokenLifetime + 1},
"missing token": {TokenLifetime: 24, Code: code, CodeLifetime: testCodeLifetime},
"zero lifetime": {Token: token, Code: code, CodeLifetime: testCodeLifetime},
"negative": {Token: token, TokenLifetime: -1, Code: code, CodeLifetime: testCodeLifetime},
"lifetime too big": {Token: token, TokenLifetime: maxTokenLifetime + 1, Code: code, CodeLifetime: testCodeLifetime},
"missing code": {Token: token, TokenLifetime: 24, CodeLifetime: testCodeLifetime},
"zero code lifetime": {Token: token, TokenLifetime: 24, Code: code},
"code lifetime seconds": {Token: token, TokenLifetime: 24, Code: code, CodeLifetime: testCodeLifetime + time.Second},
"code lifetime too long": {Token: token, TokenLifetime: 24, Code: code, CodeLifetime: 61 * time.Minute},
}
for name, data := range tokenCases {
if _, err := renderer.RenderVerification(data); !errors.Is(err, ErrInvalidTemplateData) {
t.Fatalf("RenderVerification(%s) error = %v", name, err)
}
reset := PasswordResetData(data)
if _, err := renderer.RenderPasswordReset(reset); !errors.Is(err, ErrInvalidTemplateData) {
}
resetCases := map[string]PasswordResetData{
"missing token": {TokenLifetime: 24},
"zero lifetime": {Token: token},
"negative": {Token: token, TokenLifetime: -1},
}
for name, data := range resetCases {
if _, err := renderer.RenderPasswordReset(data); !errors.Is(err, ErrInvalidTemplateData) {
t.Fatalf("RenderPasswordReset(%s) error = %v", name, err)
}
}
@@ -287,7 +314,7 @@ func TestRejectsInvalidData(t *testing.T) {
func TestLifetimeCopyUsesRussianPlural(t *testing.T) {
renderer := newTestRenderer(t)
for hours, want := range map[domain.Hours]string{1: "1 час.", 3: "3 часа.", 11: "11 часов.", 21: "21 час.", 24: "24 часа.", 48: "48 часов.", 168: "168 часов."} {
mail, err := renderer.RenderVerification(VerificationData{Token: testToken(t), TokenLifetime: hours})
mail, err := renderer.RenderVerification(VerificationData{Token: testToken(t), TokenLifetime: hours, Code: testCode(t), CodeLifetime: testCodeLifetime})
if err != nil {
t.Fatalf("RenderVerification(%d) error = %v", hours, err)
}
@@ -336,10 +363,10 @@ func TestContentHashPinsTheBody(t *testing.T) {
base Kind
}{
{"token", func() (RenderedMail, error) {
return renderer.RenderVerification(VerificationData{Token: otherToken, TokenLifetime: 24})
return renderer.RenderVerification(VerificationData{Token: otherToken, TokenLifetime: 24, Code: testCode(t), CodeLifetime: testCodeLifetime})
}, KindVerification},
{"lifetime", func() (RenderedMail, error) {
return renderer.RenderVerification(VerificationData{Token: testToken(t), TokenLifetime: 25})
return renderer.RenderVerification(VerificationData{Token: testToken(t), TokenLifetime: 25, Code: testCode(t), CodeLifetime: testCodeLifetime})
}, KindVerification},
{"body", func() (RenderedMail, error) {
data := hostileNotification()
@@ -376,7 +403,7 @@ func TestRenderingNeverReachesLogs(t *testing.T) {
slog.New(slog.NewTextHandler(&output, nil)),
slog.New(slog.NewJSONHandler(&output, nil)),
}
verification := VerificationData{Token: token, TokenLifetime: 24}
verification := VerificationData{Token: token, TokenLifetime: 24, Code: testCode(t), CodeLifetime: testCodeLifetime}
notification := hostileNotification()
// RenderedMail hides its content even in an unexported field.
// NotificationData redacts itself only where fmt can call its methods.