refactor(tests): Update all test files to use TestServerSetup pattern
This commit is contained in:
@@ -14,8 +14,7 @@ import (
|
||||
// TestCollectionsBulkOperations tests bulk collection operations
|
||||
func TestCollectionsBulkOperations(t *testing.T) {
|
||||
t.Run("BulkAddBooks_WithoutAuth", func(t *testing.T) {
|
||||
ts, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
setup := setupTestServer(t)
|
||||
|
||||
req := map[string]interface{}{
|
||||
"operations": []map[string]interface{}{
|
||||
@@ -27,7 +26,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(body))
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
@@ -39,17 +38,16 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_EmptyOperations", 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{}{
|
||||
"operations": []map[string]interface{}{},
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(body))
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -62,11 +60,10 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_InvalidCollectionID", 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{}{
|
||||
"operations": []map[string]interface{}{
|
||||
@@ -78,7 +75,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
httpReq, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(body))
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -105,10 +102,9 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_InvalidBookID", 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 collection first
|
||||
collectionReq := map[string]interface{}{
|
||||
@@ -117,7 +113,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
collectionBody, _ := json.Marshal(collectionReq)
|
||||
|
||||
collectionHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -141,7 +137,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
addBody, _ := json.Marshal(addReq)
|
||||
|
||||
addHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP.Header.Set("Content-Type", "application/json")
|
||||
addHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -160,10 +156,9 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_SingleOperation", 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 collection
|
||||
collectionReq := map[string]interface{}{
|
||||
@@ -172,7 +167,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
collectionBody, _ := json.Marshal(collectionReq)
|
||||
|
||||
collectionHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -186,7 +181,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionID := collectionResult["id"].(string)
|
||||
|
||||
// Create a book
|
||||
bookID := createTestMediaItemID(t, ts, token)
|
||||
bookID := createTestMediaItemID(t, setup.Server, token)
|
||||
|
||||
// Add book to collection
|
||||
addReq := map[string]interface{}{
|
||||
@@ -199,7 +194,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
addBody, _ := json.Marshal(addReq)
|
||||
|
||||
addHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP.Header.Set("Content-Type", "application/json")
|
||||
addHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -223,10 +218,9 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_MultipleBooksSingleCollection", 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 collection
|
||||
collectionReq := map[string]interface{}{
|
||||
@@ -235,7 +229,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
collectionBody, _ := json.Marshal(collectionReq)
|
||||
|
||||
collectionHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -249,9 +243,9 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionID := collectionResult["id"].(string)
|
||||
|
||||
// Create multiple books
|
||||
bookID1 := createTestMediaItemID(t, ts, token)
|
||||
bookID2 := createTestMediaItemID(t, ts, token)
|
||||
bookID3 := createTestMediaItemID(t, ts, token)
|
||||
bookID1 := createTestMediaItemID(t, setup.Server, token)
|
||||
bookID2 := createTestMediaItemID(t, setup.Server, token)
|
||||
bookID3 := createTestMediaItemID(t, setup.Server, token)
|
||||
|
||||
// Add all books to collection
|
||||
addReq := map[string]interface{}{
|
||||
@@ -264,7 +258,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
addBody, _ := json.Marshal(addReq)
|
||||
|
||||
addHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP.Header.Set("Content-Type", "application/json")
|
||||
addHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -282,10 +276,9 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_MultipleCollections", 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 multiple collections
|
||||
collectionReq := map[string]interface{}{
|
||||
@@ -294,7 +287,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
collectionBody, _ := json.Marshal(collectionReq)
|
||||
|
||||
collectionHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -313,7 +306,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
collectionBody2, _ := json.Marshal(collectionReq2)
|
||||
|
||||
collectionHTTP2, _ := http.NewRequest("POST", ts.URL+"/api/collections", bytes.NewBuffer(collectionBody2))
|
||||
collectionHTTP2, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections", bytes.NewBuffer(collectionBody2))
|
||||
collectionHTTP2.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP2.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -326,8 +319,8 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionID2 := collectionResult2["id"].(string)
|
||||
|
||||
// Create books
|
||||
bookID1 := createTestMediaItemID(t, ts, token)
|
||||
bookID2 := createTestMediaItemID(t, ts, token)
|
||||
bookID1 := createTestMediaItemID(t, setup.Server, token)
|
||||
bookID2 := createTestMediaItemID(t, setup.Server, token)
|
||||
|
||||
// Add books to multiple collections
|
||||
addReq := map[string]interface{}{
|
||||
@@ -344,7 +337,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
addBody, _ := json.Marshal(addReq)
|
||||
|
||||
addHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP.Header.Set("Content-Type", "application/json")
|
||||
addHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -362,10 +355,9 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_DuplicateBooks", 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 collection
|
||||
collectionReq := map[string]interface{}{
|
||||
@@ -374,7 +366,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
collectionBody, _ := json.Marshal(collectionReq)
|
||||
|
||||
collectionHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections", bytes.NewBuffer(collectionBody))
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -388,7 +380,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionID := collectionResult["id"].(string)
|
||||
|
||||
// Create a book
|
||||
bookID := createTestMediaItemID(t, ts, token)
|
||||
bookID := createTestMediaItemID(t, setup.Server, token)
|
||||
|
||||
// Add book to collection
|
||||
addReq := map[string]interface{}{
|
||||
@@ -401,7 +393,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
}
|
||||
addBody, _ := json.Marshal(addReq)
|
||||
|
||||
addHTTP, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody))
|
||||
addHTTP.Header.Set("Content-Type", "application/json")
|
||||
addHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -411,7 +403,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
|
||||
// Try to add the same book again - create new request with fresh body
|
||||
addBody2, _ := json.Marshal(addReq)
|
||||
addHTTP2, _ := http.NewRequest("POST", ts.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody2))
|
||||
addHTTP2, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody2))
|
||||
addHTTP2.Header.Set("Content-Type", "application/json")
|
||||
addHTTP2.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -424,13 +416,12 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_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/collections/bulk-add-books", bytes.NewBuffer([]byte("invalid json")))
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer([]byte("invalid json")))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user