- Added rate limiting for outbound email sends to prevent abuse, implemented in `internal/api/mail/sendguard`. - Introduced idempotency key support for email sending to avoid duplicate submissions. - Enhanced attachment handling with new limits and validation in `internal/api/mail/limits`. - Updated outbox processing to include retry logic and circuit breaker for SMTP failures. - Improved HTML sanitization for email content to enhance security. - Added unit tests for new features, ensuring robust functionality and error handling. - Updated configuration options in `.env.example` for new mail settings.
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package limits
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateAttachmentSize(t *testing.T) {
|
|
t.Parallel()
|
|
if err := ValidateAttachmentSize(MaxAttachmentBytes); err != nil {
|
|
t.Fatalf("at limit: %v", err)
|
|
}
|
|
if err := ValidateAttachmentSize(MaxAttachmentBytes + 1); !errors.Is(err, ErrAttachmentTooLarge) {
|
|
t.Fatalf("over limit: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateAttachmentQuota(t *testing.T) {
|
|
t.Parallel()
|
|
cases := []struct {
|
|
name string
|
|
count int
|
|
total int64
|
|
newSize int64
|
|
want error
|
|
}{
|
|
{"ok", 0, 0, 1, nil},
|
|
{"at count", MaxAttachmentsPerMessage, 0, 1, ErrTooManyAttachments},
|
|
{"at total", 0, MaxTotalAttachmentsPerMessageBytes, 1, ErrAttachmentsTotalTooLarge},
|
|
{"would exceed total", 1, MaxTotalAttachmentsPerMessageBytes - 10, 11, ErrAttachmentsTotalTooLarge},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
err := ValidateAttachmentQuota(tc.count, tc.total, tc.newSize)
|
|
if !errors.Is(err, tc.want) {
|
|
t.Fatalf("got %v want %v", err, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|