Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.
Go email-relayemail-routinghttp-to-smtpmail-deliverymailchimp-alternativemailgun-alternativemailsendermailservermailtemplatemailtestpostmark-alternativesend-emailsendgrid-alternativesendmailtransactional-emailtransactional-outbox
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package worker
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
"time"
"github.com/goposta/posta/internal/enterprise/enterprisetest"
"github.com/goposta/posta/internal/models"
)
type fakeRules struct {
rules []models.FormRule
hits [][]string
}
func (f *fakeRules) ListByForm(uint) ([]models.FormRule, error) { return f.rules, nil }
func (f *fakeRules) RecordHits(uuids []string, _ time.Time) error {
f.hits = append(f.hits, uuids)
return nil
}
type fakeDirectory struct{ ws uint }
func (f fakeDirectory) Members(uint) (map[uint]models.WorkspaceRole, error) {
return map[uint]models.WorkspaceRole{1: models.WorkspaceRoleOwner, 7: models.WorkspaceRoleEditor, 8: models.WorkspaceRoleEditor}, nil
}
func (f fakeDirectory) Team(id uint) (*models.Team, error) {
if id != 3 {
return nil, errors.New("not found")
}
return &models.Team{ID: 3, WorkspaceID: &f.ws, Email: "[email protected]", Members: []models.TeamMember{{UserID: 8}}}, nil
}
func salesRule(suppress bool) models.FormRule {
team, hook := uint(3), uint(9)
actions := models.RuleActions{
{Type: models.ActionNotifyTeam, TeamID: &team},
{Type: models.ActionAssignTeam, TeamID: &team},
{Type: models.ActionAssignMember, UserIDs: []uint{7}},
{Type: models.ActionAddTags, Tags: []string{"lead"}},
{Type: models.ActionSetPriority, Priority: "high"},
{Type: models.ActionWebhook, WebhookID: &hook},
}
if suppress {
actions = append(actions, models.RuleAction{Type: models.ActionSuppressDefault})
}
return models.FormRule{
UUID: "r-sales", Name: "Sales", Enabled: true,
Conditions: models.RuleConditions{{Field: "dept", Op: "eq", Value: "sales"}},
Actions: actions,
}
}
func routedFixture(suppress bool) (*MessageProcessHandler, *fakeMessageStore, *fakeDispatcher, *fakeNotifier, *fakeRules) {
ws := uint(5)
form := immediateForm()
form.WorkspaceID = &ws
form.Fields = models.FieldDefs{{Key: "dept", Label: "Dept", Type: models.FieldSelect, Options: []models.FieldOption{{Value: "sales"}}}}
store := &fakeMessageStore{msg: &models.Message{
ID: 1, WorkspaceID: &ws, Status: models.MessageStatusReceived,
Data: models.SubmissionData{"dept": "sales"}, Tags: []string{"existing"},
}}
h, d, n := newTestHandler(store, form)
rules := &fakeRules{rules: []models.FormRule{salesRule(suppress)}}
h.rules, h.directory, h.edition = rules, fakeDirectory{ws: ws}, enterprisetest.Licensed
return h, store, d, n, rules
}
func TestProcessTaskAppliesRulesOnce(t *testing.T) {
h, store, d, n, rules := routedFixture(false)
n.err = errors.New("smtp down")
if err := h.ProcessTask(context.Background(), processTask(t)); err == nil {
t.Fatal("a failed notification must fail the task")
}
m := store.msg
if m.RoutedAt == nil || !reflect.DeepEqual([]string(m.MatchedRules), []string{"r-sales"}) {
t.Fatalf("not routed: %+v", m)
}
routing := store.updates[0]
if routing["team_id"] != uint(3) || routing["assigned_to_id"] != uint(7) || routing["priority"] != "high" {
t.Errorf("routing update %+v", routing)
}
if tags := fmt.Sprint(routing["tags"]); tags != "[existing lead]" {
t.Errorf("tags %s", tags)
}
if !reflect.DeepEqual(d.direct, []uint{9}) {
t.Errorf("rule webhook dispatched to %v", d.direct)
}
// Default route (admins) plus the team: its member and its address.
// The owner is already among the admins.
if !reflect.DeepEqual(n.sent, []string{"admins", "to:[email protected]", "user"}) || !reflect.DeepEqual(n.users, []uint{8}) {
t.Errorf("sent %v users %v", n.sent, n.users)
}
// The retry notifies the same people, without routing or counting again.
n.err, n.sent, n.users = nil, nil, nil
updates := len(store.updates)
if err := h.ProcessTask(context.Background(), processTask(t)); err != nil {
t.Fatal(err)
}
if len(rules.hits) != 1 {
t.Errorf("rule hits recorded %d times", len(rules.hits))
}
if len(store.updates) != updates || len(d.direct) != 1 {
t.Errorf("the retry routed or dispatched again: %d updates, direct %v", len(store.updates)-updates, d.direct)
}
if !reflect.DeepEqual(n.sent, []string{"admins", "to:[email protected]", "user"}) {
t.Errorf("retry sent %v", n.sent)
}
}
func TestProcessTaskSuppressedDefault(t *testing.T) {
h, _, _, n, _ := routedFixture(true)
if err := h.ProcessTask(context.Background(), processTask(t)); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(n.sent, []string{"to:[email protected]", "user"}) {
t.Errorf("sent %v", n.sent)
}
}
func TestProcessTaskNoMatchKeepsDefault(t *testing.T) {
h, store, d, n, _ := routedFixture(true)
store.msg.Data = models.SubmissionData{"dept": "support"}
if err := h.ProcessTask(context.Background(), processTask(t)); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(n.sent, []string{"admins"}) || len(d.direct) != 0 || store.msg.TeamID != nil {
t.Errorf("unmatched submission: sent %v direct %v", n.sent, d.direct)
}
if store.msg.RoutedAt == nil {
t.Error("an unmatched submission is still marked routed")
}
}
func TestRulesNeedALicense(t *testing.T) {
// Without a license the rules are skipped: the form's recipients get the
// submission, and it stays unrouted for when a license is installed.
h, store, d, n, rules := routedFixture(false)
h.edition = enterprisetest.Community
if err := h.ProcessTask(context.Background(), processTask(t)); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(n.sent, []string{"admins"}) || len(d.direct) != 0 || len(rules.hits) != 0 {
t.Errorf("unlicensed: sent %v direct %v hits %v", n.sent, d.direct, rules.hits)
}
if m := store.msg; m.RoutedAt != nil || m.TeamID != nil || len(m.MatchedRules) != 0 {
t.Errorf("unlicensed submission was routed: %+v", m)
}
// An expired license keeps routing running.
h, store, _, _, _ = routedFixture(false)
h.edition = enterprisetest.Degraded
if err := h.ProcessTask(context.Background(), processTask(t)); err != nil {
t.Fatal(err)
}
if store.msg.RoutedAt == nil {
t.Error("a degraded license stopped routing")
}
}
func TestRulesNeverLowerPriority(t *testing.T) {
if higherPriority("high", "urgent") || !higherPriority("high", "") || higherPriority("", "low") {
t.Error("higherPriority")
}
if got := mergeTags([]string{"a"}, []string{"a", "b"}); !reflect.DeepEqual(got, []string{"a", "b"}) {
t.Errorf("mergeTags = %v", got)
}
}
func TestRedactPIIFromWebhook(t *testing.T) {
form := &models.Form{Fields: models.FieldDefs{
{Key: "email", Type: models.FieldEmail, Role: models.RoleSenderEmail, PII: true},
{Key: "salary", Type: models.FieldNumber, PII: true},
{Key: "dept", Type: models.FieldSelect, Role: models.RoleSubject},
}}
payload := func() MessageWebhookPayload {
return MessageWebhookPayload{
SenderEmail: "[email protected]", Subject: "sales",
Data: models.SubmissionData{"email": "[email protected]", "salary": float64(90000), "dept": "sales"},
Fields: []models.MessageField{{Key: "email", Value: "[email protected]"}, {Key: "salary", Value: "90000"}, {Key: "dept", Value: "Sales"}},
}
}
off := payload()
redactPII(&off, form)
if off.SenderEmail == "" || len(off.Data) != 3 || off.RedactedFields != nil {
t.Errorf("redaction is opt-in: %+v", off)
}
form.RedactPIIInWebhooks = true
on := payload()
redactPII(&on, form)
if on.SenderEmail != "" || on.Subject != "sales" {
t.Errorf("summary values: email %q subject %q", on.SenderEmail, on.Subject)
}
if !reflect.DeepEqual(on.Data, models.SubmissionData{"dept": "sales"}) || len(on.Fields) != 1 || on.Fields[0].Key != "dept" {
t.Errorf("answers: data %v fields %v", on.Data, on.Fields)
}
if !reflect.DeepEqual(on.RedactedFields, []string{"email", "salary"}) {
t.Errorf("redacted_fields %v", on.RedactedFields)
}
original := payload()
if original.Data["salary"] != float64(90000) {
t.Error("redaction changed the message itself")
}
}