Fix test infrastructure and device UUID handling
- Remove manual device ID generation, use database-generated IDs - Add comprehensive test helpers (setupTestServer, loginTestUser, getTestUserID) - Add cleanup step for existing test users in integration tests - Fix UUID parsing from database responses
This commit is contained in:
@@ -20,6 +20,79 @@ func TestPhase1Integration(t *testing.T) {
|
||||
t.Skip("Skipping integration test in short mode")
|
||||
}
|
||||
|
||||
// Cleanup: Try to delete test user if it exists from previous test runs
|
||||
t.Run("Cleanup_ExistingTestUser", func(t *testing.T) {
|
||||
// Try to login as the test user first
|
||||
loginReq := map[string]interface{}{
|
||||
"login": "admin@bookmann.test",
|
||||
"password": "SecurePass123!",
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(loginReq)
|
||||
resp, err := http.Post(baseTestURL+"/auth/login", "application/json", bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
t.Logf("Cleanup: No existing test user to delete (server not available)")
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// If login succeeds, try to delete the user
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
if token, ok := result["access_token"].(string); ok {
|
||||
// Delete the user using the token
|
||||
req, _ := http.NewRequest("DELETE", baseTestURL+"/auth/account", bytes.NewBuffer([]byte{}))
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
delResp, err := client.Do(req)
|
||||
if err == nil {
|
||||
defer delResp.Body.Close()
|
||||
if delResp.StatusCode == http.StatusNoContent {
|
||||
t.Logf("Cleanup: Deleted existing test user")
|
||||
} else {
|
||||
t.Logf("Cleanup: Could not delete existing test user (HTTP %d)", delResp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// Also try to delete any libraries created by this user
|
||||
req, _ = http.NewRequest("GET", baseTestURL+"/libraries", bytes.NewBuffer([]byte{}))
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
listResp, err := client.Do(req)
|
||||
if err == nil {
|
||||
defer listResp.Body.Close()
|
||||
if listResp.StatusCode == http.StatusOK {
|
||||
var libsResult map[string]interface{}
|
||||
json.NewDecoder(listResp.Body).Decode(&libsResult)
|
||||
|
||||
if data, ok := libsResult["data"].([]interface{}); ok {
|
||||
for _, lib := range data {
|
||||
if libMap, ok := lib.(map[string]interface{}); ok {
|
||||
if libID, ok := libMap["id"].(string); ok {
|
||||
// Delete the library
|
||||
req, _ = http.NewRequest("DELETE", baseTestURL+"/libraries/"+libID, bytes.NewBuffer([]byte{}))
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
delLibResp, _ := client.Do(req)
|
||||
if delLibResp != nil {
|
||||
delLibResp.Body.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a bit for cleanup to complete
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
})
|
||||
|
||||
// Step 1: Create first user (should be admin)
|
||||
t.Run("Step1_CreateFirstUser", func(t *testing.T) {
|
||||
userReq := map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user