Files
Warp/server/internal/clientapi/clientapi_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

292 lines
8.5 KiB
Go

package clientapi_test
import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/dariuszjaniak/warp/server/internal/clientapi"
"github.com/dariuszjaniak/warp/server/internal/config"
"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 a Client-Server API on a throwaway SQLite database.
//
// It is built from the real components rather than fakes: the point of these
// tests is the behaviour a client sees, and a fake userapi would only prove
// that the handlers call the methods the fake expects.
type testServer struct {
t *testing.T
handler http.Handler
users *userapi.API
rooms *roomserver.API
settings *settings.Service
fedGate *federation.Gate
}
// newTestServer builds a server. tweak adjusts the options before the
// component is created, which is how a test turns on registration tokens or
// rate limiting.
func newTestServer(t *testing.T, tweak func(*clientapi.Options)) *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 + "?_pragma=busy_timeout(5000)",
}, 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)
}
// Registration is on and the limiter is off by default: almost every
// test needs an account, and almost none is about rate limiting.
settingsService, err := settings.New(t.Context(), db.Settings(), settings.Defaults{
RegistrationEnabled: true,
RegistrationAllowRuntimeToggle: true,
FederationAllowRuntimeToggle: true,
}, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatalf("settings.New: %v", err)
}
gate := federation.NewGate(settingsService.FederationEnabled, nil)
// A throwaway signing key: the roomserver signs every event it builds,
// and the room endpoints are not registered without one.
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,
FederationEnabled: settingsService.FederationEnabled,
Logger: slog.New(slog.DiscardHandler),
})
if err != nil {
t.Fatalf("roomserver.New: %v", err)
}
opts := clientapi.Options{
ServerName: testServerName,
Users: users,
Rooms: rooms,
Settings: settingsService,
Federation: gate,
Logger: slog.New(slog.DiscardHandler),
}
if tweak != nil {
tweak(&opts)
}
api, err := clientapi.New(opts)
if err != nil {
t.Fatalf("clientapi.New: %v", err)
}
router := httputil.NewRouter(nil)
api.RegisterRoutes(router)
return &testServer{
t: t,
handler: router,
users: users,
rooms: rooms,
settings: settingsService,
fedGate: gate,
}
}
// request is one HTTP call under test.
type request struct {
method string
path string
body any
token string
remote string
}
// do performs a request. A nil body sends none at all, which is what the
// endpoints whose body is optional have to cope with.
func (ts *testServer) do(req request) *httptest.ResponseRecorder {
ts.t.Helper()
var reader io.Reader
if req.body != nil {
raw, err := json.Marshal(req.body)
if err != nil {
ts.t.Fatalf("marshalling the request body: %v", err)
}
reader = bytes.NewReader(raw)
}
httpReq := httptest.NewRequest(req.method, req.path, reader)
if req.body != nil {
httpReq.Header.Set("Content-Type", "application/json")
}
if req.token != "" {
httpReq.Header.Set("Authorization", "Bearer "+req.token)
}
if req.remote != "" {
httpReq.RemoteAddr = req.remote
}
rec := httptest.NewRecorder()
ts.handler.ServeHTTP(rec, httpReq)
return rec
}
// get is the common read-only case.
func (ts *testServer) get(path, token string) *httptest.ResponseRecorder {
ts.t.Helper()
return ts.do(request{method: http.MethodGet, path: path, token: token})
}
// decode parses a response body as JSON.
func decode[T any](t *testing.T, rec *httptest.ResponseRecorder) T {
t.Helper()
var out T
body := rec.Body.Bytes()
if err := json.Unmarshal(body, &out); err != nil {
t.Fatalf("response is not JSON (%s): %v", body, err)
}
return out
}
// jsonBody parses a response into a generic map.
func jsonBody(t *testing.T, rec *httptest.ResponseRecorder) map[string]any {
t.Helper()
return decode[map[string]any](t, rec)
}
// expectStatus fails unless the response has the given status.
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())
}
}
// expectError fails unless the response is the given Matrix error.
func expectError(t *testing.T, rec *httptest.ResponseRecorder, status int, errcode string) {
t.Helper()
expectStatus(t, rec, status)
body := jsonBody(t, rec)
if got, _ := body["errcode"].(string); got != errcode {
t.Errorf("errcode = %q, want %q (body: %s)", got, errcode, rec.Body.String())
}
if msg, _ := body["error"].(string); msg == "" {
t.Error("the error envelope must carry a human-readable message")
}
}
// account is a registered user plus its first session, which is what most
// tests need before they can do anything.
type account struct {
UserID string
Localpart string
DeviceID string
AccessToken string
Password string
}
// register walks the full two-step registration flow, as a client does.
func (ts *testServer) register(localpart, password string) account {
ts.t.Helper()
rec := ts.do(request{
method: http.MethodPost,
path: "/_matrix/client/v3/register",
body: map[string]any{"username": localpart, "password": password},
})
expectStatus(ts.t, rec, http.StatusUnauthorized)
session, _ := jsonBody(ts.t, rec)["session"].(string)
if session == "" {
ts.t.Fatalf("the challenge carried no session: %s", rec.Body.String())
}
rec = ts.do(request{
method: http.MethodPost,
path: "/_matrix/client/v3/register",
body: map[string]any{
"username": localpart,
"password": password,
"auth": map[string]any{"type": "m.login.dummy", "session": session},
},
})
expectStatus(ts.t, rec, http.StatusOK)
body := jsonBody(ts.t, rec)
acc := account{Password: password}
acc.UserID, _ = body["user_id"].(string)
acc.AccessToken, _ = body["access_token"].(string)
acc.DeviceID, _ = body["device_id"].(string)
acc.Localpart = userapi.NormaliseLocalpart(localpart)
if acc.AccessToken == "" || acc.DeviceID == "" {
ts.t.Fatalf("registration returned no session: %s", rec.Body.String())
}
return acc
}
// login creates a second session for an existing account.
func (ts *testServer) login(acc account, deviceID string) account {
ts.t.Helper()
body := map[string]any{
"type": "m.login.password",
"identifier": map[string]any{"type": "m.id.user", "user": acc.UserID},
"password": acc.Password,
}
if deviceID != "" {
body["device_id"] = deviceID
}
rec := ts.do(request{method: http.MethodPost, path: "/_matrix/client/v3/login", body: body})
expectStatus(ts.t, rec, http.StatusOK)
parsed := jsonBody(ts.t, rec)
out := acc
out.AccessToken, _ = parsed["access_token"].(string)
out.DeviceID, _ = parsed["device_id"].(string)
return out
}
// makeAdmin grants the admin flag to an existing account.
func (ts *testServer) makeAdmin(acc account) {
ts.t.Helper()
if err := ts.users.SetAdmin(ts.t.Context(), acc.Localpart, true); err != nil {
ts.t.Fatalf("SetAdmin: %v", err)
}
}
// withRateLimit is a tweak that turns on the limiter with a given policy.
func withRateLimit(limit config.RateLimit) func(*clientapi.Options) {
return func(o *clientapi.Options) { o.RateLimit = limit }
}