Deadcode-verified removals (RTA reachability from main, cross-checked with -test and textual xrefs): Go server: - whole files: middleware/transaction.go, middleware/error_handler.go, middleware/password_validator.go (live registration machinery kept), handlers/sync.go, sync/offline.go - unrouted endpoints: auth UpdateUsername/UpdateEmail, timezone settings handler, OPDS RegisterOPDS, Kobo LibrarySync handler, ReaderHandler.ShowReader, JSON-data twins on collections/library - duplicate book-matching handlers (book_matching.go Handler twins, matching.go MatchingHandler twins), superseded helpers (router.parseUUID, handlers.ValidateDeviceToken, normalizePath, userExists, generateLockID) Templ/client: - never-routed pages: api_explorer.templ, queue.templ and their client modules api-explorer.ts, api-explorer-docs.ts, queue.ts - fully orphaned wrapper modules events.ts, dom.ts - broken clearSyncQueue (called a nonexistent endpoint) and the hidden, never-populated sync-queue section on the devices page - 166+ unused exports demoted to private (knip-verified); ~85 truly dead declarations removed via tsc noUnusedLocals fixpoint - stale 'interactive API Explorer' claims dropped from docs The docs drift-gate package (internal/docs), templ-bound modules (dashboard, stats, header, index, login, profile*, bookshelf, collections, toast-error), sqlc-generated code, and web/static are intentionally untouched.
99 lines
2.4 KiB
Go
99 lines
2.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"bookhoard/internal/database"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDeviceAuth_Authenticate_BearerToken(t *testing.T) {
|
|
authToken := "test_bearer_token_123"
|
|
|
|
middleware := &DeviceAuthMiddleware{
|
|
// Can't use mockDB directly due to interface mismatch
|
|
// In real scenario, would use a mock database or test database
|
|
rateLimiter: NewDeviceRateLimiter(),
|
|
}
|
|
|
|
e := echo.New()
|
|
req := httptest.NewRequest("GET", "/api/sync/koreader/progress", nil)
|
|
req.Header.Set("Authorization", "Bearer "+authToken)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
|
|
// Create a handler that sets the device in context
|
|
next := func(c *echo.Context) error {
|
|
device, ok := c.Get("device").(database.Devices)
|
|
if ok {
|
|
c.Set("device_id", device.ID.Bytes)
|
|
return c.JSON(http.StatusOK, map[string]string{"device": device.DeviceName})
|
|
}
|
|
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "device not found"})
|
|
}
|
|
|
|
// Note: This test demonstrates the expected flow
|
|
// In practice, you'd need a test database or mock that implements database.Queries
|
|
handler := middleware.Authenticate(next)
|
|
|
|
// Would call handler(c) and assert results
|
|
_ = handler
|
|
_ = c
|
|
|
|
// Test implementation would verify:
|
|
// 1. Bearer token is extracted correctly
|
|
// 2. Device is fetched from database
|
|
// 3. Device is validated (sync_enabled)
|
|
// 4. Rate limiting is applied
|
|
// 5. Device context is set
|
|
// 6. Next handler is called
|
|
|
|
assert.True(t, true, "Test structure verified")
|
|
}
|
|
|
|
func TestDeviceAuth_GetRequestType(t *testing.T) {
|
|
middleware := &DeviceAuthMiddleware{}
|
|
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "Progress endpoint",
|
|
path: "/api/sync/koreader/progress",
|
|
expected: "progress",
|
|
},
|
|
{
|
|
name: "Metadata endpoint",
|
|
path: "/api/sync/koreader/metadata",
|
|
expected: "metadata",
|
|
},
|
|
{
|
|
name: "Library endpoint",
|
|
path: "/api/sync/koreader/library",
|
|
expected: "metadata",
|
|
},
|
|
{
|
|
name: "Bookmark endpoint",
|
|
path: "/api/sync/kobo/abc123/bookmark",
|
|
expected: "sync",
|
|
},
|
|
{
|
|
name: "Markup endpoint",
|
|
path: "/api/sync/kobo/xyz789/markup",
|
|
expected: "sync",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := middleware.getRequestType(tt.path)
|
|
assert.Equal(t, tt.expected, result)
|
|
})
|
|
}
|
|
}
|