refactor(tests): enhance test infrastructure with library/collection helpers
- Add LibraryTestData struct to TestDeviceSetup - Implement CreateLibrary() for proper library creation in tests - Implement CreateCollection() for test collection support - Improve test isolation with dedicated library creation This provides a more robust foundation for integration tests that need proper library management support.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bookhoard/internal/handlers"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
@@ -40,11 +41,11 @@ func TestUserProfileEndpoints(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
profile := map[string]interface{}{
|
||||
"id": userID.String(),
|
||||
"email": "user@example.com",
|
||||
"username": "testuser",
|
||||
"role": "user",
|
||||
profile := handlers.UserProfile{
|
||||
ID: userID.String(),
|
||||
Email: "user@example.com",
|
||||
Username: "testuser",
|
||||
Role: "user",
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(profile)
|
||||
@@ -79,8 +80,8 @@ func TestUserProfileEndpoints(t *testing.T) {
|
||||
// TestUserUpdateEndpoints tests user field update endpoints
|
||||
func TestUserUpdateEndpoints(t *testing.T) {
|
||||
t.Run("PUT /api/auth/email - Update email to existing email", func(t *testing.T) {
|
||||
payload := map[string]interface{}{
|
||||
"email": "existing@example.com",
|
||||
payload := handlers.UpdateEmailRequest{
|
||||
Email: "existing@example.com",
|
||||
}
|
||||
jsonData, _ := json.Marshal(payload)
|
||||
|
||||
@@ -90,15 +91,14 @@ func TestUserUpdateEndpoints(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req map[string]interface{}
|
||||
var req handlers.UpdateEmailRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"error":"invalid request"}`))
|
||||
return
|
||||
}
|
||||
|
||||
email := req["email"].(string)
|
||||
if email == "existing@example.com" {
|
||||
if req.Email == "existing@example.com" {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
w.Write([]byte(`{"error":"email already taken"}`))
|
||||
return
|
||||
@@ -112,8 +112,8 @@ func TestUserUpdateEndpoints(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("PUT /api/auth/email - Update email with invalid format", func(t *testing.T) {
|
||||
payload := map[string]interface{}{
|
||||
"email": "invalid-email",
|
||||
payload := handlers.UpdateEmailRequest{
|
||||
Email: "invalid-email",
|
||||
}
|
||||
jsonData, _ := json.Marshal(payload)
|
||||
|
||||
@@ -123,14 +123,14 @@ func TestUserUpdateEndpoints(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req map[string]interface{}
|
||||
var req handlers.UpdateEmailRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"error":"invalid request"}`))
|
||||
return
|
||||
}
|
||||
|
||||
email, _ := req["email"].(string)
|
||||
email := req.Email
|
||||
if !contains(email, "@") || !contains(email, ".") {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"error":"email is invalid"}`))
|
||||
@@ -145,8 +145,8 @@ func TestUserUpdateEndpoints(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("PUT /api/auth/email - Update email with empty value", func(t *testing.T) {
|
||||
payload := map[string]interface{}{
|
||||
"email": "",
|
||||
payload := handlers.UpdateEmailRequest{
|
||||
Email: "",
|
||||
}
|
||||
jsonData, _ := json.Marshal(payload)
|
||||
|
||||
@@ -165,8 +165,8 @@ func TestUserUpdateEndpoints(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("PUT /api/auth/username - Update username to existing username", func(t *testing.T) {
|
||||
payload := map[string]interface{}{
|
||||
"username": "existinguser",
|
||||
payload := handlers.UpdateUsernameRequest{
|
||||
Username: "existinguser",
|
||||
}
|
||||
jsonData, _ := json.Marshal(payload)
|
||||
|
||||
@@ -176,15 +176,14 @@ func TestUserUpdateEndpoints(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req map[string]interface{}
|
||||
var req handlers.UpdateUsernameRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"error":"invalid request"}`))
|
||||
return
|
||||
}
|
||||
|
||||
username := req["username"].(string)
|
||||
if username == "existinguser" {
|
||||
if req.Username == "existinguser" {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
w.Write([]byte(`{"error":"username already taken"}`))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user