Files
OwnCord/Server/service/user_test.go
T
Claude 5f1d6fc287 refactor(server): remove the store abstraction layer (D3)
Deletes Server/store (SQLiteStore, MemStore, the composed Store
interface) and collapses to a single sqlc-backed db package, executing
the prior audit's P4 "single data layer" direction (finding #6).

SQLiteStore was a pure pass-through to *db.DB, so consumers now depend
on narrow interfaces that *db.DB satisfies directly:

  - service.Store   (service/datastore.go, renamed from store/store.go)
  - ws.EventStore   (ws/eventstore.go)
  - plugin.PluginStore (plugin/pluginstore.go)

The event- and plugin-KV methods that lived in the store's SQLite
implementation move into the db package (db/event_queries.go,
db/plugin_queries.go), keeping their raw-SQL form.

Tests: the MemStore-based unit tests now run against a real in-memory
SQLite db opened per-test with migrations applied, via package-local
seed helpers. Fault-injection tests embed a real *db.DB and override the
single method under test, preserving error-path coverage. Full server
suite and sqlc-verify are green.

Docs: audit finding #6 and A-2026-07-06 marked resolved; decisions D3
updated; architecture server.md / data-model.md diagrams and prose
updated to the api -> service -> db layering.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
2026-07-19 16:33:58 +00:00

83 lines
2.7 KiB
Go

package service
import (
"errors"
"slices"
"testing"
"github.com/owncord/server/db"
)
// pwStore wraps a real *db.DB with controllable DeleteOtherSessions behavior
// and audit capture, so the committed-password partial-success contract (W2-2)
// is testable. Embedding *db.DB satisfies the service Store interface; the two
// overridden methods intercept the calls the contract turns on while every
// other call (UpdateUserPassword, GetUserByID) hits the real database.
type pwStore struct {
*db.DB
failRevokes int // number of DeleteOtherSessions calls that fail before succeeding
revokeCalls int
audits []string
}
func (f *pwStore) DeleteOtherSessions(_, _ int64) (int64, error) {
f.revokeCalls++
if f.revokeCalls <= f.failRevokes {
return 0, errors.New("session table locked")
}
return 2, nil
}
func (f *pwStore) LogAudit(_ int64, action, _ string, _ int64, _ string) error {
f.audits = append(f.audits, action)
return nil
}
// TestChangePassword_RevokeFailureIsPartialSuccess locks the W2-2 contract:
// once the password is committed, revocation failure must never surface as an
// error (the old password is dead; a "failed" report walks the user into the
// confirm lockout), and the audit row must still be written.
func TestChangePassword_RevokeFailureIsPartialSuccess(t *testing.T) {
database := newTestDB(t)
seedUser(t, database, &db.User{ID: 7, Username: "pat", PasswordHash: "oldhash"})
fs := &pwStore{DB: database, failRevokes: 99}
svc := NewUserService(fs)
res, err := svc.ChangePassword(7, "newhash", 1)
if err != nil {
t.Fatalf("committed password change must not return an error: %v", err)
}
if !res.RevokeFailed {
t.Fatal("RevokeFailed should be set when revocation keeps failing")
}
if u, _ := database.GetUserByID(7); u.PasswordHash != "newhash" {
t.Fatal("password should be committed")
}
if !slices.Contains(fs.audits, "password_change") {
t.Fatal("audit row must be written even when revocation fails")
}
}
// TestChangePassword_RetryRecoversRevocation: a single transient revocation
// failure is absorbed by the bounded compensating retry.
func TestChangePassword_RetryRecoversRevocation(t *testing.T) {
database := newTestDB(t)
seedUser(t, database, &db.User{ID: 7, Username: "pat", PasswordHash: "oldhash"})
fs := &pwStore{DB: database, failRevokes: 1}
svc := NewUserService(fs)
res, err := svc.ChangePassword(7, "newhash", 1)
if err != nil {
t.Fatalf("ChangePassword: %v", err)
}
if res.RevokeFailed {
t.Fatal("retry should have recovered the revocation")
}
if res.SessionsRevoked != 2 {
t.Fatalf("SessionsRevoked = %d, want 2", res.SessionsRevoked)
}
if fs.revokeCalls != 2 {
t.Fatalf("expected exactly one retry (2 calls), got %d", fs.revokeCalls)
}
}