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.
This commit is contained in:
2026-02-22 01:57:28 -05:00
parent f54508e4dd
commit 86cc9b4e59
+17 -15
View File
@@ -12,7 +12,11 @@ import (
// TestOPDSEndpoints tests OPDS (Open Publication Distribution System) endpoints // TestOPDSEndpoints tests OPDS (Open Publication Distribution System) endpoints
func TestOPDSEndpoints(t *testing.T) { func TestOPDSEndpoints(t *testing.T) {
setup := setupTestServer(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{} client := &http.Client{}
t.Run("GetDeviceCatalog_WithoutDeviceAuth", func(t *testing.T) { 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) { t.Run("DownloadBook_ValidIDs", func(t *testing.T) {
deviceSetup := setupDeviceTest(t) deviceSetup := setupDeviceTest(t)
device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-download-test") 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) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken)
resp, err := client.Do(httpReq) resp, err := client.Do(httpReq)
@@ -182,9 +185,8 @@ func TestOPDSEndpoints(t *testing.T) {
t.Run("GetCoverImage_ValidIDs", func(t *testing.T) { t.Run("GetCoverImage_ValidIDs", func(t *testing.T) {
deviceSetup := setupDeviceTest(t) deviceSetup := setupDeviceTest(t)
device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-cover-test") 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) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken)
resp, err := client.Do(httpReq) resp, err := client.Do(httpReq)
@@ -209,9 +211,8 @@ func TestOPDSEndpoints(t *testing.T) {
t.Run("ListFormats_ValidDeviceID", func(t *testing.T) { t.Run("ListFormats_ValidDeviceID", func(t *testing.T) {
deviceSetup := setupDeviceTest(t) deviceSetup := setupDeviceTest(t)
device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-formats-test") 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) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken)
resp, err := client.Do(httpReq) resp, err := client.Do(httpReq)
@@ -226,16 +227,19 @@ func TestOPDSEndpoints(t *testing.T) {
// TestOPDSConversion tests on-the-fly conversion for downloads // TestOPDSConversion tests on-the-fly conversion for downloads
func TestOPDSConversion(t *testing.T) { func TestOPDSConversion(t *testing.T) {
setup := setupTestServer(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{} client := &http.Client{}
t.Run("DownloadKEPUB_FormatParameter", func(t *testing.T) { t.Run("DownloadKEPUB_FormatParameter", func(t *testing.T) {
deviceSetup := setupDeviceTest(t) deviceSetup := setupDeviceTest(t)
device := deviceSetup.CreateDevice(t, "Test OPDS Device", "kobo", "opds-kepub-test") device := deviceSetup.CreateDevice(t, "Test OPDS Device", "kobo", "opds-kepub-test")
bookID := createTestMediaItemID(t, setup.Server)
// Request KEPUB format // 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) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken)
resp, err := client.Do(httpReq) resp, err := client.Do(httpReq)
@@ -250,10 +254,9 @@ func TestOPDSConversion(t *testing.T) {
t.Run("DownloadEPUB_DefaultFormat", func(t *testing.T) { t.Run("DownloadEPUB_DefaultFormat", func(t *testing.T) {
deviceSetup := setupDeviceTest(t) deviceSetup := setupDeviceTest(t)
device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-epub-test") device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-epub-test")
bookID := createTestMediaItemID(t, setup.Server)
// Request default format (no format parameter) // 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) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken)
resp, err := client.Do(httpReq) resp, err := client.Do(httpReq)
@@ -267,10 +270,9 @@ func TestOPDSConversion(t *testing.T) {
t.Run("Download_UnsupportedFormat", func(t *testing.T) { t.Run("Download_UnsupportedFormat", func(t *testing.T) {
deviceSetup := setupDeviceTest(t) deviceSetup := setupDeviceTest(t)
device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-unsupported-test") device := deviceSetup.CreateDevice(t, "Test OPDS Device", "koreader", "opds-unsupported-test")
bookID := createTestMediaItemID(t, setup.Server)
// Request unsupported format // 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) httpReq.Header.Set("Authorization", "Bearer "+device.AuthToken)
resp, err := client.Do(httpReq) resp, err := client.Do(httpReq)
@@ -285,7 +287,7 @@ func TestOPDSConversion(t *testing.T) {
// TestOPDSEdgeCases tests edge cases for OPDS endpoints // TestOPDSEdgeCases tests edge cases for OPDS endpoints
func TestOPDSEdgeCases(t *testing.T) { func TestOPDSEdgeCases(t *testing.T) {
setup := setupTestServer(t) setup := setupTestServer(t)
_ = createTestMediaItemID(t, setup.Server) _ = createTestMediaItemID(t, setup)
client := &http.Client{} client := &http.Client{}
t.Run("Catalog_EmptyLibrary", func(t *testing.T) { t.Run("Catalog_EmptyLibrary", func(t *testing.T) {