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
172 lines
6.8 KiB
Go
172 lines
6.8 KiB
Go
package api_test
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
// latestOf returns the newest result of a monitor: ok and the error.
|
|
func latestOf(b *browser, id string) (bool, string, bool) {
|
|
_, list := b.do("GET", "/api/v1/org/monitors", nil)
|
|
for _, m := range list["monitors"].([]any) {
|
|
m := m.(map[string]any)
|
|
if m["id"] != id {
|
|
continue
|
|
}
|
|
if l := m["latest"].([]any); len(l) > 0 {
|
|
r := l[0].(map[string]any)
|
|
msg, _ := r["error"].(string)
|
|
return r["ok"] == true, msg, true
|
|
}
|
|
}
|
|
return false, "", false
|
|
}
|
|
|
|
func TestCheckerNetwork(t *testing.T) {
|
|
// In the cloud edition, platform checkers stay off private networks
|
|
// unless an admin allows it.
|
|
cloud := newEnvWith(t, func(c *config.API) { c.Edition, c.SignupVerify = config.EditionCloud, "off" })
|
|
e := newEnv(t)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel)
|
|
go e.api.RunScheduler(ctx)
|
|
reset := func() {
|
|
_ = e.asSystem(t, func(tx pgx.Tx) error {
|
|
_, err := tx.Exec(context.Background(), `DELETE FROM platform_settings WHERE key = 'platform_checkers_private'`)
|
|
return err
|
|
})
|
|
}
|
|
reset()
|
|
t.Cleanup(reset)
|
|
|
|
// A private service: the test's own loopback.
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = l.Close() })
|
|
go func() {
|
|
for {
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = conn.Close()
|
|
}
|
|
}()
|
|
|
|
tok, platformRegion := e.platformChecker(t, uniq("eu-"))
|
|
e.startChecker(t, tok)
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("net")+".test", uniq("net-"))
|
|
if err := tenant.New(e.dbPool).Tx(context.Background(), tenant.Global(), func(q *store.Queries) error {
|
|
return q.MakeAdmin(context.Background(), owner.userIDSelf())
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
monitor := func(region string) string {
|
|
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
|
"name": "Internal", "type": "tcp", "target": l.Addr().String(), "interval_seconds": 10, "confirmations": 1, "regions": []string{region},
|
|
})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create monitor = %d %v", st, body)
|
|
}
|
|
return body["id"].(string)
|
|
}
|
|
waitResult := func(id string, ok bool, what string) {
|
|
t.Helper()
|
|
waitFor(t, 20*time.Second, what, func() bool {
|
|
got, msg, seen := latestOf(owner, id)
|
|
return seen && got == ok && (ok || strings.Contains(msg, "non-public"))
|
|
})
|
|
}
|
|
|
|
admin := cloud.browser(t)
|
|
admin.signup("a@"+uniq("net")+".test", uniq("neta-"))
|
|
if err := tenant.New(e.dbPool).Tx(context.Background(), tenant.Global(), func(q *store.Queries) error {
|
|
return q.MakeAdmin(context.Background(), admin.userIDSelf())
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, body := admin.do("GET", "/api/v1/admin/checker-network", nil); body["allow_private"] != false {
|
|
t.Errorf("cloud default = %v", body)
|
|
}
|
|
|
|
// Self-hosted, platform checkers may by default; an admin can forbid it.
|
|
pm := monitor(platformRegion)
|
|
waitResult(pm, true, "platform checker reaching a private address")
|
|
if st, body := owner.do("PUT", "/api/v1/admin/checker-network", map[string]any{"allow_private": false}); st != http.StatusNoContent {
|
|
t.Fatalf("forbid = %d %v", st, body)
|
|
}
|
|
waitResult(pm, false, "platform checker refusing the private address once forbidden")
|
|
owner.do("DELETE", "/api/v1/org/monitors/"+pm, nil)
|
|
|
|
// An organization's checker can be kept off private networks.
|
|
_, region := owner.do("POST", "/api/v1/org/regions", map[string]any{"key": uniq("office-"), "name": "Office"})
|
|
st, agent := owner.do("POST", "/api/v1/org/agents", map[string]any{"kind": "checker", "name": "office-1", "region_id": region["id"], "allow_private": false})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create agent = %d %v", st, agent)
|
|
}
|
|
e.startChecker(t, agent["token"].(string))
|
|
om := monitor(region["id"].(string))
|
|
waitResult(om, false, "org checker kept off private addresses")
|
|
_, agents := owner.do("GET", "/api/v1/org/agents", nil)
|
|
if raw := mustJSON(agents); !strings.Contains(raw, `"allow_private":false`) {
|
|
t.Errorf("agents = %s", raw)
|
|
}
|
|
if st, body := owner.do("PATCH", "/api/v1/org/agents/"+agent["id"].(string), map[string]any{"allow_private": true}); st != http.StatusNoContent {
|
|
t.Fatalf("patch agent = %d %v", st, body)
|
|
}
|
|
waitResult(om, true, "org checker reaching the private address once allowed")
|
|
|
|
// Other organizations cannot change it.
|
|
other := e.browser(t)
|
|
other.signup("x@"+uniq("net")+".test", uniq("netx-"))
|
|
st, body := other.do("PATCH", "/api/v1/org/agents/"+agent["id"].(string), map[string]any{"allow_private": false})
|
|
expect(t, "foreign agent", st, body, http.StatusNotFound, "not_found")
|
|
}
|
|
|
|
func TestOrgCheckTypes(t *testing.T) {
|
|
e := newEnv(t)
|
|
_, region := e.platformChecker(t, uniq("eu-"))
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("oct")+".test", uniq("oct-"))
|
|
other := e.browser(t)
|
|
other.signup("x@"+uniq("oct")+".test", uniq("octx-"))
|
|
|
|
_, tcp := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Port", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": []string{region}})
|
|
if st, body := owner.do("PUT", "/api/v1/org/check-types", map[string]any{"enabled": []string{"http", "dns"}}); st != http.StatusNoContent {
|
|
t.Fatalf("set = %d %v", st, body)
|
|
}
|
|
if _, body := owner.do("GET", "/api/v1/org/check-types", nil); mustJSON(body["types"]) != `["http","dns"]` {
|
|
t.Errorf("offered = %v", body["types"])
|
|
}
|
|
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Ping", "type": "icmp", "target": "example.com", "interval_seconds": 60, "regions": []string{region}})
|
|
expect(t, "switched-off type", st, body, http.StatusUnprocessableEntity, "check_type_disabled")
|
|
st, body = owner.do("PUT", "/api/v1/org/monitors/"+tcp["id"].(string), map[string]any{"name": "Port 2", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": []string{region}})
|
|
expect(t, "existing monitor of a switched-off type", st, body, http.StatusOK, "")
|
|
if _, _, page := owner.page("/monitors/new"); strings.Contains(page, `value="tcp"`) || !strings.Contains(page, `value="dns"`) {
|
|
t.Error("the editor offers types the organization switched off")
|
|
}
|
|
if _, _, page := owner.page("/settings/org"); !strings.Contains(page, `value="icmp"`) {
|
|
t.Error("the settings page lacks the check types")
|
|
}
|
|
st, body = owner.do("PUT", "/api/v1/org/check-types", map[string]any{"enabled": []string{}})
|
|
expect(t, "switching off everything", st, body, http.StatusUnprocessableEntity, "all_check_types_disabled")
|
|
|
|
// Other organizations keep everything.
|
|
if _, body := other.do("GET", "/api/v1/org/check-types", nil); !strings.Contains(mustJSON(body), `"icmp"`) {
|
|
t.Errorf("other organization offered %v", body["types"])
|
|
}
|
|
}
|