test: improve test isolation and setup management

Add Token and RegularToken fields to TestServerSetup for pre-authenticated
access. Update setupTestServer to create fresh users with valid tokens at
initialization time. Simplify createTestMediaItemID to use setup.Token.
Remove loginTestUser, loginRegularUser, loginAdminUser functions in favor
of setup.Token/setup.RegularToken. Update createTestUserOnce and
getTestUserID/getRegularUserID to be idempotent. Update all test files to
use setup.Token instead of calling login helpers.
This commit is contained in:
2026-02-22 01:57:22 -05:00
parent 9a09161f24
commit f54508e4dd
16 changed files with 819 additions and 793 deletions
+3 -9
View File
@@ -237,10 +237,8 @@ func TestDeviceAuthentication(t *testing.T) {
func TestListPendingRegistrations(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
req := httptest.NewRequest("GET", "/api/devices/pending", nil)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Authorization", "Bearer "+setup.Token)
rec := httptest.NewRecorder()
setup.Server.Config.Handler.ServeHTTP(rec, req)
@@ -257,8 +255,6 @@ func TestListPendingRegistrations(t *testing.T) {
func TestApproveDeviceRegistration(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
regRequest := map[string]interface{}{
"device_name": "Test Device for Approval",
"device_type": "koreader",
@@ -280,7 +276,7 @@ func TestApproveDeviceRegistration(t *testing.T) {
assert.True(t, ok, "Should have registration_id")
req = httptest.NewRequest("GET", fmt.Sprintf("/api/devices/approve/%s", registrationID), nil)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Authorization", "Bearer "+setup.Token)
rec = httptest.NewRecorder()
setup.Server.Config.Handler.ServeHTTP(rec, req)
@@ -295,8 +291,6 @@ func TestApproveDeviceRegistration(t *testing.T) {
func TestRejectDeviceRegistration(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
regRequest := map[string]interface{}{
"device_name": "Test Device for Rejection",
"device_type": "koreader",
@@ -318,7 +312,7 @@ func TestRejectDeviceRegistration(t *testing.T) {
assert.True(t, ok, "Should have registration_id")
req = httptest.NewRequest("POST", fmt.Sprintf("/api/devices/reject/%s", registrationID), nil)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Authorization", "Bearer "+setup.Token)
rec = httptest.NewRecorder()
setup.Server.Config.Handler.ServeHTTP(rec, req)