Skip to content

armgit / posta

Public Imported from GitHub
0

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.

3.1 KB
Raw Blame
// 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 email

import "testing"

func TestNormalizeDomain(t *testing.T) {
	cases := map[string]string{
		"[email protected]":                 "gmail.com",
		"[email protected]":                 "gmail.com",
		`"Jonas" <[email protected]>`:       "gmail.com",
		`Jonas Kaninda <[email protected]>`: "gmail.com",
		"  [email protected]  ":           "outlook.com",
		"no-at-sign":                  "",
		"":                            "",
		"weird<[email protected]>trail":     "gmail.com",
	}
	for in, want := range cases {
		if got := NormalizeDomain(in); got != want {
			t.Errorf("NormalizeDomain(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestClassifyProvider(t *testing.T) {
	cases := map[string]string{
		"[email protected]":       "Gmail",
		"[email protected]":  "Gmail",
		"[email protected]":    "Google Workspace",
		"[email protected]": "Google Workspace",
		"[email protected]": "Google Workspace",
		"[email protected]":     "Outlook",
		"[email protected]":   "Outlook",
		"[email protected]":        "Yahoo",
		"[email protected]":      "Apple iCloud",
		"[email protected]":       "Proton",
		"[email protected]":           "Proton",
		"[email protected]":         "AOL",
		"[email protected]":          "GMX",
		"[email protected]":        "Zoho",
		"[email protected]":     "Fastmail",
		"[email protected]":       "Yandex",
		"[email protected]":     "Other",
		"":                  "Other",
		"bad-address":       "Other",
		`"J" <[email protected]>`: "Gmail",
	}
	for in, want := range cases {
		if got := ClassifyProvider(in); got != want {
			t.Errorf("ClassifyProvider(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestClassifyRecipients(t *testing.T) {
	cases := []struct {
		in   []string
		want string
	}{
		{[]string{"[email protected]"}, "Gmail"},
		{[]string{"[email protected]", "[email protected]"}, "Other"}, // first parseable wins
		{[]string{"", "[email protected]"}, "Yahoo"},
		{nil, "Other"},
		{[]string{}, "Other"},
		{[]string{""}, "Other"},
	}
	for _, tc := range cases {
		if got := ClassifyRecipients(tc.in); got != tc.want {
			t.Errorf("ClassifyRecipients(%v) = %q, want %q", tc.in, got, tc.want)
		}
	}
}

// Defense against the original bug: RFC 5322 display names must not leak into
// the domain column. With the old split_part SQL, this produced "gmail.com>".
func TestRFC5322DisplayNameIsStripped(t *testing.T) {
	got := NormalizeDomain(`"Jonas Kaninda" <[email protected]>`)
	if got != "gmail.com" {
		t.Fatalf("display-name stripping failed: got %q, want %q", got, "gmail.com")
	}
}