Files
apocapoc-api/internal/infrastructure/http/auth_integration_test.go
T
david 1b69bec12f
CI/CD Pipeline / Test (push) Has been cancelled
CI/CD Pipeline / Lint (push) Has been cancelled
CI/CD Pipeline / Build and Push Docker Image (push) Has been cancelled
Fix integration tests to comply with password requirements
Update test passwords from 'password123' to 'Password123!' to meet security requirements (uppercase, lowercase, digit, special char).

Fix migrations test to match actual habit_entries schema (removed non-existent deleted_at column).
2025-11-27 23:38:36 +01:00

140 lines
3.3 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!",
Timezone: "UTC",
}
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 AuthResponse
decodeResponse(t, rr, &resp)
if resp.Token == "" {
t.Error("Expected token in response")
}
if resp.UserID == "" {
t.Error("Expected user ID in response")
}
})
t.Run("Register duplicate email", func(t *testing.T) {
reqBody := RegisterRequest{
Email: "duplicate@example.com",
Password: "Password123!",
Timezone: "UTC",
}
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!",
Timezone: "UTC",
}
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",
Timezone: "UTC",
}
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!",
Timezone: "UTC",
}
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!",
Timezone: "UTC",
}
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)
}
})
}