Files
apocapoc-api/internal/infrastructure/http/auth_integration_test.go
T
david f780c69806 Add tests for RequestPasswordResetHandler and fix test compilation errors
- Add comprehensive tests for RequestPasswordResetHandler covering success and error cases
- Fix timezone-related test failures after removal of User.Timezone field:
  - Remove Timezone assertions from login_user_test.go
  - Remove Timezone field from RegisterRequest in integration tests
  - Update ValidateRegistration test cases (no longer validates timezone)
  - Update migrations_test to check current user table schema
- Fix syntax errors in user_repository_test.go (duplicate closing braces on lines 114 and 210)
2025-12-01 23:04:40 +01:00

134 lines
3.2 KiB
Go

package http
import (
"net/http"
"testing"
)
func TestAuthFlow(t *testing.T) {
ts := setupTestServer(t)
defer ts.Close()
t.Run("Register new user", func(t *testing.T) {
reqBody := RegisterRequest{
Email: "test@example.com",
Password: "Password123!",
}
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/auth/register", reqBody, "")
if rr.Code != http.StatusCreated {
t.Errorf("Expected status 201, got %d. Body: %s", rr.Code, rr.Body.String())
}
var resp RegisterResponse
decodeResponse(t, rr, &resp)
if resp.UserID == "" {
t.Error("Expected user ID in response")
}
if resp.Message == "" {
t.Error("Expected message in response")
}
})
t.Run("Register duplicate email", func(t *testing.T) {
reqBody := RegisterRequest{
Email: "duplicate@example.com",
Password: "Password123!",
}
makeRequest(t, *ts.Router, "POST", "/api/v1/auth/register", reqBody, "")
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/auth/register", reqBody, "")
if rr.Code != http.StatusConflict {
t.Errorf("Expected status 409, got %d", rr.Code)
}
})
t.Run("Register with invalid email", func(t *testing.T) {
reqBody := RegisterRequest{
Email: "invalid-email",
Password: "Password123!",
}
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/auth/register", reqBody, "")
if rr.Code != http.StatusBadRequest {
t.Errorf("Expected status 400, got %d", rr.Code)
}
})
t.Run("Register with short password", func(t *testing.T) {
reqBody := RegisterRequest{
Email: "short@example.com",
Password: "123",
}
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/auth/register", reqBody, "")
if rr.Code != http.StatusBadRequest {
t.Errorf("Expected status 400, got %d", rr.Code)
}
})
t.Run("Login with valid credentials", func(t *testing.T) {
registerBody := RegisterRequest{
Email: "login@example.com",
Password: "Password123!",
}
makeRequest(t, *ts.Router, "POST", "/api/v1/auth/register", registerBody, "")
loginBody := LoginRequest{
Email: "login@example.com",
Password: "Password123!",
}
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/auth/login", loginBody, "")
if rr.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", rr.Code)
}
var resp AuthResponse
decodeResponse(t, rr, &resp)
if resp.Token == "" {
t.Error("Expected token in response")
}
})
t.Run("Login with invalid password", func(t *testing.T) {
registerBody := RegisterRequest{
Email: "wrongpass@example.com",
Password: "Password123!",
}
makeRequest(t, *ts.Router, "POST", "/api/v1/auth/register", registerBody, "")
loginBody := LoginRequest{
Email: "wrongpass@example.com",
Password: "wrongpassword",
}
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/auth/login", loginBody, "")
if rr.Code != http.StatusUnauthorized {
t.Errorf("Expected status 401, got %d", rr.Code)
}
})
t.Run("Login with non-existent user", func(t *testing.T) {
loginBody := LoginRequest{
Email: "nonexistent@example.com",
Password: "Password123!",
}
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/auth/login", loginBody, "")
if rr.Code != http.StatusUnauthorized {
t.Errorf("Expected status 401, got %d", rr.Code)
}
})
}