69 lines
2.5 KiB
Go
69 lines
2.5 KiB
Go
package domain
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func testMailRequest() MailRequest {
|
|
return MailRequest{
|
|
ID: uuid.MustParse("01926f3a-7c00-7000-8000-000000000101"),
|
|
RecipientUserID: uuid.MustParse("01926f3a-7c00-7000-8000-000000000102"),
|
|
Kind: MailKindNotification,
|
|
Notification: NotificationText{Subject: "PRIVATE-SUBJECT", Body: "PRIVATE-BODY", LinkPath: "/candidate/invitations"},
|
|
CreatedAt: time.Date(2026, 10, 6, 9, 0, 0, 0, time.UTC),
|
|
}
|
|
}
|
|
|
|
func TestNewMailRequestChecksTheStorageRules(t *testing.T) {
|
|
if _, err := NewMailRequest(testMailRequest()); err != nil {
|
|
t.Fatalf("NewMailRequest() error = %v", err)
|
|
}
|
|
tests := map[string]func(*MailRequest){
|
|
"nil ID": func(request *MailRequest) { request.ID = uuid.Nil },
|
|
"nil recipient": func(request *MailRequest) { request.RecipientUserID = uuid.Nil },
|
|
"token mail kind": func(request *MailRequest) { request.Kind = MailKindVerification },
|
|
"zero creation time": func(request *MailRequest) { request.CreatedAt = time.Time{} },
|
|
"NUL in the body": func(request *MailRequest) { request.Notification.Body += "\x00" },
|
|
"invalid UTF-8": func(request *MailRequest) { request.Notification.Subject = "\xff" },
|
|
"oversized body": func(request *MailRequest) {
|
|
request.Notification.Body = strings.Repeat("я", maxMailRequestBodyRunes+1)
|
|
},
|
|
"oversized link": func(request *MailRequest) {
|
|
request.Notification.LinkPath = "/" + strings.Repeat("a", maxMailRequestLinkPathSize)
|
|
},
|
|
}
|
|
for name, change := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
request := testMailRequest()
|
|
change(&request)
|
|
if _, err := NewMailRequest(request); !errors.Is(err, ErrInvalidMailRequest) {
|
|
t.Fatalf("NewMailRequest() error = %v, want ErrInvalidMailRequest", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMailRequestNeverPrintsItsText(t *testing.T) {
|
|
request := testMailRequest()
|
|
var output bytes.Buffer
|
|
for _, format := range []string{"%v", "%+v", "%#v", "%s"} {
|
|
fmt.Fprintf(&output, format+"\n", request)
|
|
fmt.Fprintf(&output, format+"\n", &request)
|
|
}
|
|
type holder struct{ Request MailRequest }
|
|
for _, logger := range []*slog.Logger{slog.New(slog.NewJSONHandler(&output, nil)), slog.New(slog.NewTextHandler(&output, nil))} {
|
|
logger.Info("request", "request", request, "holder", holder{Request: request})
|
|
}
|
|
if printed := output.String(); strings.Contains(printed, "PRIVATE") {
|
|
t.Fatalf("printed mail request contains its text:\n%s", printed)
|
|
}
|
|
}
|