refactor(tests): Update all test files to use TestServerSetup pattern

This commit is contained in:
2026-02-10 13:01:12 -05:00
parent f3141f18ef
commit 6c610465eb
14 changed files with 484 additions and 608 deletions
+60 -81
View File
@@ -12,11 +12,10 @@ 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)
defer ts.Close()
setup := setupTestServer(t)
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/catalog", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/catalog", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -29,10 +28,9 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceCatalog_InvalidDeviceID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/catalog", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/invalid-uuid/catalog", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -44,15 +42,14 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceCatalog_ValidDevice", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
// Note: Device registration requires different endpoint
// For now, test with a valid UUID format
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/catalog", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/catalog", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -65,10 +62,9 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("SearchDeviceCatalog_InvalidDeviceID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/search?query=test", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/invalid-uuid/search?query=test", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -79,13 +75,12 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("SearchDeviceCatalog_ValidDevice", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/search?query=test", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/search?query=test", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -98,10 +93,9 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceNavigation_InvalidDeviceID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/nav", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/invalid-uuid/nav", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -112,13 +106,12 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetDeviceNavigation_ValidDevice", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/nav", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/nav", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -131,11 +124,10 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("DownloadBook_InvalidDeviceID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
bookID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/download/"+bookID.String(), nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/invalid-uuid/download/"+bookID.String(), nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -146,11 +138,10 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("DownloadBook_InvalidBookID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/download/invalid-uuid", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/download/invalid-uuid", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -161,14 +152,13 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("DownloadBook_ValidIDs", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
bookID := createTestMediaItemID(t, ts, token)
bookID := createTestMediaItemID(t, setup.Server, token)
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID, nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID, nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -182,11 +172,10 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetCoverImage_InvalidDeviceID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
bookID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/cover/"+bookID.String(), nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/invalid-uuid/cover/"+bookID.String(), nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -197,11 +186,10 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetCoverImage_InvalidBookID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/cover/invalid-uuid", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/cover/invalid-uuid", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -212,14 +200,13 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("GetCoverImage_ValidIDs", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
bookID := createTestMediaItemID(t, ts, token)
bookID := createTestMediaItemID(t, setup.Server, token)
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/cover/"+bookID, nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/cover/"+bookID, nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -232,11 +219,10 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("ListFormats_InvalidDeviceID", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
bookID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/invalid-uuid/formats/"+bookID.String(), nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/invalid-uuid/formats/"+bookID.String(), nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -247,14 +233,13 @@ func TestOPDSEndpoints(t *testing.T) {
})
t.Run("ListFormats_ValidDeviceID", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
bookID := createTestMediaItemID(t, ts, token)
bookID := createTestMediaItemID(t, setup.Server, token)
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/formats/"+bookID, nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/formats/"+bookID, nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -270,15 +255,14 @@ 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)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
bookID := createTestMediaItemID(t, ts, token)
bookID := createTestMediaItemID(t, setup.Server, token)
// Request KEPUB format
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID+"?format=kepub", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID+"?format=kepub", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -292,15 +276,14 @@ func TestOPDSConversion(t *testing.T) {
})
t.Run("DownloadEPUB_DefaultFormat", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
bookID := createTestMediaItemID(t, ts, token)
bookID := createTestMediaItemID(t, setup.Server, token)
// Request default format (no format parameter)
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID, nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID, nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -313,15 +296,14 @@ func TestOPDSConversion(t *testing.T) {
})
t.Run("Download_UnsupportedFormat", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
bookID := createTestMediaItemID(t, ts, token)
bookID := createTestMediaItemID(t, setup.Server, token)
// Request unsupported format
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID+"?format=pdf", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/download/"+bookID+"?format=pdf", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -337,13 +319,12 @@ 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)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/catalog", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/catalog", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -356,14 +337,13 @@ func TestOPDSEdgeCases(t *testing.T) {
})
t.Run("Search_SpecialCharacters", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
// Search with special characters
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/search?query=test%20%26%20more", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/search?query=test%20%26%20more", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -376,13 +356,12 @@ func TestOPDSEdgeCases(t *testing.T) {
})
t.Run("Search_EmptyQuery", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
deviceID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/opds/devices/"+deviceID.String()+"/search?query=", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/opds/devices/"+deviceID.String()+"/search?query=", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}