Files
J3vbandClaude Fable 5 6afa9e974c refactor(server): thread context.Context through the db layer and all callers
Fixes all 109 golangci-lint findings (106 contextcheck, 1 gocritic,
2 gosec) that accumulated after D2 wired dbgen (whose queries take ctx)
under ctx-less db.DB wrappers while CI lint was quota-dead. No nolint
comments added; every finding fixed by genuinely threading context.

- db: all 138 hand-written db.DB methods take ctx first; the dbCtx()
  Background shim is deleted; raw Query/QueryRow/Exec/Begin use their
  Context variants; the four redundant ctx-less passthroughs removed.
  db.Auditor/WriteAudit gain ctx.
- Seams: permissions.Checker (DB iface, HasChannelPerm,
  RequireChannelAccess) and the service.Store interface mirror the new
  signatures (ws.EventStore and plugin.PluginStore already did).
- Callers: api/admin handlers use r.Context(); ws per-message paths use
  the connection ctx via DispatchV2; hub loops and startup wiring use
  context.Background(); service methods thread ctx where they have one
  and Background where no ctx exists. Public service surface reached by
  ctx-holding chains (PermissionService.HasChannelPerm/GetRoleForUser/
  RequireChannelAccess, message/dm/block/invite/profile methods) is now
  ctx-first.
- Detached (context.WithoutCancel) where cancellation would break an
  invariant, found by a 3-lens adversarial review of the diff:
  * voice-leave background retries (a dead webhook/connection ctx killed
    retry 2 before it ran, leaving ghost capacity-holding voice rows)
  * rollbackVoiceJoin's compensating delete (its trigger IS the cancel)
  * post-2FA-change DeleteOtherSessions and logout DeleteSession (the
    security tail of a committed change must not die with the request)
  * all api/ws audit writes (a banned user could suppress their own
    login_blocked_banned row by aborting the request mid-bcrypt)
  * admin backup VACUUM INTO (an interrupt left a truncated .db that
    the backup list presented as restorable)
  * post-commit message/edit refetches (a committed message must still
    fan out when the sender disconnects)
  * hub settings-cache refresh (one dead connection could pin stale
    values for the 30s TTL)
- gocritic rangeValCopy fixed (index iteration); gosec G306 excluded in
  config with justification (generated source must stay world-readable)
  instead of flipping genprotocol output to 0o600.

Verified: gofmt/vet, all four build-tag variants, full suite, deadlock
pass, full -race pass, golangci-lint 0 issues uncapped.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-23 17:03:52 +02:00

183 lines
5.9 KiB
Go

package api_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
)
// ─── Contract tests: verify REST responses match API.md shapes ──────────────
// These tests assert that responses include all documented fields with the
// correct types, catching drift between implementation and specification.
// ─── GET /api/v1/channels/{id}/messages: response shape ─────────────────────
func TestContract_Messages_HasRequiredFields(t *testing.T) {
database := newChannelTestDB(t)
router := buildChannelRouter(database)
token := chTestCreateToken(t, database, "contract-msg1", 1)
user, _ := database.GetUserByUsername(context.Background(), "contract-msg1")
chID, _ := database.CreateChannel(context.Background(), "contract-ch", "text", "", "", 0)
_, _ = database.CreateMessage(context.Background(), chID, user.ID, "contract test message", nil)
rr := chGet(t, router, fmt.Sprintf("/api/v1/channels/%d/messages", chID), token)
if rr.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body: %s", rr.Code, rr.Body.String())
}
var resp struct {
Messages []json.RawMessage `json:"messages"`
HasMore *bool `json:"has_more"`
}
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.HasMore == nil {
t.Error("response missing 'has_more' field")
}
if len(resp.Messages) == 0 {
t.Fatal("expected at least 1 message")
}
// Parse the first message and verify all API.md fields are present.
var msg map[string]any
if err := json.Unmarshal(resp.Messages[0], &msg); err != nil {
t.Fatalf("decode message: %v", err)
}
requiredFields := []string{
"id", "channel_id", "user", "content", "reply_to",
"attachments", "reactions", "pinned", "edited_at",
"deleted", "timestamp",
}
for _, field := range requiredFields {
if _, ok := msg[field]; !ok {
t.Errorf("message missing required field %q (per API.md)", field)
}
}
// Verify 'user' is an object with id, username.
userObj, ok := msg["user"].(map[string]any)
if !ok {
t.Fatal("'user' is not an object")
}
for _, f := range []string{"id", "username"} {
if _, ok := userObj[f]; !ok {
t.Errorf("user object missing field %q", f)
}
}
// Verify 'attachments' is an array (even if empty).
if _, ok := msg["attachments"].([]any); !ok {
t.Error("'attachments' is not an array")
}
// Verify 'reactions' is an array (even if empty).
if _, ok := msg["reactions"].([]any); !ok {
t.Error("'reactions' is not an array")
}
}
// TestContract_Messages_ReactionsHaveMeFlag verifies that when a reaction
// exists, the response includes the 'me' boolean per API.md.
func TestContract_Messages_ReactionsHaveMeFlag(t *testing.T) {
database := newChannelTestDB(t)
router := buildChannelRouter(database)
token := chTestCreateToken(t, database, "contract-react1", 1)
user, _ := database.GetUserByUsername(context.Background(), "contract-react1")
chID, _ := database.CreateChannel(context.Background(), "react-ch", "text", "", "", 0)
msgID, _ := database.CreateMessage(context.Background(), chID, user.ID, "reaction target", nil)
_ = database.AddReaction(context.Background(), msgID, user.ID, "👍")
rr := chGet(t, router, fmt.Sprintf("/api/v1/channels/%d/messages", chID), token)
if rr.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rr.Code)
}
var resp struct {
Messages []struct {
Reactions []struct {
Emoji string `json:"emoji"`
Count int `json:"count"`
Me *bool `json:"me"`
} `json:"reactions"`
} `json:"messages"`
}
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
t.Fatalf("decode: %v", err)
}
if len(resp.Messages) == 0 {
t.Fatal("expected at least 1 message")
}
if len(resp.Messages[0].Reactions) == 0 {
t.Fatal("expected at least 1 reaction")
}
r := resp.Messages[0].Reactions[0]
if r.Emoji != "👍" {
t.Errorf("emoji = %q, want 👍", r.Emoji)
}
if r.Count != 1 {
t.Errorf("count = %d, want 1", r.Count)
}
if r.Me == nil {
t.Error("reaction missing 'me' boolean field (per API.md)")
} else if !*r.Me {
t.Error("me = false, want true (requesting user added the reaction)")
}
}
// ─── GET /api/v1/search: response shape ─────────────────────────────────────
func TestContract_Search_HasRequiredFields(t *testing.T) {
database := newChannelTestDB(t)
router := buildChannelRouter(database)
token := chTestCreateToken(t, database, "contract-search1", 1)
user, _ := database.GetUserByUsername(context.Background(), "contract-search1")
chID, _ := database.CreateChannel(context.Background(), "search-ch", "text", "", "", 0)
_, _ = database.CreateMessage(context.Background(), chID, user.ID, "contractsearchterm in body", nil)
rr := chGet(t, router, "/api/v1/search?q=contractsearchterm", token)
if rr.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []json.RawMessage `json:"results"`
}
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
t.Fatalf("decode: %v", err)
}
if len(resp.Results) == 0 {
t.Fatal("expected at least 1 search result")
}
var result map[string]any
if err := json.Unmarshal(resp.Results[0], &result); err != nil {
t.Fatalf("decode result: %v", err)
}
// Per API.md, search results must have these fields.
requiredFields := []string{
"message_id", "channel_id", "channel_name", "user",
"content", "timestamp",
}
for _, field := range requiredFields {
if _, ok := result[field]; !ok {
t.Errorf("search result missing required field %q (per API.md)", field)
}
}
// Verify 'user' is an object with id and username.
userObj, ok := result["user"].(map[string]any)
if !ok {
t.Fatal("search result 'user' is not an object")
}
for _, f := range []string{"id", "username"} {
if _, ok := userObj[f]; !ok {
t.Errorf("search result user object missing field %q", f)
}
}
}