diff --git a/cmd/server/tests/device_cap_test.go b/cmd/server/tests/device_cap_test.go index 921c1c0..92a846f 100644 --- a/cmd/server/tests/device_cap_test.go +++ b/cmd/server/tests/device_cap_test.go @@ -21,10 +21,10 @@ func TestUpdateUserMaxDevices(t *testing.T) { // Create test user with admin role loginTestUser(t, setup.Server, setup.DB) adminUserID := getTestUserID(t, setup.DB) - adminToken := getAdminToken(t, ts, adminUserID) + adminToken := getAdminToken(t, setup.Server, adminUserID) // Create a test user - userID := createTestUserForMaxDevices(t, ts, adminToken) + userID := createTestUserForMaxDevices(t, setup.Server, adminToken) tests := []struct { name string @@ -65,7 +65,7 @@ func TestUpdateUserMaxDevices(t *testing.T) { } jsonData, _ := json.Marshal(payload) - req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+adminToken) @@ -90,12 +90,12 @@ func TestUpdateUserMaxDevicesValidation(t *testing.T) { // Create admin user and get token adminToken := loginTestUser(t, setup.Server, setup.DB) - createAdminUser(t, ts, adminToken) + createAdminUser(t, setup.Server, adminToken) adminUserID := getTestUserID(t, setup.DB) - adminToken = getAdminToken(t, ts, adminUserID) + adminToken = getAdminToken(t, setup.Server, adminUserID) // Create test user - userID := createTestUserForMaxDevices(t, ts, adminToken) + userID := createTestUserForMaxDevices(t, setup.Server, adminToken) tests := []struct { name string @@ -131,7 +131,7 @@ func TestUpdateUserMaxDevicesValidation(t *testing.T) { } jsonData, _ := json.Marshal(payload) - req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+adminToken) @@ -151,12 +151,12 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) { // Create admin user adminToken := loginTestUser(t, setup.Server, setup.DB) - createAdminUser(t, ts, adminToken) + createAdminUser(t, setup.Server, adminToken) adminUserID := getTestUserID(t, setup.DB) - adminToken = getAdminToken(t, ts, adminUserID) + adminToken = getAdminToken(t, setup.Server, adminUserID) // Create regular user - userID := createTestUserForMaxDevices(t, ts, adminToken) + userID := createTestUserForMaxDevices(t, setup.Server, adminToken) t.Run("No authorization", func(t *testing.T) { payload := map[string]interface{}{ @@ -164,7 +164,7 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) { } jsonData, _ := json.Marshal(payload) - req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} @@ -177,9 +177,9 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) { t.Run("Non-admin user", func(t *testing.T) { // Create another regular user and get their token - _ = createTestUserForMaxDevices(t, ts, adminToken) + _ = createTestUserForMaxDevices(t, setup.Server, adminToken) // Login as the maxdevices user (who is a regular user, not admin) - regularToken := loginTestUserByCredentials(t, ts, "maxdevices@example.com", "Test@Pass123!") + regularToken := loginTestUserByCredentials(t, setup.Server, "maxdevices@example.com", "Test@Pass123!") require.NotEmpty(t, regularToken, "Failed to login as regular user") payload := map[string]interface{}{ @@ -187,7 +187,7 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) { } jsonData, _ := json.Marshal(payload) - req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+regularToken) @@ -206,9 +206,9 @@ func TestUpdateUserMaxDevicesNonExistentUser(t *testing.T) { // Create admin user adminToken := loginTestUser(t, setup.Server, setup.DB) - createAdminUser(t, ts, adminToken) + createAdminUser(t, setup.Server, adminToken) adminUserID := getTestUserID(t, setup.DB) - adminToken = getAdminToken(t, ts, adminUserID) + adminToken = getAdminToken(t, setup.Server, adminUserID) // Use a non-existent user ID nonExistentUserID := uuid.New() @@ -218,7 +218,7 @@ func TestUpdateUserMaxDevicesNonExistentUser(t *testing.T) { } jsonData, _ := json.Marshal(payload) - req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+nonExistentUserID.String()+"/max-devices", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+nonExistentUserID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+adminToken) @@ -237,9 +237,9 @@ func TestUpdateUserMaxDevicesMissingUserID(t *testing.T) { // Create admin user adminToken := loginTestUser(t, setup.Server, setup.DB) - createAdminUser(t, ts, adminToken) + createAdminUser(t, setup.Server, adminToken) adminUserID := getTestUserID(t, setup.DB) - adminToken = getAdminToken(t, ts, adminUserID) + adminToken = getAdminToken(t, setup.Server, adminUserID) payload := map[string]interface{}{ "max_devices": 10, @@ -247,7 +247,7 @@ func TestUpdateUserMaxDevicesMissingUserID(t *testing.T) { jsonData, _ := json.Marshal(payload) // Missing user ID in URL - req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users//max-devices", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users//max-devices", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+adminToken) @@ -265,11 +265,11 @@ func TestListUsersIncludesMaxDevices(t *testing.T) { // Create admin user adminToken := loginTestUser(t, setup.Server, setup.DB) - createAdminUser(t, ts, adminToken) + createAdminUser(t, setup.Server, adminToken) adminUserID := getTestUserID(t, setup.DB) - adminToken = getAdminToken(t, ts, adminUserID) + adminToken = getAdminToken(t, setup.Server, adminUserID) - req, _ := http.NewRequest("GET", setup.Server.URL+"/api/auth/users", nil) + req, _ := http.NewRequest("GET", ts.URL+"/api/auth/users", nil) req.Header.Set("Authorization", "Bearer "+adminToken) client := &http.Client{} @@ -304,7 +304,7 @@ func createAdminUser(t *testing.T, ts *httptest.Server, token string) { } jsonData, _ := json.Marshal(createUserPayload) - req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/register", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("POST", ts.URL+"/api/auth/register", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+token) @@ -324,7 +324,7 @@ func createTestUserForMaxDevices(t *testing.T, ts *httptest.Server, adminToken s } jsonData, _ := json.Marshal(createUserPayload) - req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/register", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("POST", ts.URL+"/api/auth/register", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+adminToken) @@ -342,7 +342,7 @@ func createTestUserForMaxDevices(t *testing.T, ts *httptest.Server, adminToken s "password": "Test@Pass123!", } loginData, _ := json.Marshal(loginPayload) - loginReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(loginData)) + loginReq, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(loginData)) loginReq.Header.Set("Content-Type", "application/json") loginResp, err := client.Do(loginReq) @@ -420,7 +420,7 @@ func getAdminToken(t *testing.T, ts *httptest.Server, userID uuid.UUID) string { } jsonData, _ := json.Marshal(loginPayload) - req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} @@ -448,7 +448,7 @@ func loginTestUserByCredentials(t *testing.T, ts *httptest.Server, email, passwo } jsonData, _ := json.Marshal(loginPayload) - req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(jsonData)) + req, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} diff --git a/cmd/server/tests/device_test.go b/cmd/server/tests/device_test.go index f90e941..0aa82ed 100644 --- a/cmd/server/tests/device_test.go +++ b/cmd/server/tests/device_test.go @@ -29,7 +29,7 @@ func TestDeviceRegistrationFlow(t *testing.T) { req := httptest.NewRequest("POST", "/api/devices/register", bytes.NewReader(regBody)) req.Header.Set("Content-Type", "application/json") rec := httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusCreated, rec.Code, "Should initiate device registration") @@ -53,7 +53,7 @@ func TestDeviceRegistrationFlow(t *testing.T) { req = httptest.NewRequest("POST", "/api/devices/register/status", bytes.NewReader(statusBody)) req.Header.Set("Content-Type", "application/json") rec = httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusOK, rec.Code, "Should check registration status") @@ -74,7 +74,7 @@ func TestDeviceRegistrationFlow(t *testing.T) { req = httptest.NewRequest("POST", "/api/auth/login", bytes.NewReader(loginBody)) req.Header.Set("Content-Type", "application/json") rec = httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusOK, rec.Code, "Should login successfully") @@ -89,7 +89,7 @@ func TestDeviceRegistrationFlow(t *testing.T) { req.Header.Set("Authorization", "Bearer "+token) req.Header.Set("Content-Type", "application/json") rec = httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusOK, rec.Code, "Should approve device") @@ -97,7 +97,7 @@ func TestDeviceRegistrationFlow(t *testing.T) { req = httptest.NewRequest("POST", "/api/devices/register/status", bytes.NewReader(statusBody)) req.Header.Set("Content-Type", "application/json") rec = httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusOK, rec.Code, "Should check registration status after approval") @@ -203,7 +203,7 @@ func TestDeviceAuthentication(t *testing.T) { userID := getTestUserID(t, setup.DB) deviceToken := fmt.Sprintf("dev_%s", uuid.New().String()) - _, err := db.CreateDevice(context.Background(), database.CreateDeviceParams{ + _, err := setup.DB.CreateDevice(context.Background(), database.CreateDeviceParams{ UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true}, DeviceName: "Test Device", DeviceType: "koreader", @@ -220,7 +220,7 @@ func TestDeviceAuthentication(t *testing.T) { req := httptest.NewRequest("GET", "/api/devices", nil) req.Header.Set("Authorization", "Bearer "+deviceToken) rec := httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) // This should fail because device auth middleware is not applied to /api/devices // Device auth is for sync endpoints only @@ -235,7 +235,7 @@ func TestListPendingRegistrations(t *testing.T) { req := httptest.NewRequest("GET", "/api/devices/pending", nil) req.Header.Set("Authorization", "Bearer "+token) rec := httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusOK, rec.Code, "Should list pending registrations") @@ -262,7 +262,7 @@ func TestApproveDeviceRegistration(t *testing.T) { req := httptest.NewRequest("POST", "/api/devices/register", bytes.NewReader(regBody)) req.Header.Set("Content-Type", "application/json") rec := httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusCreated, rec.Code, "Should initiate device registration") @@ -275,7 +275,7 @@ func TestApproveDeviceRegistration(t *testing.T) { req = httptest.NewRequest("GET", fmt.Sprintf("/api/devices/approve/%s", registrationID), nil) req.Header.Set("Authorization", "Bearer "+token) rec = httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusOK, rec.Code, "Should approve device registration") @@ -300,7 +300,7 @@ func TestRejectDeviceRegistration(t *testing.T) { req := httptest.NewRequest("POST", "/api/devices/register", bytes.NewReader(regBody)) req.Header.Set("Content-Type", "application/json") rec := httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusCreated, rec.Code, "Should initiate device registration") @@ -313,7 +313,7 @@ func TestRejectDeviceRegistration(t *testing.T) { req = httptest.NewRequest("POST", fmt.Sprintf("/api/devices/reject/%s", registrationID), nil) req.Header.Set("Authorization", "Bearer "+token) rec = httptest.NewRecorder() - ts.Config.Handler.ServeHTTP(rec, req) + setup.Server.Config.Handler.ServeHTTP(rec, req) assert.Equal(t, http.StatusOK, rec.Code, "Should reject device registration") diff --git a/cmd/server/tests/goroutine_leak_test.go b/cmd/server/tests/goroutine_leak_test.go index a4eff57..684812a 100644 --- a/cmd/server/tests/goroutine_leak_test.go +++ b/cmd/server/tests/goroutine_leak_test.go @@ -18,7 +18,8 @@ func TestGoroutineCleanup(t *testing.T) { t.Logf("Initial goroutine count: %d", initialGoroutines) // Start server - setup := setupTestServer(t) +setup := setupTestServer(t) + defer setup.Close() // Wait for startup time.Sleep(200 * time.Millisecond) @@ -26,7 +27,7 @@ func TestGoroutineCleanup(t *testing.T) { t.Logf("Goroutines while running: %d (delta: +%d)", runningGoroutines, runningGoroutines-initialGoroutines) // Close server - ts.Close() + setup.Close() // Wait for cleanup time.Sleep(500 * time.Millisecond) diff --git a/cmd/server/tests/kobo_test.go b/cmd/server/tests/kobo_test.go index 465de7b..3a86475 100644 --- a/cmd/server/tests/kobo_test.go +++ b/cmd/server/tests/kobo_test.go @@ -20,7 +20,6 @@ func TestKoboInitialization(t *testing.T) { } setup := setupTestServer(t) - defer closeTestServer(t, ts, db) token := loginTestUser(t, setup.Server, setup.DB) _ = getTestUserID(t, setup.DB) @@ -53,7 +52,6 @@ func TestKoboLibrarySync(t *testing.T) { } setup := setupTestServer(t) - defer closeTestServer(t, ts, db) token := loginTestUser(t, setup.Server, setup.DB) _ = createTestMediaItemID(t, setup.Server, token) @@ -84,7 +82,6 @@ func TestKoboMarkupSync(t *testing.T) { } setup := setupTestServer(t) - defer closeTestServer(t, ts, db) token := loginTestUser(t, setup.Server, setup.DB) mediaItemID := createTestMediaItemID(t, setup.Server, token) @@ -154,7 +151,6 @@ func TestKoboBookmarkSync(t *testing.T) { } setup := setupTestServer(t) - defer closeTestServer(t, ts, db) token := loginTestUser(t, setup.Server, setup.DB) mediaItemID := createTestMediaItemID(t, setup.Server, token) @@ -210,7 +206,6 @@ func TestKoboAnalyticsGettests(t *testing.T) { } setup := setupTestServer(t) - defer closeTestServer(t, ts, db) token := loginTestUser(t, setup.Server, setup.DB) mediaItemID := createTestMediaItemID(t, setup.Server, token)