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.0 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 verifier

import (
	"encoding/json"
	"testing"
)

func TestDecide(t *testing.T) {
	cases := []struct {
		name                              string
		syntaxOK, disposable, role, hasMX bool
		wantStatus                        Status
		wantScore                         int
	}{
		{"bad syntax", false, false, false, false, StatusInvalid, 0},
		{"disposable beats everything else", true, true, true, true, StatusDisposable, 10},
		{"no mx", true, false, false, false, StatusInvalid, 0},
		{"role account with mx", true, false, true, true, StatusRisky, 60},
		{"clean valid", true, false, false, true, StatusValid, 90},
		{"disposable even without mx", true, true, false, false, StatusDisposable, 10},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			status, score, _ := decide(c.syntaxOK, c.disposable, c.role, c.hasMX)
			if status != c.wantStatus || score != c.wantScore {
				t.Fatalf("decide() = (%s, %d), want (%s, %d)", status, score, c.wantStatus, c.wantScore)
			}
		})
	}
}

func TestIsDisposable(t *testing.T) {
	if !isDisposable("Mailinator.com") {
		t.Error("expected mailinator.com to be disposable (case-insensitive)")
	}
	if isDisposable("gmail.com") {
		t.Error("gmail.com should not be disposable")
	}
}

func TestIsRoleAccount(t *testing.T) {
	for _, local := range []string{"info", "ADMIN", "no-reply", "support"} {
		if !isRoleAccount(local) {
			t.Errorf("expected %q to be a role account", local)
		}
	}
	if isRoleAccount("jonas") {
		t.Error("jonas should not be a role account")
	}
}

func TestCacheKeys(t *testing.T) {
	if got := addrKey("[email protected]"); got != "verify:addr:[email protected]" {
		t.Errorf("addrKey = %q", got)
	}
	if got := mxKey("B.com"); got != "verify:mx:b.com" {
		t.Errorf("mxKey = %q, want lowercased", got)
	}
}

func TestResultJSONRoundTrip(t *testing.T) {
	r := &Result{
		Email:  "[email protected]",
		Status: StatusValid,
		Score:  90,
		Checks: Checks{Syntax: true, MX: true, SMTP: "skipped"},
	}
	b, err := json.Marshal(r)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	var back Result
	if err := json.Unmarshal(b, &back); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if back.Status != StatusValid || back.Score != 90 || !back.Checks.MX {
		t.Fatalf("round-trip mismatch: %+v", back)
	}
}