sub_addr_test.go

 1package fetcher
 2
 3import (
 4	"testing"
 5
 6	"github.com/floatpane/matcha/config"
 7)
 8
 9func TestAddressMatches(t *testing.T) {
10	gmail := &config.Account{ServiceProvider: "gmail"}
11	custom := &config.Account{ServiceProvider: "custom"}
12
13	cases := []struct {
14		name      string
15		candidate string
16		fetch     string
17		account   *config.Account
18		want      bool
19	}{
20		{"exact match", "user@gmail.com", "user@gmail.com", gmail, true},
21		{"case insensitive", "User@Gmail.com", "user@gmail.com", gmail, true},
22		{"whitespace", "  user@gmail.com  ", "user@gmail.com", gmail, true},
23		{"gmail subaddress matches", "user+work@gmail.com", "user@gmail.com", gmail, true},
24		{"gmail subaddress on configured side", "user@gmail.com", "user+work@gmail.com", gmail, true},
25		{"gmail dots ignored", "u.s.e.r@gmail.com", "user@gmail.com", gmail, true},
26		{"gmail dots on configured side", "user@gmail.com", "u.ser@gmail.com", gmail, true},
27		{"gmail dots and subaddress combined", "u.ser+work@gmail.com", "user@gmail.com", gmail, true},
28		{"different local rejected", "other@gmail.com", "user@gmail.com", gmail, false},
29		{"different domain rejected", "user+x@example.com", "user@gmail.com", gmail, false},
30		{"non-gmail provider ignores plus", "user+work@example.com", "user@example.com", custom, false},
31		{"non-gmail provider keeps dots", "u.ser@example.com", "user@example.com", custom, false},
32		{"empty candidate", "", "user@gmail.com", gmail, false},
33		{"empty fetch", "user@gmail.com", "", gmail, false},
34		{"nil account exact still works", "user@example.com", "user@example.com", nil, true},
35	}
36
37	for _, tc := range cases {
38		t.Run(tc.name, func(t *testing.T) {
39			if got := addressMatches(tc.candidate, tc.fetch, tc.account); got != tc.want {
40				t.Errorf("addressMatches(%q, %q) = %v, want %v", tc.candidate, tc.fetch, got, tc.want)
41			}
42		})
43	}
44}
45
46func TestNormalizeGmailAddress(t *testing.T) {
47	cases := map[string]string{
48		"user+tag@gmail.com":     "user@gmail.com",
49		"user@gmail.com":         "user@gmail.com",
50		"user+a+b@example.com":   "user@example.com",
51		"u.s.e.r@gmail.com":      "user@gmail.com",
52		"u.ser+work@gmail.com":   "user@gmail.com",
53		"first.last@example.com": "firstlast@example.com",
54		"no-at-sign":             "no-at-sign",
55		"":                       "",
56	}
57	for in, want := range cases {
58		if got := normalizeGmailAddress(in); got != want {
59			t.Errorf("normalizeGmailAddress(%q) = %q, want %q", in, got, want)
60		}
61	}
62}