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.9 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 clientinfo

import "testing"

func TestParse(t *testing.T) {
	tests := []struct {
		name        string
		ua          string
		wantBrowser string
		wantOS      string
		wantDevice  string
	}{
		{
			name:        "chrome on macos",
			ua:          "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
			wantBrowser: "Chrome", wantOS: "macOS", wantDevice: "Desktop",
		},
		{
			// Edge carries both "Chrome" and "Safari" tokens; must resolve to Edge.
			name:        "edge on windows",
			ua:          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
			wantBrowser: "Edge", wantOS: "Windows", wantDevice: "Desktop",
		},
		{
			// Opera also carries a "Chrome" token.
			name:        "opera on windows",
			ua:          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 OPR/105.0.0.0",
			wantBrowser: "Opera", wantOS: "Windows", wantDevice: "Desktop",
		},
		{
			// Safari must not be misread as Chrome.
			name:        "safari on iphone",
			ua:          "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1",
			wantBrowser: "Safari", wantOS: "iOS", wantDevice: "Mobile",
		},
		{
			name:        "chrome on android",
			ua:          "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36",
			wantBrowser: "Chrome", wantOS: "Android", wantDevice: "Mobile",
		},
		{
			name:        "firefox on linux",
			ua:          "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0",
			wantBrowser: "Firefox", wantOS: "Linux", wantDevice: "Desktop",
		},
		{
			name:        "ipad is tablet",
			ua:          "Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/604.1",
			wantBrowser: "Safari", wantOS: "iOS", wantDevice: "Tablet",
		},
		{
			name:        "api client",
			ua:          "curl/8.4.0",
			wantBrowser: "API client", wantOS: "Unknown", wantDevice: "Bot",
		},
		{
			name:        "empty",
			ua:          "",
			wantBrowser: "Unknown", wantOS: "Unknown", wantDevice: "Unknown",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := Parse(tt.ua)
			if got.Browser != tt.wantBrowser {
				t.Errorf("Browser = %q, want %q", got.Browser, tt.wantBrowser)
			}
			if got.OS != tt.wantOS {
				t.Errorf("OS = %q, want %q", got.OS, tt.wantOS)
			}
			if got.Device != tt.wantDevice {
				t.Errorf("Device = %q, want %q", got.Device, tt.wantDevice)
			}
		})
	}
}

func TestLabelAndSignature(t *testing.T) {
	c := Client{Browser: "Chrome", OS: "macOS", Device: "Desktop"}
	if got := c.Label(); got != "Chrome on macOS" {
		t.Errorf("Label() = %q, want %q", got, "Chrome on macOS")
	}
	if got := c.Signature(); got != "Chrome|macOS" {
		t.Errorf("Signature() = %q, want %q", got, "Chrome|macOS")
	}
	if got := (Client{Browser: "Unknown", OS: "Unknown"}).Label(); got != "Unknown device" {
		t.Errorf("Label() = %q, want %q", got, "Unknown device")
	}
}