Files
Warp/server/internal/clientapi/rooms_test.go
T
tajniak81andClaude Opus 5 d187ad0d17
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 4: sync
Implements PLAN.md Phase 4: the read model. /sync, /messages and /context,
the filters that narrow them, and the streams a response carries that are
not room events — typing, receipts, presence and to-device messages. With
this a client can be used against Warp, which is milestone M1.

The one architectural choice is that the syncapi has no copy of the rooms:
it reads them through the roomserver. Dendrite gives its syncapi its own
tables and feeds them from the output stream, which is right for a design
where the two can be separate processes, and Dendrite's could be. Warp is
one process by decision, and there the copy buys nothing and costs the
hardest class of bug a homeserver has — a read model that disagrees with
the room, where a client is shown a message that was rejected and the only
way to find out is to compare two tables. So the roomserver grew a read
model (stream.go, roomsync.go, visibility.go) rather than exporting its
store: reading an event means parsing it under its room's version, and a
second copy of the version registry, the parser and the client projection
would be a second place for them to disagree.

syncapi: stream tokens naming a position in each of five independent
streams, so a client that has seen receipt 7 but only event 4 can say so in
one opaque string. A token with fewer positions than this build writes is
accepted and the rest read as zero, which is what lets a token stored
before a stream existed go on working. The PDU position comes from
MAX(stream_ordering) and not from the counter row the other four use: the
counter is advanced inside the writing transaction, so a token taken from
it would name a position whose event no reader can see yet, and the event
would be skipped for good once that transaction committed.

Long polling goes through a notifier that wakes by room and by user rather
than globally — an event wakes the listeners of its room, a membership
event also wakes the user it names, so an invite reaches a client that is
not in the room yet. A wake-up is only ever a hint: the listener re-reads
the streams and decides for itself what changed, which is what makes it
safe for the notifier to hold no state about what each client has seen. The
listener registers before the read, so an event landing in between leaves
the channel signalled and the sync reads again rather than blocking on a
change it just missed. EventPersisted must not block, because it is called
with the room's write lock still held.

"Something happened" is not "a token moved". A stream can move without this
user being concerned by it, and returning on every such move would turn a
long poll back into a busy loop. The same trap caught presence twice over:
a client says it is still online by syncing, so ?set_presence= makes every
sync a presence write, and a stream that moved each time would wake every
peer, which would sync, which would move it again. SetPresence therefore
reports whether anything a peer would notice actually changed, and a write
that only refreshes the last-active timestamp leaves the stream alone.

Timeline windows are named by their two ends and a direction, Low exclusive
and High inclusive in both directions, rather than by "from" and "to" as
the wire format spells them. Each of those would otherwise be the low bound
in one direction and the high bound in the other, which is exactly the
off-by-one every pagination bug is made of; the handlers translate once and
nothing below them thinks about it again. A sync's timeline is read
backwards and reported forwards, because a client wants the newest events
of the window it missed — a forward read with a limit hands it the oldest
and then calls the rest a gap.

visibility: history visibility is now a span rather than a yes or no, which
is what Phase 3 deferred here. A user who has left a room is served the
room as it was when they left — their state reads, their members and their
timeline — instead of being refused, and is bounded above at their own
membership event so a kicked client cannot go on streaming. Their span is
found from their own member events, so a user who joined, left and rejoined
sees their latest span. ?at= on /members is honoured for the same reason.
What is left is that the span is computed from the room's current
history_visibility where the spec judges each event by the one in force
when it was sent; that is a state read per event and belongs with open
question 1.

Typing is held in memory and nowhere else: notifications expire in seconds,
are worthless across a restart, and arrive one per keystroke-burst per
room, so persisting them would be the busiest table in the database holding
the least valuable data in it. Receipts only ever advance, compared on the
position of the event acknowledged rather than on arrival, because two of a
user's clients read at different speeds and the slower one must not drag
the marker backwards. To-device queues are per device and drained by the
next sync rather than by the one that delivered them, so a client that
crashes in between gets the batch again — the right way round, since a lost
key share leaves a message permanently undecryptable and a duplicate is
ignored. A wildcard addressee is expanded when sent, not stored, because
"every device" means the ones that existed then.

Presence is off by default, which answers PLAN.md open question 3. It is
the noisiest thing in Matrix and the one genuinely fan-out notification
here; a private homeserver gains little from it. With it off the endpoints
refuse rather than disappearing, because a client that gets a 404 from
/presence concludes the server is too old to have it and stops asking.

storage: one migration per driver adding the receipt, to-device, presence
and filter tables with a counter row each, and a stream position on the
existing account data table rather than a parallel one — there is exactly
one current value per (user, room, type), and a second table would be a
second thing to keep in step. Account data is reported in an event list but
is not an event, so it gets its own response type: a room event with an
empty sender and no timestamp would make a client decide what that meant.

Deliberately missing, each named in docs/sync.md: notification counts (they
need push rules, Phase 8), device lists (the keyserver, Phase 7), anything
remote (Phase 5), threads (Phase 10). device_lists is sent as two empty
lists because a client reads a missing one as malformed; the notification
counts are left out entirely, because a zero there is a claim and an absent
field is not.

Tested through HTTP rather than against the builder, because what is worth
pinning down is the response a client sees — which section a room lands in,
whether a token handed back can be handed in again, whether paging
terminates and loses nothing. The storage tests run against both SQL
drivers; the notifier has its own tests for the concurrent case. A live
server was driven end to end, which is what caught the two bugs the unit
tests had missed: a backwards /messages page must be in reverse
chronological order, and `start` must name where the page began rather than
position zero when ?from= was omitted.

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

527 lines
21 KiB
Go

package clientapi_test
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/dariuszjaniak/warp/server/internal/httputil"
)
// createRoom calls POST /createRoom and returns the room ID.
func (ts *testServer) createRoom(acc account, body map[string]any) string {
ts.t.Helper()
rec := ts.do(request{
method: http.MethodPost,
path: "/_matrix/client/v3/createRoom",
body: body,
token: acc.AccessToken,
})
expectStatus(ts.t, rec, http.StatusOK)
roomID, _ := jsonBody(ts.t, rec)["room_id"].(string)
if roomID == "" {
ts.t.Fatalf("createRoom returned no room ID: %s", rec.Body.String())
}
return roomID
}
// put is a PUT with a body, which most of the room endpoints are.
func (ts *testServer) put(path, token string, body any) *httptest.ResponseRecorder {
ts.t.Helper()
return ts.do(request{method: http.MethodPut, path: path, body: body, token: token})
}
// post is a POST with a body.
func (ts *testServer) post(path, token string, body any) *httptest.ResponseRecorder {
ts.t.Helper()
return ts.do(request{method: http.MethodPost, path: path, body: body, token: token})
}
func TestCreateRoomEndpoint(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
rec := ts.do(request{
method: http.MethodPost,
path: "/_matrix/client/v3/createRoom",
body: map[string]any{
"name": "Test Room",
"topic": "Testing",
"preset": "public_chat",
"visibility": "public",
"room_alias_name": "test",
},
token: alice.AccessToken,
})
expectStatus(t, rec, http.StatusOK)
body := jsonBody(t, rec)
roomID, _ := body["room_id"].(string)
if roomID == "" || roomID[0] != '!' {
t.Fatalf("createRoom returned room_id %q", roomID)
}
if got, _ := body["room_alias"].(string); got != "#test:"+testServerName {
t.Errorf("createRoom returned room_alias %q", got)
}
// A token is required: a room has to be created by somebody.
rec = ts.do(request{
method: http.MethodPost,
path: "/_matrix/client/v3/createRoom",
body: map[string]any{},
})
expectError(t, rec, http.StatusUnauthorized, httputil.ErrCodeMissingToken)
// An unknown room version is reported as such, so a client can offer
// the user the versions that do exist.
rec = ts.do(request{
method: http.MethodPost,
path: "/_matrix/client/v3/createRoom",
body: map[string]any{"room_version": "99"},
token: alice.AccessToken,
})
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeUnsupportedVer)
// A taken alias is a conflict, not a bad request.
rec = ts.do(request{
method: http.MethodPost,
path: "/_matrix/client/v3/createRoom",
body: map[string]any{"room_alias_name": "test"},
token: alice.AccessToken,
})
expectError(t, rec, http.StatusConflict, httputil.ErrCodeRoomInUse)
}
func TestSendAndReadEvents(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{"preset": "public_chat"})
rec := ts.put("/_matrix/client/v3/rooms/"+url.PathEscape(roomID)+"/send/m.room.message/txn1",
alice.AccessToken, map[string]any{"msgtype": "m.text", "body": "hello"})
expectStatus(t, rec, http.StatusOK)
eventID, _ := jsonBody(t, rec)["event_id"].(string)
if eventID == "" || eventID[0] != '$' {
t.Fatalf("send returned event_id %q", eventID)
}
// A retry with the same transaction ID returns the first event.
rec = ts.put("/_matrix/client/v3/rooms/"+url.PathEscape(roomID)+"/send/m.room.message/txn1",
alice.AccessToken, map[string]any{"msgtype": "m.text", "body": "hello"})
expectStatus(t, rec, http.StatusOK)
if again, _ := jsonBody(t, rec)["event_id"].(string); again != eventID {
t.Errorf("a retried send returned %q, want %q", again, eventID)
}
rec = ts.get("/_matrix/client/v3/rooms/"+url.PathEscape(roomID)+"/event/"+
url.PathEscape(eventID), alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
fetched := jsonBody(t, rec)
if fetched["event_id"] != eventID {
t.Errorf("the event came back as %v", fetched["event_id"])
}
if fetched["room_id"] != roomID {
t.Errorf("the event's room_id is %v, want %q", fetched["room_id"], roomID)
}
if fetched["sender"] != alice.UserID {
t.Errorf("the event's sender is %v, want %q", fetched["sender"], alice.UserID)
}
// A client is given the ID explicitly and never the signatures: in
// every room version since 3 the ID is a hash, not a property, and the
// signatures are a server's business.
for _, forbidden := range []string{"hashes", "signatures", "auth_events", "prev_events", "depth"} {
if _, present := fetched[forbidden]; present {
t.Errorf("the client event carries %q", forbidden)
}
}
// An unknown event in a known room is a 404.
rec = ts.get("/_matrix/client/v3/rooms/"+url.PathEscape(roomID)+"/event/%24nope",
alice.AccessToken)
expectError(t, rec, http.StatusNotFound, httputil.ErrCodeNotFound)
// A malformed room ID is caught before anything is looked up.
rec = ts.get("/_matrix/client/v3/rooms/not-a-room/state", alice.AccessToken)
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeInvalidParam)
}
func TestStateEndpoints(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{"preset": "public_chat", "name": "Original"})
escaped := url.PathEscape(roomID)
// The whole state comes back as an array.
rec := ts.get("/_matrix/client/v3/rooms/"+escaped+"/state", alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
state := decode[[]map[string]any](t, rec)
if len(state) < 6 {
t.Fatalf("the room state has %d events, want the full setup: %s", len(state), rec.Body.String())
}
// One piece of state comes back as bare content, which is a quirk of
// the Client-Server API that every client relies on.
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.name/", alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["name"].(string); got != "Original" {
t.Errorf("the state content is %s", rec.Body.String())
}
// Without a trailing slash the state key is the empty string too.
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.name", alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["name"].(string); got != "Original" {
t.Errorf("the state content without a trailing slash is %s", rec.Body.String())
}
// ?format=event asks for the whole event instead.
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.name?format=event",
alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["type"].(string); got != "m.room.name" {
t.Errorf("?format=event returned %s", rec.Body.String())
}
rec = ts.put("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.topic",
alice.AccessToken, map[string]any{"topic": "Changed"})
expectStatus(t, rec, http.StatusOK)
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.topic", alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["topic"].(string); got != "Changed" {
t.Errorf("the topic is %s", rec.Body.String())
}
// State this room has never set is a 404.
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.avatar", alice.AccessToken)
expectError(t, rec, http.StatusNotFound, httputil.ErrCodeNotFound)
// A stranger cannot read the state of a room they are not in.
bob := ts.register("bob", "correct-horse-battery-staple")
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state", bob.AccessToken)
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
// Nor can they write it.
rec = ts.put("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.topic",
bob.AccessToken, map[string]any{"topic": "Hijacked"})
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
}
func TestMembershipEndpoints(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
bob := ts.register("bob", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{"preset": "public_chat"})
escaped := url.PathEscape(roomID)
rec := ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", bob.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["room_id"].(string); got != roomID {
t.Errorf("join returned room_id %q, want %q", got, roomID)
}
rec = ts.get("/_matrix/client/v3/joined_rooms", bob.AccessToken)
expectStatus(t, rec, http.StatusOK)
rooms := decode[struct {
JoinedRooms []string `json:"joined_rooms"`
}](t, rec)
if len(rooms.JoinedRooms) != 1 || rooms.JoinedRooms[0] != roomID {
t.Errorf("bob's joined rooms = %v", rooms.JoinedRooms)
}
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/joined_members", bob.AccessToken)
expectStatus(t, rec, http.StatusOK)
joined := decode[struct {
Joined map[string]struct {
DisplayName string `json:"display_name"`
} `json:"joined"`
}](t, rec)
if len(joined.Joined) != 2 {
t.Errorf("joined_members returned %d members, want 2", len(joined.Joined))
}
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/members", bob.AccessToken)
expectStatus(t, rec, http.StatusOK)
members := decode[struct {
Chunk []map[string]any `json:"chunk"`
}](t, rec)
if len(members.Chunk) != 2 {
t.Errorf("members returned %d events, want 2", len(members.Chunk))
}
// An invented membership filter is refused rather than ignored.
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/members?membership=lurking",
bob.AccessToken)
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeBadJSON)
// ?at= names a stream position to report the membership as of; the
// zero token is the start of the room, which every member postdates.
// TestMembersAtAnEarlierPosition covers what it actually returns.
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/members?at=s0_0", bob.AccessToken)
expectStatus(t, rec, http.StatusOK)
// Moderation.
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/kick", alice.AccessToken,
map[string]any{"user_id": bob.UserID, "reason": "behave"})
expectStatus(t, rec, http.StatusOK)
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/ban", alice.AccessToken,
map[string]any{"user_id": bob.UserID})
expectStatus(t, rec, http.StatusOK)
// A banned user cannot come back.
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", bob.AccessToken, map[string]any{})
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/unban", alice.AccessToken,
map[string]any{"user_id": bob.UserID})
expectStatus(t, rec, http.StatusOK)
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", bob.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/leave", bob.AccessToken, nil)
expectStatus(t, rec, http.StatusOK)
// user_id is required where it is the whole point of the call.
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/kick", alice.AccessToken, map[string]any{})
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeMissingParam)
// Moderation without the power is M_FORBIDDEN, decided by the room's
// power levels rather than by this handler.
carol := ts.register("carol", "correct-horse-battery-staple")
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", carol.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/kick", carol.AccessToken,
map[string]any{"user_id": alice.UserID})
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
}
func TestInviteEndpoint(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
bob := ts.register("bob", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{"preset": "private_chat"})
escaped := url.PathEscape(roomID)
// An invite-only room cannot be walked into.
rec := ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", bob.AccessToken, map[string]any{})
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/invite", alice.AccessToken,
map[string]any{"user_id": bob.UserID})
expectStatus(t, rec, http.StatusOK)
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", bob.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
// Inviting a user of another server needs federation, and says so.
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/invite", alice.AccessToken,
map[string]any{"user_id": "@eve:elsewhere.example"})
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
if msg, _ := jsonBody(t, rec)["error"].(string); msg == "" {
t.Error("the federation refusal carried no explanation")
}
// A third-party invite goes through an identity server, which this
// server does not talk to.
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/invite", alice.AccessToken,
map[string]any{"id_server": "id.example", "medium": "email", "address": "a@b.example"})
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeUnknown)
}
func TestRedactEndpoint(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{"preset": "public_chat"})
escaped := url.PathEscape(roomID)
rec := ts.put("/_matrix/client/v3/rooms/"+escaped+"/send/m.room.message/t1",
alice.AccessToken, map[string]any{"msgtype": "m.text", "body": "oops"})
expectStatus(t, rec, http.StatusOK)
eventID, _ := jsonBody(t, rec)["event_id"].(string)
rec = ts.put("/_matrix/client/v3/rooms/"+escaped+"/redact/"+
url.PathEscape(eventID)+"/r1", alice.AccessToken, map[string]any{"reason": "a mistake"})
expectStatus(t, rec, http.StatusOK)
redaction, _ := jsonBody(t, rec)["event_id"].(string)
if redaction == "" || redaction == eventID {
t.Fatalf("redact returned event_id %q", redaction)
}
// The event is readable and empty, and says why.
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/event/"+url.PathEscape(eventID),
alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
fetched := jsonBody(t, rec)
content, _ := fetched["content"].(map[string]any)
if len(content) != 0 {
t.Errorf("the redacted event still has content: %v", content)
}
unsigned, _ := fetched["unsigned"].(map[string]any)
because, _ := unsigned["redacted_because"].(map[string]any)
if because["event_id"] != redaction {
t.Errorf("unsigned.redacted_because = %v, want the redaction %q", because, redaction)
}
// A malformed event ID in the path is caught before anything else.
rec = ts.put("/_matrix/client/v3/rooms/"+escaped+"/redact/nope/r2",
alice.AccessToken, map[string]any{})
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeInvalidParam)
}
func TestDirectoryEndpoints(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
bob := ts.register("bob", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{"preset": "public_chat"})
escaped := url.PathEscape(roomID)
const alias = "#chat:" + testServerName
escapedAlias := url.PathEscape(alias)
rec := ts.put("/_matrix/client/v3/directory/room/"+escapedAlias, alice.AccessToken,
map[string]any{"room_id": roomID})
expectStatus(t, rec, http.StatusOK)
// Resolving an alias needs no token: a client handed a link has to be
// able to follow it before it decides whether to log in.
rec = ts.get("/_matrix/client/v3/directory/room/"+escapedAlias, "")
expectStatus(t, rec, http.StatusOK)
resolved := jsonBody(t, rec)
if resolved["room_id"] != roomID {
t.Errorf("the alias resolves to %v, want %q", resolved["room_id"], roomID)
}
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/aliases", alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
aliases := decode[struct {
Aliases []string `json:"aliases"`
}](t, rec)
if len(aliases.Aliases) != 1 || aliases.Aliases[0] != alias {
t.Errorf("the room's aliases = %v", aliases.Aliases)
}
// An unknown alias is a 404.
rec = ts.get("/_matrix/client/v3/directory/room/"+url.PathEscape("#nope:"+testServerName), "")
expectError(t, rec, http.StatusNotFound, httputil.ErrCodeNotFound)
// The public room directory.
rec = ts.get("/_matrix/client/v3/publicRooms", "")
expectStatus(t, rec, http.StatusOK)
if total, _ := jsonBody(t, rec)["total_room_count_estimate"].(float64); total != 0 {
t.Errorf("an unpublished room is in the directory (total %v)", total)
}
rec = ts.put("/_matrix/client/v3/directory/list/room/"+escaped, alice.AccessToken,
map[string]any{"visibility": "public"})
expectStatus(t, rec, http.StatusOK)
rec = ts.get("/_matrix/client/v3/directory/list/room/"+escaped, "")
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["visibility"].(string); got != "public" {
t.Errorf("visibility = %q, want public", got)
}
rec = ts.get("/_matrix/client/v3/publicRooms?limit=10", "")
expectStatus(t, rec, http.StatusOK)
directory := decode[struct {
Chunk []map[string]any `json:"chunk"`
Total int `json:"total_room_count_estimate"`
}](t, rec)
if directory.Total != 1 || len(directory.Chunk) != 1 {
t.Fatalf("the directory has %d of %d rooms, want 1 of 1",
len(directory.Chunk), directory.Total)
}
if directory.Chunk[0]["room_id"] != roomID {
t.Errorf("the directory lists %v, want %q", directory.Chunk[0]["room_id"], roomID)
}
// The POST form takes a filter.
rec = ts.post("/_matrix/client/v3/publicRooms", alice.AccessToken, map[string]any{
"filter": map[string]any{"generic_search_term": "nothing matches this"},
})
expectStatus(t, rec, http.StatusOK)
filtered := decode[struct {
Chunk []map[string]any `json:"chunk"`
}](t, rec)
if len(filtered.Chunk) != 0 {
t.Errorf("a search that matches nothing returned %d rooms", len(filtered.Chunk))
}
// An ordinary member cannot publish or unpublish the room.
rec = ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", bob.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
rec = ts.put("/_matrix/client/v3/directory/list/room/"+escaped, bob.AccessToken,
map[string]any{"visibility": "private"})
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
// Another server's directory needs federation.
rec = ts.get("/_matrix/client/v3/publicRooms?server=other.example", "")
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
// A bad limit is a bad request rather than a default.
rec = ts.get("/_matrix/client/v3/publicRooms?limit=lots", "")
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeInvalidParam)
}
// TestJoinByAlias is the endpoint a client uses when all it was given is a
// link.
func TestJoinByAlias(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
bob := ts.register("bob", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{
"preset": "public_chat", "room_alias_name": "open",
})
alias := "#open:" + testServerName
rec := ts.post("/_matrix/client/v3/join/"+url.PathEscape(alias), bob.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["room_id"].(string); got != roomID {
t.Errorf("joining by alias returned %q, want %q", got, roomID)
}
// The same endpoint takes a room ID.
rec = ts.post("/_matrix/client/v3/join/"+url.PathEscape(roomID), bob.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
// Anything that is neither is a bad request.
rec = ts.post("/_matrix/client/v3/join/nonsense", bob.AccessToken, map[string]any{})
expectError(t, rec, http.StatusBadRequest, httputil.ErrCodeInvalidParam)
// A room on another server needs federation.
rec = ts.post("/_matrix/client/v3/join/"+url.PathEscape("!x:other.example"),
bob.AccessToken, map[string]any{})
expectError(t, rec, http.StatusForbidden, httputil.ErrCodeForbidden)
}
// TestStateKeyWithSpecialCharacters is about the router, not the rooms: a
// membership state key is a user ID, so it carries an '@' and a ':', and
// the empty state key arrives in two spellings. All of them have to route.
func TestStateKeyWithSpecialCharacters(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "correct-horse-battery-staple")
bob := ts.register("bob", "correct-horse-battery-staple")
roomID := ts.createRoom(alice, map[string]any{"preset": "public_chat"})
escaped := url.PathEscape(roomID)
rec := ts.post("/_matrix/client/v3/rooms/"+escaped+"/join", bob.AccessToken, map[string]any{})
expectStatus(t, rec, http.StatusOK)
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.member/"+
url.PathEscape(bob.UserID), alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["membership"].(string); got != "join" {
t.Errorf("bob's membership content is %s", rec.Body.String())
}
// And writing one back through the state endpoint.
rec = ts.put("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.member/"+
url.PathEscape(bob.UserID), alice.AccessToken,
map[string]any{"membership": "ban"})
expectStatus(t, rec, http.StatusOK)
rec = ts.get("/_matrix/client/v3/rooms/"+escaped+"/state/m.room.member/"+
url.PathEscape(bob.UserID), alice.AccessToken)
expectStatus(t, rec, http.StatusOK)
if got, _ := jsonBody(t, rec)["membership"].(string); got != "ban" {
t.Errorf("bob's membership after the ban is %s", rec.Body.String())
}
}