New accounts confirm their email address before signing in: sign-up mails a link valid for 24 hours, and its page confirms only when the visitor presses the button, so mail scanners opening links do not sign in. Confirming signs the account in; unconfirmed accounts are told so only after giving the right password, can ask for a new link, and are deleted after a week. UPTIME_SIGNUP_VERIFY requires confirmation always in the cloud edition and self-hosted wherever mail can be sent; without a mail server, sign-up that needs it stays closed. One account per mailbox: users.email_key drops +suffixes, and dots for Gmail, and is unique, including for accounts created through invitations and SSO, which count as confirmed. A client address (IPv6: its /64) may create UPTIME_SIGNUPS_PER_IP accounts a day, 2 by default; refused sign-ups do not count. Sign-up asks only for name, email and password. Accounts without an organization are led to create one; signed-in users create further organizations from the switcher, within the per-account limit, and POST /api/v1/orgs does the same. Closes #112 Closes #113 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestPaginate(t *testing.T) {
|
|
// A list of 0..9 shown in order, read the way the queries do: from a
|
|
// cursor in reading direction, one row more than the page.
|
|
all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
key := func(i int) cursor { return cursor{S: string(rune('a' + i)), ID: uuid.Nil} }
|
|
read := func(p pageReq) []int {
|
|
var out []int
|
|
if p.backward() {
|
|
for i := len(all) - 1; i >= 0; i-- {
|
|
if key(all[i]).S < p.before.S {
|
|
out = append(out, all[i])
|
|
}
|
|
}
|
|
} else {
|
|
for _, v := range all {
|
|
if p.after == nil || key(v).S > p.after.S {
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
}
|
|
return out[:min(len(out), int(p.fetch()))]
|
|
}
|
|
page := func(p pageReq) ([]int, pageInfo) { return paginate(p, read(p), key) }
|
|
|
|
first, info := page(pageReq{limit: 4})
|
|
if !slices.Equal(first, []int{0, 1, 2, 3}) || info.Prev != "" || info.Next == "" {
|
|
t.Fatalf("first = %v %+v", first, info)
|
|
}
|
|
next, _ := decodeCursor(info.Next)
|
|
second, info := page(pageReq{limit: 4, after: next})
|
|
if !slices.Equal(second, []int{4, 5, 6, 7}) || info.Prev == "" || info.Next == "" {
|
|
t.Fatalf("second = %v %+v", second, info)
|
|
}
|
|
n3, _ := decodeCursor(info.Next)
|
|
last, lastInfo := page(pageReq{limit: 4, after: n3})
|
|
if !slices.Equal(last, []int{8, 9}) || lastInfo.Next != "" || lastInfo.Prev == "" {
|
|
t.Fatalf("last = %v %+v", last, lastInfo)
|
|
}
|
|
// Back from the last page and from the second to the first.
|
|
prev, _ := decodeCursor(lastInfo.Prev)
|
|
back, backInfo := page(pageReq{limit: 4, before: prev})
|
|
if !slices.Equal(back, []int{4, 5, 6, 7}) || backInfo.Prev == "" || backInfo.Next == "" {
|
|
t.Fatalf("back = %v %+v", back, backInfo)
|
|
}
|
|
p2, _ := decodeCursor(backInfo.Prev)
|
|
start, startInfo := page(pageReq{limit: 4, before: p2})
|
|
if !slices.Equal(start, []int{0, 1, 2, 3}) || startInfo.Prev != "" || startInfo.Next == "" {
|
|
t.Fatalf("start = %v %+v", start, startInfo)
|
|
}
|
|
if _, ok := decodeCursor("not a cursor!"); ok {
|
|
t.Error("garbage cursor accepted")
|
|
}
|
|
}
|
|
|
|
func TestPageSlice(t *testing.T) {
|
|
items := []string{"a", "b", "c", "d", "e"}
|
|
key := func(s string) cursor { return cursor{S: s} }
|
|
first, info := pageSlice(items, pageReq{limit: 2}, key)
|
|
next, _ := decodeCursor(info.Next)
|
|
second, info2 := pageSlice(items, pageReq{limit: 2, after: next}, key)
|
|
prev, _ := decodeCursor(info2.Prev)
|
|
back, _ := pageSlice(items, pageReq{limit: 2, before: prev}, key)
|
|
if !slices.Equal(first, []string{"a", "b"}) || !slices.Equal(second, []string{"c", "d"}) || !slices.Equal(back, first) {
|
|
t.Errorf("first %v, second %v, back %v", first, second, back)
|
|
}
|
|
}
|
|
|
|
func TestEmailKey(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"Max.Power+uptime@googlemail.com": "maxpower@gmail.com",
|
|
"max.power@gmail.com": "maxpower@gmail.com",
|
|
"a.b+c@example.com": "a.b@example.com",
|
|
"A.B@Example.com": "a.b@example.com",
|
|
} {
|
|
if got := emailKey(in); got != want {
|
|
t.Errorf("emailKey(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
if clientKey("2001:db8:1:2:aaaa::1") != clientKey("2001:db8:1:2:bbbb::9") || clientKey("2001:db8:1:3::1") == clientKey("2001:db8:1:2::1") {
|
|
t.Error("IPv6 clients are not grouped by /64")
|
|
}
|
|
if clientKey("203.0.113.7") != "203.0.113.7" {
|
|
t.Error("IPv4 clients are grouped")
|
|
}
|
|
}
|