| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
"heckel.io/ntfy/v2/model" |
| 11 |
"heckel.io/ntfy/v2/user" |
| 12 |
"heckel.io/ntfy/v2/util" |
| 13 |
) |
| 14 |
|
| 15 |
// captureMailer is a fake mailer that records the magic links it is asked to send, so tests can |
| 16 |
// "click" them without a real SMTP server. The notification side is a no-op. |
| 17 |
type captureMailer struct { |
| 18 |
verifyLinks map[string]string // email -> verification link |
| 19 |
resetLinks map[string]string // email -> reset link |
| 20 |
} |
| 21 |
|
| 22 |
func newCaptureMailer() *captureMailer { |
| 23 |
return &captureMailer{verifyLinks: map[string]string{}, resetLinks: map[string]string{}} |
| 24 |
} |
| 25 |
|
| 26 |
func (c *captureMailer) SendEmailVerification(to, link string) error { |
| 27 |
c.verifyLinks[to] = link |
| 28 |
return nil |
| 29 |
} |
| 30 |
|
| 31 |
func (c *captureMailer) SendPasswordReset(to, link string) error { |
| 32 |
c.resetLinks[to] = link |
| 33 |
return nil |
| 34 |
} |
| 35 |
|
| 36 |
func (c *captureMailer) SendNotification(to string, m *model.Message, senderIP string) error { |
| 37 |
return nil |
| 38 |
} |
| 39 |
|
| 40 |
func (c *captureMailer) NotificationCounts() (total int64, success int64, failure int64) { |
| 41 |
return 0, 0, 0 |
| 42 |
} |
| 43 |
|
| 44 |
// newEmailTestServer creates a server with email sending "enabled" (SMTP + base-url configured) |
| 45 |
// and a capturing mailer injected, plus a tier-less user "ben" logged in via basic auth. |
| 46 |
func newEmailTestServer(t *testing.T, databaseURL string) (*Server, *captureMailer, map[string]string) { |
| 47 |
conf := newTestConfigWithAuthFile(t, databaseURL) |
| 48 |
conf.SMTPSenderAddr = "localhost:25" |
| 49 |
conf.SMTPSenderFrom = "[email protected]" |
| 50 |
conf.BaseURL = "https://ntfy.example.com" |
| 51 |
s := newTestServer(t, conf) |
| 52 |
mailer := newCaptureMailer() |
| 53 |
s.mailer = mailer |
| 54 |
require.Nil(t, s.userManager.AddUser("ben", "ben", user.RoleUser, false)) |
| 55 |
auth := map[string]string{"Authorization": util.BasicAuth("ben", "ben")} |
| 56 |
return s, mailer, auth |
| 57 |
} |
| 58 |
|
| 59 |
func getAccount(t *testing.T, s *Server, auth map[string]string) *apiAccountResponse { |
| 60 |
rr := request(t, s, "GET", "/v1/account", "", auth) |
| 61 |
require.Equal(t, 200, rr.Code) |
| 62 |
account, err := util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body)) |
| 63 |
require.Nil(t, err) |
| 64 |
return account |
| 65 |
} |
| 66 |
|
| 67 |
// verifiedAddrs / pendingAddrs / primaryAddr extract the addresses from the structured email |
| 68 |
// list returned by GET /v1/account, so assertions stay readable. |
| 69 |
func verifiedAddrs(account *apiAccountResponse) []string { |
| 70 |
addrs := make([]string, 0) |
| 71 |
for _, e := range account.Emails { |
| 72 |
if !e.Pending { |
| 73 |
addrs = append(addrs, e.Address) |
| 74 |
} |
| 75 |
} |
| 76 |
return addrs |
| 77 |
} |
| 78 |
|
| 79 |
func pendingAddrs(account *apiAccountResponse) []string { |
| 80 |
addrs := make([]string, 0) |
| 81 |
for _, e := range account.Emails { |
| 82 |
if e.Pending { |
| 83 |
addrs = append(addrs, e.Address) |
| 84 |
} |
| 85 |
} |
| 86 |
return addrs |
| 87 |
} |
| 88 |
|
| 89 |
func primaryAddr(account *apiAccountResponse) string { |
| 90 |
for _, e := range account.Emails { |
| 91 |
if e.Primary { |
| 92 |
return e.Address |
| 93 |
} |
| 94 |
} |
| 95 |
return "" |
| 96 |
} |
| 97 |
|
| 98 |
func tokenFromLink(t *testing.T, link, prefix string) string { |
| 99 |
require.True(t, strings.HasPrefix(link, prefix), "link %q missing prefix %q", link, prefix) |
| 100 |
return strings.TrimPrefix(link, prefix) |
| 101 |
} |
| 102 |
|
| 103 |
func TestAccount_Email_AddVerifySetsPrimary(t *testing.T) { |
| 104 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 105 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 106 |
defer s.closeDatabases() |
| 107 |
|
| 108 |
// Start verification |
| 109 |
rr := request(t, s, "PUT", "/v1/account/email", `{"email":"[email protected]"}`, auth) |
| 110 |
require.Equal(t, 200, rr.Code) |
| 111 |
|
| 112 |
// Pending, not yet verified, no primary |
| 113 |
account := getAccount(t, s, auth) |
| 114 |
require.Equal(t, []string{"[email protected]"}, pendingAddrs(account)) |
| 115 |
require.Empty(t, verifiedAddrs(account)) |
| 116 |
require.Equal(t, "", primaryAddr(account)) |
| 117 |
|
| 118 |
// "Click" the captured link (unauthenticated POST) |
| 119 |
token := tokenFromLink(t, mailer.verifyLinks["[email protected]"], "https://ntfy.example.com/account/email/verify/") |
| 120 |
rr = request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, token), nil) |
| 121 |
require.Equal(t, 200, rr.Code) |
| 122 |
|
| 123 |
// Now verified + primary, no longer pending |
| 124 |
account = getAccount(t, s, auth) |
| 125 |
require.Equal(t, []string{"[email protected]"}, verifiedAddrs(account)) |
| 126 |
require.Equal(t, "[email protected]", primaryAddr(account)) |
| 127 |
require.Empty(t, pendingAddrs(account)) |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
func TestAccount_Email_VerifyInvalidToken(t *testing.T) { |
| 132 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 133 |
s, _, _ := newEmailTestServer(t, databaseURL) |
| 134 |
defer s.closeDatabases() |
| 135 |
|
| 136 |
rr := request(t, s, "POST", "/v1/account/email/verify", `{"token":"doesnotexist"}`, nil) |
| 137 |
require.Equal(t, 400, rr.Code) |
| 138 |
require.Equal(t, 40051, toHTTPError(t, rr.Body.String()).Code) |
| 139 |
|
| 140 |
// Empty token also rejected |
| 141 |
rr = request(t, s, "POST", "/v1/account/email/verify", `{"token":""}`, nil) |
| 142 |
require.Equal(t, 400, rr.Code) |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
func TestAccount_Email_DeletePending(t *testing.T) { |
| 147 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 148 |
s, _, auth := newEmailTestServer(t, databaseURL) |
| 149 |
defer s.closeDatabases() |
| 150 |
|
| 151 |
require.Equal(t, 200, request(t, s, "PUT", "/v1/account/email", `{"email":"[email protected]"}`, auth).Code) |
| 152 |
require.Equal(t, []string{"[email protected]"}, pendingAddrs(getAccount(t, s, auth))) |
| 153 |
|
| 154 |
// Deleting the pending address clears it (no verification ever happened) |
| 155 |
require.Equal(t, 200, request(t, s, "DELETE", "/v1/account/email", `{"email":"[email protected]"}`, auth).Code) |
| 156 |
account := getAccount(t, s, auth) |
| 157 |
require.Empty(t, pendingAddrs(account)) |
| 158 |
require.Empty(t, verifiedAddrs(account)) |
| 159 |
}) |
| 160 |
} |
| 161 |
|
| 162 |
func TestAccount_Email_Resend(t *testing.T) { |
| 163 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 164 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 165 |
defer s.closeDatabases() |
| 166 |
|
| 167 |
require.Equal(t, 200, request(t, s, "PUT", "/v1/account/email", `{"email":"[email protected]"}`, auth).Code) |
| 168 |
firstLink := mailer.verifyLinks["[email protected]"] |
| 169 |
require.NotEmpty(t, firstLink) |
| 170 |
|
| 171 |
// Resend issues a fresh link (the old one is replaced) |
| 172 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/resend", `{"email":"[email protected]"}`, auth).Code) |
| 173 |
require.NotEqual(t, firstLink, mailer.verifyLinks["[email protected]"]) |
| 174 |
|
| 175 |
// The old token no longer verifies; the new one does |
| 176 |
oldToken := tokenFromLink(t, firstLink, "https://ntfy.example.com/account/email/verify/") |
| 177 |
require.Equal(t, 400, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, oldToken), nil).Code) |
| 178 |
newToken := tokenFromLink(t, mailer.verifyLinks["[email protected]"], "https://ntfy.example.com/account/email/verify/") |
| 179 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, newToken), nil).Code) |
| 180 |
|
| 181 |
// Resending for a non-pending address is rejected |
| 182 |
require.Equal(t, 400, request(t, s, "POST", "/v1/account/email/resend", `{"email":"[email protected]"}`, auth).Code) |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
func TestAccount_Email_SetPrimaryCollision(t *testing.T) { |
| 187 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 188 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 189 |
defer s.closeDatabases() |
| 190 |
|
| 191 |
// ben verifies shared@ -> becomes his primary |
| 192 |
require.Equal(t, 200, request(t, s, "PUT", "/v1/account/email", `{"email":"[email protected]"}`, auth).Code) |
| 193 |
benToken := tokenFromLink(t, mailer.verifyLinks["[email protected]"], "https://ntfy.example.com/account/email/verify/") |
| 194 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, benToken), nil).Code) |
| 195 |
require.Equal(t, "[email protected]", primaryAddr(getAccount(t, s, auth))) |
| 196 |
|
| 197 |
// alice verifies the same address -> allowed as secondary, but it is not her primary |
| 198 |
require.Nil(t, s.userManager.AddUser("alice", "alice", user.RoleUser, false)) |
| 199 |
aliceAuth := map[string]string{"Authorization": util.BasicAuth("alice", "alice")} |
| 200 |
require.Equal(t, 200, request(t, s, "PUT", "/v1/account/email", `{"email":"[email protected]"}`, aliceAuth).Code) |
| 201 |
aliceToken := tokenFromLink(t, mailer.verifyLinks["[email protected]"], "https://ntfy.example.com/account/email/verify/") |
| 202 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, aliceToken), nil).Code) |
| 203 |
aliceAccount := getAccount(t, s, aliceAuth) |
| 204 |
require.Equal(t, []string{"[email protected]"}, verifiedAddrs(aliceAccount)) |
| 205 |
require.Equal(t, "", primaryAddr(aliceAccount)) |
| 206 |
|
| 207 |
// alice trying to promote it to primary collides with ben's |
| 208 |
rr := request(t, s, "POST", "/v1/account/email/primary", `{"email":"[email protected]"}`, aliceAuth) |
| 209 |
require.Equal(t, 409, rr.Code) |
| 210 |
require.Equal(t, 40908, toHTTPError(t, rr.Body.String()).Code) |
| 211 |
}) |
| 212 |
} |
| 213 |
|
| 214 |
// verifyEmailFor runs the full add->click flow so the user ends up with a verified primary email. |
| 215 |
func verifyEmailFor(t *testing.T, s *Server, mailer *captureMailer, auth map[string]string, email string) { |
| 216 |
require.Equal(t, 200, request(t, s, "PUT", "/v1/account/email", fmt.Sprintf(`{"email":"%s"}`, email), auth).Code) |
| 217 |
token := tokenFromLink(t, mailer.verifyLinks[email], "https://ntfy.example.com/account/email/verify/") |
| 218 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, token), nil).Code) |
| 219 |
} |
| 220 |
|
| 221 |
// canLogin returns true if username/password authenticates (via the token-create endpoint). |
| 222 |
func canLogin(t *testing.T, s *Server, username, password string) bool { |
| 223 |
rr := request(t, s, "POST", "/v1/account/token", "", map[string]string{"Authorization": util.BasicAuth(username, password)}) |
| 224 |
return rr.Code == 200 |
| 225 |
} |
| 226 |
|
| 227 |
func TestAccount_LoginByPrimaryEmail(t *testing.T) { |
| 228 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 229 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 230 |
defer s.closeDatabases() |
| 231 |
verifyEmailFor(t, s, mailer, auth, "[email protected]") |
| 232 |
|
| 233 |
// Basic Auth works with either the username or the verified primary email |
| 234 |
require.True(t, canLogin(t, s, "ben", "ben")) |
| 235 |
require.True(t, canLogin(t, s, "[email protected]", "ben")) |
| 236 |
|
| 237 |
// ...but not with the wrong password or an unknown email |
| 238 |
require.False(t, canLogin(t, s, "[email protected]", "wrong")) |
| 239 |
require.False(t, canLogin(t, s, "[email protected]", "ben")) |
| 240 |
}) |
| 241 |
} |
| 242 |
|
| 243 |
func TestAccount_PasswordReset_ByUsername(t *testing.T) { |
| 244 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 245 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 246 |
defer s.closeDatabases() |
| 247 |
verifyEmailFor(t, s, mailer, auth, "[email protected]") |
| 248 |
|
| 249 |
// Request reset by username |
| 250 |
rr := request(t, s, "POST", "/v1/account/password/reset/request", `{"identifier":"ben"}`, nil) |
| 251 |
require.Equal(t, 200, rr.Code) |
| 252 |
token := tokenFromLink(t, mailer.resetLinks["[email protected]"], "https://ntfy.example.com/account/password/reset/") |
| 253 |
|
| 254 |
// Confirm with a new password |
| 255 |
rr = request(t, s, "POST", "/v1/account/password/reset", fmt.Sprintf(`{"token":"%s","password":"brandnew"}`, token), nil) |
| 256 |
require.Equal(t, 200, rr.Code) |
| 257 |
|
| 258 |
require.True(t, canLogin(t, s, "ben", "brandnew")) |
| 259 |
require.False(t, canLogin(t, s, "ben", "ben")) |
| 260 |
}) |
| 261 |
} |
| 262 |
|
| 263 |
func TestAccount_PasswordReset_ByEmail(t *testing.T) { |
| 264 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 265 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 266 |
defer s.closeDatabases() |
| 267 |
verifyEmailFor(t, s, mailer, auth, "[email protected]") |
| 268 |
|
| 269 |
rr := request(t, s, "POST", "/v1/account/password/reset/request", `{"identifier":"[email protected]"}`, nil) |
| 270 |
require.Equal(t, 200, rr.Code) |
| 271 |
token := tokenFromLink(t, mailer.resetLinks["[email protected]"], "https://ntfy.example.com/account/password/reset/") |
| 272 |
rr = request(t, s, "POST", "/v1/account/password/reset", fmt.Sprintf(`{"token":"%s","password":"brandnew"}`, token), nil) |
| 273 |
require.Equal(t, 200, rr.Code) |
| 274 |
require.True(t, canLogin(t, s, "ben", "brandnew")) |
| 275 |
}) |
| 276 |
} |
| 277 |
|
| 278 |
func TestAccount_PasswordReset_EmailLookalikeUsernameDoesNotShadow(t *testing.T) { |
| 279 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 280 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 281 |
defer s.closeDatabases() |
| 282 |
|
| 283 |
// Account A (the email owner): user "ben" with verified primary email "[email protected]" |
| 284 |
verifyEmailFor(t, s, mailer, auth, "[email protected]") |
| 285 |
|
| 286 |
// Account B (the squatter): a different account whose USERNAME looks like A's email, with |
| 287 |
// its own, different verified primary email |
| 288 |
require.Nil(t, s.userManager.AddUser("[email protected]", "squatterpass", user.RoleUser, false)) |
| 289 |
squatter, err := s.userManager.User("[email protected]") |
| 290 |
require.Nil(t, err) |
| 291 |
require.Nil(t, s.userManager.AddEmail(squatter.ID, "[email protected]")) |
| 292 |
require.Nil(t, s.userManager.SetPrimaryEmail(squatter.ID, "[email protected]")) |
| 293 |
|
| 294 |
// Reset by the ambiguous identifier: the verified email must win over the look-alike username |
| 295 |
rr := request(t, s, "POST", "/v1/account/password/reset/request", `{"identifier":"[email protected]"}`, nil) |
| 296 |
require.Equal(t, 200, rr.Code) |
| 297 |
require.NotEmpty(t, mailer.resetLinks["[email protected]"]) // sent to the email owner (account A) |
| 298 |
require.Empty(t, mailer.resetLinks["[email protected]"]) // NOT the username squatter (account B) |
| 299 |
|
| 300 |
// The token resets account A (ben); the squatter's password is untouched |
| 301 |
token := tokenFromLink(t, mailer.resetLinks["[email protected]"], "https://ntfy.example.com/account/password/reset/") |
| 302 |
rr = request(t, s, "POST", "/v1/account/password/reset", fmt.Sprintf(`{"token":"%s","password":"brandnew"}`, token), nil) |
| 303 |
require.Equal(t, 200, rr.Code) |
| 304 |
require.True(t, canLogin(t, s, "ben", "brandnew")) // account A was reset |
| 305 |
require.True(t, canLogin(t, s, "[email protected]", "squatterpass")) // account B unaffected |
| 306 |
}) |
| 307 |
} |
| 308 |
|
| 309 |
func TestAccount_PasswordReset_UnknownIdentifierUniform(t *testing.T) { |
| 310 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 311 |
s, mailer, _ := newEmailTestServer(t, databaseURL) |
| 312 |
defer s.closeDatabases() |
| 313 |
|
| 314 |
// Unknown identifier still returns a uniform 200, and no email is sent |
| 315 |
rr := request(t, s, "POST", "/v1/account/password/reset/request", `{"identifier":"ghost"}`, nil) |
| 316 |
require.Equal(t, 200, rr.Code) |
| 317 |
require.Empty(t, mailer.resetLinks) |
| 318 |
}) |
| 319 |
} |
| 320 |
|
| 321 |
func TestAccount_PasswordReset_NoPrimaryEmailNoSend(t *testing.T) { |
| 322 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 323 |
s, mailer, _ := newEmailTestServer(t, databaseURL) |
| 324 |
defer s.closeDatabases() |
| 325 |
|
| 326 |
// ben exists but has no verified primary email -> uniform 200, nothing sent |
| 327 |
rr := request(t, s, "POST", "/v1/account/password/reset/request", `{"identifier":"ben"}`, nil) |
| 328 |
require.Equal(t, 200, rr.Code) |
| 329 |
require.Empty(t, mailer.resetLinks) |
| 330 |
}) |
| 331 |
} |
| 332 |
|
| 333 |
func TestAccount_Signup_WithEmail_SendsVerification(t *testing.T) { |
| 334 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 335 |
conf := newTestConfigWithAuthFile(t, databaseURL) |
| 336 |
conf.EnableSignup = true |
| 337 |
conf.SMTPSenderAddr = "localhost:25" |
| 338 |
conf.SMTPSenderFrom = "[email protected]" |
| 339 |
conf.BaseURL = "https://ntfy.example.com" |
| 340 |
s := newTestServer(t, conf) |
| 341 |
mailer := newCaptureMailer() |
| 342 |
s.mailer = mailer |
| 343 |
defer s.closeDatabases() |
| 344 |
|
| 345 |
// Sign up with an optional email -> account created and a verification link sent |
| 346 |
rr := request(t, s, "POST", "/v1/account", `{"username":"emma","password":"emmapass","email":"[email protected]"}`, nil) |
| 347 |
require.Equal(t, 200, rr.Code) |
| 348 |
link := mailer.verifyLinks["[email protected]"] |
| 349 |
require.NotEmpty(t, link) |
| 350 |
|
| 351 |
// Verifying the link makes it the (first) primary email |
| 352 |
token := tokenFromLink(t, link, "https://ntfy.example.com/account/email/verify/") |
| 353 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, token), nil).Code) |
| 354 |
account := getAccount(t, s, map[string]string{"Authorization": util.BasicAuth("emma", "emmapass")}) |
| 355 |
require.Equal(t, []string{"[email protected]"}, verifiedAddrs(account)) |
| 356 |
require.Equal(t, "[email protected]", primaryAddr(account)) |
| 357 |
}) |
| 358 |
} |
| 359 |
|
| 360 |
func TestAccount_Signup_WithoutEmail_NoSend(t *testing.T) { |
| 361 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 362 |
conf := newTestConfigWithAuthFile(t, databaseURL) |
| 363 |
conf.EnableSignup = true |
| 364 |
conf.SMTPSenderAddr = "localhost:25" |
| 365 |
conf.SMTPSenderFrom = "[email protected]" |
| 366 |
conf.BaseURL = "https://ntfy.example.com" |
| 367 |
s := newTestServer(t, conf) |
| 368 |
mailer := newCaptureMailer() |
| 369 |
s.mailer = mailer |
| 370 |
defer s.closeDatabases() |
| 371 |
|
| 372 |
// No email -> account created, nothing sent |
| 373 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account", `{"username":"emma","password":"emmapass"}`, nil).Code) |
| 374 |
require.Empty(t, mailer.verifyLinks) |
| 375 |
|
| 376 |
// Invalid email -> rejected |
| 377 |
rr := request(t, s, "POST", "/v1/account", `{"username":"otto","password":"ottopass","email":"not-an-email"}`, nil) |
| 378 |
require.Equal(t, 400, rr.Code) |
| 379 |
require.Equal(t, 40050, toHTTPError(t, rr.Body.String()).Code) |
| 380 |
}) |
| 381 |
} |
| 382 |
|
| 383 |
func TestAccount_Email_ProvisionedPrimary(t *testing.T) { |
| 384 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 385 |
hash, err := user.HashPassword("provpass", user.DefaultUserPasswordBcryptCost) |
| 386 |
require.Nil(t, err) |
| 387 |
conf := newTestConfigWithAuthFile(t, databaseURL) |
| 388 |
conf.SMTPSenderAddr = "localhost:25" |
| 389 |
conf.SMTPSenderFrom = "[email protected]" |
| 390 |
conf.BaseURL = "https://ntfy.example.com" |
| 391 |
conf.AuthUsers = []*user.User{{Name: "prov", Hash: hash, Role: user.RoleUser}} |
| 392 |
s := newTestServer(t, conf) |
| 393 |
mailer := newCaptureMailer() |
| 394 |
s.mailer = mailer |
| 395 |
defer s.closeDatabases() |
| 396 |
auth := map[string]string{"Authorization": util.BasicAuth("prov", "provpass")} |
| 397 |
|
| 398 |
// A provisioned user's first verified email becomes their primary (used by X-Email: yes; |
| 399 |
// password reset stays blocked separately for provisioned users) |
| 400 |
verifyEmailFor(t, s, mailer, auth, "[email protected]") |
| 401 |
account := getAccount(t, s, auth) |
| 402 |
require.Equal(t, []string{"[email protected]"}, verifiedAddrs(account)) |
| 403 |
require.Equal(t, "[email protected]", primaryAddr(account)) |
| 404 |
|
| 405 |
// Verify a second address and explicitly set it primary -> allowed, star moves |
| 406 |
verifyEmailFor(t, s, mailer, auth, "[email protected]") |
| 407 |
rr := request(t, s, "POST", "/v1/account/email/primary", `{"email":"[email protected]"}`, auth) |
| 408 |
require.Equal(t, 200, rr.Code) |
| 409 |
account = getAccount(t, s, auth) |
| 410 |
require.Equal(t, "[email protected]", primaryAddr(account)) |
| 411 |
}) |
| 412 |
} |
| 413 |
|
| 414 |
func TestAccount_PasswordReset_ProvisionedUserNoSend(t *testing.T) { |
| 415 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 416 |
// Provision a user via config (AuthUsers), with email sending enabled |
| 417 |
conf := newTestConfigWithAuthFile(t, databaseURL) |
| 418 |
conf.SMTPSenderAddr = "localhost:25" |
| 419 |
conf.SMTPSenderFrom = "[email protected]" |
| 420 |
conf.BaseURL = "https://ntfy.example.com" |
| 421 |
conf.AuthUsers = []*user.User{ |
| 422 |
{Name: "prov", Hash: "$2a$10$YLiO8U21sX1uhZamTLJXHuxgVC0Z/GKISibrKCLohPgtG7yIxSk4C", Role: user.RoleUser}, |
| 423 |
} |
| 424 |
s := newTestServer(t, conf) |
| 425 |
mailer := newCaptureMailer() |
| 426 |
s.mailer = mailer |
| 427 |
defer s.closeDatabases() |
| 428 |
|
| 429 |
// Give the provisioned user a verified primary email anyway |
| 430 |
prov, err := s.userManager.User("prov") |
| 431 |
require.Nil(t, err) |
| 432 |
require.True(t, prov.Provisioned) |
| 433 |
require.Nil(t, s.userManager.AddEmail(prov.ID, "[email protected]")) |
| 434 |
require.Nil(t, s.userManager.SetPrimaryEmail(prov.ID, "[email protected]")) |
| 435 |
|
| 436 |
// Reset request by username and by email -> uniform 200, but no email sent (can't reset) |
| 437 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/password/reset/request", `{"identifier":"prov"}`, nil).Code) |
| 438 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/password/reset/request", `{"identifier":"[email protected]"}`, nil).Code) |
| 439 |
require.Empty(t, mailer.resetLinks) |
| 440 |
}) |
| 441 |
} |
| 442 |
|
| 443 |
func TestAccount_PasswordReset_InvalidToken(t *testing.T) { |
| 444 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 445 |
s, _, _ := newEmailTestServer(t, databaseURL) |
| 446 |
defer s.closeDatabases() |
| 447 |
|
| 448 |
rr := request(t, s, "POST", "/v1/account/password/reset", `{"token":"nope","password":"brandnew"}`, nil) |
| 449 |
require.Equal(t, 400, rr.Code) |
| 450 |
require.Equal(t, 40054, toHTTPError(t, rr.Body.String()).Code) |
| 451 |
}) |
| 452 |
} |
| 453 |
|
| 454 |
func TestAccount_Email_AddDuplicateVerified(t *testing.T) { |
| 455 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 456 |
s, mailer, auth := newEmailTestServer(t, databaseURL) |
| 457 |
defer s.closeDatabases() |
| 458 |
|
| 459 |
require.Equal(t, 200, request(t, s, "PUT", "/v1/account/email", `{"email":"[email protected]"}`, auth).Code) |
| 460 |
token := tokenFromLink(t, mailer.verifyLinks["[email protected]"], "https://ntfy.example.com/account/email/verify/") |
| 461 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, token), nil).Code) |
| 462 |
|
| 463 |
// Adding the same already-verified address is a conflict |
| 464 |
rr := request(t, s, "PUT", "/v1/account/email", `{"email":"[email protected]"}`, auth) |
| 465 |
require.Equal(t, 409, rr.Code) |
| 466 |
require.Equal(t, 40907, toHTTPError(t, rr.Body.String()).Code) |
| 467 |
}) |
| 468 |
} |