From 86cc9b4e59185365db3216a2b7e4c00f6c500207 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sun, 22 Feb 2026 01:57:28 -0500 Subject: [PATCH] test(opds): fix token invalidation by restructuring tests Restructure TestOPDSEndpoints and TestOPDSConversion to follow the Kobo test pattern. Create all media items at parent level before any subtests run, avoiding token invalidation when setupDeviceTest is called. Subtests now use pre-created media IDs and device.AuthToken for authentication. --- cmd/server/tests/opds_test.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/cmd/server/tests/opds_test.go b/cmd/server/tests/opds_test.go index c5413c5..d35c9ce 100644 --- a/cmd/server/tests/opds_test.go +++ b/cmd/server/tests/opds_test.go @@ -12,7 +12,11 @@ import ( // TestOPDSEndpoints tests OPDS (Open Publication Distribution System) endpoints func TestOPDSEndpoints(t *testing.T) { setup := setupTestServer(t) - _ = createTestMediaItemID(t, setup.Server) + // Create ALL media items needed for ALL subtests BEFORE any t.Run (following Kobo pattern) + _ = createTestMediaItemID(t, setup) + bookID1 := createTestMediaItemID(t, setup) + bookID2 := createTestMediaItemID(t, setup) + bookID3 := createTestMediaItemID(t, setup) client := &http.Client{} t.Run("GetDeviceCatalog_WithoutDeviceAuth", func(t *testing.T) { @@ -145,9 +149,8 @@ func TestOPDSEndpoints(t *testing.T) { t.Run("DownloadBook_ValidIDs", func(t *testing.T) { deviceSetup := setupDeviceTest(t) device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-download-test") - bookID := createTestMediaItemID(t, setup.Server) - httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID, nil) + httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID1, nil) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken) resp, err := client.Do(httpReq) @@ -182,9 +185,8 @@ func TestOPDSEndpoints(t *testing.T) { t.Run("GetCoverImage_ValidIDs", func(t *testing.T) { deviceSetup := setupDeviceTest(t) device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-cover-test") - bookID := createTestMediaItemID(t, setup.Server) - httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/cover/"+bookID, nil) + httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/cover/"+bookID2, nil) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken) resp, err := client.Do(httpReq) @@ -209,9 +211,8 @@ func TestOPDSEndpoints(t *testing.T) { t.Run("ListFormats_ValidDeviceID", func(t *testing.T) { deviceSetup := setupDeviceTest(t) device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-formats-test") - bookID := createTestMediaItemID(t, setup.Server) - httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/formats/"+bookID, nil) + httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/formats/"+bookID3, nil) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken) resp, err := client.Do(httpReq) @@ -226,16 +227,19 @@ func TestOPDSEndpoints(t *testing.T) { // TestOPDSConversion tests on-the-fly conversion for downloads func TestOPDSConversion(t *testing.T) { setup := setupTestServer(t) - _ = createTestMediaItemID(t, setup.Server) + // Create ALL media items needed for ALL subtests BEFORE any t.Run (following Kobo pattern) + _ = createTestMediaItemID(t, setup) + bookID1 := createTestMediaItemID(t, setup) + bookID2 := createTestMediaItemID(t, setup) + bookID3 := createTestMediaItemID(t, setup) client := &http.Client{} t.Run("DownloadKEPUB_FormatParameter", func(t *testing.T) { deviceSetup := setupDeviceTest(t) device := deviceSetup.CreateDevice(t, "Test OPDS Device", "kobo", "opds-kepub-test") - bookID := createTestMediaItemID(t, setup.Server) // Request KEPUB format - httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID+"?format=kepub", nil) + httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID1+"?format=kepub", nil) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken) resp, err := client.Do(httpReq) @@ -250,10 +254,9 @@ func TestOPDSConversion(t *testing.T) { t.Run("DownloadEPUB_DefaultFormat", func(t *testing.T) { deviceSetup := setupDeviceTest(t) device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-epub-test") - bookID := createTestMediaItemID(t, setup.Server) // Request default format (no format parameter) - httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID, nil) + httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID2, nil) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken) resp, err := client.Do(httpReq) @@ -267,10 +270,9 @@ func TestOPDSConversion(t *testing.T) { t.Run("Download_UnsupportedFormat", func(t *testing.T) { deviceSetup := setupDeviceTest(t) device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-unsupported-test") - bookID := createTestMediaItemID(t, setup.Server) // Request unsupported format - httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID+"?format=pdf", nil) + httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+device.ID.String()+"/download/"+bookID3+"?format=pdf", nil) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken) resp, err := client.Do(httpReq) @@ -285,7 +287,7 @@ func TestOPDSConversion(t *testing.T) { // TestOPDSEdgeCases tests edge cases for OPDS endpoints func TestOPDSEdgeCases(t *testing.T) { setup := setupTestServer(t) - _ = createTestMediaItemID(t, setup.Server) + _ = createTestMediaItemID(t, setup) client := &http.Client{} t.Run("Catalog_EmptyLibrary", func(t *testing.T) {