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
+22 -28
View File
@@ -14,7 +14,6 @@ import (
// TestBookMatchingQueryBooks tests the book query endpoint
func TestBookMatchingQueryBooks(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
t.Run("QueryBooks_WithoutAuth", func(t *testing.T) {
req := map[string]interface{}{
@@ -34,7 +33,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
})
t.Run("QueryBooks_WithAuth_ByTitle", func(t *testing.T) {
_ = createTestMediaItemID(t, setup.Server)
_ = createTestMediaItemID(t, setup)
req := map[string]interface{}{
"title": "Test Ebook",
@@ -43,7 +42,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -63,7 +62,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
// Send invalid JSON
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/books/query", bytes.NewBuffer([]byte("invalid json")))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -81,7 +80,7 @@ func TestBookMatchingQueryBooks(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/books/query", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -106,7 +105,6 @@ func TestBookMatchingQueryBooks(t *testing.T) {
// TestBookMatchingBulkLink tests bulk linking operations
func TestBookMatchingBulkLink(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
t.Run("BulkLinkBooks_WithoutAuth", func(t *testing.T) {
req := map[string]interface{}{
@@ -139,7 +137,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -157,7 +155,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
})
t.Run("BulkLinkBooks_InvalidUnlinkedBookID", func(t *testing.T) {
bookID := createTestMediaItemID(t, setup.Server)
bookID := createTestMediaItemID(t, setup)
req := map[string]interface{}{
"links": []map[string]interface{}{
@@ -172,7 +170,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -218,7 +216,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/bulk-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -239,7 +237,6 @@ func TestBookMatchingBulkLink(t *testing.T) {
// TestBookMatchingAutoLink tests automatic linking
func TestBookMatchingAutoLink(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
t.Run("AutoLinkBooks_WithoutAuth", func(t *testing.T) {
req := map[string]interface{}{
@@ -265,7 +262,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -290,7 +287,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -313,7 +310,7 @@ func TestBookMatchingAutoLink(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/sync/auto-link-books", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -333,7 +330,6 @@ func TestBookMatchingAutoLink(t *testing.T) {
// TestBookMatchingSuggestions tests getting suggestions for unlinked books
func TestBookMatchingSuggestions(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
t.Run("GetUnlinkedBookSuggestions_WithoutAuth", func(t *testing.T) {
testID := uuid.New()
@@ -349,7 +345,7 @@ func TestBookMatchingSuggestions(t *testing.T) {
t.Run("GetUnlinkedBookSuggestions_InvalidUUID", func(t *testing.T) {
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/sync/unlinked-books/invalid-uuid/suggestions", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -362,7 +358,7 @@ func TestBookMatchingSuggestions(t *testing.T) {
t.Run("GetUnlinkedBookSuggestions_BookNotFound", func(t *testing.T) {
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -377,7 +373,7 @@ func TestBookMatchingSuggestions(t *testing.T) {
// For now, test with a non-existent ID to check response structure
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/sync/unlinked-books/"+testID.String()+"/suggestions", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -392,7 +388,6 @@ func TestBookMatchingSuggestions(t *testing.T) {
// TestBookMatchingDeviceFileAliases tests device file alias operations
func TestBookMatchingDeviceFileAliases(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
t.Run("GetDeviceFileAliases_WithoutAuth", func(t *testing.T) {
testID := uuid.New()
@@ -409,7 +404,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
t.Run("GetDeviceFileAliases_WithAuth", func(t *testing.T) {
testID := uuid.New()
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/devices/"+testID.String()+"/file-aliases", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -462,7 +457,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/devices/invalid-uuid/file-aliases", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -485,7 +480,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/devices/"+deviceID.String()+"/file-aliases", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -505,7 +500,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
httpReq, _ := http.NewRequest("PUT", setup.Server.URL+"/api/devices/"+deviceID.String()+"/file-aliases/invalid-uuid", bytes.NewBuffer(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -519,7 +514,7 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
deviceID := uuid.New()
httpReq, _ := http.NewRequest("DELETE", setup.Server.URL+"/api/devices/"+deviceID.String()+"/file-aliases/invalid-uuid", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -533,7 +528,6 @@ func TestBookMatchingDeviceFileAliases(t *testing.T) {
// TestBookMatchingGetBookMatches tests the book matches endpoint
func TestBookMatchingGetBookMatches(t *testing.T) {
setup := setupTestServer(t)
token := loginTestUser(t, setup.Server, setup.DB)
t.Run("GetBookMatches_WithoutAuth", func(t *testing.T) {
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?title=Test", nil)
@@ -548,7 +542,7 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
t.Run("GetBookMatches_WithAuth_ByTitle", func(t *testing.T) {
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?title=Test", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -566,7 +560,7 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
t.Run("GetBookMatches_InvalidFileSize", func(t *testing.T) {
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?title=Test&file_size=invalid", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)
@@ -578,7 +572,7 @@ func TestBookMatchingGetBookMatches(t *testing.T) {
t.Run("GetBookMatches_MultipleIdentifiers", func(t *testing.T) {
httpReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/books/match?identifier=id1&identifier=id2&title=Test", nil)
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(httpReq)