Add integration tests for HTTP layer
- Create test server setup with in-memory SQLite - Add auth flow tests (register, login, validation) - Add habit CRUD tests (create, read, update, archive) - Add habit entries tests (mark, unmark, get entries) - Test authorization and cross-user access control
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user