Merge branch 'main' of ssh://git.linuxhg.com:2222/Bookhoard/bookhoard

This commit is contained in:
2026-02-06 19:39:51 -05:00
35 changed files with 1558 additions and 795 deletions
+479
View File
@@ -0,0 +1,479 @@
# Router Refactoring Execution Plan
## Objective
Refactor 858-line `cmd/server/main.go` by migrating route definitions to `internal/router/` package while maintaining 100% API compatibility and passing all verification tests.
## Current State
-`internal/router/` package created with 7 files
- ✅ Route stubs implemented for: auth, library, device, frontend, docs
- ❌ Router package NOT integrated (never called from main.go)
- ❌ All routes still defined in main.go (duplicates)
- ⚠️ main.go: 858 lines (target: ~200 lines)
## Success Criteria
1. All 26 verification checks pass (`scripts/verify-guidelines.sh`)
2. All Go tests pass (`go test ./...`)
3. All Bruno/curl API tests pass
4. No API behavior changes (routes, handlers, responses identical)
5. main.go reduced to ~200 lines
6. Code compiles without errors
7. Application runs successfully (containers start, health check returns 200)
## Migration Strategy: Incremental with Rollback Safety
### Phase 1: Create Safety Branch ✅
- [x] Create branch `continue-router-refactor`
- [x] Router package structure exists
### Phase 2: Integrate Router Package (DO THIS FIRST)
#### Step 2.1: Add Router Import and Config
**File:** `cmd/server/main.go`
Add to imports:
```go
"bookhoard/internal/router"
```
Add after line 130 (after rateLimiter initialization):
```go
// ========================================================================
// ROUTER REGISTRATION - Migrate routes to internal/router/ package
// ========================================================================
routerConfig := &router.Config{
Echo: e,
Queries: queries,
Cfg: cfg,
DBPool: dbPool,
AuthHandler: authHandler,
LibraryHandler: libraryHandler,
DeviceHandler: deviceHandler,
KOReaderHandler: koreaderHandler,
WSHandler: wsHandler,
ConflictHandler: conflictHandler,
AnalyticsHandler: analyticsHandler,
QueueHandler: queueHandler,
CollectionHandler: collectionHandler,
OPDSHandler: opdsHandler,
ConnManager: connManager,
QueueProcessor: queueProcessor,
DeviceAuthMiddleware: deviceAuthMiddleware,
LoginTracker: loginAttemptTracker,
}
```
#### Step 2.2: Call Router.RegisterRoutes()
Add immediately after routerConfig:
```go
router.RegisterRoutes(routerConfig)
```
**IMPORTANT:** Do NOT remove any routes from main.go yet!
#### Step 2.3: Test Compilation
```bash
go build ./cmd/server
```
**Expected:** Should compile (routes will be duplicated but that's OK temporarily)
#### Step 2.4: Test Application
```bash
# Stop containers if running
podman-compose down
# Rebuild and start
podman-compose up -d --build
# Wait for startup
sleep 10
# Test health endpoint
curl -s http://localhost:8765/health | jq .
# Test frontend
curl -s http://localhost:8765/ | grep -o "<title>.*</title>"
# Run verification
bash scripts/verify-guidelines.sh
```
**Expected:** All should pass (duplicate routes don't break Echo)
**ROLLBACK IF:** Compilation fails or health check returns non-200
- `git checkout -- cmd/server/main.go`
---
### Phase 3: Remove Duplicate Routes from main.go
⚠️ **CRITICAL:** Remove ONE route group at a time, test after each removal!
#### Step 3.1: Remove Auth Routes (lines 132-191)
**Lines to remove:** From `// Auth routes` to `// JWT middleware for protected routes` (before jwtMiddleware creation)
**Actually:** Keep jwtMiddleware creation (it's used by other routes)
Remove: auth POST endpoints and protected auth routes that are now in router/auth.go
**Test after removal:**
```bash
go build ./cmd/server
podman-compose up -d --build
sleep 10
# Test auth endpoints
curl -X POST http://localhost:8765/api/auth/register -H "Content-Type: application/json" -d '{"email":"test@test.com","username":"test","password":"Test123!"}'
```
#### Step 3.2: Remove Library Routes (lines 192-235)
**Lines to remove:** From `// Library management routes` to visibility routes
**Test after removal:**
```bash
go build ./cmd/server
podman-compose up -d --build
# Test library endpoints
curl -s http://localhost:8765/api/libraries/types | jq .
```
#### Step 3.3: Remove Device Registration Routes (lines 236-239)
**Lines to remove:** Device register and status endpoints
**Test after removal:**
```bash
go build ./cmd/server
# Device registration test
```
#### Step 3.4: Remove Frontend Routes (lines 627-823)
**Lines to remove:** From `// FRONTEND ROUTES` to before `// HEALTH CHECK`
**Test after removal:**
```bash
go build ./cmd/server
curl -s http://localhost:8765/ | grep -o "<title>.*</title>"
```
#### Step 3.5: Remove Health Check (lines 824-844)
**Lines to remove:** From `// HEALTH CHECK` to before `// DOCUMENTATION ROUTES`
**Test after removal:**
```bash
go build ./cmd/server
curl -s http://localhost:8765/health | jq .
```
#### Step 3.6: Remove Documentation Routes (lines 845-858)
**Lines to remove:** From `// DOCUMENTATION ROUTES` to end
**Test after removal:**
```bash
go build ./cmd/server
curl -s http://localhost:8765/docs | grep -o "<title>.*</title>"
```
---
### Phase 4: Implement Remaining Router Stubs
#### Step 4.1: Create `router/sync.go`
```bash
# Create file with sync routes (KOReader, Kobo, websocket)
# Copy sync route definitions from main.go
```
**Routes to migrate:**
- KOReader sync routes (device authentication required)
- Kobo sync routes (device authentication required)
- Book matching routes
- WebSocket endpoint
#### Step 4.2: Create `router/media.go`
**Routes to migrate:**
- Media item routes (download, shelf management)
- Bulk book operations
#### Step 4.3: Create `router/analytics.go`
**Routes to migrate:**
- Analytics routes (API + SSR)
#### Step 4.4: Create `router/queue.go`
**Routes to migrate:**
- Sync queue management routes (API + SSR)
#### Step 4.5: Create `router/opds.go`
**Routes to migrate:**
- OPDS routes (public - device authentication optional)
#### Step 4.6: Update `router/collections.go`
**Routes to migrate:**
- Collection routes (API + SSR)
---
### Phase 5: Complete Migration
For each new route file created in Phase 4:
1. Add `registerXYZRoutes(cfg *Config)` function
2. Call it from `router.RegisterRoutes()` in router.go
3. Remove corresponding routes from main.go
4. Test with: `go build ./cmd/server`
5. Test with: `podman-compose up -d --build`
6. Test specific endpoints with curl
7. Run: `bash scripts/verify-guidelines.sh`
---
### Phase 6: Final Verification
#### Step 6.1: Full Test Suite
```bash
# Compilation
go build ./cmd/server
go test ./...
# Verification
bash scripts/verify-guidelines.sh
# Container test
podman-compose down
podman-compose up -d --build
sleep 15
# Critical endpoint tests
curl -s http://localhost:8765/health | jq .
curl -s http://localhost:8765/ | grep -o "<title>.*</title>"
curl -s http://localhost:8765/api/libraries/types | jq .
curl -s http://localhost:8765/docs | grep -o "<title>.*</title>"
# Run Bruno tests (if available)
# bruno test ...
```
#### Step 6.2: Verify main.go Size
```bash
wc -l cmd/server/main.go
```
**Expected:** ~200 lines (down from 858)
#### Step 6.3: Code Review Checklist
- [ ] No routes duplicated (each route defined once)
- [ ] All route groups use JWT middleware correctly
- [ ] Admin middleware applied where needed
- [ ] Rate limiting applied to auth endpoints
- [ ] No compilation errors
- [ ] All imports used
- [ ] Consistent code style with rest of codebase
---
### Phase 7: Commit and Push
#### Step 7.1: Review Changes
```bash
git diff cmd/server/main.go | head -100
git diff internal/router/
```
#### Step 7.2: Run Verification
```bash
bash scripts/verify-guidelines.sh
```
#### Step 7.3: Commit Changes
```bash
git add cmd/server/main.go internal/router/
git commit -m "refactor: complete router package migration
- Migrate all routes from main.go to internal/router/ package
- Reduce main.go from 858 lines to ~200 lines
- Create separate files for route groups:
- auth.go: Authentication routes
- library.go: Library management
- device.go: Device registration & management
- sync.go: KOReader/Kobo/WebSocket sync routes
- media.go: Media items and bulk operations
- analytics.go: Analytics API + SSR
- queue.go: Sync queue management
- opds.go: OPDS feeds
- collections.go: Collection management
- frontend.go: SSR pages and health check
- docs.go: Documentation routes
- All 26 verification checks pass
- All API endpoints tested and working
- Zero API behavior changes (100% compatible)
- Follows Go standard project layout
Breaking Change: None - API compatibility maintained"
```
#### Step 7.4: Push
```bash
git push origin continue-router-refactor
```
---
## Rollback Procedures
### If compilation fails at any point:
```bash
git checkout -- cmd/server/main.go
# Or
git reset --hard HEAD
```
### If tests fail:
1. Check which endpoint failed
2. Verify route is registered in router package
3. Check handler method exists
4. Check middleware is applied correctly
5. Review error logs: `podman logs bookhoard`
### If verification fails:
1. Check which specific check failed
2. Fix the issue
3. Re-run verification
4. Commit the fix separately
---
## Testing Commands (Quick Reference)
```bash
# Compile
go build ./cmd/server
# Verification
bash scripts/verify-guidelines.sh
# Unit tests
go test ./...
# Rebuild containers
podman-compose down
podman-compose up -d --build
# Wait for startup
sleep 10
# Health check
curl -s http://localhost:8765/health | jq .
# Frontend
curl -s http://localhost:8765/ | grep -o "<title>.*</title>"
# Auth endpoint test
curl -X POST http://localhost:8765/api/auth/login \
-H "Content-Type: application/json" \
-d '{"login":"test","password":"wrong"}'
# Library types
curl -s http://localhost:8765/api/libraries/types | jq .
# Documentation
curl -s http://localhost:8765/docs | grep -o "<title>.*</title>"
# Check container logs
podman logs bookhoard | tail -30
# Check container status
podman ps
```
---
## Files Created/Modified
### Created:
- `internal/router/router.go` - Main router configuration
- `internal/router/auth.go` - Authentication routes
- `internal/router/library.go` - Library management routes
- `internal/router/device.go` - Device routes
- `internal/router/frontend.go` - Frontend SSR routes
- `internal/router/docs.go` - Documentation routes
- `internal/router/helpers.go` - Template helpers
- `internal/router/sync.go` - Sync routes (Phase 4)
- `internal/router/media.go` - Media routes (Phase 4)
- `internal/router/analytics.go` - Analytics routes (Phase 4)
- `internal/router/queue.go` - Queue routes (Phase 4)
- `internal/router/opds.go` - OPDS routes (Phase 4)
- `internal/router/collections.go` - Collection routes (Phase 4)
### Modified:
- `cmd/server/main.go` - Reduced from 858 to ~200 lines
---
## Estimated Time
- Phase 2: 15 minutes (integration and initial testing)
- Phase 3: 45 minutes (incremental route removal and testing)
- Phase 4: 90 minutes (implement remaining route groups)
- Phase 5: 30 minutes (complete migration)
- Phase 6: 30 minutes (final verification)
- Phase 7: 15 minutes (commit and push)
**Total: ~4 hours**
---
## Notes for AI Assistants
1. **Always test after each change** - don't batch multiple route removals
2. **Keep main.go functional** - it should compile at all times
3. **Verify API compatibility** - routes must respond identically
4. **Use git commits** - commit after each successful phase to enable rollback
5. **Check logs** - if something fails, check `podman logs bookhoard`
6. **Verification script is authority** - if it fails, fix before continuing
7. **Echo allows duplicate routes** - temporarily OK during migration
8. **Middleware order matters** - maintain exact middleware application order
9. **Import statements** - remove unused imports after route removal
10. **Handler methods** - verify handler methods exist before calling them
---
## Troubleshooting
### Error: "route already registered"
- **Cause:** Route defined multiple times
- **Fix:** Remove from main.go, keep in router package only
### Error: "handler method not found"
- **Cause:** Typo in method name or handler not initialized in Config
- **Fix:** Check method name in handler file, ensure handler is passed in Config
### Error: "undefined: jwtMiddleware"
- **Cause:** JWT middleware not created in that route file
- **Fix:** Add JWT middleware creation at top of register function
### Error: "404 on previously working endpoint"
- **Cause:** Route not registered or middleware blocking access
- **Fix:** Check route is registered, check middleware conditions
### Health check returns 503
- **Cause:** Database not connected or dbPool not passed to router
- **Fix:** Ensure DBPool is set in routerConfig
### Verification fails with "Build failed"
- **Cause:** Compilation error
- **Fix:** Run `go build ./cmd/server` to see specific error
---
## Success Metrics
Before:
- `cmd/server/main.go`: 858 lines
- All routes defined inline
- Mixed concerns (setup + routes + server start)
After:
- `cmd/server/main.go`: ~200 lines
- Routes organized by domain in `internal/router/`
- Clear separation: setup → router registration → server start
- Follows Go standard project layout
- Easy to maintain and extend
---
## End of Plan
@@ -5,7 +5,7 @@ meta {
}
post {
url: {{baseUrl}}/api/devices/register
url: {{base_url}}/api/devices/register
body: json
auth: none
}
+3 -3
View File
@@ -12,9 +12,9 @@ post {
body:json {
{
"email": "admin@example.com",
"username": "admin",
"password": "admin123",
"email": "admin2@example.com",
"username": "admin2",
"password": "!Admin@123",
"first_name": "Admin",
"last_name": "User",
"role": "admin"
+35 -508
View File
@@ -3,25 +3,21 @@ package main
import (
"bookhoard/internal/config"
"bookhoard/internal/database"
"bookhoard/internal/docs"
"bookhoard/internal/handlers"
"bookhoard/internal/middleware"
ratelimit "bookhoard/internal/middleware"
"bookhoard/internal/router"
"bookhoard/internal/services"
"bookhoard/internal/sync"
"bookhoard/templates"
"bytes"
"context"
"log"
"net/http"
"time"
"github.com/go-playground/validator/v10"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo-jwt/v4"
"github.com/labstack/echo/v4"
echomiddleware "github.com/labstack/echo/v4/middleware"
)
@@ -121,514 +117,45 @@ func main() {
e.Use(ratelimit.RequestTracingMiddleware(cfg))
// Rate limiter for auth endpoints
rateLimiterConfig := ratelimit.RateLimiterConfig{
Enabled: cfg.RateLimitEnabled,
RequestsPerMinute: cfg.RequestsPerMinute,
CleanupInterval: 5 * time.Minute,
// rateLimiterConfig := ratelimit.RateLimiterConfig{
// Enabled: cfg.RateLimitEnabled,
// RequestsPerMinute: cfg.RequestsPerMinute,
// CleanupInterval: 5 * time.Minute,
// }
// rateLimiter := ratelimit.NewRateLimiter(rateLimiterConfig)
// rateLimitMiddleware := ratelimit.RateLimiterMiddleware(rateLimiter) // Now in router/auth.go
// ========================================================================
// ROUTER REGISTRATION - Migrate routes to internal/router/ package
// ========================================================================
routerConfig := &router.Config{
Echo: e,
Queries: queries,
Cfg: cfg,
DBPool: dbPool,
AuthHandler: authHandler,
LibraryHandler: libraryHandler,
DeviceHandler: deviceHandler,
KOReaderHandler: koreaderHandler,
WSHandler: wsHandler,
ConflictHandler: conflictHandler,
AnalyticsHandler: analyticsHandler,
QueueHandler: queueHandler,
CollectionHandler: nil, // TODO: Initialize collection handler
OPDSHandler: opdsHandler,
ConnManager: connManager,
QueueProcessor: queueProcessor,
DeviceAuthMiddleware: deviceAuthMiddleware,
LoginTracker: loginAttemptTracker,
}
rateLimiter := ratelimit.NewRateLimiter(rateLimiterConfig)
rateLimitMiddleware := ratelimit.RateLimiterMiddleware(rateLimiter)
// Auth routes (no auth required, but rate limited)
e.POST("/api/auth/register", rateLimitMiddleware(authHandler.Register))
e.POST("/api/auth/login", rateLimitMiddleware(authHandler.Login))
// JWT middleware for protected routes
jwtMiddleware := echojwt.WithConfig(echojwt.Config{
SigningKey: []byte(cfg.JWTSecret),
ContextKey: "user",
SuccessHandler: func(c echo.Context) {
token := c.Get("user").(*jwt.Token)
claims := token.Claims.(jwt.MapClaims)
c.Set("user_id", claims["user_id"])
c.Set("user_role", claims["user_role"])
c.Set("user_email", claims["user_email"])
c.Set("user_username", claims["user_username"])
// Parse UUID from string claims
userIDStr, _ := claims["user_id"].(string)
userUUID, err := uuid.Parse(userIDStr)
if err != nil {
c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user ID in token"})
return
}
c.Set("user", database.Users{
ID: pgtype.UUID{Bytes: [16]byte(userUUID), Valid: true},
Email: claims["user_email"].(string),
Username: claims["user_username"].(string),
Role: claims["user_role"].(string),
})
},
})
// Protected routes
protected := e.Group("/api", jwtMiddleware)
// Setup ebook handler routes first (so we can use it for library scan)
h := handlers.SetupRoutes(protected, queries, connManager)
router.RegisterRoutes(routerConfig)
// Public library types endpoint (no authentication required)
e.GET("/api/libraries/types", libraryHandler.GetLibraryTypes)
// Refresh token endpoint (no authentication required - uses refresh token from body)
e.POST("/api/auth/refresh", authHandler.RefreshAccessToken)
// Logout endpoint (optional authentication - can revoke tokens if provided)
e.POST("/api/auth/logout", authHandler.Logout)
// Protected routes
protected = e.Group("/api", jwtMiddleware)
// Setup ebook handler routes first (so we can use it for library scan)
protected.GET("/auth/profile", authHandler.GetProfile)
protected.PUT("/auth/profile", authHandler.UpdateProfile)
// Admin-only routes for user and folder management
admin := protected.Group("/auth", handlers.AdminMiddleware)
admin.GET("/users", authHandler.ListUsers)
admin.PUT("/users/:id/max-devices", authHandler.UpdateUserMaxDevices)
// Library management routes
library := protected.Group("/libraries")
// Admin-only library routes
adminLibrary := library.Group("", handlers.AdminMiddleware)
adminLibrary.POST("", libraryHandler.CreateLibrary)
adminLibrary.GET("", libraryHandler.ListLibraries)
adminLibrary.GET("/:id", libraryHandler.GetLibrary)
adminLibrary.PUT("/:id", libraryHandler.UpdateLibrary)
adminLibrary.DELETE("/:id", libraryHandler.DeleteLibrary)
adminLibrary.POST("/:id/folders", libraryHandler.AddLibraryFolder)
adminLibrary.GET("/:id/folders", libraryHandler.GetLibraryFolders)
adminLibrary.DELETE("/:id/folders", libraryHandler.DeleteLibraryFolder)
adminLibrary.GET("/:id/stats", libraryHandler.GetLibraryStats)
adminLibrary.POST("/:id/scan", func(c echo.Context) error {
libraryID := c.Param("id")
scanReq := map[string]interface{}{
"library_id": libraryID,
}
c.Set("scan_request", scanReq)
return h.ScanEbooks(c)
})
adminLibrary.GET("/:id/media-items", func(c echo.Context) error {
libraryID := c.Param("id")
c.QueryParams().Set("library_id", libraryID)
return h.ListMediaItems(c)
})
// User library visibility control
protected.POST("/libraries/visibility", libraryHandler.SetLibraryVisibility)
protected.GET("/libraries/visible", libraryHandler.GetUserVisibleLibraries)
protected.DELETE("/auth/account", authHandler.DeleteAccount)
protected.PUT("/library/scan-settings", authHandler.UpdateScanSettings)
protected.GET("/library/scan-settings", authHandler.GetScanSettings)
// Auth update routes
authGroup := e.Group("/api/auth", jwtMiddleware)
authGroup.PUT("/email", authHandler.UpdateEmail)
authGroup.PUT("/username", authHandler.UpdateUsername)
authGroup.PUT("/password", authHandler.UpdatePassword)
authGroup.PUT("/theme", authHandler.UpdateTheme)
// force rebuild
// Device management routes (public - for registration)
e.POST("/api/devices/register", deviceHandler.InitiateRegistration)
e.POST("/api/devices/register/status", deviceHandler.CheckRegistrationStatus)
// KOReader sync routes (device authentication required)
koreaderSync := e.Group("/api/sync/koreader")
koreaderSync.POST("/progress", deviceAuthMiddleware.Authenticate(koreaderHandler.SyncProgress))
koreaderSync.GET("/metadata/:uuid", deviceAuthMiddleware.Authenticate(koreaderHandler.GetMetadata))
koreaderSync.GET("/library", deviceAuthMiddleware.Authenticate(koreaderHandler.GetLibrary))
koreaderSync.POST("/bookmarks", deviceAuthMiddleware.Authenticate(koreaderHandler.SyncBookmarks))
// Kobo sync routes (device authentication required)
koboHandler := handlers.NewKoboHandler(queries, connManager)
koboSync := e.Group("/api/sync/kobo")
koboSync.POST("/markup", deviceAuthMiddleware.Authenticate(koboHandler.Markup))
koboSync.POST("/bookmark", deviceAuthMiddleware.Authenticate(koboHandler.Bookmark))
koboSync.POST("/v1/analytics/gettests", deviceAuthMiddleware.Authenticate(koboHandler.AnalyticsGettests))
koboSync.GET("/v1/initialization", deviceAuthMiddleware.Authenticate(koboHandler.Initialization))
koboSync.POST("/sync-from-server", deviceAuthMiddleware.Authenticate(koboHandler.SyncFromServer))
// Book matching and unlinked book resolution routes
sync := protected.Group("/sync")
sync.POST("/bulk-link-books", h.BulkLinkBooks)
sync.POST("/auto-link-books", h.AutoLinkBooks)
sync.GET("/unlinked-books/:id/suggestions", h.GetUnlinkedBookSuggestions)
// Media item routes (download and shelf management)
mediaHandler := handlers.NewMediaHandler(queries)
e.GET("/api/books/:uuid/download", mediaHandler.DownloadBook)
protected.POST("/devices/:id/shelves", mediaHandler.AddToShelf)
protected.GET("/devices/:id/shelves", mediaHandler.GetShelf)
protected.DELETE("/devices/:id/shelves", mediaHandler.RemoveFromShelf)
protected.DELETE("/devices/:id/shelves/clear", mediaHandler.ClearShelf)
// Bulk book operations (protected - require user auth)
books := protected.Group("/books")
books.POST("/bulk-delete", mediaHandler.HandleBulkDelete)
books.POST("/bulk-update", mediaHandler.HandleBulkUpdate)
// Device management routes (protected - require user auth)
devices := protected.Group("/devices")
devices.GET("", deviceHandler.ListDevices)
devices.GET("/:id", deviceHandler.GetDevice)
devices.PUT("/:id", deviceHandler.UpdateDevice)
devices.DELETE("/:id", deviceHandler.DeleteDevice)
devices.GET("/pending", deviceHandler.ListPendingRegistrations)
devices.GET("/approve/:registration_id", deviceHandler.ApproveDevice)
devices.POST("/reject/:registration_id", deviceHandler.RejectDevice)
// Conflict resolution routes (protected - require user auth)
conflicts := protected.Group("/conflicts")
conflicts.GET("", conflictHandler.ListConflicts)
conflicts.GET("/:id", conflictHandler.GetConflict)
conflicts.POST("/:id/resolve", conflictHandler.ResolveConflict)
conflicts.DELETE("/:id", conflictHandler.DeleteConflict)
conflicts.POST("/dismiss-all", conflictHandler.DismissAllResolved)
conflicts.POST("/bulk-resolve", conflictHandler.BulkResolveConflicts)
conflicts.POST("/bulk-dismiss", conflictHandler.BulkDismissConflicts)
// Analytics routes (protected - require user auth)
analytics := protected.Group("/analytics")
analytics.GET("/reading-stats", analyticsHandler.GetReadingStats)
analytics.GET("/device-usage", analyticsHandler.GetDeviceUsage)
analytics.GET("/popular-books", analyticsHandler.GetPopularBooks)
// Sync queue management routes (protected - require user auth)
queue := protected.Group("/queue")
queue.GET("/devices/:device_id/stats", queueHandler.GetDeviceQueueStats)
queue.GET("/devices/:device_id/items", queueHandler.ListDeviceQueueItems)
queue.POST("/items/:item_id/retry", queueHandler.RetryQueueItem)
queue.DELETE("/items/:item_id", queueHandler.DeleteQueueItem)
queue.DELETE("/devices/:device_id/clear", queueHandler.ClearDeviceQueue)
// Admin-only queue routes
adminQueue := queue.Group("", handlers.AdminMiddleware)
adminQueue.GET("/items", queueHandler.ListAllQueueItems)
// WebSocket endpoint for real-time sync
e.GET("/ws/sync", wsHandler.HandleWebSocket)
// OPDS routes (public - device authentication optional)
opds := e.Group("/opds/devices")
opds.GET("/:deviceId/catalog", opdsHandler.GetDeviceCatalog)
opds.GET("/:deviceId/search", opdsHandler.SearchDeviceCatalog)
opds.GET("/:deviceId/nav", opdsHandler.GetDeviceNavigation)
opds.GET("/:deviceId/download/:bookId", opdsHandler.DownloadBook)
opds.GET("/:deviceId/cover/:bookId", opdsHandler.GetCoverImage)
opds.GET("/:deviceId/formats/:bookId", opdsHandler.ListFormats)
// Static files
e.Static("/static", "web/static")
// Start scheduler for auto-scanning
go h.StartScheduler()
defer h.StopScheduler()
// Start watch mode for all libraries (background)
go func() {
time.Sleep(2 * time.Second) // Wait a bit for server to be ready
if err := h.StartWatchModeForAllLibraries(context.Background()); err != nil {
log.Printf("Warning: failed to start watch mode for libraries: %v", err)
}
}()
// Bookshelf route (protected) - new default for logged-in users
protected.GET("/bookshelf", func(c echo.Context) error {
user := c.Get("user").(database.Users)
userUUID := uuid.UUID(user.ID.Bytes)
// Fetch libraries server-side for SSR
librariesData, err := libraryHandler.GetUserVisibleLibrariesData(c.Request().Context(), pgtype.UUID{Bytes: userUUID, Valid: true})
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading libraries")
}
// Convert to template format
libraries := make([]templates.LibraryData, len(librariesData))
for i, lib := range librariesData {
libUUID := uuid.UUID(lib.ID.Bytes)
description := ""
if lib.Description.Valid {
description = lib.Description.String
}
libraries[i] = templates.LibraryData{
ID: libUUID.String(),
Name: lib.Name,
Description: description,
TypeName: lib.TypeName,
}
}
userTemplate := templates.User{
ID: userUUID.String(),
Email: user.Email,
Username: user.Username,
Role: user.Role,
}
// Render template WITH libraries data (SSR)
var buf bytes.Buffer
err = templates.BookShelf(userTemplate, libraries).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Analytics route (protected) - SSR version
protected.GET("/analytics", func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, queries)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
var buf bytes.Buffer
err = templates.Analytics(user).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Queue Management route (protected) - SSR version
// Progress visualization route (protected) - SSR version
protected.GET("/progress", func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, queries)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
progressData, err := h.GetAllProgressData(c)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading progress")
}
progressItems := make([]templates.ProgressItemData, len(progressData))
for i, p := range progressData {
deviceIcon := ""
deviceType := ""
switch p.LastSyncDevice {
case "koreader":
deviceIcon = "📖"
deviceType = "KOReader"
case "kobo":
deviceIcon = "📚"
deviceType = "Kobo"
case "web":
deviceIcon = "🌐"
deviceType = "Web"
case "mobile":
deviceIcon = "📱"
deviceType = "Mobile"
}
progressItems[i] = templates.ProgressItemData{
MediaItemID: uuid.UUID(p.MediaItemID).String(),
Title: p.Title,
Author: p.Author,
CoverImagePath: p.CoverImagePath,
CurrentPage: p.CurrentPage,
TotalPages: p.TotalPages,
ProgressPercentage: p.Percentage,
LastUpdated: p.LastReadAt.Format("2006-01-02T15:04:05Z07:00"),
DeviceName: p.LastSyncDevice,
DeviceType: deviceType,
DeviceIcon: deviceIcon,
EpubCFI: p.Epubcfi,
}
}
var buf bytes.Buffer
err = templates.Progress(user, progressItems).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Queue Management route (protected) - SSR version
protected.GET("/queue", func(c echo.Context) error {
userID := c.Get("user_id").(string)
userEmail := c.Get("user_email").(string)
userUsername := c.Get("user_username").(string)
userRole := c.Get("user_role").(string)
user := templates.User{
ID: userID,
Email: userEmail,
Username: userUsername,
Role: userRole,
}
// Fetch queue items for SSR (uses existing handler method)
queueItems, err := queueHandler.GetQueueData(c)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading queue")
}
// Calculate stats from items
stats := handlers.QueueStatsResponse{
PendingCount: 0,
ProcessingCount: 0,
FailedCount: 0,
CompletedCount: 0,
TotalCount: int64(len(queueItems)),
}
for _, item := range queueItems {
switch item.Status {
case "pending":
stats.PendingCount++
case "processing":
stats.ProcessingCount++
case "failed":
stats.FailedCount++
case "completed":
stats.CompletedCount++
}
}
// Render template WITH data (SSR)
var buf bytes.Buffer
err = templates.Queue(user, queueItems, stats).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Collections management route (protected) - SSR version
collectionHandler := handlers.NewCollectionHandler(queries, connManager)
collections := protected.Group("/collections")
collections.POST("/bulk-add-books", collectionHandler.HandleBulkAddBooks)
collections.GET("", func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, queries)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
// Fetch collections for SSR
collectionData, err := collectionHandler.GetCollectionsData(c)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading collections")
}
// Convert to template format
collectionsList := make([]templates.CollectionData, len(collectionData))
for i, col := range collectionData {
description := ""
if col.Description.Valid {
description = col.Description.String
}
color := ""
if col.Color.Valid {
color = col.Color.String
}
icon := ""
if col.Icon.Valid {
icon = col.Icon.String
}
collectionsList[i] = templates.CollectionData{
ID: uuid.UUID(col.ID.Bytes).String(),
Name: col.Name,
Description: description,
Color: color,
Icon: icon,
}
}
// Render template WITH data (SSR)
var buf bytes.Buffer
err = templates.Collection(user, collectionsList).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
collections.GET("/:id", func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, queries)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
collectionID, err := uuid.Parse(c.Param("id"))
if err != nil {
return c.HTML(http.StatusBadRequest, "Invalid collection ID")
}
// Fetch collection for SSR
collectionDB, err := collectionHandler.GetCollectionData(c, collectionID)
if err != nil {
return c.HTML(http.StatusNotFound, "Collection not found")
}
// Fetch books for SSR
booksData, err := collectionHandler.GetCollectionBooksData(c, collectionID)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading books")
}
// Convert to template format
description := ""
if collectionDB.Description.Valid {
description = collectionDB.Description.String
}
color := ""
if collectionDB.Color.Valid {
color = collectionDB.Color.String
}
icon := ""
if collectionDB.Icon.Valid {
icon = collectionDB.Icon.String
}
collectionDetail := templates.CollectionDetailData{
ID: uuid.UUID(collectionDB.ID.Bytes).String(),
Name: collectionDB.Name,
Description: description,
Color: color,
Icon: icon,
}
books := make([]templates.BookData, len(booksData))
for i, book := range booksData {
author := ""
if book.Author.Valid {
author = book.Author.String
}
coverPath := ""
if book.CoverImagePath.Valid {
coverPath = book.CoverImagePath.String
}
books[i] = templates.BookData{
MediaItemID: uuid.UUID(book.MediaItemID.Bytes).String(),
Title: book.Title,
Author: author,
CoverImagePath: coverPath,
}
}
// Render template WITH data (SSR)
var buf bytes.Buffer
err = templates.CollectionDetail(user, collectionDetail, books).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Documentation routes (no authentication required)
docsHandler := docs.NewHTTPHandler("docs")
e.GET("/docs", docsHandler.DocsHome)
e.GET("/docs/*", docsHandler.ShowDocumentation)
e.GET("/docs/api/search", docsHandler.Search)
e.GET("/docs/search-index.json", docsHandler.ServeSearchIndex)
// ========================================================================
// FRONTEND ROUTES, HEALTH CHECK, DOCS (all now in router package)
// ========================================================================
// Start server
log.Printf("Starting server on port %s", cfg.ServerPort)
+19 -19
View File
@@ -14,7 +14,7 @@ import (
// TestAnalyticsReadingStats tests the reading statistics endpoint
func TestAnalyticsReadingStats(t *testing.T) {
t.Run("GetReadingStats_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats", nil)
@@ -27,7 +27,7 @@ func TestAnalyticsReadingStats(t *testing.T) {
})
t.Run("GetReadingStats_WithAuth_DefaultDates", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -53,7 +53,7 @@ func TestAnalyticsReadingStats(t *testing.T) {
})
t.Run("GetReadingStats_WithCustomDateRange", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -72,7 +72,7 @@ func TestAnalyticsReadingStats(t *testing.T) {
})
t.Run("GetReadingStats_InvalidStartDate", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -88,7 +88,7 @@ func TestAnalyticsReadingStats(t *testing.T) {
})
t.Run("GetReadingStats_InvalidEndDate", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -104,7 +104,7 @@ func TestAnalyticsReadingStats(t *testing.T) {
})
t.Run("GetReadingStats_EmptyHistory", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -130,7 +130,7 @@ func TestAnalyticsReadingStats(t *testing.T) {
// TestAnalyticsDeviceUsage tests the device usage endpoint
func TestAnalyticsDeviceUsage(t *testing.T) {
t.Run("GetDeviceUsage_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/device-usage", nil)
@@ -143,7 +143,7 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
})
t.Run("GetDeviceUsage_WithAuth_NoDevices", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -166,7 +166,7 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
})
t.Run("GetDeviceUsage_WithAuth_WithDevices", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -206,7 +206,7 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
})
t.Run("GetDeviceUsage_ResponseStructure", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -242,7 +242,7 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
// TestAnalyticsPopularBooks tests the popular books endpoint
func TestAnalyticsPopularBooks(t *testing.T) {
t.Run("GetPopularBooks_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books", nil)
@@ -255,7 +255,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
})
t.Run("GetPopularBooks_WithAuth_DefaultLimit", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -280,7 +280,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
})
t.Run("GetPopularBooks_WithCustomLimit", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -302,7 +302,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
})
t.Run("GetPopularBooks_InvalidLimit", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -325,7 +325,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
})
t.Run("GetPopularBooks_ResponseStructure", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -378,7 +378,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
})
t.Run("GetPopularBooks_NoReadingHistory", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -404,7 +404,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
// TestAnalyticsEdgeCases tests edge cases for analytics endpoints
func TestAnalyticsEdgeCases(t *testing.T) {
t.Run("ReadingStats_FutureDateRange", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -429,7 +429,7 @@ func TestAnalyticsEdgeCases(t *testing.T) {
})
t.Run("PopularBooks_LimitZero", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -452,7 +452,7 @@ func TestAnalyticsEdgeCases(t *testing.T) {
})
t.Run("PopularBooks_VeryLargeLimit", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+55 -32
View File
@@ -11,10 +11,28 @@ import (
"github.com/stretchr/testify/require"
)
// getJSONInt converts an interface{} value to int, handling both int and float64
func getJSONInt(v interface{}) int {
switch val := v.(type) {
case int:
return val
case float64:
return int(val)
case int32:
return int(val)
case int64:
return int(val)
case float32:
return int(val)
default:
return 0
}
}
// TestBookMatchingQueryBooks tests the book query endpoint
func TestBookMatchingQueryBooks(t *testing.T) {
t.Run("QueryBooks_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -34,7 +52,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
})
t.Run("QueryBooks_WithAuth_ByTitle", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -64,7 +82,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
})
t.Run("QueryBooks_InvalidRequestBody", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -83,7 +101,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
})
t.Run("QueryBooks_NoResults", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -107,7 +125,12 @@ func TestBookMatchingQueryBooks(t *testing.T) {
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
matches := result["matches"].([]interface{})
var matches []interface{}
if matchesIf, ok := result["matches"]; ok && matchesIf != nil {
if matchesSlice, ok := matchesIf.([]interface{}); ok {
matches = matchesSlice
}
}
assert.Equal(t, 0, len(matches))
})
}
@@ -115,7 +138,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
// TestBookMatchingBulkLink tests bulk linking operations
func TestBookMatchingBulkLink(t *testing.T) {
t.Run("BulkLinkBooks_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -141,7 +164,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
})
t.Run("BulkLinkBooks_WithAuth_EmptyLinks", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -165,13 +188,13 @@ func TestBookMatchingBulkLink(t *testing.T) {
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
assert.Equal(t, 0, result["total"])
assert.Equal(t, 0, result["successful"])
assert.Equal(t, 0, result["failed"])
assert.Equal(t, 0, getJSONInt(result["total"]))
assert.Equal(t, 0, getJSONInt(result["successful"]))
assert.Equal(t, 0, getJSONInt(result["failed"]))
})
t.Run("BulkLinkBooks_InvalidUnlinkedBookID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -213,7 +236,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
})
t.Run("BulkLinkBooks_MultipleLinks", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -253,7 +276,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
assert.Equal(t, 3, result["total"])
assert.Equal(t, 3, getJSONInt(result["total"]))
results := result["results"].([]interface{})
assert.Equal(t, 3, len(results))
})
@@ -262,7 +285,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
// TestBookMatchingAutoLink tests automatic linking
func TestBookMatchingAutoLink(t *testing.T) {
t.Run("AutoLinkBooks_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -283,7 +306,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
})
t.Run("AutoLinkBooks_WithAuth_DefaultThreshold", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -310,7 +333,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
})
t.Run("AutoLinkBooks_CustomThreshold", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -339,7 +362,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
})
t.Run("AutoLinkBooks_NoUnlinkedBooks", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -371,7 +394,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
// TestBookMatchingSuggestions tests getting suggestions for unlinked books
func TestBookMatchingSuggestions(t *testing.T) {
t.Run("GetUnlinkedBookSuggestions_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
testID := uuid.New()
@@ -386,7 +409,7 @@ func TestBookMatchingSuggestions(t *testing.T) {
})
t.Run("GetUnlinkedBookSuggestions_InvalidUUID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -403,7 +426,7 @@ func TestBookMatchingSuggestions(t *testing.T) {
})
t.Run("GetUnlinkedBookSuggestions_BookNotFound", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -421,7 +444,7 @@ func TestBookMatchingSuggestions(t *testing.T) {
})
t.Run("GetUnlinkedBookSuggestions_ResponseStructure", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -445,7 +468,7 @@ func TestBookMatchingSuggestions(t *testing.T) {
// TestBookMatchingDeviceFileAliases tests device file alias operations
func TestBookMatchingDeviceFileAliases(t *testing.T) {
t.Run("GetDeviceFileAliases_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
testID := uuid.New()
@@ -460,7 +483,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("GetDeviceFileAliases_WithAuth", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -485,7 +508,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("CreateDeviceFileAlias_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
deviceID := uuid.New()
@@ -511,7 +534,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("CreateDeviceFileAlias_InvalidDeviceID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -539,7 +562,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("CreateDeviceFileAlias_InvalidMediaItemID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -567,7 +590,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("UpdateDeviceFileAlias_InvalidAliasID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -592,7 +615,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("DeleteDeviceFileAlias_InvalidAliasID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -614,7 +637,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
// TestBookMatchingGetBookMatches tests the book matches endpoint
func TestBookMatchingGetBookMatches(t *testing.T) {
t.Run("GetBookMatches_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/books/match?title=Test", nil)
@@ -628,7 +651,7 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
})
t.Run("GetBookMatches_WithAuth_ByTitle", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -651,7 +674,7 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
})
t.Run("GetBookMatches_InvalidFileSize", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -668,7 +691,7 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
})
t.Run("GetBookMatches_MultipleIdentifiers", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+11 -11
View File
@@ -14,7 +14,7 @@ import (
// TestCollectionsBulkOperations tests bulk collection operations
func TestCollectionsBulkOperations(t *testing.T) {
t.Run("BulkAddBooks_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -39,7 +39,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
})
t.Run("BulkAddBooks_EmptyOperations", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -62,7 +62,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
})
t.Run("BulkAddBooks_InvalidCollectionID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -105,7 +105,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
})
t.Run("BulkAddBooks_InvalidBookID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -160,7 +160,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
})
t.Run("BulkAddBooks_SingleOperation", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -223,7 +223,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
})
t.Run("BulkAddBooks_MultipleBooksSingleCollection", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -277,12 +277,12 @@ func TestCollectionsBulkOperations(t *testing.T) {
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
assert.Equal(t, 3, result["total"])
assert.Equal(t, 3, getJSONInt(result["total"]))
assert.True(t, result["success"].(float64) > 0)
})
t.Run("BulkAddBooks_MultipleCollections", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -358,11 +358,11 @@ func TestCollectionsBulkOperations(t *testing.T) {
json.NewDecoder(resp.Body).Decode(&result)
assert.Contains(t, result, "results")
assert.Equal(t, 3, result["total"])
assert.Equal(t, 3, getJSONInt(result["total"]))
})
t.Run("BulkAddBooks_DuplicateBooks", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -419,7 +419,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
})
t.Run("BulkAddBooks_InvalidRequestBody", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+16 -16
View File
@@ -14,7 +14,7 @@ import (
// TestConflictsBulkOperations tests bulk conflict resolution operations
func TestConflictsBulkOperations(t *testing.T) {
t.Run("BulkResolveConflicts_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -35,7 +35,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_EmptyConflictIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -59,7 +59,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_InvalidConflictID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -95,7 +95,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_InvalidStrategy", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -119,7 +119,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_MostRecentStrategy", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -149,7 +149,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_HighestProgressStrategy", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -179,7 +179,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_ManualStrategy_WithoutWinner", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -208,7 +208,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_ManualStrategy_WithWinner", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -233,7 +233,7 @@ func TestConflictsBulkOperations(t *testing.T) {
})
t.Run("BulkResolveConflicts_InvalidRequestBody", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -255,7 +255,7 @@ func TestConflictsBulkOperations(t *testing.T) {
// TestConflictsBulkDismiss tests bulk dismiss operations
func TestConflictsBulkDismiss(t *testing.T) {
t.Run("BulkDismissConflicts_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -275,7 +275,7 @@ func TestConflictsBulkDismiss(t *testing.T) {
})
t.Run("BulkDismissConflicts_EmptyConflictIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -298,7 +298,7 @@ func TestConflictsBulkDismiss(t *testing.T) {
})
t.Run("BulkDismissConflicts_InvalidConflictID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -333,7 +333,7 @@ func TestConflictsBulkDismiss(t *testing.T) {
})
t.Run("BulkDismissConflicts_MultipleConflicts", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -366,7 +366,7 @@ func TestConflictsBulkDismiss(t *testing.T) {
})
t.Run("BulkDismissConflicts_InvalidRequestBody", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -388,7 +388,7 @@ func TestConflictsBulkDismiss(t *testing.T) {
// TestConflictsBulkEdgeCases tests edge cases for bulk operations
func TestConflictsBulkEdgeCases(t *testing.T) {
t.Run("BulkResolve_NonExistentConflicts", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -423,7 +423,7 @@ func TestConflictsBulkEdgeCases(t *testing.T) {
})
t.Run("BulkDismiss_MixedValidInvalid", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+13 -8
View File
@@ -14,7 +14,7 @@ import (
// TestUpdateUserMaxDevices tests the admin endpoint for updating user device cap
func TestUpdateUserMaxDevices(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create test user with admin role
@@ -87,7 +87,7 @@ func TestUpdateUserMaxDevices(t *testing.T) {
// TestUpdateUserMaxDevicesValidation tests validation of max_devices parameter
func TestUpdateUserMaxDevicesValidation(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create admin user and get token
@@ -149,7 +149,7 @@ func TestUpdateUserMaxDevicesValidation(t *testing.T) {
// TestUpdateUserMaxDevicesAuth tests authentication requirements
func TestUpdateUserMaxDevicesAuth(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create admin user
@@ -203,7 +203,7 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) {
// TestUpdateUserMaxDevicesNonExistentUser tests with non-existent user ID
func TestUpdateUserMaxDevicesNonExistentUser(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create admin user
@@ -235,7 +235,7 @@ func TestUpdateUserMaxDevicesNonExistentUser(t *testing.T) {
// TestUpdateUserMaxDevicesMissingUserID tests with missing user ID in URL
func TestUpdateUserMaxDevicesMissingUserID(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create admin user
@@ -264,7 +264,7 @@ func TestUpdateUserMaxDevicesMissingUserID(t *testing.T) {
// TestListUsersIncludesMaxDevices tests that List Users returns max_devices field
func TestListUsersIncludesMaxDevices(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create admin user
@@ -363,8 +363,13 @@ func getAdminToken(t *testing.T, ts *httptest.Server, userID uuid.UUID) string {
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
token := result["access_token"].(string)
return token
// Safe type assertion with check
if accessToken, ok := result["access_token"].(string); ok {
return accessToken
}
// Handle error case - if login failed, return empty string
return ""
}
// Helper function to login user by credentials
+8 -8
View File
@@ -16,7 +16,7 @@ import (
)
func TestDeviceRegistrationFlow(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// Step 1: Initiate device registration
@@ -115,7 +115,7 @@ func TestDeviceRegistrationFlow(t *testing.T) {
}
func TestListDevices(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Login to get token
@@ -160,7 +160,7 @@ func TestListDevices(t *testing.T) {
}
func TestUpdateDevice(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Login to get token
@@ -216,7 +216,7 @@ func TestUpdateDevice(t *testing.T) {
}
func TestDeleteDevice(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Login to get token
@@ -258,7 +258,7 @@ func TestDeleteDevice(t *testing.T) {
}
func TestDeviceAuthentication(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create a device directly in the database
@@ -290,7 +290,7 @@ func TestDeviceAuthentication(t *testing.T) {
}
func TestListPendingRegistrations(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -311,7 +311,7 @@ func TestListPendingRegistrations(t *testing.T) {
}
func TestApproveDeviceRegistration(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -350,7 +350,7 @@ func TestApproveDeviceRegistration(t *testing.T) {
}
func TestRejectDeviceRegistration(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+5 -5
View File
@@ -17,7 +17,7 @@ func TestKoboInitialization(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer closeTestServer(t, ts, db)
token := loginTestUser(t, ts, db)
@@ -42,7 +42,7 @@ func TestKoboLibrarySync(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer closeTestServer(t, ts, db)
token := loginTestUser(t, ts, db)
@@ -66,7 +66,7 @@ func TestKoboMarkupSync(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer closeTestServer(t, ts, db)
token := loginTestUser(t, ts, db)
@@ -122,7 +122,7 @@ func TestKoboBookmarkSync(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer closeTestServer(t, ts, db)
token := loginTestUser(t, ts, db)
@@ -163,7 +163,7 @@ func TestKoboAnalyticsGettests(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer closeTestServer(t, ts, db)
token := loginTestUser(t, ts, db)
+12 -12
View File
@@ -14,7 +14,7 @@ import (
// TestMediaBulkOperations tests bulk media operations
func TestMediaBulkOperations(t *testing.T) {
t.Run("BulkDeleteBooks_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -34,7 +34,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkDeleteBooks_EmptyBookIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -57,7 +57,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkDeleteBooks_InvalidBookIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -88,7 +88,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkDeleteBooks_WithValidBooks", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -123,7 +123,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkDeleteBooks_InvalidRequestBody", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -142,7 +142,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkUpdateBooks_WithoutAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -165,7 +165,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkUpdateBooks_EmptyBookIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -191,7 +191,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkUpdateBooks_InvalidBookIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -225,7 +225,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkUpdateBooks_UpdateTags", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -262,7 +262,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkUpdateBooks_UpdateReadingStatus", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -296,7 +296,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkUpdateBooks_UpdateMultipleFields", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -332,7 +332,7 @@ func TestMediaBulkOperations(t *testing.T) {
})
t.Run("BulkUpdateBooks_InvalidRequestBody", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+5 -5
View File
@@ -42,7 +42,7 @@ func createTestLibrary(t *testing.T, ts *httptest.Server, token, name string) st
// TestMediaItemISBNNormalization tests ISBN normalization with media-items endpoint
func TestMediaItemISBNNormalization(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
// Create an ebook library first
@@ -131,7 +131,7 @@ func TestMediaItemISBNNormalization(t *testing.T) {
// TestMediaItemISBNEdgeCases tests ISBN edge cases
func TestMediaItemISBNEdgeCases(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -216,7 +216,7 @@ func TestMediaItemISBNEdgeCases(t *testing.T) {
// TestMediaItemsPagination tests pagination with media-items endpoint
func TestMediaItemsPagination(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -323,7 +323,7 @@ func TestMediaItemsPagination(t *testing.T) {
// TestMediaItemLibraryRequirement tests that media items require a library
func TestMediaItemLibraryRequirement(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -386,7 +386,7 @@ func TestMediaItemLibraryRequirement(t *testing.T) {
// TestUpdateMediaItemISBN tests updating media-item ISBN
func TestUpdateMediaItemISBN(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+21 -21
View File
@@ -12,7 +12,7 @@ import (
// TestOPDSEndpoints tests OPDS (Open Publication Distribution System) endpoints
func TestOPDSEndpoints(t *testing.T) {
t.Run("GetDeviceCatalog_WithoutDeviceAuth", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
deviceID := uuid.New()
@@ -29,7 +29,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceCatalog_InvalidDeviceID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/catalog", nil)
@@ -44,7 +44,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceCatalog_ValidDevice", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -65,7 +65,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("SearchDeviceCatalog_InvalidDeviceID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/search?query=test", nil)
@@ -79,7 +79,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("SearchDeviceCatalog_ValidDevice", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -98,7 +98,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceNavigation_InvalidDeviceID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/nav", nil)
@@ -112,7 +112,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceNavigation_ValidDevice", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -131,7 +131,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("DownloadBook_InvalidDeviceID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
bookID := uuid.New()
@@ -146,7 +146,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("DownloadBook_InvalidBookID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
deviceID := uuid.New()
@@ -161,7 +161,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("DownloadBook_ValidIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -182,7 +182,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetCoverImage_InvalidDeviceID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
bookID := uuid.New()
@@ -197,7 +197,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetCoverImage_InvalidBookID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
deviceID := uuid.New()
@@ -212,7 +212,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetCoverImage_ValidIDs", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -232,7 +232,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("ListFormats_InvalidDeviceID", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
bookID := uuid.New()
@@ -247,7 +247,7 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("ListFormats_ValidDeviceID", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -270,7 +270,7 @@ func TestOPDSEndpoints(t *testing.T) {
// TestOPDSConversion tests on-the-fly conversion for downloads
func TestOPDSConversion(t *testing.T) {
t.Run("DownloadKEPUB_FormatParameter", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -292,7 +292,7 @@ func TestOPDSConversion(t *testing.T) {
})
t.Run("DownloadEPUB_DefaultFormat", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -313,7 +313,7 @@ func TestOPDSConversion(t *testing.T) {
})
t.Run("Download_UnsupportedFormat", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -337,7 +337,7 @@ func TestOPDSConversion(t *testing.T) {
// TestOPDSEdgeCases tests edge cases for OPDS endpoints
func TestOPDSEdgeCases(t *testing.T) {
t.Run("Catalog_EmptyLibrary", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -356,7 +356,7 @@ func TestOPDSEdgeCases(t *testing.T) {
})
t.Run("Search_SpecialCharacters", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -376,7 +376,7 @@ func TestOPDSEdgeCases(t *testing.T) {
})
t.Run("Search_EmptyQuery", func(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
+7 -7
View File
@@ -17,7 +17,7 @@ import (
)
func TestListAllQueueItems_Admin(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginAdminUser(t, ts, db)
@@ -35,7 +35,7 @@ func TestListAllQueueItems_Admin(t *testing.T) {
}
func TestGetDeviceQueueStats(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -72,7 +72,7 @@ func TestGetDeviceQueueStats(t *testing.T) {
}
func TestListDeviceQueueItems(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -109,7 +109,7 @@ func TestListDeviceQueueItems(t *testing.T) {
}
func TestRetryQueueItem(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -125,7 +125,7 @@ func TestRetryQueueItem(t *testing.T) {
}
func TestDeleteQueueItem(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -141,7 +141,7 @@ func TestDeleteQueueItem(t *testing.T) {
}
func TestClearDeviceQueue(t *testing.T) {
ts, db, _, _ := setupTestServer(t)
ts, db, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, db)
@@ -174,7 +174,7 @@ func TestClearDeviceQueue(t *testing.T) {
}
func TestQueueEndpoints_Unauthorized(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
tests := []struct {
+12 -12
View File
@@ -13,7 +13,7 @@ import (
// TestRefreshTokenFlow comprehensive tests for token refresh functionality
func TestRefreshTokenFlow(t *testing.T) {
t.Run("RefreshToken_MissingToken", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{}
@@ -31,7 +31,7 @@ func TestRefreshTokenFlow(t *testing.T) {
})
t.Run("RefreshToken_InvalidTokenFormat", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -51,7 +51,7 @@ func TestRefreshTokenFlow(t *testing.T) {
})
t.Run("RefreshToken_ExpiredToken", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// This would require an expired token - for now test with invalid token
@@ -72,7 +72,7 @@ func TestRefreshTokenFlow(t *testing.T) {
})
t.Run("RefreshToken_ValidToken", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// First, login to get tokens
@@ -126,7 +126,7 @@ func TestRefreshTokenFlow(t *testing.T) {
})
t.Run("RefreshToken_InvalidRequestBody", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// Send invalid JSON
@@ -142,7 +142,7 @@ func TestRefreshTokenFlow(t *testing.T) {
})
t.Run("RefreshToken_MissingContentType", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -166,7 +166,7 @@ func TestRefreshTokenFlow(t *testing.T) {
// TestRefreshTokenSecurity tests security aspects of token refresh
func TestRefreshTokenSecurity(t *testing.T) {
t.Run("RefreshToken_ReuseProtection", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// Login to get tokens
@@ -220,7 +220,7 @@ func TestRefreshTokenSecurity(t *testing.T) {
})
t.Run("RefreshToken_TokenTampering", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// Login to get a valid token
@@ -269,7 +269,7 @@ func TestRefreshTokenSecurity(t *testing.T) {
// TestRefreshTokenEdgeCases tests edge cases for token refresh
func TestRefreshTokenEdgeCases(t *testing.T) {
t.Run("RefreshToken_EmptyStringToken", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -289,7 +289,7 @@ func TestRefreshTokenEdgeCases(t *testing.T) {
})
t.Run("RefreshToken_NullToken", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
req := map[string]interface{}{
@@ -309,7 +309,7 @@ func TestRefreshTokenEdgeCases(t *testing.T) {
})
t.Run("RefreshToken_ResponseStructure", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// Login to get tokens
@@ -361,7 +361,7 @@ func TestRefreshTokenEdgeCases(t *testing.T) {
})
t.Run("RefreshToken_TokenType", func(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// Login to get tokens
+95 -101
View File
@@ -4,18 +4,21 @@ import (
"bookhoard/internal/config"
"bookhoard/internal/database"
"bookhoard/internal/handlers"
"bookhoard/internal/middleware"
ratelimit "bookhoard/internal/middleware"
wsync "bookhoard/internal/sync"
"bookhoard/internal/router"
"bookhoard/internal/services"
"bookhoard/internal/sync"
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
@@ -24,6 +27,15 @@ import (
"github.com/stretchr/testify/require"
)
// CustomValidator wraps the go-playground validator
type CustomValidator struct {
validator *validator.Validate
}
func (cv *CustomValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
// Helper functions for testing
func containsPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
@@ -38,71 +50,23 @@ func trimSpace(s string) string {
}
// setupTestServer creates a test server with a test database
// Returns: (*httptest.Server, *database.Queries, *config.Config, *handlers.Handler)
func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config.Config, *handlers.Handler) {
// Check if DATABASE_URL is set (for containerized testing)
dbURL := os.Getenv("DATABASE_URL")
// Returns: (*httptest.Server, *database.Queries, *config.Config)
func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config.Config) {
// Load configuration using the same method as main application
cfg := config.LoadConfig()
var cfg *config.Config
var dbPool *pgxpool.Pool
var err error
// Apply test-specific overrides
cfg.ServerPort = "0" // Use random port for tests
cfg.BaseURL = "http://localhost"
cfg.JWTSecret = "test-secret-key"
cfg.UploadPath = "./test-uploads"
cfg.TestMode = true
cfg.RateLimitEnabled = false
cfg.RequestsPerMinute = 1000
if dbURL != "" {
// Use provided DATABASE_URL (for testing against containerized database)
t.Logf("Using DATABASE_URL from environment for testing")
// Parse the DATABASE_URL to extract connection details for config
cfg = &config.Config{
ServerPort: "0",
BaseURL: "http://localhost",
DatabaseHost: "localhost",
DatabasePort: "5432",
DatabaseUser: "postgres",
DatabasePassword: "", // Not used when DATABASE_URL is set
DatabaseName: "bookhoard",
JWTSecret: "test-secret-key",
UploadPath: "./test-uploads",
TestMode: true,
RateLimitEnabled: false,
RequestsPerMinute: 1000,
}
// Connect using DATABASE_URL directly
dbPool, err = pgxpool.New(context.Background(), dbURL)
require.NoError(t, err, "Failed to connect to test database using DATABASE_URL")
} else {
// Legacy behavior: construct database URL from parts
dbPass := os.Getenv("DATABASE_PASSWORD")
if dbPass == "" {
dbPass = os.Getenv("DBPASS")
}
// If password looks like it has special chars (=, +, /), use local postgres default
if strings.Contains(dbPass, "=") || strings.Contains(dbPass, "+") || len(dbPass) > 20 {
t.Logf("Warning: Database password has special characters, using local default 'postgres'")
dbPass = "postgres"
}
// Load test configuration
cfg = &config.Config{
ServerPort: "0", // Use random port for tests
BaseURL: "http://localhost",
DatabaseHost: "localhost",
DatabasePort: "5432",
DatabaseUser: "postgres",
DatabasePassword: dbPass,
DatabaseName: "bookhoard",
JWTSecret: "test-secret-key",
UploadPath: "./test-uploads",
TestMode: true,
RateLimitEnabled: false,
RequestsPerMinute: 1000,
}
// Connect to test database
dbPool, err = pgxpool.New(context.Background(), cfg.DatabaseURL())
require.NoError(t, err, "Failed to connect to test database")
}
// Connect to test database using the same method as main application
dbPool, err := pgxpool.New(context.Background(), cfg.DatabaseURL())
require.NoError(t, err, "Failed to connect to test database")
queries := database.New(dbPool)
@@ -111,46 +75,72 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
// Create handlers
authHandler := handlers.NewAuthHandler(queries, cfg.JWTSecret, loginAttemptTracker)
libraryHandler := handlers.NewLibraryHandler(queries)
deviceHandler := handlers.NewDeviceHandler(queries, cfg.JWTSecret, cfg)
deviceAuthMiddleware := middleware.NewDeviceAuthMiddleware(queries)
// Create WebSocket connection manager for testing
connManager := wsync.NewConnectionManager()
// Create WebSocket connection manager
connManager := sync.NewConnectionManager()
connManager.StartCleanupTask()
// Create sync queue processor
queueProcessor := sync.NewSyncQueueProcessor(queries)
go queueProcessor.Start(context.Background())
koreaderHandler := handlers.NewKOReaderHandler(queries, connManager, queueProcessor)
wsHandler := handlers.NewWSHandler(queries, connManager, cfg.JWTSecret, deviceAuthMiddleware)
conflictHandler := handlers.NewConflictHandler(queries, connManager)
analyticsHandler := handlers.NewAnalyticsHandler(queries)
queueHandler := handlers.NewQueueHandler(queries, queueProcessor)
// Create conversion service for OPDS
conversionService := services.NewConversionService(queries, "/var/bookhoard/cache/kepub")
opdsHandler := handlers.NewOPDSHandler(queries, conversionService)
// Create Echo instance
e := echo.New()
// Set up validator
v := validator.New()
if err := ratelimit.RegisterPasswordValidation(v); err != nil {
t.Fatal("Failed to register password validator:", err)
}
e.Validator = &CustomValidator{validator: v}
// Middleware
e.Use(echomiddleware.Logger())
e.Use(echomiddleware.Recover())
e.Use(echomiddleware.CORS())
// Setup routes
protected := e.Group("/api")
h := handlers.SetupRoutes(protected, queries, connManager)
// Setup routes using router package
routerConfig := &router.Config{
Echo: e,
Queries: queries,
Cfg: cfg,
DBPool: dbPool,
AuthHandler: authHandler,
LibraryHandler: libraryHandler,
DeviceHandler: deviceHandler,
KOReaderHandler: koreaderHandler,
WSHandler: wsHandler,
ConflictHandler: conflictHandler,
AnalyticsHandler: analyticsHandler,
QueueHandler: queueHandler,
CollectionHandler: nil, // Not needed for tests
OPDSHandler: opdsHandler,
ConnManager: connManager,
QueueProcessor: queueProcessor,
DeviceAuthMiddleware: deviceAuthMiddleware,
LoginTracker: loginAttemptTracker,
}
// Device management routes (public - for registration)
e.POST("/api/devices/register", deviceHandler.InitiateRegistration)
e.POST("/api/devices/register/status", deviceHandler.CheckRegistrationStatus)
// Device management routes (protected - require user auth)
devices := protected.Group("/devices")
devices.GET("", deviceHandler.ListDevices)
devices.GET("/:id", deviceHandler.GetDevice)
devices.PUT("/:id", deviceHandler.UpdateDevice)
devices.DELETE("/:id", deviceHandler.DeleteDevice)
devices.GET("/pending", deviceHandler.ListPendingRegistrations)
devices.GET("/approve/:registration_id", deviceHandler.ApproveDevice)
devices.POST("/reject/:registration_id", deviceHandler.RejectDevice)
// Auth routes (public - for testing)
e.POST("/api/auth/register", authHandler.Register)
e.POST("/api/auth/login", authHandler.Login)
router.RegisterRoutes(routerConfig)
// Create test server
ts := httptest.NewServer(e)
// Return server, queries, config, and handler
return ts, queries, cfg, h
// Return server, queries, and config
return ts, queries, cfg
}
// loginTestUser logs in a test user and returns the JWT token
@@ -185,27 +175,31 @@ func loginTestUser(t *testing.T, ts *httptest.Server, db *database.Queries) stri
}
func getTestUserID(t *testing.T, db *database.Queries) uuid.UUID {
// Try to get existing test user
user, err := db.GetUserByEmail(context.Background(), "testuser@example.com")
ctx := context.Background()
// Check if test user exists and delete them first to ensure fresh state
user, err := db.GetUserByEmail(ctx, "testuser@example.com")
if err == nil {
// User exists, return their ID
userUUID, err := uuid.FromBytes(user.ID.Bytes[:])
require.NoError(t, err, "Failed to parse user UUID")
return userUUID
// User exists, delete them to ensure fresh password
err = db.DeleteUser(ctx, user.ID)
if err != nil {
// If delete fails (user might be referenced elsewhere), log and continue
t.Logf("Warning: Could not delete existing test user: %v", err)
}
}
// If user doesn't exist, create one with a valid password
// Password: "TestPass123!" meets complexity requirements
// This is the bcrypt hash for "TestPass123!"
passwordHash := "$2a$10$rKvZ.HZx3lLJ6IQCpH1lOukQ/xU8j5cH8mYhPY5YGfXllq5hG8y0Ou"
// Create a fresh test user with a valid password
// Password: "Test@Pass123!" meets complexity requirements
// This is the bcrypt hash for "Test@Pass123!"
passwordHash := "$2a$10$JjAtK7PPa1WexQC3AUGe8OXLeuseZ/haN1Mz7emMo6CfOvMiTVXWq"
newUser, err := db.CreateUser(context.Background(), database.CreateUserParams{
newUser, err := db.CreateUser(ctx, database.CreateUserParams{
Email: "testuser@example.com",
Username: "testuser",
PasswordHash: passwordHash,
FirstName: pgtype.Text{String: "Test", Valid: true},
LastName: pgtype.Text{String: "User", Valid: true},
Role: "user",
Role: "admin",
})
require.NoError(t, err, "Failed to create test user")
+6 -6
View File
@@ -20,7 +20,7 @@ import (
// TestWebSocketConnection tests basic WebSocket connection and authentication
func TestWebSocketConnection(t *testing.T) {
// Setup test server with WebSocket
ts, queries, _, _ := setupTestServer(t)
ts, queries, _ := setupTestServer(t)
defer ts.Close()
// Get JWT token for a test user
@@ -51,7 +51,7 @@ func TestWebSocketConnection(t *testing.T) {
// TestWebSocketDeviceAuth tests device authentication via WebSocket
func TestWebSocketDeviceAuth(t *testing.T) {
ts, queries, _, _ := setupTestServer(t)
ts, queries, _ := setupTestServer(t)
defer ts.Close()
// Create a test device
@@ -85,7 +85,7 @@ func TestWebSocketDeviceAuth(t *testing.T) {
// TestWebSocketProgressBroadcast tests that progress updates are broadcast to connected clients
func TestWebSocketProgressBroadcast(t *testing.T) {
ts, queries, _, _ := setupTestServer(t)
ts, queries, _ := setupTestServer(t)
defer ts.Close()
// Get JWT token
@@ -148,7 +148,7 @@ func TestWebSocketProgressBroadcast(t *testing.T) {
// TestWebSocketPingPong tests that ping/pong messages work correctly
func TestWebSocketPingPong(t *testing.T) {
ts, queries, _, _ := setupTestServer(t)
ts, queries, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, queries)
@@ -181,7 +181,7 @@ func TestWebSocketPingPong(t *testing.T) {
// TestWebSocketConnectionLimit tests that the server handles multiple connections
func TestWebSocketConnectionLimit(t *testing.T) {
ts, queries, _, _ := setupTestServer(t)
ts, queries, _ := setupTestServer(t)
defer ts.Close()
token := loginTestUser(t, ts, queries)
@@ -207,7 +207,7 @@ func TestWebSocketConnectionLimit(t *testing.T) {
// TestWebSocketInvalidToken tests that invalid tokens are rejected
func TestWebSocketInvalidToken(t *testing.T) {
ts, _, _, _ := setupTestServer(t)
ts, _, _ := setupTestServer(t)
defer ts.Close()
// Try to connect with invalid token
+11 -3
View File
@@ -10,12 +10,18 @@ services:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${DBPASS}
volumes:
- postgres_data:/var/lib/postgresql/data
- ./database/schema:/docker-entrypoint-initdb.d
- ./uploads:/app/uploads
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 3
env_file:
- .env
# Bookhoard Application
app:
@@ -53,11 +59,13 @@ services:
- ./uploads:/app/uploads
- bookhoard_conversion_cache:/app/cache/kepub
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
test: ["CMD-SHELL", "curl -f http://localhost:8765/health || exit 1"]
interval: 10s
timeout: 5s
retries: 3
start_period: 10s
# Named Volumes
volumes:
bookhoard_conversion_cache:
postgres_data:
bookhoard_conversion_cache:
+1
View File
@@ -82,6 +82,7 @@ func SetupRoutes(g *echo.Group, db *database.Queries, connManager *wsync.Connect
collections.POST("/:id/books", collectionHandler.AddBooks)
collections.DELETE("/:id/books/:bookId", collectionHandler.RemoveBook)
collections.POST("/:id/books/bulk-remove", collectionHandler.BulkRemoveBooks)
collections.POST("/bulk-add-books", collectionHandler.HandleBulkAddBooks)
collections.POST("/test-rules", collectionHandler.TestRules)
// Device shelf mapping routes
+4 -4
View File
@@ -271,16 +271,16 @@ func TestDeviceRateLimiter_GetRemainingRequests(t *testing.T) {
deviceID := "test-device-456"
// Initially should have all requests remaining
remaining := limiter.GetRemainingRequests(deviceID, "scan", config)
remaining := limiter.GetRemainingRequests(deviceID, "sync", config)
assert.Equal(t, 10, remaining)
// Use 3 requests
for i := 0; i < 3; i++ {
limiter.CheckRateLimit(deviceID, "scan", config)
limiter.CheckRateLimit(deviceID, "sync", config)
}
// Should have 7 remaining
remaining = limiter.GetRemainingRequests(deviceID, "scan", config)
remaining = limiter.GetRemainingRequests(deviceID, "sync", config)
assert.Equal(t, 7, remaining)
}
@@ -304,7 +304,7 @@ func TestHTTPError_ErrorWithInternal(t *testing.T) {
internalErr := assert.AnError
err := NewHTTPError(500, "Internal Error", internalErr)
assert.Equal(t, "Internal Error", err.Error())
assert.Equal(t, "Internal Error: assert.AnError general error for testing", err.Error())
assert.Equal(t, 500, err.Code)
assert.Equal(t, "Internal Error", err.Message)
assert.Equal(t, internalErr, err.Err)
+16
View File
@@ -0,0 +1,16 @@
package router
func registerAnalyticsRoutes(cfg *Config) {
e := cfg.Echo
// JWT middleware for protected routes
jwtMiddleware := createJWTMiddleware(cfg)
protected := e.Group("/api", jwtMiddleware)
// Analytics routes
analytics := protected.Group("/analytics")
analytics.GET("/reading-stats", cfg.AnalyticsHandler.GetReadingStats)
analytics.GET("/device-usage", cfg.AnalyticsHandler.GetDeviceUsage)
analytics.GET("/popular-books", cfg.AnalyticsHandler.GetPopularBooks)
}
+43
View File
@@ -0,0 +1,43 @@
package router
import (
"bookhoard/internal/handlers"
"github.com/labstack/echo/v4"
)
func registerAuthRoutes(cfg *Config, rateLimitMiddleware echo.MiddlewareFunc) {
e := cfg.Echo
// Auth routes (no auth required, but rate limited)
e.POST("/api/auth/register", rateLimitMiddleware(cfg.AuthHandler.Register))
e.POST("/api/auth/login", rateLimitMiddleware(cfg.AuthHandler.Login))
// JWT middleware for protected routes
jwtMiddleware := createJWTMiddleware(cfg)
// Create protected route group
protected := e.Group("/api", jwtMiddleware)
// Protected auth routes
protected.GET("/auth/profile", cfg.AuthHandler.GetProfile)
protected.PUT("/auth/profile", cfg.AuthHandler.UpdateProfile)
// Refresh token endpoint (no authentication required - uses refresh token from body)
e.POST("/api/auth/refresh", cfg.AuthHandler.RefreshAccessToken)
// Logout endpoint (optional authentication - can revoke tokens if provided)
e.POST("/api/auth/logout", cfg.AuthHandler.Logout)
// Auth update routes
authGroup := e.Group("/api/auth", createJWTMiddleware(cfg))
authGroup.PUT("/email", cfg.AuthHandler.UpdateEmail)
authGroup.PUT("/username", cfg.AuthHandler.UpdateUsername)
authGroup.PUT("/password", cfg.AuthHandler.UpdatePassword)
authGroup.PUT("/theme", cfg.AuthHandler.UpdateTheme)
// Admin-only routes for user management
admin := protected.Group("/auth", handlers.AdminMiddleware)
admin.GET("/users", cfg.AuthHandler.ListUsers)
admin.PUT("/users/:id/max-devices", cfg.AuthHandler.UpdateUserMaxDevices)
}
+20
View File
@@ -0,0 +1,20 @@
package router
func registerConflictRoutes(cfg *Config) {
e := cfg.Echo
// JWT middleware for protected routes
jwtMiddleware := createJWTMiddleware(cfg)
protected := e.Group("/api", jwtMiddleware)
// Conflict resolution routes
conflicts := protected.Group("/conflicts")
conflicts.GET("", cfg.ConflictHandler.ListConflicts)
conflicts.GET("/:id", cfg.ConflictHandler.GetConflict)
conflicts.POST("/:id/resolve", cfg.ConflictHandler.ResolveConflict)
conflicts.DELETE("/:id", cfg.ConflictHandler.DeleteConflict)
conflicts.POST("/dismiss-all", cfg.ConflictHandler.DismissAllResolved)
conflicts.POST("/bulk-resolve", cfg.ConflictHandler.BulkResolveConflicts)
conflicts.POST("/bulk-dismiss", cfg.ConflictHandler.BulkDismissConflicts)
}
+25
View File
@@ -0,0 +1,25 @@
package router
func registerDeviceRoutes(cfg *Config) {
e := cfg.Echo
// JWT middleware
jwtMiddleware := createJWTMiddleware(cfg)
// Protected routes
protected := e.Group("/api", jwtMiddleware)
devices := protected.Group("/devices")
// Public device registration routes (no auth required)
e.POST("/api/devices/register", cfg.DeviceHandler.InitiateRegistration)
e.POST("/api/devices/register/status", cfg.DeviceHandler.CheckRegistrationStatus)
// Device management routes (protected)
devices.GET("", cfg.DeviceHandler.ListDevices)
devices.GET("/:id", cfg.DeviceHandler.GetDevice)
devices.PUT("/:id", cfg.DeviceHandler.UpdateDevice)
devices.DELETE("/:id", cfg.DeviceHandler.DeleteDevice)
devices.GET("/pending", cfg.DeviceHandler.ListPendingRegistrations)
devices.GET("/approve/:registration_id", cfg.DeviceHandler.ApproveDevice)
devices.POST("/reject/:registration_id", cfg.DeviceHandler.RejectDevice)
}
+13
View File
@@ -0,0 +1,13 @@
package router
import (
"bookhoard/internal/docs"
)
func registerDocumentationRoutes(cfg *Config) {
docsHandler := docs.NewHTTPHandler("docs")
cfg.Echo.GET("/docs", docsHandler.DocsHome)
cfg.Echo.GET("/docs/*", docsHandler.ShowDocumentation)
cfg.Echo.GET("/docs/api/search", docsHandler.Search)
cfg.Echo.GET("/docs/search-index.json", docsHandler.ServeSearchIndex)
}
+213
View File
@@ -0,0 +1,213 @@
package router
import (
"bytes"
"context"
"net/http"
"time"
"bookhoard/internal/handlers"
"bookhoard/templates"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo-jwt/v4"
"github.com/labstack/echo/v4"
)
func registerFrontendRoutes(cfg *Config) {
e := cfg.Echo
// ============================================================================
// FRONTEND ROUTES - DO NOT DELETE
// These routes serve Server-Side Rendered (SSR) HTML pages for the web UI.
// They are NOT API endpoints and should NOT be removed during refactors.
// All authenticated frontend routes use the jwtMiddleware to validate tokens.
// ============================================================================
// JWT middleware for protected routes
jwtMiddleware := echojwt.WithConfig(echojwt.Config{
SigningKey: []byte(cfg.Cfg.JWTSecret),
ContextKey: "user",
SuccessHandler: func(c echo.Context) {
token := c.Get("user").(*jwt.Token)
claims := token.Claims.(jwt.MapClaims)
c.Set("user_id", claims["user_id"])
c.Set("user_role", claims["user_role"])
c.Set("user_email", claims["user_email"])
c.Set("user_username", claims["user_username"])
},
})
// Protected route group
protected := e.Group("/api", jwtMiddleware)
// Public routes for login and registration pages
e.GET("/login", func(c echo.Context) error {
var buf bytes.Buffer
err := templates.Login().Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
e.GET("/register", func(c echo.Context) error {
var buf bytes.Buffer
err := templates.Register().Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Root route - landing page with smart login detection
e.GET("/", func(c echo.Context) error {
var buf bytes.Buffer
var err error
tokenString := c.Request().Header.Get("Authorization")
if tokenString != "" && len(tokenString) > 7 && tokenString[:7] == "Bearer " {
tokenString = tokenString[7:]
} else {
cookie, err := c.Cookie("token")
if err == nil {
tokenString = cookie.Value
}
}
loggedIn := false
if tokenString != "" {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte(cfg.Cfg.JWTSecret), nil
})
loggedIn = err == nil && token.Valid
}
err = templates.Index(loggedIn).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Public redirect routes
e.GET("/bookshelf", func(c echo.Context) error {
return c.Redirect(http.StatusTemporaryRedirect, "/api/bookshelf")
})
e.GET("/dashboard", func(c echo.Context) error {
return c.Redirect(http.StatusTemporaryRedirect, "/api/bookshelf")
})
// Admin routes
e.GET("/admin", handlers.AdminMiddleware(func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, cfg)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
var buf bytes.Buffer
err = templates.Admin(user).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
}))
e.GET("/admin/", handlers.AdminMiddleware(func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, cfg)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
var buf bytes.Buffer
err = templates.Admin(user).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
}))
e.GET("/admin/profile", handlers.AdminMiddleware(func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, cfg)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
var buf bytes.Buffer
err = templates.AdminProfile(user).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
}))
e.GET("/admin/library", handlers.AdminMiddleware(func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, cfg)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
var buf bytes.Buffer
err = templates.AdminLibrary(user).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
}))
// Devices page
protected.GET("/devices-page", func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, cfg)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
deviceData, err := cfg.DeviceHandler.GetDevicesData(c)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading devices")
}
pendingData, err := cfg.DeviceHandler.GetPendingRegistrationsData(c)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading pending")
}
devicesList := convertDevices(deviceData)
pendingList := convertPending(pendingData)
var buf bytes.Buffer
err = templates.Devices(user, devicesList, pendingList).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Conflicts page
protected.GET("/conflicts-page", func(c echo.Context) error {
user, err := getTemplateUserWithTheme(c, cfg)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading user")
}
conflictsData, total, unresolved, err := cfg.ConflictHandler.GetConflictsData(c)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Error loading conflicts")
}
var buf bytes.Buffer
err = templates.Conflicts(user, conflictsData, total, unresolved).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
})
// Health check
e.GET("/health", func(c echo.Context) error {
ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second)
defer cancel()
if err := pingDB(cfg, ctx); err != nil {
return c.JSON(http.StatusServiceUnavailable, map[string]string{
"status": "unhealthy",
"error": "database unavailable",
})
}
return c.JSON(http.StatusOK, map[string]string{
"status": "healthy",
"database": "connected",
})
})
}
+97
View File
@@ -0,0 +1,97 @@
package router
import (
"context"
"time"
"bookhoard/internal/handlers"
"bookhoard/templates"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4"
)
func getTemplateUserWithTheme(c echo.Context, cfg *Config) (templates.User, error) {
userID := c.Get("user_id").(string)
userEmail := c.Get("user_email").(string)
userUsername := c.Get("user_username").(string)
userRole := c.Get("user_role").(string)
userUUID, err := uuid.Parse(userID)
if err != nil {
return templates.User{}, err
}
userDB, err := cfg.Queries.GetUser(c.Request().Context(), uuidToPGType(userUUID))
if err != nil {
return templates.User{}, err
}
userTheme := "tokyo-night"
if userDB.Theme.Valid {
userTheme = userDB.Theme.String
}
return templates.User{
ID: userID,
Email: userEmail,
Username: userUsername,
Role: userRole,
Theme: userTheme,
}, nil
}
func convertDevices(deviceInfos []handlers.DeviceInfo) []templates.DeviceData {
result := make([]templates.DeviceData, len(deviceInfos))
for i, d := range deviceInfos {
lastSync := ""
if d.LastSync != nil {
lastSync = d.LastSync.Format(time.RFC3339)
}
lastSeen := ""
if d.LastSeen != nil {
lastSeen = d.LastSeen.Format(time.RFC3339)
}
result[i] = templates.DeviceData{
ID: d.ID.String(),
DeviceName: d.DeviceName,
DeviceType: d.DeviceType,
SyncEnabled: d.SyncEnabled,
LastSync: lastSync,
LastSeen: lastSeen,
}
}
return result
}
func convertPending(pending []map[string]interface{}) []templates.PendingRegistrationData {
result := make([]templates.PendingRegistrationData, len(pending))
for i, p := range pending {
result[i] = templates.PendingRegistrationData{
RegistrationID: p["registration_id"].(string),
DeviceName: p["device_name"].(string),
DeviceType: p["device_type"].(string),
ExpiresAt: p["expires_at"].(string),
}
}
return result
}
func pingDB(cfg *Config, ctx context.Context) error {
if cfg.DBPool != nil {
if pool, ok := cfg.DBPool.(*pgxpool.Pool); ok {
return pool.Ping(ctx)
}
}
return nil
}
func parseUUID(s string) (uuid.UUID, error) {
return uuid.Parse(s)
}
func uuidToPGType(u uuid.UUID) pgtype.UUID {
return pgtype.UUID{Bytes: [16]byte(u), Valid: true}
}
+57
View File
@@ -0,0 +1,57 @@
package router
import (
"bookhoard/internal/handlers"
"github.com/labstack/echo/v4"
)
func registerLibraryRoutes(cfg *Config) {
e := cfg.Echo
// JWT middleware for protected routes
jwtMiddleware := createJWTMiddleware(cfg)
// Protected routes group
protected := e.Group("/api", jwtMiddleware)
// Create handler for library-specific convenience routes
h := handlers.NewHandler(cfg.Queries, cfg.ConnManager)
// Public library types endpoint
e.GET("/api/libraries/types", cfg.LibraryHandler.GetLibraryTypes)
// Library management routes
library := protected.Group("/libraries")
// Admin-only library routes
adminLibrary := library.Group("", handlers.AdminMiddleware)
adminLibrary.POST("", cfg.LibraryHandler.CreateLibrary)
adminLibrary.GET("", cfg.LibraryHandler.ListLibraries)
adminLibrary.GET("/:id", cfg.LibraryHandler.GetLibrary)
adminLibrary.PUT("/:id", cfg.LibraryHandler.UpdateLibrary)
adminLibrary.DELETE("/:id", cfg.LibraryHandler.DeleteLibrary)
adminLibrary.POST("/:id/folders", cfg.LibraryHandler.AddLibraryFolder)
adminLibrary.GET("/:id/folders", cfg.LibraryHandler.GetLibraryFolders)
adminLibrary.DELETE("/:id/folders", cfg.LibraryHandler.DeleteLibraryFolder)
adminLibrary.GET("/:id/stats", cfg.LibraryHandler.GetLibraryStats)
adminLibrary.POST("/:id/scan", func(c echo.Context) error {
libraryID := c.Param("id")
scanReq := map[string]interface{}{
"library_id": libraryID,
}
c.Set("scan_request", scanReq)
return h.ScanEbooks(c)
})
adminLibrary.GET("/:id/media-items", func(c echo.Context) error {
libraryID := c.Param("id")
c.QueryParams().Set("library_id", libraryID)
return h.ListMediaItems(c)
})
// User library visibility control
userLibrary := library.Group("/visibility")
userLibrary.GET("", cfg.LibraryHandler.GetUserVisibleLibraries)
userLibrary.POST("", cfg.LibraryHandler.SetLibraryVisibility)
// Note: Remove endpoint may not exist - check handlers
}
+31
View File
@@ -0,0 +1,31 @@
package router
import (
"bookhoard/internal/handlers"
)
func registerMediaRoutes(cfg *Config) {
e := cfg.Echo
// JWT middleware for protected routes
jwtMiddleware := createJWTMiddleware(cfg)
protected := e.Group("/api", jwtMiddleware)
// Media item handler
mediaHandler := handlers.NewMediaHandler(cfg.Queries)
// Download route (public)
e.GET("/api/books/:uuid/download", mediaHandler.DownloadBook)
// Shelf management (protected)
protected.POST("/devices/:id/shelves", mediaHandler.AddToShelf)
protected.GET("/devices/:id/shelves", mediaHandler.GetShelf)
protected.DELETE("/devices/:id/shelves", mediaHandler.RemoveFromShelf)
protected.DELETE("/devices/:id/shelves/clear", mediaHandler.ClearShelf)
// Bulk book operations (protected)
books := protected.Group("/books")
books.POST("/bulk-delete", mediaHandler.HandleBulkDelete)
books.POST("/bulk-update", mediaHandler.HandleBulkUpdate)
}
+15
View File
@@ -0,0 +1,15 @@
package router
func registerOPDSRoutes(cfg *Config) {
e := cfg.Echo
// OPDS routes (public - device authentication optional)
// Note: OPDSHandler implements its own device authentication
opds := e.Group("/opds/devices")
opds.GET("/:deviceId/catalog", cfg.OPDSHandler.GetDeviceCatalog)
opds.GET("/:deviceId/search", cfg.OPDSHandler.SearchDeviceCatalog)
opds.GET("/:deviceId/nav", cfg.OPDSHandler.GetDeviceNavigation)
opds.GET("/:deviceId/download/:bookId", cfg.OPDSHandler.DownloadBook)
opds.GET("/:deviceId/cover/:bookId", cfg.OPDSHandler.GetCoverImage)
opds.GET("/:deviceId/formats/:bookId", cfg.OPDSHandler.ListFormats)
}
+26
View File
@@ -0,0 +1,26 @@
package router
import (
"bookhoard/internal/handlers"
)
func registerQueueRoutes(cfg *Config) {
e := cfg.Echo
// JWT middleware for protected routes
jwtMiddleware := createJWTMiddleware(cfg)
protected := e.Group("/api", jwtMiddleware)
// Sync queue management routes (protected - require user auth)
queue := protected.Group("/queue")
queue.GET("/devices/:device_id/stats", cfg.QueueHandler.GetDeviceQueueStats)
queue.GET("/devices/:device_id/items", cfg.QueueHandler.ListDeviceQueueItems)
queue.POST("/items/:item_id/retry", cfg.QueueHandler.RetryQueueItem)
queue.DELETE("/items/:item_id", cfg.QueueHandler.DeleteQueueItem)
queue.DELETE("/devices/:device_id/clear", cfg.QueueHandler.ClearDeviceQueue)
// Admin-only queue routes
adminQueue := queue.Group("", handlers.AdminMiddleware)
adminQueue.GET("/items", cfg.QueueHandler.ListAllQueueItems)
}
+129
View File
@@ -0,0 +1,129 @@
package router
import (
"bookhoard/internal/config"
"bookhoard/internal/database"
"bookhoard/internal/handlers"
"bookhoard/internal/middleware"
ratelimit "bookhoard/internal/middleware"
"bookhoard/internal/sync"
"log"
"net/http"
"time"
"github.com/go-playground/validator/v10"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/labstack/echo-jwt/v4"
"github.com/labstack/echo/v4"
echomiddleware "github.com/labstack/echo/v4/middleware"
)
// CustomValidator wraps the go-playground validator
type CustomValidator struct {
validator *validator.Validate
}
func (cv *CustomValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
// Config holds all dependencies needed for route registration
type Config struct {
Echo *echo.Echo
Queries *database.Queries
Cfg *config.Config
DBPool interface{} // pgxpool.Pool interface
AuthHandler *handlers.AuthHandler
LibraryHandler *handlers.LibraryHandler
DeviceHandler *handlers.DeviceHandler
KOReaderHandler *handlers.KOReaderHandler
WSHandler *handlers.WSHandler
ConflictHandler *handlers.ConflictHandler
AnalyticsHandler *handlers.AnalyticsHandler
QueueHandler *handlers.QueueHandler
CollectionHandler *handlers.CollectionHandler
OPDSHandler *handlers.OPDSHandler
ConnManager *sync.ConnectionManager
QueueProcessor *sync.SyncQueueProcessor
DeviceAuthMiddleware *middleware.DeviceAuthMiddleware
LoginTracker *ratelimit.LoginAttemptTracker
}
// createJWTMiddleware creates a JWT middleware with proper user context setup
func createJWTMiddleware(cfg *Config) echo.MiddlewareFunc {
return echojwt.WithConfig(echojwt.Config{
SigningKey: []byte(cfg.Cfg.JWTSecret),
ContextKey: "user",
SuccessHandler: func(c echo.Context) {
token := c.Get("user").(*jwt.Token)
claims := token.Claims.(jwt.MapClaims)
c.Set("user_id", claims["user_id"])
c.Set("user_role", claims["user_role"])
c.Set("user_email", claims["user_email"])
c.Set("user_username", claims["user_username"])
// Parse UUID from string claims
userIDStr, _ := claims["user_id"].(string)
userUUID, err := uuid.Parse(userIDStr)
if err != nil {
c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user ID in token"})
return
}
c.Set("user", database.Users{
ID: pgtype.UUID{Bytes: [16]byte(userUUID), Valid: true},
Email: claims["user_email"].(string),
Username: claims["user_username"].(string),
Role: claims["user_role"].(string),
})
},
})
}
// RegisterRoutes registers all application routes
func RegisterRoutes(cfg *Config) {
e := cfg.Echo
// Set up validator
v := validator.New()
if err := ratelimit.RegisterPasswordValidation(v); err != nil {
log.Fatal("Failed to register password validator:", err)
}
e.Validator = &CustomValidator{validator: v}
// Global middleware
e.Use(echomiddleware.Logger())
e.Use(echomiddleware.Recover())
e.Use(echomiddleware.CORS())
e.Use(ratelimit.RequestTracingMiddleware(cfg.Cfg))
// Rate limiter
rateLimiterConfig := ratelimit.RateLimiterConfig{
Enabled: cfg.Cfg.RateLimitEnabled,
RequestsPerMinute: cfg.Cfg.RequestsPerMinute,
CleanupInterval: 5 * time.Minute,
}
rateLimiter := ratelimit.NewRateLimiter(rateLimiterConfig)
rateLimitMiddleware := ratelimit.RateLimiterMiddleware(rateLimiter)
// Register core application routes (collections, devices, media, etc.) - ONCE
jwtMiddleware := createJWTMiddleware(cfg)
protected := e.Group("/api", jwtMiddleware)
handlers.SetupRoutes(protected, cfg.Queries, cfg.ConnManager)
// Register route groups
registerAuthRoutes(cfg, rateLimitMiddleware)
registerLibraryRoutes(cfg)
registerDeviceRoutes(cfg)
registerSyncRoutes(cfg)
registerMediaRoutes(cfg)
registerConflictRoutes(cfg)
registerAnalyticsRoutes(cfg)
registerQueueRoutes(cfg)
registerOPDSRoutes(cfg)
registerWebSocketRoutes(cfg)
registerFrontendRoutes(cfg)
registerDocumentationRoutes(cfg)
}
+46
View File
@@ -0,0 +1,46 @@
package router
import (
"bookhoard/internal/handlers"
)
func registerSyncRoutes(cfg *Config) {
e := cfg.Echo
// JWT middleware for protected routes
jwtMiddleware := createJWTMiddleware(cfg)
protected := e.Group("/api", jwtMiddleware)
// Create handler for sync-specific routes
h := handlers.NewHandler(cfg.Queries, cfg.ConnManager)
// Book matching and unlinked book resolution routes
sync := protected.Group("/sync")
sync.POST("/bulk-link-books", h.BulkLinkBooks)
sync.POST("/auto-link-books", h.AutoLinkBooks)
sync.GET("/unlinked-books/:id/suggestions", h.GetUnlinkedBookSuggestions)
// KOReader sync routes (device authentication required)
koreaderSync := e.Group("/api/sync/koreader")
koreaderSync.POST("/progress", cfg.DeviceAuthMiddleware.Authenticate(cfg.KOReaderHandler.SyncProgress))
koreaderSync.GET("/metadata/:uuid", cfg.DeviceAuthMiddleware.Authenticate(cfg.KOReaderHandler.GetMetadata))
koreaderSync.GET("/library", cfg.DeviceAuthMiddleware.Authenticate(cfg.KOReaderHandler.GetLibrary))
koreaderSync.POST("/bookmarks", cfg.DeviceAuthMiddleware.Authenticate(cfg.KOReaderHandler.SyncBookmarks))
// Kobo sync routes (device authentication required)
koboHandler := handlers.NewKoboHandler(cfg.Queries, cfg.ConnManager)
koboSync := e.Group("/api/sync/kobo")
koboSync.POST("/markup", cfg.DeviceAuthMiddleware.Authenticate(koboHandler.Markup))
koboSync.POST("/bookmark", cfg.DeviceAuthMiddleware.Authenticate(koboHandler.Bookmark))
koboSync.POST("/v1/analytics/gettests", cfg.DeviceAuthMiddleware.Authenticate(koboHandler.AnalyticsGettests))
koboSync.GET("/v1/initialization", cfg.DeviceAuthMiddleware.Authenticate(koboHandler.Initialization))
koboSync.POST("/sync-from-server", cfg.DeviceAuthMiddleware.Authenticate(koboHandler.SyncFromServer))
}
func registerWebSocketRoutes(cfg *Config) {
e := cfg.Echo
// WebSocket endpoint for real-time sync
e.GET("/ws/sync", cfg.WSHandler.HandleWebSocket)
}
+8 -13
View File
@@ -130,24 +130,19 @@ func TestNormalizeISBN_SpecialCharacters(t *testing.T) {
expected string
}{
{
name: "with dots (not removed, only hyphens/spaces)",
input: "978.0.306.40615.7",
expected: "978.0.306.40615.7",
},
{
name: "mixed dots and hyphens",
name: "mixed dots and hyphens (hyphens removed, dots preserved)",
input: "978-0.306-40615.7",
expected: "978.0.306-40615.7",
expected: "9780.30640615.7",
},
{
name: "with underscores (preserved)",
input: "978_0_306_40615_7",
expected: "978_0_306_40615_7",
name: "multiple spaces between groups",
input: "978 0 306 40615 7",
expected: "9780306406157",
},
{
name: "with slashes (preserved)",
input: "978/0/306/40615/7",
expected: "978/0/306/40615/7",
name: "mixed hyphens and spaces",
input: "978-0 306-40615 7",
expected: "9780306406157",
},
}