mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
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>
201 lines
6.5 KiB
Go
201 lines
6.5 KiB
Go
package db_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/owncord/server/db"
|
|
)
|
|
|
|
// ─── DeleteAccount — last admin guard ────────────────────────────────────────
|
|
|
|
func TestDeleteAccount_LastOwnerBlocked(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
// Create a single owner (role_id=1). No other admins exist.
|
|
ownerID := seedUser(t, database, "owner")
|
|
setRole(t, database, ownerID, 1) // Owner
|
|
|
|
err := database.DeleteAccount(context.Background(), ownerID)
|
|
if !errors.Is(err, db.ErrLastAdmin) {
|
|
t.Errorf("DeleteAccount(last owner) = %v, want ErrLastAdmin", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_LastAdminBlocked(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
adminID := seedUser(t, database, "admin")
|
|
setRole(t, database, adminID, 2) // Admin
|
|
|
|
err := database.DeleteAccount(context.Background(), adminID)
|
|
if !errors.Is(err, db.ErrLastAdmin) {
|
|
t.Errorf("DeleteAccount(last admin) = %v, want ErrLastAdmin", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_AllowedWhenOtherAdminExists(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
admin1 := seedUser(t, database, "admin1")
|
|
admin2 := seedUser(t, database, "admin2")
|
|
setRole(t, database, admin1, 2) // Admin
|
|
setRole(t, database, admin2, 2) // Admin
|
|
|
|
err := database.DeleteAccount(context.Background(), admin1)
|
|
if err != nil {
|
|
t.Fatalf("DeleteAccount with another admin present: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_AdminAllowedWhenOwnerExists(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
ownerID := seedUser(t, database, "owner")
|
|
adminID := seedUser(t, database, "admin")
|
|
setRole(t, database, ownerID, 1) // Owner
|
|
setRole(t, database, adminID, 2) // Admin
|
|
|
|
// Admin can delete because owner still exists.
|
|
err := database.DeleteAccount(context.Background(), adminID)
|
|
if err != nil {
|
|
t.Fatalf("DeleteAccount(admin with owner present): %v", err)
|
|
}
|
|
}
|
|
|
|
// ─── DeleteAccount — member deletion ─────────────────────────────────────────
|
|
|
|
func TestDeleteAccount_MemberSucceeds(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
userID := seedUser(t, database, "alice") // default role_id=4
|
|
|
|
err := database.DeleteAccount(context.Background(), userID)
|
|
if err != nil {
|
|
t.Fatalf("DeleteAccount(member): %v", err)
|
|
}
|
|
}
|
|
|
|
// ─── DeleteAccount — anonymisation ───────────────────────────────────────────
|
|
|
|
func TestDeleteAccount_AnonymisesUsername(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
userID := seedUser(t, database, "alice")
|
|
|
|
if err := database.DeleteAccount(context.Background(), userID); err != nil {
|
|
t.Fatalf("DeleteAccount: %v", err)
|
|
}
|
|
|
|
user, err := database.GetUserByID(context.Background(), userID)
|
|
if err != nil {
|
|
t.Fatalf("GetUserByID after delete: %v", err)
|
|
}
|
|
|
|
expected := fmt.Sprintf("[deleted-%d]", userID)
|
|
if user.Username != expected {
|
|
t.Errorf("Username = %q, want %q", user.Username, expected)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_ClearsPassword(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
userID := seedUser(t, database, "bob")
|
|
|
|
database.DeleteAccount(context.Background(), userID) //nolint:errcheck
|
|
|
|
user, _ := database.GetUserByID(context.Background(), userID)
|
|
if user.PasswordHash != "" {
|
|
t.Errorf("PasswordHash = %q, want empty", user.PasswordHash)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_ClearsAvatarAndTOTP(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
userID := seedUser(t, database, "charlie")
|
|
|
|
// Set avatar and TOTP before deletion.
|
|
database.ExecContext(context.Background(), "UPDATE users SET avatar = 'pic.png', totp_secret = 'SECRET' WHERE id = ?", userID) //nolint:errcheck
|
|
|
|
database.DeleteAccount(context.Background(), userID) //nolint:errcheck
|
|
|
|
user, _ := database.GetUserByID(context.Background(), userID)
|
|
if user.Avatar != nil {
|
|
t.Errorf("Avatar = %v, want nil", user.Avatar)
|
|
}
|
|
if user.TOTPSecret != nil {
|
|
t.Errorf("TOTPSecret = %v, want nil", user.TOTPSecret)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_SetsBannedAndOffline(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
userID := seedUser(t, database, "dave")
|
|
|
|
database.DeleteAccount(context.Background(), userID) //nolint:errcheck
|
|
|
|
user, _ := database.GetUserByID(context.Background(), userID)
|
|
if !user.Banned {
|
|
t.Error("Banned should be true after deletion")
|
|
}
|
|
if user.Status != "offline" {
|
|
t.Errorf("Status = %q, want 'offline'", user.Status)
|
|
}
|
|
}
|
|
|
|
// ─── DeleteAccount — related data cleanup ────────────────────────────────────
|
|
|
|
func TestDeleteAccount_DeletesSessions(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
userID := seedUser(t, database, "eve")
|
|
|
|
// Insert a session directly.
|
|
database.ExecContext(context.Background(),
|
|
"INSERT INTO sessions (user_id, token, expires_at) VALUES (?, 'tok123', datetime('now', '+1 day'))",
|
|
userID,
|
|
) //nolint:errcheck
|
|
|
|
database.DeleteAccount(context.Background(), userID) //nolint:errcheck
|
|
|
|
var count int
|
|
database.QueryRowContext(context.Background(), "SELECT COUNT(*) FROM sessions WHERE user_id = ?", userID).Scan(&count) //nolint:errcheck
|
|
if count != 0 {
|
|
t.Errorf("sessions count = %d, want 0", count)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_SoftDeletesMessages(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
userID := seedUser(t, database, "frank")
|
|
chID := seedChannel(t, database, "general")
|
|
|
|
msgID, _ := database.CreateMessage(context.Background(), chID, userID, "hello world", nil)
|
|
|
|
database.DeleteAccount(context.Background(), userID) //nolint:errcheck
|
|
|
|
msg, err := database.GetMessage(context.Background(), msgID)
|
|
if err != nil {
|
|
t.Fatalf("GetMessage after delete: %v", err)
|
|
}
|
|
if !msg.Deleted {
|
|
t.Error("message should be soft-deleted")
|
|
}
|
|
if msg.Content != "" {
|
|
t.Errorf("message content = %q, want empty", msg.Content)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccount_NonexistentUser(t *testing.T) {
|
|
database := openMigratedMemory(t)
|
|
|
|
err := database.DeleteAccount(context.Background(), 999999)
|
|
if err == nil {
|
|
t.Error("DeleteAccount(nonexistent) should return error")
|
|
}
|
|
}
|
|
|
|
// ─── Helper ──────────────────────────────────────────────────────────────────
|
|
|
|
func setRole(t *testing.T, database *db.DB, userID, roleID int64) {
|
|
t.Helper()
|
|
if _, err := database.ExecContext(context.Background(), "UPDATE users SET role_id = ? WHERE id = ?", roleID, userID); err != nil {
|
|
t.Fatalf("setRole(%d, %d): %v", userID, roleID, err)
|
|
}
|
|
}
|