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.

5.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 inbound

import (
	"strings"
	"testing"
	"unicode/utf8"
)

func TestParsePlainText(t *testing.T) {
	raw := "From: [email protected]\r\n" +
		"To: [email protected]\r\n" +
		"Subject: Hello\r\n" +
		"Message-Id: <[email protected]>\r\n" +
		"Content-Type: text/plain; charset=UTF-8\r\n" +
		"\r\n" +
		"Hi Bob.\r\n"

	p, err := ParseRawEmail([]byte(raw))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if p.From != "[email protected]" {
		t.Errorf("From = %q, want [email protected]", p.From)
	}
	if len(p.To) != 1 || p.To[0] != "[email protected]" {
		t.Errorf("To = %v, want [[email protected]]", p.To)
	}
	if p.MessageID != "[email protected]" {
		t.Errorf("MessageID = %q, want [email protected]", p.MessageID)
	}
	if !strings.Contains(p.TextBody, "Hi Bob.") {
		t.Errorf("TextBody missing body content: %q", p.TextBody)
	}
}

func TestParseMultipartAlternative(t *testing.T) {
	raw := "From: [email protected]\r\n" +
		"To: [email protected]\r\n" +
		"Subject: Multi\r\n" +
		"Content-Type: multipart/alternative; boundary=\"xyz\"\r\n" +
		"\r\n" +
		"--xyz\r\n" +
		"Content-Type: text/plain\r\n\r\n" +
		"plain body\r\n" +
		"--xyz\r\n" +
		"Content-Type: text/html\r\n\r\n" +
		"<p>html body</p>\r\n" +
		"--xyz--\r\n"

	p, err := ParseRawEmail([]byte(raw))
	if err != nil {
		t.Fatalf("parse: %v", err)
	}
	if !strings.Contains(p.TextBody, "plain body") {
		t.Errorf("TextBody = %q", p.TextBody)
	}
	if !strings.Contains(p.HTMLBody, "html body") {
		t.Errorf("HTMLBody = %q", p.HTMLBody)
	}
}

func TestParseMultipartWithAttachment(t *testing.T) {
	raw := "From: [email protected]\r\n" +
		"To: [email protected]\r\n" +
		"Subject: Attach\r\n" +
		"Content-Type: multipart/mixed; boundary=\"B\"\r\n" +
		"\r\n" +
		"--B\r\n" +
		"Content-Type: text/plain\r\n\r\n" +
		"hi\r\n" +
		"--B\r\n" +
		"Content-Type: application/octet-stream; name=\"x.bin\"\r\n" +
		"Content-Disposition: attachment; filename=\"x.bin\"\r\n" +
		"Content-Transfer-Encoding: base64\r\n\r\n" +
		"aGVsbG8=\r\n" +
		"--B--\r\n"

	p, err := ParseRawEmail([]byte(raw))
	if err != nil {
		t.Fatalf("parse: %v", err)
	}
	if len(p.Attachments) != 1 {
		t.Fatalf("attachments = %d, want 1", len(p.Attachments))
	}
	att := p.Attachments[0]
	if att.Filename != "x.bin" {
		t.Errorf("filename = %q, want x.bin", att.Filename)
	}
	if string(att.Content) != "hello" {
		t.Errorf("decoded content = %q, want hello", string(att.Content))
	}
}

func TestParseISO88591Body(t *testing.T) {
	body := []byte{'G', 'r', 0xfc, 0xdf, 'e', '\r', '\n'}
	raw := append([]byte(
		"From: [email protected]\r\n"+
			"To: [email protected]\r\n"+
			"Subject: Hi\r\n"+
			"Content-Type: text/plain; charset=iso-8859-1\r\n"+
			"\r\n"), body...)

	p, err := ParseRawEmail(raw)
	if err != nil {
		t.Fatalf("parse: %v", err)
	}
	if !utf8.ValidString(p.TextBody) {
		t.Errorf("TextBody is not valid UTF-8: %q", p.TextBody)
	}
	if !strings.Contains(p.TextBody, "Grüße") {
		t.Errorf("TextBody = %q, want to contain Grüße", p.TextBody)
	}
}

func TestParseISO88591EncodedHeader(t *testing.T) {
	raw := "From: [email protected]\r\n" +
		"To: [email protected]\r\n" +
		"Subject: =?iso-8859-1?Q?Gr=FC=DFe?=\r\n" +
		"Content-Type: text/plain; charset=UTF-8\r\n" +
		"\r\n" +
		"hi\r\n"

	p, err := ParseRawEmail([]byte(raw))
	if err != nil {
		t.Fatalf("parse: %v", err)
	}
	if p.Subject != "Grüße" {
		t.Errorf("Subject = %q, want Grüße", p.Subject)
	}
}

func TestParseUndeclaredNonUTF8BodyFallsBackToLatin1(t *testing.T) {
	body := []byte{0xdf, 0x65, '\r', '\n'}
	raw := append([]byte(
		"From: [email protected]\r\n"+
			"To: [email protected]\r\n"+
			"Subject: Hi\r\n"+
			"Content-Type: text/plain\r\n"+
			"\r\n"), body...)

	p, err := ParseRawEmail(raw)
	if err != nil {
		t.Fatalf("parse: %v", err)
	}
	if !utf8.ValidString(p.TextBody) {
		t.Errorf("TextBody is not valid UTF-8: %q", p.TextBody)
	}
}

func TestParseQuotedPrintable(t *testing.T) {
	raw := "From: [email protected]\r\n" +
		"To: [email protected]\r\n" +
		"Subject: =?UTF-8?Q?Caf=C3=A9?=\r\n" +
		"Content-Type: text/plain; charset=UTF-8\r\n" +
		"Content-Transfer-Encoding: quoted-printable\r\n" +
		"\r\n" +
		"Caf=C3=A9\r\n"

	p, err := ParseRawEmail([]byte(raw))
	if err != nil {
		t.Fatalf("parse: %v", err)
	}
	if p.Subject != "Café" {
		t.Errorf("Subject = %q, want Café", p.Subject)
	}
	if !strings.Contains(p.TextBody, "Café") {
		t.Errorf("TextBody = %q, want to contain Café", p.TextBody)
	}
}