fix: handle existing user in integration test setup
- Add fallback to login when user registration returns 409 Conflict - Prevents empty user token error when test user already exists - Allows integration tests to run reliably across multiple executions - Test now attempts to log in with existing credentials if registration fails This fixes the issue where the test would fail if the user 'integrationuser@test.com' already existed from a previous test run.
This commit is contained in:
@@ -218,6 +218,26 @@ func setupTestSuite(t *testing.T) *TestContext {
|
||||
ctx.UserToken = extractToken(authResp)
|
||||
ctx.UserID = authResp.User.ID
|
||||
t.Logf("Created new user: %s (%s)", authResp.User.Email, authResp.User.Role)
|
||||
} else if resp.StatusCode == http.StatusConflict {
|
||||
// User already exists, log in instead
|
||||
t.Logf("User already exists, attempting login...")
|
||||
loginReq := map[string]interface{}{
|
||||
"login": "integrationuser@test.com",
|
||||
"password": "UserPass123!",
|
||||
}
|
||||
loginResp := makeRequest(t, "POST", "/api/auth/login", loginReq, "")
|
||||
defer loginResp.Body.Close()
|
||||
|
||||
require.Equal(t, http.StatusOK, loginResp.StatusCode, "Login should succeed for existing user")
|
||||
|
||||
var authResp AuthResponse
|
||||
body, _ := io.ReadAll(loginResp.Body)
|
||||
err := json.Unmarshal(body, &authResp)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx.UserToken = extractToken(authResp)
|
||||
ctx.UserID = authResp.User.ID
|
||||
t.Logf("Logged in as existing user: %s (%s)", authResp.User.Email, authResp.User.Role)
|
||||
}
|
||||
|
||||
require.NotEmpty(t, ctx.UserToken, "User token is empty")
|
||||
|
||||
Reference in New Issue
Block a user