1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
package main
// Naming a contact, and saying when a name is not unique.
//
// The duplicate report is the part worth testing rather than asserting: it is
// the one thing standing between "assigning a name already in use is allowed"
// and "assigning a name already in use happens in silence", and mailweb(1) has
// described it since petnames landed.
import (
"strings"
"testing"
)
func TestSharersOf(t *testing.T) {
// ~klara names two addresses, which is the case the notice exists for: one
// correspondent with a work address and a personal one.
petnames := map[string]string{
"klara@example.org": "klara",
"klara@work.example": "klara",
"k@third.example": "klara",
"bob@example.org": "bob",
}
for _, c := range []struct {
address string
want []string
}{
// Sorted, and never including the address being asked about: the page
// says "other addresses", and listing the one on screen among them
// would misstate the count.
{"klara@example.org", []string{"k@third.example", "klara@work.example"}},
{"klara@work.example", []string{"k@third.example", "klara@example.org"}},
{"k@third.example", []string{"klara@example.org", "klara@work.example"}},
// A unique name shares with nobody, and the notice never renders.
{"bob@example.org", nil},
// An address with no petname at all: not "everything unnamed", which
// is what keying on the empty string would produce.
{"stranger@example.org", nil},
} {
got := sharersOf(petnames, c.address)
if len(got) != len(c.want) {
t.Errorf("sharersOf(%q) = %v, want %v", c.address, got, c.want)
continue
}
for i := range got {
if got[i] != c.want[i] {
t.Errorf("sharersOf(%q) = %v, want %v", c.address, got, c.want)
break
}
}
}
}
// TestSharersOfCanonicalises checks the lookup routes through contactAddress,
// as every other use of a contact address must: a caller holding the address
// as typed must get the same answer as one holding it canonical.
func TestSharersOfCanonicalises(t *testing.T) {
petnames := map[string]string{
"klara@example.org": "klara",
"klara@work.example": "klara",
}
if got := sharersOf(petnames, " Klara@Example.ORG "); len(got) != 1 {
t.Errorf("sharersOf with an uncanonical address = %v, want one sharer", got)
}
}
// TestSafeReturn checks that the pen's way back cannot lead off this machine.
//
// The value is rendered as an href the reader is invited to click, so an
// unchecked one turns a typed URL into a link that appears to come from
// mailweb and goes somewhere else.
func TestSafeReturn(t *testing.T) {
for in, want := range map[string]string{
"/contacts": "/contacts",
"/contacts?show=hidden": "/contacts?show=hidden",
"/contact/a%40b.com": "/contact/a%40b.com",
"": "/contacts",
"//evil.example": "/contacts",
"https://evil.example": "/contacts",
"javascript:alert(1)": "/contacts",
"contacts": "/contacts",
} {
if got := safeReturn(in); got != want {
t.Errorf("safeReturn(%q) = %q, want %q", in, got, want)
}
}
}
// TestContactSettingsPage renders the page and checks it says the things it
// exists to say.
func TestContactSettingsPage(t *testing.T) {
accountName = "test"
out := renderTemplate(t, "contactsettings", contactSettingsData{
Contact: "klara@example.org",
Name: resolveAddress(map[string]string{"klara@example.org": "klara"}, "klara@example.org", "Klara Meyer"),
AddrEscaped: "klara%40example.org",
Sharers: []string{"klara@work.example"},
MessageCount: 12,
Return: "/contacts?show=hidden",
})
for _, want := range []string{
// The name, and the sender's claim marked as a claim rather than
// replaced by the petname.
"klara",
"Klara Meyer",
// The duplicate notice, with its count.
"1 other address",
"klara@work.example",
// The way back is where the reader came from, not the bare listing.
`href="/contacts?show=hidden"`,
// The petname form posts to the route, and asks to come back here.
`action="/contact/klara%40example.org/petname"`,
`name="return" value="settings"`,
} {
if !strings.Contains(out, want) {
t.Errorf("settings page should contain %q", want)
}
}
// Reporting spam is a link to a form, never a button that files one: the
// act hands something to a third party under the account owner's name.
if strings.Contains(out, `action="/contact/klara%40example.org/report-spam"`) {
t.Error("the settings page must link to the spam form, never post to it")
}
if !strings.Contains(out, `href="/contact/klara%40example.org/report-spam"`) {
t.Error("the settings page should link to the spam report form")
}
}
// TestContactSettingsNoSharersNotice checks the notice is absent when the name
// is unique, since it reads as a warning and must not appear for the ordinary
// case.
func TestContactSettingsNoSharersNotice(t *testing.T) {
accountName = "test"
out := renderTemplate(t, "contactsettings", contactSettingsData{
Contact: "bob@example.org",
Name: resolveAddress(nil, "bob@example.org", "Bob"),
AddrEscaped: "bob%40example.org",
Return: "/contacts",
})
if strings.Contains(out, "other address") {
t.Error("a unique petname should raise no duplicate notice")
}
}
// TestListingsLinkToSettings checks the pen replaced the inline petname field
// in both places it used to live, and that neither still posts a name.
func TestListingsLinkToSettings(t *testing.T) {
accountName = "test"
myAddress = "me@example.org"
myAddresses = map[string]bool{myAddress: true}
msg := testMsgRow()
contacts := renderTemplate(t, "contacts", contactsPageData{
Contacts: []contactEntry{{
Address: "klara@example.org",
AddrEscaped: "klara%40example.org",
Name: msg.From,
LastSeen: msg.Date,
Count: 3,
}},
ReturnURL: "/contacts?show=hidden",
})
detail := renderTemplate(t, "contactdetail", contactDetailData{
Contact: "klara@example.org", Name: msg.From,
AddrEscaped: "klara%40example.org", Messages: []msgRow{msg},
})
for name, out := range map[string]string{"contacts": contacts, "detail": detail} {
if !strings.Contains(out, "/contact/klara%40example.org/settings") {
t.Errorf("%s: should link to the settings page", name)
}
// The inline forms are gone; naming happens in one place.
if strings.Contains(out, `action="/contact/klara%40example.org/petname"`) {
t.Errorf("%s: should no longer carry an inline petname form", name)
}
}
// The listing's pen returns to the listing as it was being read, or
// ?show=hidden is lost on the way back. html/template escapes the query
// value with lowercase hex, hence the fold: what is being checked is that
// the parameter is there and escaped, not which case the escaper picks.
if !strings.Contains(strings.ToLower(contacts), "return=%2fcontacts%3fshow%3dhidden") {
t.Error("the listing's pen should carry the listing it was clicked from")
}
}
|