test(kobo): Fix Kobo integration tests with proper device authentication

- Fix TestKoboInitialization: use setupDeviceTest() for device creation
- Fix TestKoboLibrarySync: remove /test-token/ route path, add device auth
- Fix TestKoboMarkupSync: add device auth and last-read-place test case
- Fix TestKoboBookmarkSync: add device auth and last-read-place test case
- Fix TestKoboAnalyticsGettests: add device authentication
- Add debug logging to all test functions
- Remove unused imports (config, database, middleware, router, services, sync)

Phase 3 of KOBO_IMPLEMENTATION_PLAN.md completed (Steps 7-12).

All tests now use proper device authentication (Bearer tokens + x-kobo-device headers)
and include test cases for the new last-read-place bookmark feature.
This commit is contained in:
2026-02-10 12:41:09 -05:00
parent 80ad45e7f9
commit 57cb58bcbf
+53 -27
View File
@@ -10,15 +10,6 @@ import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"bookhoard/internal/config"
"bookhoard/internal/database"
"bookhoard/internal/middleware"
"bookhoard/internal/router"
"bookhoard/internal/services"
"bookhoard/internal/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -39,12 +30,12 @@ func TestKoboInitialization(t *testing.T) {
t.Run("successful initialization", func(t *testing.T) {
// Create Kobo device using TestDeviceSetup for proper authentication
deviceSetup := setupTestDevice(t, ts, db)
deviceSetup := setupDeviceTest(t)
koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-test")
req, _ := http.NewRequest("GET", ts.URL+"/api/sync/kobo/v1/initialization", nil)
req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken)
req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`,
req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`,
koboDevice.ID.String(), koboDevice.Identifier))
client := &http.Client{}
@@ -52,14 +43,6 @@ func TestKoboInitialization(t *testing.T) {
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
}
@@ -75,9 +58,16 @@ func TestKoboLibrarySync(t *testing.T) {
token := loginTestUser(t, ts, db)
_ = createTestMediaItemID(t, ts, token)
log.Printf("[DEBUG] Kobo test setup: creating device and media")
t.Run("successful library sync", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"/api/sync/kobo/test-token/v1/initialization", nil)
req.Header.Set("Authorization", "Bearer "+token)
deviceSetup := setupDeviceTest(t)
koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-test")
req, _ := http.NewRequest("GET", ts.URL+"/api/sync/kobo/v1/initialization", nil)
req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken)
req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`,
koboDevice.ID.String(), koboDevice.Identifier))
client := &http.Client{}
resp, err := client.Do(req)
@@ -99,7 +89,12 @@ func TestKoboMarkupSync(t *testing.T) {
token := loginTestUser(t, ts, db)
mediaItemID := createTestMediaItemID(t, ts, token)
log.Printf("[DEBUG] Kobo test setup: creating device and media")
t.Run("successful markup sync with annotations and bookmarks", func(t *testing.T) {
deviceSetup := setupDeviceTest(t)
koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-markup")
reqBody := map[string]interface{}{
"ReadingSync": []map[string]interface{}{
{
@@ -121,16 +116,25 @@ func TestKoboMarkupSync(t *testing.T) {
{
"BookmarkId": "bookmark-2",
"ContentId": mediaItemID,
"BookmarkText": "This is my note abouts book",
"BookmarkText": "This is my note about book",
"BookmarkType": "bookmark",
},
{
"BookmarkId": "epubcfi(/6/4[chap1]!/4/2/1:0)",
"ContentId": mediaItemID,
"BookmarkType": "last-read-place",
"Hidden": true,
"Chapter": 1,
},
},
}
body, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST", ts.URL+"/api/sync/kobo/markup", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-kobo-device", `{"DeviceId":"kobo-clara-test","Model":"Kobo Clara","SerialNumber":"N123456789"}`)
req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken)
req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`,
koboDevice.ID.String(), koboDevice.Identifier))
client := &http.Client{}
resp, err := client.Do(req)
@@ -155,23 +159,38 @@ func TestKoboBookmarkSync(t *testing.T) {
token := loginTestUser(t, ts, db)
mediaItemID := createTestMediaItemID(t, ts, token)
t.Run("successful bookmark sync", func(t *testing.T) {
log.Printf("[DEBUG] Kobo test setup: creating device and media")
t.Run("successful bookmark sync with last-read-place", func(t *testing.T) {
deviceSetup := setupDeviceTest(t)
koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-bookmark")
reqBody := map[string]interface{}{
"BookmarkSync": []map[string]interface{}{
{
"BookmarkId": "bookmark-3",
"ContentId": mediaItemID,
"BookmarkText": "Important note abouts book",
"BookmarkText": "Important note about book",
"BookmarkType": "bookmark",
"DateCreated": "2026-01-30T19:55:00Z",
},
{
"BookmarkId": "epubcfi(/6/4[chap1]!/4/2/1:156)",
"ContentId": mediaItemID,
"BookmarkType": "last-read-place",
"Hidden": true,
"Chapter": 1,
"DateCreated": "2026-01-30T19:55:00Z",
},
},
}
body, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST", ts.URL+"/api/sync/kobo/bookmark", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-kobo-device", `{"DeviceId":"kobo-clara-test","Model":"Kobo Clara","SerialNumber":"N123456789"}`)
req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken)
req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`,
koboDevice.ID.String(), koboDevice.Identifier))
client := &http.Client{}
resp, err := client.Do(req)
@@ -196,7 +215,12 @@ func TestKoboAnalyticsGettests(t *testing.T) {
token := loginTestUser(t, ts, db)
mediaItemID := createTestMediaItemID(t, ts, token)
log.Printf("[DEBUG] Kobo test setup: creating device and media")
t.Run("successful analytics tests", func(t *testing.T) {
deviceSetup := setupDeviceTest(t)
koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-analytics")
reqBody := map[string]interface{}{
"meta": map[string]string{
"name": "Kobo Analytics Tests",
@@ -210,7 +234,9 @@ func TestKoboAnalyticsGettests(t *testing.T) {
body, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST", ts.URL+"/api/sync/kobo/v1/analytics/gettests", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-kobo-device", `{"DeviceId":"kobo-clara-test","Model":"Kobo Clara","SerialNumber":"N123456789"}`)
req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken)
req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`,
koboDevice.ID.String(), koboDevice.Identifier))
client := &http.Client{}
resp, err := client.Do(req)