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
+25 -30
View File
@@ -23,7 +23,7 @@ func createTestLibrary(t *testing.T, ts *httptest.Server, token, name string) st
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/libraries", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/libraries", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -42,11 +42,10 @@ func createTestLibrary(t *testing.T, ts *httptest.Server, token, name string) st
// TestMediaItemISBNNormalization tests ISBN normalization with media-items endpoint
func TestMediaItemISBNNormalization(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupTestServer(t)
// Create an ebook library first
token := loginTestUser(t, ts, db)
token := loginTestUser(t, setup.Server, setup.DB)
libID := createTestLibrary(t, ts, token, "test-isbn-lib")
// Test ISBN normalization cases
@@ -109,7 +108,7 @@ func TestMediaItemISBNNormalization(t *testing.T) {
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -131,10 +130,9 @@ func TestMediaItemISBNNormalization(t *testing.T) {
// TestMediaItemISBNEdgeCases tests ISBN edge cases
func TestMediaItemISBNEdgeCases(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)
libID := createTestLibrary(t, ts, token, "test-isbn-edge-lib")
t.Run("Empty ISBN should be accepted", func(t *testing.T) {
@@ -147,7 +145,7 @@ func TestMediaItemISBNEdgeCases(t *testing.T) {
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -170,7 +168,7 @@ func TestMediaItemISBNEdgeCases(t *testing.T) {
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -197,7 +195,7 @@ func TestMediaItemISBNEdgeCases(t *testing.T) {
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -216,10 +214,9 @@ func TestMediaItemISBNEdgeCases(t *testing.T) {
// TestMediaItemsPagination tests pagination with media-items endpoint
func TestMediaItemsPagination(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)
libID := createTestLibrary(t, ts, token, "test-pagination-lib")
// Create some test media items
@@ -234,7 +231,7 @@ func TestMediaItemsPagination(t *testing.T) {
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -245,7 +242,7 @@ func TestMediaItemsPagination(t *testing.T) {
}
t.Run("Valid pagination parameters", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"/api/media-items?library_id="+libID+"&limit=2&offset=0", nil)
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/media-items?library_id="+libID+"&limit=2&offset=0", nil)
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -263,7 +260,7 @@ func TestMediaItemsPagination(t *testing.T) {
})
t.Run("Pagination with offset", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"/api/media-items?library_id="+libID+"&limit=2&offset=2", nil)
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/media-items?library_id="+libID+"&limit=2&offset=2", nil)
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -281,7 +278,7 @@ func TestMediaItemsPagination(t *testing.T) {
})
t.Run("Negative limit should fail", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"/api/media-items?library_id="+libID+"&limit=-10&offset=0", nil)
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/media-items?library_id="+libID+"&limit=-10&offset=0", nil)
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -294,7 +291,7 @@ func TestMediaItemsPagination(t *testing.T) {
})
t.Run("Negative offset should fail", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"/api/media-items?library_id="+libID+"&limit=10&offset=-5", nil)
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/media-items?library_id="+libID+"&limit=10&offset=-5", nil)
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -307,7 +304,7 @@ func TestMediaItemsPagination(t *testing.T) {
})
t.Run("Limit exceeds maximum", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"/api/media-items?library_id="+libID+"&limit=10000&offset=0", nil)
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/media-items?library_id="+libID+"&limit=10000&offset=0", nil)
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
@@ -323,10 +320,9 @@ func TestMediaItemsPagination(t *testing.T) {
// TestMediaItemLibraryRequirement tests that media items require a library
func TestMediaItemLibraryRequirement(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)
t.Run("Create media-item without library should fail gracefully", func(t *testing.T) {
payload := map[string]interface{}{
@@ -338,7 +334,7 @@ func TestMediaItemLibraryRequirement(t *testing.T) {
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -364,7 +360,7 @@ func TestMediaItemLibraryRequirement(t *testing.T) {
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -386,10 +382,9 @@ func TestMediaItemLibraryRequirement(t *testing.T) {
// TestUpdateMediaItemISBN tests updating media-item ISBN
func TestUpdateMediaItemISBN(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)
libID := createTestLibrary(t, ts, token, "test-update-lib")
// First create a media item
@@ -403,7 +398,7 @@ func TestUpdateMediaItemISBN(t *testing.T) {
}
body, _ := json.Marshal(createPayload)
req, _ := http.NewRequest("POST", ts.URL+"/api/media-items", bytes.NewBuffer(body))
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
@@ -425,7 +420,7 @@ func TestUpdateMediaItemISBN(t *testing.T) {
}
body, _ := json.Marshal(updatePayload)
req, _ := http.NewRequest("PUT", ts.URL+"/api/media-items/"+mediaItemID, bytes.NewBuffer(body))
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/media-items/"+mediaItemID, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)