mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
`Server/go.mod` declared `github.com/owncord/server` while the public repository is `github.com/J3vb/OwnCord`. Nothing resolves that path — there is no `owncord` GitHub org and no vanity-import host serving go-import metadata for it — so every import line in the tree named a location that does not exist. It compiles because a main module's own path is never fetched, which is exactly why it went unnoticed. The obvious fix — an AST-aware import rewriter (`gomvpkg`, `go mod edit`) — is wrong here, and provably so. Six of the 722 occurrences are not imports at all: `api/main_test.go:20` (a goleak `IgnoreTopFunction` pattern), `telemetry/metrics.go:17-19` (three OTel instrumentation-scope names), `invariants/syncutil_locks.go:73` (a diagnostic message), and `invariants/syncutil_locks_test.go:56` (an import line inside a raw-string Go fixture). An import rewriter touches none of them, and the compiler cannot see any of them either. Done as one scripted substitution over `git ls-files`, anchored on the full `github.com/owncord/server` string. The anchor matters: `owncord-server` is a different identifier — the OTel `service.name` (`config/config.go`, `telemetry/telemetry_otel.go`) and the GHCR image name (`.github/workflows/release.yml`, `docker-compose.yml`) — and a looser pattern would have moved it. It is untouched: 10 occurrences across 9 files, before and after. 350 files, 728 insertions, 728 deletions. 722 occurrences in 344 Go files, plus `go.mod:1`, the `sed` at `Makefile:67`, `Server/CLAUDE.md:3`, `docs/architecture/server.md:5`, and the ledger pair (`findings-ledger.json:3758` plus a `render-ledger.mjs` re-render of `FINDINGS.md`). Zero in any workflow, zero in the Dockerfile, zero in `Server/.golangci.yml` (no `local-prefixes`, `gci`, `importas` or `depguard` rule keys on the module path, so import grouping is not configured anywhere). The plan's blast-radius estimate missed one thing, and it is the one that would have gone red: **gofmt**. `J` (0x4A) sorts before every lowercase letter, so in the 36 files where a module-local import shares a contiguous group with a third-party one, the module's imports must move above `github.com/go-chi/...`. `gofmt -l` was clean before the substitution and listed exactly 36 files after it; `gofmt -w` on those 36 restores it to clean. `gofmt` is an enforced gate — the `formatters` block in `Server/.golangci.yml`, which is S-05 — so a substitution-only commit fails Lint. Verified: both directions, and the line accounting is exact. Every added line in this diff contains the new module path (728) and every removed line contains the old one (728); the count of changed lines containing neither is **zero**, so the gofmt re-sort moved module-path lines only and touched no third-party import. The residual check (`git ls-files -z | xargs -0 grep -n 'github\.com/owncord/server'`) returns exactly two hits, both deliberately out of scope: the RL-13 row in `docs/audit-2026-08-23-repository-layout.md` and the measurement row in this phase's own plan. The compiler-invisible half was proven by reverting *only* `api/main_test.go:20` to the old path on the otherwise-renamed tree: `go build ./...` and `go vet ./api/` both still pass — they see nothing wrong — while `go test ./api/` FAILS, because the runtime function name now carries the new path and goleak stops ignoring `ws.(*Hub).Run.func1`. Restoring the line makes it pass. `go.sum` is byte-identical (no `go mod tidy` was run and none was needed). All four build-tag variants compile; `go vet ./...`, `go vet -tags otel,wazero ./...` and `go vet -tags deadlock ./...` pass; `go test -race ./...` is 16/16 packages green; `go test -tags deadlock ./...` passes; the tag-gated `./plugin/...` (wazero) and `./telemetry/...` (otel) runs pass. `golangci-lint` v2.11.3 — the pinned CI version, rebuilt locally against Go 1.26 because the packaged binary cannot load a 1.26 config — reports **0 issues**. `go run ./cmd/genprotocol` leaves `git diff --exit-code ws/message_types.go ../Client/src/lib/protocolTypes.ts` clean, so the rename does not reach the generated protocol constants. `npx prettier --check .` and `node .superpowers/render-ledger.mjs --check` pass. Not included: `docs/audit-2026-08-23-repository-layout.md` and `docs/plans/b1-repository-foundation-2026-08-25.md` keep the old path — they are the audit row and the measurement that motivated this change, and rewriting them would erase the record of what was measured. They are why the residual check needs a two-path allowance rather than being empty; that allowance is stated above rather than hidden in a pathspec. `telemetry/metrics.go:19` declares `scopeVoice` for a `Server/voice` package that does not exist; the substitution carried the dead path forward verbatim as `github.com/J3vb/OwnCord/Server/voice` rather than fixing it, because correcting a real observability bug inside a mechanical rename would hide it in a 350-file diff. It needs its own item. No `go.work`, no second module, and no vanity-import host was set up — the new path resolves against the real repository, but nothing imports this module as a library, so `go get` reachability was not exercised either way. Refs RL-13, L-12
617 lines
20 KiB
Go
617 lines
20 KiB
Go
package api_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/J3vb/OwnCord/Server/api"
|
|
"github.com/J3vb/OwnCord/Server/auth"
|
|
"github.com/J3vb/OwnCord/Server/db"
|
|
"github.com/J3vb/OwnCord/Server/service"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
// ─── DM test schema ─────────────────────────────────────────────────────────
|
|
|
|
// dmTestSchema includes roles, users, sessions, channels, messages, and DM
|
|
// tables needed by DM handler tests.
|
|
var dmTestSchema = []byte(`
|
|
CREATE TABLE IF NOT EXISTS roles (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL UNIQUE,
|
|
color TEXT,
|
|
permissions INTEGER NOT NULL DEFAULT 0,
|
|
position INTEGER NOT NULL DEFAULT 0,
|
|
is_default INTEGER NOT NULL DEFAULT 0
|
|
);
|
|
INSERT OR IGNORE INTO roles (id, name, color, permissions, position, is_default) VALUES
|
|
(1, 'Owner', '#E74C3C', 2147483647, 100, 0),
|
|
(2, 'Admin', '#F39C12', 1073741823, 80, 0),
|
|
(3, 'Moderator', '#3498DB', 1048575, 60, 0),
|
|
(4, 'Member', NULL, 1635, 40, 1);
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
|
password TEXT NOT NULL,
|
|
avatar TEXT,
|
|
role_id INTEGER NOT NULL DEFAULT 4 REFERENCES roles(id),
|
|
totp_secret TEXT,
|
|
status TEXT NOT NULL DEFAULT 'offline',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
last_seen TEXT,
|
|
banned INTEGER NOT NULL DEFAULT 0,
|
|
ban_reason TEXT,
|
|
ban_expires TEXT,
|
|
identity_public_key TEXT,
|
|
display_name TEXT,
|
|
about TEXT,
|
|
custom_status TEXT
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
token TEXT NOT NULL UNIQUE,
|
|
device TEXT,
|
|
ip_address TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
last_used TEXT NOT NULL DEFAULT (datetime('now')),
|
|
expires_at TEXT NOT NULL
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions(token);
|
|
|
|
-- AuthMiddleware falls through to an API-token lookup whenever a bearer
|
|
-- token matches no session (auth.ResolveTokenHash), so this table must exist
|
|
-- even in DM-only fixtures — otherwise an ordinary "no such session" lookup
|
|
-- for a garbage/unknown token hits GetActiveAPIToken and fails with a real
|
|
-- "no such table" SQL error instead of the intended not-found sentinel.
|
|
CREATE TABLE IF NOT EXISTS api_tokens (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
token_hash TEXT NOT NULL UNIQUE,
|
|
label TEXT NOT NULL DEFAULT '',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
last_used_at TEXT,
|
|
expires_at TEXT,
|
|
revoked_at TEXT
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS channels (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
type TEXT NOT NULL DEFAULT 'text',
|
|
category TEXT,
|
|
topic TEXT,
|
|
position INTEGER NOT NULL DEFAULT 0,
|
|
slow_mode INTEGER NOT NULL DEFAULT 0,
|
|
archived INTEGER NOT NULL DEFAULT 0,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
voice_max_users INTEGER NOT NULL DEFAULT 0,
|
|
voice_quality TEXT,
|
|
mixing_threshold INTEGER,
|
|
voice_max_video INTEGER NOT NULL DEFAULT 0,
|
|
nsfw INTEGER NOT NULL DEFAULT 0,
|
|
is_group INTEGER NOT NULL DEFAULT 0
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS messages (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
channel_id INTEGER NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
|
|
user_id INTEGER NOT NULL REFERENCES users(id),
|
|
content TEXT NOT NULL,
|
|
reply_to INTEGER REFERENCES messages(id) ON DELETE SET NULL,
|
|
edited_at TEXT,
|
|
deleted INTEGER NOT NULL DEFAULT 0,
|
|
pinned INTEGER NOT NULL DEFAULT 0,
|
|
timestamp TEXT NOT NULL DEFAULT (datetime('now')),
|
|
mentions_everyone INTEGER NOT NULL DEFAULT 0
|
|
);
|
|
CREATE TABLE IF NOT EXISTS message_mentions (
|
|
message_id INTEGER NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
|
mentioned_user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
PRIMARY KEY (message_id, mentioned_user_id)
|
|
);
|
|
|
|
-- Mirrors migrations/001. Required, not optional decoration: the DM close
|
|
-- path (db.LeaveGroupDM) unlinks a channel's attachments before hard-deleting
|
|
-- the channel row, because messages.channel_id and attachments.message_id both
|
|
-- cascade ON DELETE and the cascade would otherwise destroy the rows the
|
|
-- orphan sweep needs to reclaim the files. Without this table the handler
|
|
-- answers 500.
|
|
CREATE TABLE IF NOT EXISTS attachments (
|
|
id TEXT PRIMARY KEY,
|
|
message_id INTEGER REFERENCES messages(id) ON DELETE CASCADE,
|
|
filename TEXT NOT NULL,
|
|
stored_as TEXT NOT NULL,
|
|
mime_type TEXT NOT NULL,
|
|
size INTEGER NOT NULL,
|
|
uploaded_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS dm_participants (
|
|
channel_id INTEGER NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
|
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
PRIMARY KEY (channel_id, user_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS dm_open_state (
|
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
channel_id INTEGER NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
|
|
opened_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
PRIMARY KEY (user_id, channel_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS read_states (
|
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
channel_id INTEGER NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
|
|
last_message_id INTEGER NOT NULL DEFAULT 0,
|
|
mention_count INTEGER NOT NULL DEFAULT 0,
|
|
PRIMARY KEY (user_id, channel_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS user_blocks (
|
|
blocker_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
blocked_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
PRIMARY KEY (blocker_id, blocked_id),
|
|
CHECK (blocker_id != blocked_id)
|
|
);
|
|
`)
|
|
|
|
// ─── helpers ────────────────────────────────────────────────────────────────
|
|
|
|
func newDMTestDB(t *testing.T) *db.DB {
|
|
t.Helper()
|
|
database, err := db.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatalf("db.Open: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = database.Close() })
|
|
migrFS := fstest.MapFS{"001_schema.sql": {Data: dmTestSchema}}
|
|
if err := db.MigrateFS(database, migrFS); err != nil {
|
|
t.Fatalf("MigrateFS: %v", err)
|
|
}
|
|
return database
|
|
}
|
|
|
|
// mockBroadcaster implements api.DMBroadcaster for tests.
|
|
type mockBroadcaster struct {
|
|
sent []mockBroadcastMsg
|
|
}
|
|
|
|
type mockBroadcastMsg struct {
|
|
UserID int64
|
|
Msg []byte
|
|
}
|
|
|
|
func (m *mockBroadcaster) SendToUser(userID int64, msg []byte) bool {
|
|
m.sent = append(m.sent, mockBroadcastMsg{UserID: userID, Msg: msg})
|
|
return true
|
|
}
|
|
|
|
// dmCreateToken creates a user+session and returns the plaintext token.
|
|
func dmCreateToken(t *testing.T, database *db.DB, username string, roleID int) string {
|
|
t.Helper()
|
|
_, err := database.CreateUser(context.Background(), username, "$2a$12$fake", roleID)
|
|
if err != nil {
|
|
t.Fatalf("CreateUser %q: %v", username, err)
|
|
}
|
|
token := "dmtest-token-" + username
|
|
hash := auth.HashToken(token)
|
|
_, err = database.ExecContext(context.Background(),
|
|
`INSERT INTO sessions (user_id, token, device, ip_address, expires_at)
|
|
SELECT id, ?, 'test', '127.0.0.1', '2099-01-01T00:00:00Z' FROM users WHERE username = ?`,
|
|
hash, username,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("insert session for %q: %v", username, err)
|
|
}
|
|
return token
|
|
}
|
|
|
|
func buildDMRouter(database *db.DB, broadcaster api.DMBroadcaster) http.Handler {
|
|
r := chi.NewRouter()
|
|
svc := service.New(database, auth.NewRateLimiter())
|
|
api.MountDMRoutes(r, database, svc, broadcaster)
|
|
return r
|
|
}
|
|
|
|
func dmPost(t *testing.T, router http.Handler, path, token string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
raw, _ := json.Marshal(body)
|
|
req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(raw))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
req.RemoteAddr = "127.0.0.1:9999"
|
|
rr := httptest.NewRecorder()
|
|
router.ServeHTTP(rr, req)
|
|
return rr
|
|
}
|
|
|
|
func dmGet(t *testing.T, router http.Handler, path, token string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
req.RemoteAddr = "127.0.0.1:9999"
|
|
rr := httptest.NewRecorder()
|
|
router.ServeHTTP(rr, req)
|
|
return rr
|
|
}
|
|
|
|
func dmDelete(t *testing.T, router http.Handler, path, token string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodDelete, path, nil)
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
req.RemoteAddr = "127.0.0.1:9999"
|
|
rr := httptest.NewRecorder()
|
|
router.ServeHTTP(rr, req)
|
|
return rr
|
|
}
|
|
|
|
// ─── POST /api/v1/dms (handleCreateDM) ─────────────────────────────────────
|
|
|
|
func TestCreateDM_Success_NewDM(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
broadcaster := &mockBroadcaster{}
|
|
router := buildDMRouter(database, broadcaster)
|
|
|
|
tokenAlice := dmCreateToken(t, database, "alice", 4)
|
|
_ = dmCreateToken(t, database, "bob", 4)
|
|
bob, _ := database.GetUserByUsername(context.Background(), "bob")
|
|
|
|
rr := dmPost(t, router, "/api/v1/dms", tokenAlice, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
|
|
if rr.Code != http.StatusCreated {
|
|
t.Errorf("CreateDM new: status = %d, want 201; body = %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
_ = json.NewDecoder(rr.Body).Decode(&resp)
|
|
if resp["created"] != true {
|
|
t.Errorf("CreateDM new: created = %v, want true", resp["created"])
|
|
}
|
|
if resp["channel_id"] == nil {
|
|
t.Error("CreateDM new: missing channel_id")
|
|
}
|
|
recipient, ok := resp["recipient"].(map[string]any)
|
|
if !ok || recipient["username"] != "bob" {
|
|
t.Errorf("CreateDM new: recipient = %v, want bob", resp["recipient"])
|
|
}
|
|
}
|
|
|
|
func TestCreateDM_Success_ExistingDM(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
broadcaster := &mockBroadcaster{}
|
|
router := buildDMRouter(database, broadcaster)
|
|
|
|
tokenAlice := dmCreateToken(t, database, "alice2", 4)
|
|
_ = dmCreateToken(t, database, "bob2", 4)
|
|
bob, _ := database.GetUserByUsername(context.Background(), "bob2")
|
|
|
|
// First call creates the DM.
|
|
rr1 := dmPost(t, router, "/api/v1/dms", tokenAlice, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
if rr1.Code != http.StatusCreated {
|
|
t.Fatalf("first CreateDM: status = %d, want 201", rr1.Code)
|
|
}
|
|
|
|
// Second call returns the existing one.
|
|
rr2 := dmPost(t, router, "/api/v1/dms", tokenAlice, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
if rr2.Code != http.StatusOK {
|
|
t.Errorf("existing CreateDM: status = %d, want 200; body = %s", rr2.Code, rr2.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
_ = json.NewDecoder(rr2.Body).Decode(&resp)
|
|
if resp["created"] != false {
|
|
t.Errorf("existing CreateDM: created = %v, want false", resp["created"])
|
|
}
|
|
}
|
|
|
|
func TestCreateDM_BadRequest_EmptyBody(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
token := dmCreateToken(t, database, "empty_body_user", 4)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/dms", bytes.NewReader([]byte("")))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.RemoteAddr = "127.0.0.1:9999"
|
|
rr := httptest.NewRecorder()
|
|
router.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("empty body: status = %d, want 400", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateDM_BadRequest_NegativeRecipientID(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
token := dmCreateToken(t, database, "neg_user", 4)
|
|
|
|
rr := dmPost(t, router, "/api/v1/dms", token, map[string]any{
|
|
"recipient_id": -1,
|
|
})
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("negative recipient_id: status = %d, want 400", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateDM_BadRequest_ZeroRecipientID(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
token := dmCreateToken(t, database, "zero_user", 4)
|
|
|
|
rr := dmPost(t, router, "/api/v1/dms", token, map[string]any{
|
|
"recipient_id": 0,
|
|
})
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("zero recipient_id: status = %d, want 400", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateDM_BadRequest_SelfDM(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
token := dmCreateToken(t, database, "selfuser", 4)
|
|
self, _ := database.GetUserByUsername(context.Background(), "selfuser")
|
|
|
|
rr := dmPost(t, router, "/api/v1/dms", token, map[string]any{
|
|
"recipient_id": self.ID,
|
|
})
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("self DM: status = %d, want 400; body = %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCreateDM_NotFound_RecipientMissing(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
token := dmCreateToken(t, database, "lonely_user", 4)
|
|
|
|
rr := dmPost(t, router, "/api/v1/dms", token, map[string]any{
|
|
"recipient_id": 99999,
|
|
})
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Errorf("missing recipient: status = %d, want 404", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateDM_Unauthorized(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
|
|
rr := dmPost(t, router, "/api/v1/dms", "", map[string]any{
|
|
"recipient_id": 1,
|
|
})
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Errorf("no auth: status = %d, want 401", rr.Code)
|
|
}
|
|
}
|
|
|
|
// ─── GET /api/v1/dms (handleListDMs) ────────────────────────────────────────
|
|
|
|
func TestListDMs_ReturnsOpenDMs(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
broadcaster := &mockBroadcaster{}
|
|
router := buildDMRouter(database, broadcaster)
|
|
|
|
tokenAlice := dmCreateToken(t, database, "list_alice", 4)
|
|
_ = dmCreateToken(t, database, "list_bob", 4)
|
|
bob, _ := database.GetUserByUsername(context.Background(), "list_bob")
|
|
|
|
// Create a DM.
|
|
rr1 := dmPost(t, router, "/api/v1/dms", tokenAlice, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
if rr1.Code != http.StatusCreated {
|
|
t.Fatalf("setup CreateDM: status = %d", rr1.Code)
|
|
}
|
|
|
|
// List DMs.
|
|
rr := dmGet(t, router, "/api/v1/dms", tokenAlice)
|
|
if rr.Code != http.StatusOK {
|
|
t.Errorf("ListDMs: status = %d, want 200; body = %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
_ = json.NewDecoder(rr.Body).Decode(&resp)
|
|
channels, ok := resp["dm_channels"].([]any)
|
|
if !ok {
|
|
t.Fatalf("ListDMs: dm_channels not an array: %v", resp)
|
|
}
|
|
if len(channels) != 1 {
|
|
t.Errorf("ListDMs: got %d channels, want 1", len(channels))
|
|
}
|
|
}
|
|
|
|
func TestListDMs_EmptyArray(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
token := dmCreateToken(t, database, "no_dms_user", 4)
|
|
|
|
rr := dmGet(t, router, "/api/v1/dms", token)
|
|
if rr.Code != http.StatusOK {
|
|
t.Errorf("ListDMs empty: status = %d, want 200", rr.Code)
|
|
}
|
|
|
|
var resp map[string]any
|
|
_ = json.NewDecoder(rr.Body).Decode(&resp)
|
|
channels, ok := resp["dm_channels"].([]any)
|
|
if !ok || len(channels) != 0 {
|
|
t.Errorf("ListDMs empty: expected empty array, got %v", resp["dm_channels"])
|
|
}
|
|
}
|
|
|
|
func TestListDMs_Unauthorized(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
|
|
rr := dmGet(t, router, "/api/v1/dms", "")
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Errorf("ListDMs no auth: status = %d, want 401", rr.Code)
|
|
}
|
|
}
|
|
|
|
// ─── DELETE /api/v1/dms/{channelId} (handleCloseDM) ────────────────────────
|
|
|
|
func TestCloseDM_Success(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
broadcaster := &mockBroadcaster{}
|
|
router := buildDMRouter(database, broadcaster)
|
|
|
|
tokenAlice := dmCreateToken(t, database, "close_alice", 4)
|
|
_ = dmCreateToken(t, database, "close_bob", 4)
|
|
bob, _ := database.GetUserByUsername(context.Background(), "close_bob")
|
|
|
|
// Create a DM.
|
|
rr1 := dmPost(t, router, "/api/v1/dms", tokenAlice, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
if rr1.Code != http.StatusCreated {
|
|
t.Fatalf("setup CreateDM: status = %d", rr1.Code)
|
|
}
|
|
var createResp map[string]any
|
|
_ = json.NewDecoder(rr1.Body).Decode(&createResp)
|
|
channelID := createResp["channel_id"]
|
|
|
|
// Close the DM.
|
|
rr := dmDelete(t, router, fmt.Sprintf("/api/v1/dms/%v", channelID), tokenAlice)
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Errorf("CloseDM: status = %d, want 204; body = %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
// Verify broadcaster was notified.
|
|
if len(broadcaster.sent) == 0 {
|
|
t.Error("CloseDM: expected broadcaster SendToUser call")
|
|
}
|
|
}
|
|
|
|
func TestCloseDM_Success_VerifyRemovedFromList(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
broadcaster := &mockBroadcaster{}
|
|
router := buildDMRouter(database, broadcaster)
|
|
|
|
tokenAlice := dmCreateToken(t, database, "closelist_alice", 4)
|
|
_ = dmCreateToken(t, database, "closelist_bob", 4)
|
|
bob, _ := database.GetUserByUsername(context.Background(), "closelist_bob")
|
|
|
|
// Create a DM.
|
|
rr1 := dmPost(t, router, "/api/v1/dms", tokenAlice, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
if rr1.Code != http.StatusCreated {
|
|
t.Fatalf("setup CreateDM: status = %d", rr1.Code)
|
|
}
|
|
var createResp map[string]any
|
|
_ = json.NewDecoder(rr1.Body).Decode(&createResp)
|
|
channelID := createResp["channel_id"]
|
|
|
|
// Close the DM.
|
|
dmDelete(t, router, fmt.Sprintf("/api/v1/dms/%v", channelID), tokenAlice)
|
|
|
|
// List should be empty for alice now.
|
|
rr := dmGet(t, router, "/api/v1/dms", tokenAlice)
|
|
var listResp map[string]any
|
|
_ = json.NewDecoder(rr.Body).Decode(&listResp)
|
|
channels := listResp["dm_channels"].([]any)
|
|
if len(channels) != 0 {
|
|
t.Errorf("CloseDM verify: expected 0 DMs after close, got %d", len(channels))
|
|
}
|
|
}
|
|
|
|
func TestCloseDM_Forbidden_NotParticipant(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
broadcaster := &mockBroadcaster{}
|
|
router := buildDMRouter(database, broadcaster)
|
|
|
|
tokenAlice := dmCreateToken(t, database, "forbid_alice", 4)
|
|
_ = dmCreateToken(t, database, "forbid_bob", 4)
|
|
tokenCharlie := dmCreateToken(t, database, "forbid_charlie", 4)
|
|
bob, _ := database.GetUserByUsername(context.Background(), "forbid_bob")
|
|
|
|
// Alice creates DM with Bob.
|
|
rr1 := dmPost(t, router, "/api/v1/dms", tokenAlice, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
if rr1.Code != http.StatusCreated {
|
|
t.Fatalf("setup CreateDM: status = %d", rr1.Code)
|
|
}
|
|
var createResp map[string]any
|
|
_ = json.NewDecoder(rr1.Body).Decode(&createResp)
|
|
channelID := createResp["channel_id"]
|
|
|
|
// Charlie (not a participant) tries to close it.
|
|
rr := dmDelete(t, router, fmt.Sprintf("/api/v1/dms/%v", channelID), tokenCharlie)
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Errorf("CloseDM not-found: status = %d, want 404; body = %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCloseDM_BadRequest_InvalidChannelID(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
token := dmCreateToken(t, database, "badid_user", 4)
|
|
|
|
rr := dmDelete(t, router, "/api/v1/dms/abc", token)
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("CloseDM bad ID: status = %d, want 400", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestCloseDM_Unauthorized(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil)
|
|
|
|
rr := dmDelete(t, router, "/api/v1/dms/1", "")
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Errorf("CloseDM no auth: status = %d, want 401", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestCloseDM_NilBroadcaster(t *testing.T) {
|
|
database := newDMTestDB(t)
|
|
router := buildDMRouter(database, nil) // nil broadcaster
|
|
|
|
token := dmCreateToken(t, database, "nilbc_alice", 4)
|
|
_ = dmCreateToken(t, database, "nilbc_bob", 4)
|
|
bob, _ := database.GetUserByUsername(context.Background(), "nilbc_bob")
|
|
|
|
// Create a DM.
|
|
rr1 := dmPost(t, router, "/api/v1/dms", token, map[string]any{
|
|
"recipient_id": bob.ID,
|
|
})
|
|
if rr1.Code != http.StatusCreated {
|
|
t.Fatalf("setup: status = %d", rr1.Code)
|
|
}
|
|
var createResp map[string]any
|
|
_ = json.NewDecoder(rr1.Body).Decode(&createResp)
|
|
channelID := createResp["channel_id"]
|
|
|
|
// Close should still succeed even with nil broadcaster.
|
|
rr := dmDelete(t, router, fmt.Sprintf("/api/v1/dms/%v", channelID), token)
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Errorf("CloseDM nil broadcaster: status = %d, want 204", rr.Code)
|
|
}
|
|
}
|