Files
Warp/server/internal/adminapi/adminapi_test.go
T
tajniak81andClaude Opus 5 c96f88e994
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Phase 3: rooms, local only
Implements PLAN.md Phase 3: the roomserver, the room half of storage, the
Client-Server room endpoints and the admin room endpoints. A client can now
create a room, invite, join, leave, kick, ban, send messages and state,
redact, name a room with an alias and find one in the public directory.
Nothing talks to another server; every operation that would need to is
refused with a message that says so rather than a bare M_FORBIDDEN.

roomserver: one input pipeline every event goes through, whatever sent it —
validate, authorise, resolve, persist, emit. The rules are not
reimplemented here; internal/matrix is pure library code and this is what
connects it to a database and to a room's history. The Client-Server
handlers do no authorisation of their own, deliberately: whether Alice may
kick Bob is a question about the room's power levels, and the one place
that is answered correctly is the authorisation rules inside the pipeline.

A mutex per room serialises writers. Two events racing into one room would
each read the same forward extremities, both become one, and fork the room
for no reason — which would then need state resolution to put back
together. State resolution is wired in anyway for the multi-extremity
case. It is unreachable from outside in this phase, and the alternative is
a pipeline that authorises against one arbitrary branch of a fork without
saying so.

Room versions 3 to 12 all work and are each tested end to end. Version 12
earns that coverage: its room ID is the create event's own ID, its create
event carries no room_id, and the create event is excluded from every other
event's auth_events — so the state auth.Check is given is the event's
auth_events plus the create event, which that version identifies by room ID
rather than citing.

createRoom sends the spec's events in the spec's order, each authorised
against what the ones before it produced. A failure part-way leaves an
incomplete room, which is the lesser evil: the alternative is holding every
event of a new room in memory and writing them in one transaction, making
the pipeline that authorises them a special case. The one failure a second
client can cause at any time, an alias clash, takes the room row back out.

Redactions stay two decisions, as the spec keeps them. The rules let almost
any redaction into the room; whether it applies to its target is a separate
test, run inside the same transaction as the redaction itself so that a
client holding an event ID can always read the stripped event.

storage: events keyed by event ID rather than by a surrogate, since an
event ID appears in prev_events, auth_events, state snapshots and client
requests alike. Stream ordering comes from a counter row bumped inside the
write transaction, because a PostgreSQL sequence is explicitly not
transactional and a rolled-back write would leave a hole in the stream that
/sync would have to be taught to skip. State snapshots are stored per event
and deduplicated by content hash, so the table grows with state changes
rather than with traffic — and that is what makes "the state after a
forward extremity" answerable at all. The room's listing columns are
denormalised onto its row because the public directory orders every
published room by member count and pages the result, which cannot be done
by reading each room's state in turn.

Federation-disabled behaviour from PLAN.md section 3 is enforced here.
Restricted joins are the exception that works: the rule needs a signature
from the authorising user's server verified, and while federation is off
that server is always this one.

adminapi: room list and detail, purge, force-leave, and room and event
counters on the dashboard. clientapi gains its own rate-limit buckets for
createRoom and for joining, both of which write events rather than read
them.

Tested against the real event core and a real database rather than fakes:
what is under test is whether an event this server signs is one the
authorisation rules accept, and a fake of either would only prove that the
pipeline calls what the fake expects. The storage tests run against both
SQL drivers. docs/rooms.md describes the pipeline, the storage decisions
and the known gaps — chief among them that history visibility is
room-level rather than per-event, and that /sync still reports no rooms,
both of which belong with the event stream in Phase 4.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-16 10:34:57 +02:00

434 lines
14 KiB
Go

package adminapi_test
import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/dariuszjaniak/warp/server/internal/adminapi"
"github.com/dariuszjaniak/warp/server/internal/federation"
"github.com/dariuszjaniak/warp/server/internal/httputil"
"github.com/dariuszjaniak/warp/server/internal/matrix/signing"
"github.com/dariuszjaniak/warp/server/internal/roomserver"
"github.com/dariuszjaniak/warp/server/internal/settings"
"github.com/dariuszjaniak/warp/server/internal/storage"
_ "github.com/dariuszjaniak/warp/server/internal/storage/drivers"
"github.com/dariuszjaniak/warp/server/internal/userapi"
)
const testServerName = "warp.example"
// testServer is an admin API on a throwaway SQLite database.
type testServer struct {
t *testing.T
handler http.Handler
users *userapi.API
rooms *roomserver.API
settings *settings.Service
}
// newTestServer builds the component. defaults are the config-file values
// the settings service starts from, which is how a test pins a switch.
func newTestServer(t *testing.T, defaults settings.Defaults) *testServer {
t.Helper()
path := filepath.ToSlash(filepath.Join(t.TempDir(), "warp.db"))
db, err := storage.Open(t.Context(), storage.Config{
Driver: storage.DriverSQLite,
ConnectionString: "file:" + path,
}, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatalf("open storage: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
if err := db.Migrate(t.Context()); err != nil {
t.Fatalf("migrate: %v", err)
}
users, err := userapi.New(userapi.Options{
ServerName: testServerName,
Store: db.Users(),
Logger: slog.New(slog.DiscardHandler),
})
if err != nil {
t.Fatalf("userapi.New: %v", err)
}
settingsService, err := settings.New(t.Context(), db.Settings(), defaults, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatalf("settings.New: %v", err)
}
key, err := signing.Generate("test")
if err != nil {
t.Fatalf("signing.Generate: %v", err)
}
rooms, err := roomserver.New(roomserver.Options{
ServerName: testServerName,
Store: db.Rooms(),
Profiles: users,
SigningKey: key,
Logger: slog.New(slog.DiscardHandler),
})
if err != nil {
t.Fatalf("roomserver.New: %v", err)
}
api, err := adminapi.New(adminapi.Options{
ServerName: testServerName,
Users: users,
Rooms: rooms,
Settings: settingsService,
Federation: federation.NewGate(settingsService.FederationEnabled,
federation.NewPolicy([]string{"friend.example"}, []string{"spam.example"})),
Database: db,
Logger: slog.New(slog.DiscardHandler),
})
if err != nil {
t.Fatalf("adminapi.New: %v", err)
}
router := httputil.NewRouter(nil)
api.RegisterRoutes(router)
return &testServer{
t: t, handler: router, users: users, rooms: rooms, settings: settingsService,
}
}
func (ts *testServer) do(method, path, token string, body any) *httptest.ResponseRecorder {
ts.t.Helper()
var reader io.Reader
if body != nil {
raw, err := json.Marshal(body)
if err != nil {
ts.t.Fatal(err)
}
reader = bytes.NewReader(raw)
}
req := httptest.NewRequest(method, path, reader)
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
rec := httptest.NewRecorder()
ts.handler.ServeHTTP(rec, req)
return rec
}
// admin creates an administrator and returns its access token.
func (ts *testServer) admin(localpart string) string {
ts.t.Helper()
return ts.account(localpart, true)
}
// user creates an ordinary account and returns its access token.
func (ts *testServer) user(localpart string) string {
ts.t.Helper()
return ts.account(localpart, false)
}
func (ts *testServer) account(localpart string, admin bool) string {
ts.t.Helper()
if _, err := ts.users.CreateAccount(ts.t.Context(), userapi.NewAccountRequest{
Localpart: localpart,
Password: "sUp3rs3kr1t",
Admin: admin,
}); err != nil {
ts.t.Fatalf("CreateAccount: %v", err)
}
session, err := ts.users.CreateSession(ts.t.Context(), userapi.SessionRequest{Localpart: localpart})
if err != nil {
ts.t.Fatalf("CreateSession: %v", err)
}
return session.AccessToken
}
func jsonBody(t *testing.T, rec *httptest.ResponseRecorder) map[string]any {
t.Helper()
var out map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatalf("response is not JSON (%s): %v", rec.Body.String(), err)
}
return out
}
func expectStatus(t *testing.T, rec *httptest.ResponseRecorder, want int) {
t.Helper()
if rec.Code != want {
t.Fatalf("status = %d, want %d (body: %s)", rec.Code, want, rec.Body.String())
}
}
func expectError(t *testing.T, rec *httptest.ResponseRecorder, status int, errcode string) {
t.Helper()
expectStatus(t, rec, status)
if got := jsonBody(t, rec)["errcode"]; got != errcode {
t.Errorf("errcode = %v, want %s (body: %s)", got, errcode, rec.Body.String())
}
}
const (
federationPath = adminapi.Prefix + "/federation"
registrationPath = adminapi.Prefix + "/registration"
usersPath = adminapi.Prefix + "/users"
statsPath = adminapi.Prefix + "/stats"
)
var toggleable = settings.Defaults{
FederationAllowRuntimeToggle: true,
RegistrationAllowRuntimeToggle: true,
}
// TestAdminAuth is the gate everything else here depends on.
func TestAdminAuth(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
ordinary := ts.user("alice")
for name, tc := range map[string]struct {
token string
status int
code string
}{
"no token": {"", http.StatusUnauthorized, "M_MISSING_TOKEN"},
"unknown token": {"not-a-token", http.StatusUnauthorized, "M_UNKNOWN_TOKEN"},
"ordinary user": {ordinary, http.StatusForbidden, "M_FORBIDDEN"},
} {
t.Run(name, func(t *testing.T) {
expectError(t, ts.do(http.MethodGet, statsPath, tc.token, nil), tc.status, tc.code)
})
}
expectStatus(t, ts.do(http.MethodGet, statsPath, admin, nil), http.StatusOK)
}
// TestAdminTokenNotAcceptedInQueryString: an admin token in a URL ends up in
// every access log between here and the browser.
func TestAdminTokenNotAcceptedInQueryString(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
expectError(t, ts.do(http.MethodGet, statsPath+"?access_token="+admin, "", nil),
http.StatusUnauthorized, "M_MISSING_TOKEN")
}
// TestFederationSwitch is the admin half of PLAN.md section 3: the switch
// flips at runtime and the gate follows.
func TestFederationSwitch(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
rec := ts.do(http.MethodGet, federationPath, admin, nil)
expectStatus(t, rec, http.StatusOK)
body := jsonBody(t, rec)
if body["enabled"] != false {
t.Errorf("federation should start disabled, got %v", body["enabled"])
}
if body["toggleable"] != true {
t.Errorf("toggleable = %v, want true", body["toggleable"])
}
// The dashboard needs to show which servers a running Warp will talk
// to, even though editing the lists is Phase 9.
if allowlist, _ := body["allowlist"].([]any); len(allowlist) != 1 || allowlist[0] != "friend.example" {
t.Errorf("allowlist = %v", body["allowlist"])
}
if denylist, _ := body["denylist"].([]any); len(denylist) != 1 || denylist[0] != "spam.example" {
t.Errorf("denylist = %v", body["denylist"])
}
rec = ts.do(http.MethodPut, federationPath, admin, map[string]any{"enabled": true})
expectStatus(t, rec, http.StatusOK)
if jsonBody(t, rec)["enabled"] != true {
t.Errorf("the PUT should report the new state, got %s", rec.Body.String())
}
if !ts.settings.FederationEnabled() {
t.Error("the settings service did not pick the change up")
}
rec = ts.do(http.MethodPut, federationPath, admin, map[string]any{"enabled": false})
expectStatus(t, rec, http.StatusOK)
if ts.settings.FederationEnabled() {
t.Error("switching back off did not take effect")
}
}
func TestRegistrationSwitch(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
rec := ts.do(http.MethodPut, registrationPath, admin, map[string]any{"enabled": true})
expectStatus(t, rec, http.StatusOK)
if !ts.settings.RegistrationEnabled() {
t.Error("registration was not enabled")
}
rec = ts.do(http.MethodGet, registrationPath, admin, nil)
expectStatus(t, rec, http.StatusOK)
if jsonBody(t, rec)["enabled"] != true {
t.Errorf("GET = %s", rec.Body.String())
}
}
// TestSwitchPinnedByConfig covers settings.ErrNotToggleable: the request was
// fine, the server's own configuration is what refuses it, so it is a
// conflict rather than a bad request.
func TestSwitchPinnedByConfig(t *testing.T) {
ts := newTestServer(t, settings.Defaults{})
admin := ts.admin("root")
for _, path := range []string{federationPath, registrationPath} {
rec := ts.do(http.MethodPut, path, admin, map[string]any{"enabled": true})
expectError(t, rec, http.StatusConflict, "M_FORBIDDEN")
if msg, _ := jsonBody(t, rec)["error"].(string); msg == "" {
t.Error("the refusal must say which setting is pinned")
}
// And the GET says so in advance, so the panel can grey the
// switch out instead of offering something that will bounce.
rec = ts.do(http.MethodGet, path, admin, nil)
expectStatus(t, rec, http.StatusOK)
if jsonBody(t, rec)["toggleable"] != false {
t.Errorf("GET %s reports toggleable, but it is pinned", path)
}
}
}
func TestSwitchRequiresTheEnabledField(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
expectError(t, ts.do(http.MethodPut, federationPath, admin, map[string]any{}),
http.StatusBadRequest, "M_MISSING_PARAM")
}
func TestListUsers(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
ts.user("alice")
ts.user("bob")
rec := ts.do(http.MethodGet, usersPath, admin, nil)
expectStatus(t, rec, http.StatusOK)
body := jsonBody(t, rec)
if total, _ := body["total"].(float64); total != 3 {
t.Errorf("total = %v, want 3", body["total"])
}
users, _ := body["users"].([]any)
if len(users) != 3 {
t.Fatalf("got %d users, want 3", len(users))
}
first, _ := users[0].(map[string]any)
if first["user_id"] != "@alice:warp.example" {
t.Errorf("users are not ordered by localpart: %s", rec.Body.String())
}
if first["admin"] != false {
t.Errorf("alice should not be an admin: %v", first)
}
// Paging reports the full total so the panel can show how many pages
// there are.
rec = ts.do(http.MethodGet, usersPath+"?limit=1&offset=1", admin, nil)
expectStatus(t, rec, http.StatusOK)
body = jsonBody(t, rec)
users, _ = body["users"].([]any)
if len(users) != 1 || users[0].(map[string]any)["user_id"] != "@bob:warp.example" {
t.Errorf("paged listing = %s", rec.Body.String())
}
if total, _ := body["total"].(float64); total != 3 {
t.Errorf("paged total = %v, want 3", body["total"])
}
rec = ts.do(http.MethodGet, usersPath+"?search=ali", admin, nil)
expectStatus(t, rec, http.StatusOK)
if total, _ := jsonBody(t, rec)["total"].(float64); total != 1 {
t.Errorf("search found %v users, want 1", jsonBody(t, rec)["total"])
}
expectError(t, ts.do(http.MethodGet, usersPath+"?limit=lots", admin, nil),
http.StatusBadRequest, "M_INVALID_PARAM")
expectError(t, ts.do(http.MethodGet, usersPath+"?offset=-1", admin, nil),
http.StatusBadRequest, "M_INVALID_PARAM")
}
// TestCreateUser covers the way an operator adds an account to a server with
// registration switched off — which is the normal state of a private Warp.
func TestCreateUser(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
if ts.settings.RegistrationEnabled() {
t.Fatal("this test needs registration to be off")
}
rec := ts.do(http.MethodPost, usersPath, admin, map[string]any{
"username": "Alice",
"password": "sUp3rs3kr1t",
"display_name": "Alice",
"admin": true,
})
expectStatus(t, rec, http.StatusCreated)
body := jsonBody(t, rec)
if body["user_id"] != "@alice:warp.example" {
t.Errorf("user_id = %v; the localpart should be normalised", body["user_id"])
}
if body["admin"] != true {
t.Errorf("admin = %v, want true", body["admin"])
}
// The new account can really log in, which is the only thing that
// makes creating it useful.
if err := ts.users.CheckPassword(t.Context(), "alice", "sUp3rs3kr1t"); err != nil {
t.Errorf("the created account cannot log in: %v", err)
}
expectError(t, ts.do(http.MethodPost, usersPath, admin, map[string]any{
"username": "alice", "password": "sUp3rs3kr1t",
}), http.StatusBadRequest, "M_USER_IN_USE")
expectError(t, ts.do(http.MethodPost, usersPath, admin, map[string]any{
"username": "not valid", "password": "sUp3rs3kr1t",
}), http.StatusBadRequest, "M_INVALID_USERNAME")
expectError(t, ts.do(http.MethodPost, usersPath, admin, map[string]any{"username": "bob"}),
http.StatusBadRequest, "M_MISSING_PARAM")
expectError(t, ts.do(http.MethodPost, usersPath, admin, map[string]any{"password": "x"}),
http.StatusBadRequest, "M_MISSING_PARAM")
}
func TestStats(t *testing.T) {
ts := newTestServer(t, toggleable)
admin := ts.admin("root")
ts.user("alice")
rec := ts.do(http.MethodGet, statsPath, admin, nil)
expectStatus(t, rec, http.StatusOK)
body := jsonBody(t, rec)
if body["server_name"] != testServerName {
t.Errorf("server_name = %v", body["server_name"])
}
if body["database"] != storage.DriverSQLite {
t.Errorf("database = %v, want sqlite", body["database"])
}
if users, _ := body["users"].(float64); users != 2 {
t.Errorf("users = %v, want 2", body["users"])
}
if admins, _ := body["admins"].(float64); admins != 1 {
t.Errorf("admins = %v, want 1", body["admins"])
}
// Each account above got one session, so the device count follows.
if devices, _ := body["devices"].(float64); devices != 2 {
t.Errorf("devices = %v, want 2", body["devices"])
}
if body["federation_enabled"] != false || body["registration_enabled"] != false {
t.Errorf("switch state = %s", rec.Body.String())
}
}