a18f96912d
interfaces - refactor: split UserRepository into AuthUserRepository and AdminUserRepository capabilities - refactor: split SessionRepository into AuthSessionRepository and repository-owned CLI/admin methods - refactor: replace generic Repository Update/Delete with operation-specific params and ownership predicates - refactor: replace CredentialRepository generic CRUD with passkey-specific methods - refactor: replace FileRepository generic Create/Update/Delete with UploadedFileParams, DirectoryParams, and owned soft-delete - refactor: remove repository fields from WebApp struct; repositories are now composition-time wiring only - feat: add domain error kinds ErrParentNotFound, ErrParentNotDir, ErrDirectoryNotEmpty, ErrInvalidMove - feat: add CredentialTypeAppPasskey constant - feat: add testutil.SetUserAdmin for test fixture setup that bypasses production service ports - test: add architecture test banning GORM Save in repository package - test: add capability interface contract tests ensuring each service receives the minimal interface - test: add blockingStorage helper for concurrent promotion tests - test: add preserved DSN parameter test for sqliteImmediateDSN - docs: update architecture decisions with repository write rules and capability separation - docs: update roadmap to clarify atomic single-use refresh sessions - docs: add -race test target to development docs
257 lines
7.0 KiB
Go
257 lines
7.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/dhao2001/mygo/internal/config"
|
|
"github.com/dhao2001/mygo/internal/model"
|
|
)
|
|
|
|
func setupSessionRepo(t *testing.T) SessionRepository {
|
|
t.Helper()
|
|
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.Session{}); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
|
|
return NewSessionRepository(db)
|
|
}
|
|
|
|
func createSessionRecord(ctx context.Context, repo SessionRepository, session *model.Session) error {
|
|
return repo.CreateRefreshSession(ctx, RefreshSessionParams{
|
|
ID: session.ID, UserID: session.UserID, TokenHash: session.TokenHash, ExpiresAt: session.ExpiresAt,
|
|
})
|
|
}
|
|
|
|
func TestSessionRepository_Create(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
session := &model.Session{
|
|
ID: "session-1",
|
|
UserID: "user-1",
|
|
TokenHash: "hash-abc",
|
|
ExpiresAt: time.Now().Add(24 * time.Hour),
|
|
}
|
|
|
|
if err := createSessionRecord(ctx, repo, session); err != nil {
|
|
t.Fatalf("Create = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_CreateDuplicateHash(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
s1 := &model.Session{ID: "session-1", UserID: "user-1", TokenHash: "hash-abc", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
s2 := &model.Session{ID: "session-2", UserID: "user-2", TokenHash: "hash-abc", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
|
|
if err := createSessionRecord(ctx, repo, s1); err != nil {
|
|
t.Fatalf("Create = %v", err)
|
|
}
|
|
|
|
err := createSessionRecord(ctx, repo, s2)
|
|
if err != model.ErrDuplicate {
|
|
t.Fatalf("expected ErrDuplicate, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_FindByID(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
session := &model.Session{ID: "session-1", UserID: "user-1", TokenHash: "hash-abc", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
if err := createSessionRecord(ctx, repo, session); err != nil {
|
|
t.Fatalf("Create = %v", err)
|
|
}
|
|
|
|
found, err := repo.FindByID(ctx, "session-1")
|
|
if err != nil {
|
|
t.Fatalf("FindByID = %v", err)
|
|
}
|
|
if found.UserID != "user-1" {
|
|
t.Errorf("user_id = %q, want %q", found.UserID, "user-1")
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_FindByTokenHash(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
session := &model.Session{ID: "session-1", UserID: "user-1", TokenHash: "hash-abc", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
if err := createSessionRecord(ctx, repo, session); err != nil {
|
|
t.Fatalf("Create = %v", err)
|
|
}
|
|
|
|
found, err := repo.FindByTokenHash(ctx, "hash-abc")
|
|
if err != nil {
|
|
t.Fatalf("FindByTokenHash = %v", err)
|
|
}
|
|
if found.ID != "session-1" {
|
|
t.Errorf("id = %q, want %q", found.ID, "session-1")
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_FindByTokenHashNotFound(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := repo.FindByTokenHash(ctx, "nonexistent")
|
|
if err != model.ErrNotFound {
|
|
t.Fatalf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_Delete(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
session := &model.Session{ID: "session-1", UserID: "user-1", TokenHash: "hash-abc", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
if err := createSessionRecord(ctx, repo, session); err != nil {
|
|
t.Fatalf("Create = %v", err)
|
|
}
|
|
|
|
consumed, err := repo.ConsumeRefreshSession(ctx, "hash-abc")
|
|
if err != nil {
|
|
t.Fatalf("ConsumeRefreshSession = %v", err)
|
|
}
|
|
if consumed.ID != "session-1" {
|
|
t.Fatalf("consumed ID = %q, want session-1", consumed.ID)
|
|
}
|
|
|
|
_, err = repo.FindByID(ctx, "session-1")
|
|
if err != model.ErrNotFound {
|
|
t.Fatalf("expected ErrNotFound after delete, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_DeleteByUserID(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
s1 := &model.Session{ID: "session-1", UserID: "user-1", TokenHash: "hash-1", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
s2 := &model.Session{ID: "session-2", UserID: "user-1", TokenHash: "hash-2", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
s3 := &model.Session{ID: "session-3", UserID: "user-2", TokenHash: "hash-3", ExpiresAt: time.Now().Add(24 * time.Hour)}
|
|
|
|
for _, s := range []*model.Session{s1, s2, s3} {
|
|
if err := createSessionRecord(ctx, repo, s); err != nil {
|
|
t.Fatalf("Create = %v", err)
|
|
}
|
|
}
|
|
|
|
if err := repo.DeleteSessionsByUserID(ctx, "user-1"); err != nil {
|
|
t.Fatalf("DeleteByUserID = %v", err)
|
|
}
|
|
|
|
_, err := repo.FindByID(ctx, "session-1")
|
|
if err != model.ErrNotFound {
|
|
t.Fatalf("session-1 should have been deleted")
|
|
}
|
|
_, err = repo.FindByID(ctx, "session-2")
|
|
if err != model.ErrNotFound {
|
|
t.Fatalf("session-2 should have been deleted")
|
|
}
|
|
if _, err := repo.FindByID(ctx, "session-3"); err != nil {
|
|
t.Fatalf("session-3 should still exist: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_DeleteExpired(t *testing.T) {
|
|
repo := setupSessionRepo(t)
|
|
ctx := context.Background()
|
|
|
|
expired := &model.Session{
|
|
ID: "session-1", UserID: "user-1", TokenHash: "hash-old",
|
|
ExpiresAt: time.Now().Add(-1 * time.Hour),
|
|
}
|
|
valid := &model.Session{
|
|
ID: "session-2", UserID: "user-1", TokenHash: "hash-new",
|
|
ExpiresAt: time.Now().Add(24 * time.Hour),
|
|
}
|
|
|
|
for _, s := range []*model.Session{expired, valid} {
|
|
if err := createSessionRecord(ctx, repo, s); err != nil {
|
|
t.Fatalf("Create = %v", err)
|
|
}
|
|
}
|
|
|
|
count, err := repo.DeleteExpiredSessions(ctx)
|
|
if err != nil {
|
|
t.Fatalf("DeleteExpired = %v", err)
|
|
}
|
|
if count != 1 {
|
|
t.Errorf("DeleteExpired count = %d, want 1", count)
|
|
}
|
|
|
|
if _, err := repo.FindByID(ctx, "session-1"); err != model.ErrNotFound {
|
|
t.Fatalf("expired session should have been deleted")
|
|
}
|
|
if _, err := repo.FindByID(ctx, "session-2"); err != nil {
|
|
t.Fatalf("valid session should still exist: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionRepository_ConsumeRefreshSessionIsAtomic(t *testing.T) {
|
|
db, err := Open(config.DatabaseConfig{
|
|
Driver: "sqlite3",
|
|
SQLite: config.SQLiteConfig{Path: filepath.Join(t.TempDir(), "sessions.db")},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
if err := AutoMigrate(db); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
repo := NewSessionRepository(db)
|
|
ctx := context.Background()
|
|
if err := repo.CreateRefreshSession(ctx, RefreshSessionParams{
|
|
ID: "session-1", UserID: "user-1", TokenHash: "single-use", ExpiresAt: time.Now().Add(time.Hour),
|
|
}); err != nil {
|
|
t.Fatalf("CreateRefreshSession = %v", err)
|
|
}
|
|
|
|
start := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
var wg sync.WaitGroup
|
|
wg.Add(2)
|
|
for range 2 {
|
|
go func() {
|
|
defer wg.Done()
|
|
<-start
|
|
_, err := repo.ConsumeRefreshSession(ctx, "single-use")
|
|
results <- err
|
|
}()
|
|
}
|
|
close(start)
|
|
wg.Wait()
|
|
close(results)
|
|
|
|
succeeded := 0
|
|
notFound := 0
|
|
for err := range results {
|
|
switch {
|
|
case err == nil:
|
|
succeeded++
|
|
case errors.Is(err, model.ErrNotFound):
|
|
notFound++
|
|
default:
|
|
t.Fatalf("ConsumeRefreshSession unexpected error: %v", err)
|
|
}
|
|
}
|
|
if succeeded != 1 || notFound != 1 {
|
|
t.Fatalf("consume results: success=%d not_found=%d, want 1 each", succeeded, notFound)
|
|
}
|
|
}
|