refactor(tests): Update all test files to use TestServerSetup pattern
This commit is contained in:
@@ -14,10 +14,9 @@ import (
|
||||
// TestAnalyticsReadingStats tests the reading statistics endpoint
|
||||
func TestAnalyticsReadingStats(t *testing.T) {
|
||||
t.Run("GetReadingStats_WithoutAuth", func(t *testing.T) {
|
||||
ts, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
setup := setupTestServer(t)
|
||||
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats", nil)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
@@ -27,12 +26,33 @@ func TestAnalyticsReadingStats(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetReadingStats_WithAuth_DefaultDates", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
assert.Contains(t, result, "total_books")
|
||||
assert.Contains(t, result, "total_reading_time")
|
||||
assert.Contains(t, result, "completion_rate")
|
||||
assert.Contains(t, result, "daily_reading_minutes")
|
||||
})
|
||||
|
||||
t.Run("GetReadingStats_WithAuth_DefaultDates", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -53,15 +73,14 @@ func TestAnalyticsReadingStats(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetReadingStats_WithCustomDateRange", 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)
|
||||
|
||||
startDate := time.Now().AddDate(0, -2, 0).Format("2006-01-02")
|
||||
endDate := time.Now().Format("2006-01-02")
|
||||
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats?start_date="+startDate+"&end_date="+endDate, nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats?start_date="+startDate+"&end_date="+endDate, nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -72,12 +91,11 @@ func TestAnalyticsReadingStats(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetReadingStats_InvalidStartDate", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats?start_date=invalid-date", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats?start_date=invalid-date", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -88,12 +106,11 @@ func TestAnalyticsReadingStats(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetReadingStats_InvalidEndDate", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats?end_date=not-a-date", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats?end_date=not-a-date", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -104,12 +121,11 @@ func TestAnalyticsReadingStats(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetReadingStats_EmptyHistory", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -130,10 +146,9 @@ func TestAnalyticsReadingStats(t *testing.T) {
|
||||
// TestAnalyticsDeviceUsage tests the device usage endpoint
|
||||
func TestAnalyticsDeviceUsage(t *testing.T) {
|
||||
t.Run("GetDeviceUsage_WithoutAuth", func(t *testing.T) {
|
||||
ts, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
setup := setupTestServer(t)
|
||||
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/device-usage", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/device-usage", nil)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
@@ -143,12 +158,11 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetDeviceUsage_WithAuth_NoDevices", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/device-usage", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/device-usage", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -166,10 +180,9 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetDeviceUsage_WithAuth_WithDevices", 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)
|
||||
|
||||
// First create a device
|
||||
deviceReq := map[string]interface{}{
|
||||
@@ -178,7 +191,7 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
|
||||
}
|
||||
deviceBody, _ := json.Marshal(deviceReq)
|
||||
|
||||
deviceReqHTTP, _ := http.NewRequest("POST", ts.URL+"/api/devices/register", bytes.NewBuffer(deviceBody))
|
||||
deviceReqHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/devices/register", bytes.NewBuffer(deviceBody))
|
||||
deviceReqHTTP.Header.Set("Content-Type", "application/json")
|
||||
deviceReqHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -188,7 +201,7 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
|
||||
resp.Body.Close()
|
||||
|
||||
// Now get device usage
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/device-usage", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/device-usage", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err = client.Do(req)
|
||||
require.NoError(t, err)
|
||||
@@ -206,12 +219,11 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetDeviceUsage_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)
|
||||
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/device-usage", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/device-usage", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -242,10 +254,9 @@ func TestAnalyticsDeviceUsage(t *testing.T) {
|
||||
// TestAnalyticsPopularBooks tests the popular books endpoint
|
||||
func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
t.Run("GetPopularBooks_WithoutAuth", func(t *testing.T) {
|
||||
ts, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
setup := setupTestServer(t)
|
||||
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books", nil)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
@@ -255,12 +266,11 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetPopularBooks_WithAuth_DefaultLimit", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -280,12 +290,11 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetPopularBooks_WithCustomLimit", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books?limit=5", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books?limit=5", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -302,12 +311,11 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetPopularBooks_InvalidLimit", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books?limit=invalid", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books?limit=invalid", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -325,13 +333,12 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetPopularBooks_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)
|
||||
|
||||
// First create a book and some reading history
|
||||
bookID := createTestMediaItemID(t, ts, token)
|
||||
bookID := createTestMediaItemID(t, setup.Server, token)
|
||||
|
||||
// Create reading history for the book
|
||||
historyReq := map[string]interface{}{
|
||||
@@ -342,7 +349,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
}
|
||||
historyBody, _ := json.Marshal(historyReq)
|
||||
|
||||
historyHTTP, _ := http.NewRequest("POST", ts.URL+"/api/media-items/"+bookID+"/progress", bytes.NewBuffer(historyBody))
|
||||
historyHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items/"+bookID+"/progress", bytes.NewBuffer(historyBody))
|
||||
historyHTTP.Header.Set("Content-Type", "application/json")
|
||||
historyHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -352,7 +359,7 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
resp.Body.Close()
|
||||
|
||||
// Now get popular books
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err = client.Do(req)
|
||||
require.NoError(t, err)
|
||||
@@ -378,12 +385,11 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetPopularBooks_NoReadingHistory", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -404,15 +410,14 @@ func TestAnalyticsPopularBooks(t *testing.T) {
|
||||
// TestAnalyticsEdgeCases tests edge cases for analytics endpoints
|
||||
func TestAnalyticsEdgeCases(t *testing.T) {
|
||||
t.Run("ReadingStats_FutureDateRange", 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)
|
||||
|
||||
startDate := time.Now().AddDate(0, 0, 7).Format("2006-01-02")
|
||||
endDate := time.Now().AddDate(0, 0, 14).Format("2006-01-02")
|
||||
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/analytics/reading-stats?start_date="+startDate+"&end_date="+endDate, nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/reading-stats?start_date="+startDate+"&end_date="+endDate, nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -429,12 +434,11 @@ func TestAnalyticsEdgeCases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("PopularBooks_LimitZero", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books?limit=0", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books?limit=0", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -452,12 +456,11 @@ func TestAnalyticsEdgeCases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("PopularBooks_VeryLargeLimit", 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, _ := http.NewRequest("GET", ts.URL+"/api/analytics/popular-books?limit=999999", nil)
|
||||
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/analytics/popular-books?limit=999999", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
|
||||
Reference in New Issue
Block a user