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
+76 -103
View File
@@ -14,15 +14,14 @@ import (
// TestBookMatchingQueryBooks tests the book query endpoint
func TestBookMatchingQueryBooks(t *testing.T) {
t.Run("QueryBooks_WithoutAuth", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
req := map[string]interface{}{
"title": "Test Book",
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
@@ -34,18 +33,17 @@ func TestBookMatchingQueryBooks(t *testing.T) {
})
t.Run("QueryBooks_WithAuth_ByTitle", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
_ = createTestMediaItemID(t, ts, token)
token := loginTestUser(t, setup.Server, setup.DB)
_ = createTestMediaItemID(t, setup.Server, token)
req := map[string]interface{}{
"title": "Test Ebook",
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -64,13 +62,12 @@ func TestBookMatchingQueryBooks(t *testing.T) {
})
t.Run("QueryBooks_InvalidRequestBody", 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)
// Send invalid JSON
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/books/query", bytes.NewBuffer([]byte("invalid json")))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/books/query", bytes.NewBuffer([]byte("invalid json")))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -83,17 +80,16 @@ func TestBookMatchingQueryBooks(t *testing.T) {
})
t.Run("QueryBooks_NoResults", 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)
req := map[string]interface{}{
"title": "NonExistentBookTitleThatDoesNotExist123456789",
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -120,8 +116,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
// TestBookMatchingBulkLink tests bulk linking operations
func TestBookMatchingBulkLink(t *testing.T) {
t.Run("BulkLinkBooks_WithoutAuth", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
req := map[string]interface{}{
"links": []map[string]interface{}{
@@ -134,7 +129,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
@@ -146,17 +141,16 @@ func TestBookMatchingBulkLink(t *testing.T) {
})
t.Run("BulkLinkBooks_WithAuth_EmptyLinks", 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)
req := map[string]interface{}{
"links": []map[string]interface{}{},
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -176,11 +170,10 @@ func TestBookMatchingBulkLink(t *testing.T) {
})
t.Run("BulkLinkBooks_InvalidUnlinkedBookID", func(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
token := loginTestUser(t, ts, db)
bookID := createTestMediaItemID(t, ts, token)
token := loginTestUser(t, setup.Server, setup.DB)
bookID := createTestMediaItemID(t, setup.Server, token)
req := map[string]interface{}{
"links": []map[string]interface{}{
@@ -193,7 +186,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -218,10 +211,9 @@ func TestBookMatchingBulkLink(t *testing.T) {
})
t.Run("BulkLinkBooks_MultipleLinks", 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)
req := map[string]interface{}{
"links": []map[string]interface{}{
@@ -244,7 +236,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -267,8 +259,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
// TestBookMatchingAutoLink tests automatic linking
func TestBookMatchingAutoLink(t *testing.T) {
t.Run("AutoLinkBooks_WithoutAuth", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
req := map[string]interface{}{
"confidence_threshold": 0.8,
@@ -276,7 +267,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
@@ -288,15 +279,14 @@ func TestBookMatchingAutoLink(t *testing.T) {
})
t.Run("AutoLinkBooks_WithAuth_DefaultThreshold", 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)
req := map[string]interface{}{}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -315,10 +305,9 @@ func TestBookMatchingAutoLink(t *testing.T) {
})
t.Run("AutoLinkBooks_CustomThreshold", 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)
req := map[string]interface{}{
"confidence_threshold": 0.95,
@@ -326,7 +315,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -344,17 +333,16 @@ func TestBookMatchingAutoLink(t *testing.T) {
})
t.Run("AutoLinkBooks_NoUnlinkedBooks", 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)
req := map[string]interface{}{
"limit": 5,
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -376,11 +364,10 @@ func TestBookMatchingAutoLink(t *testing.T) {
// TestBookMatchingSuggestions tests getting suggestions for unlinked books
func TestBookMatchingSuggestions(t *testing.T) {
t.Run("GetUnlinkedBookSuggestions_WithoutAuth", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -391,12 +378,11 @@ func TestBookMatchingSuggestions(t *testing.T) {
})
t.Run("GetUnlinkedBookSuggestions_InvalidUUID", 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)
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/sync/unlinked-books/invalid-uuid/suggestions", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/sync/unlinked-books/invalid-uuid/suggestions", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -408,13 +394,12 @@ func TestBookMatchingSuggestions(t *testing.T) {
})
t.Run("GetUnlinkedBookSuggestions_BookNotFound", 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)
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -426,15 +411,14 @@ func TestBookMatchingSuggestions(t *testing.T) {
})
t.Run("GetUnlinkedBookSuggestions_ResponseStructure", 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)
// Create a test device and unlinked book would go here
// For now, test with a non-existent ID to check response structure
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -450,11 +434,10 @@ func TestBookMatchingSuggestions(t *testing.T) {
// TestBookMatchingDeviceFileAliases tests device file alias operations
func TestBookMatchingDeviceFileAliases(t *testing.T) {
t.Run("GetDeviceFileAliases_WithoutAuth", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/devices/"+testID.String()+"/file-aliases", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/devices/"+testID.String()+"/file-aliases", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -465,13 +448,12 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("GetDeviceFileAliases_WithAuth", 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)
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/devices/"+testID.String()+"/file-aliases", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/devices/"+testID.String()+"/file-aliases", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -490,8 +472,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("CreateDeviceFileAlias_WithoutAuth", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
deviceID := uuid.New()
mediaItemID := uuid.New()
@@ -504,7 +485,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/devices/"+deviceID.String()+"/file-aliases", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/devices/"+deviceID.String()+"/file-aliases", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
@@ -516,10 +497,9 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("CreateDeviceFileAlias_InvalidDeviceID", 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)
mediaItemID := uuid.New()
@@ -531,7 +511,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/devices/invalid-uuid/file-aliases", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/devices/invalid-uuid/file-aliases", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -544,10 +524,9 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("CreateDeviceFileAlias_InvalidMediaItemID", 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()
@@ -559,7 +538,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/devices/"+deviceID.String()+"/file-aliases", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/devices/"+deviceID.String()+"/file-aliases", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -572,10 +551,9 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("UpdateDeviceFileAlias_InvalidAliasID", 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()
@@ -584,7 +562,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
}
body, _ := json.Marshal(req)
httpReq, _ := http.NewRequest("PUT", ts.URL+"/api/devices/"+deviceID.String()+"/file-aliases/invalid-uuid", bytes.NewBuffer(body))
httpReq, _ := http.NewRequest("PUT", setup.Server.URL+"/api/devices/"+deviceID.String()+"/file-aliases/invalid-uuid", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
@@ -597,14 +575,13 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
})
t.Run("DeleteDeviceFileAlias_InvalidAliasID", 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("DELETE", ts.URL+"/api/devices/"+deviceID.String()+"/file-aliases/invalid-uuid", nil)
httpReq, _ := http.NewRequest("DELETE", setup.Server.URL+"/api/devices/"+deviceID.String()+"/file-aliases/invalid-uuid", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -619,10 +596,9 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
// TestBookMatchingGetBookMatches tests the book matches endpoint
func TestBookMatchingGetBookMatches(t *testing.T) {
t.Run("GetBookMatches_WithoutAuth", func(t *testing.T) {
ts, _, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/books/match?title=Test", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?title=Test", nil)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -633,12 +609,11 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
})
t.Run("GetBookMatches_WithAuth_ByTitle", 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)
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/books/match?title=Test", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?title=Test", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -656,12 +631,11 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
})
t.Run("GetBookMatches_InvalidFileSize", 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)
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/books/match?title=Test&file_size=invalid", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?title=Test&file_size=invalid", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -673,12 +647,11 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
})
t.Run("GetBookMatches_MultipleIdentifiers", 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)
httpReq, _ := http.NewRequest("GET", ts.URL+"/api/books/match?identifier=id1&identifier=id2&title=Test", nil)
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?identifier=id1&identifier=id2&title=Test", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}