test: add handler and config tests (auth, tasks, habits, savings, profile, interest, freeze)
Some checks failed
CI / lint-test (push) Failing after 1s

This commit is contained in:
Cosmo
2026-03-26 19:03:12 +00:00
parent 999f9911a9
commit 3c8dd575c3
9 changed files with 2509 additions and 0 deletions

View File

@@ -0,0 +1,268 @@
package handler
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestAuthHandler_Register_EmptyBody(t *testing.T) {
// Test that invalid JSON returns 400
req := httptest.NewRequest("POST", "/auth/register", bytes.NewBufferString("{invalid"))
rr := httptest.NewRecorder()
// We can't easily mock authService since it's a concrete type,
// but we can test request parsing logic
h := &AuthHandler{authService: nil}
h.Register(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
var resp map[string]string
json.NewDecoder(rr.Body).Decode(&resp)
if resp["error"] != "invalid request body" {
t.Errorf("expected 'invalid request body', got '%s'", resp["error"])
}
}
func TestAuthHandler_Register_MissingFields(t *testing.T) {
tests := []struct {
name string
body map[string]string
}{
{"missing email", map[string]string{"username": "test", "password": "123456"}},
{"missing username", map[string]string{"email": "test@test.com", "password": "123456"}},
{"missing password", map[string]string{"email": "test@test.com", "username": "test"}},
{"all empty", map[string]string{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body, _ := json.Marshal(tt.body)
req := httptest.NewRequest("POST", "/auth/register", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.Register(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
})
}
}
func TestAuthHandler_Login_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString("not json"))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.Login(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_Login_MissingFields(t *testing.T) {
tests := []struct {
name string
body map[string]string
}{
{"missing email", map[string]string{"password": "123456"}},
{"missing password", map[string]string{"email": "test@test.com"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body, _ := json.Marshal(tt.body)
req := httptest.NewRequest("POST", "/auth/login", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.Login(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
})
}
}
func TestAuthHandler_Refresh_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.Refresh(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_Refresh_EmptyToken(t *testing.T) {
body, _ := json.Marshal(map[string]string{"refresh_token": ""})
req := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.Refresh(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_VerifyEmail_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/auth/verify", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.VerifyEmail(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_VerifyEmail_EmptyToken(t *testing.T) {
body, _ := json.Marshal(map[string]string{"token": ""})
req := httptest.NewRequest("POST", "/auth/verify", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.VerifyEmail(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_ResendVerification_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/auth/resend", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ResendVerification(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_ResendVerification_EmptyEmail(t *testing.T) {
body, _ := json.Marshal(map[string]string{"email": ""})
req := httptest.NewRequest("POST", "/auth/resend", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ResendVerification(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_ForgotPassword_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/auth/forgot", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ForgotPassword(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_ForgotPassword_EmptyEmail(t *testing.T) {
body, _ := json.Marshal(map[string]string{"email": ""})
req := httptest.NewRequest("POST", "/auth/forgot", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ForgotPassword(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_ResetPassword_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/auth/reset", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ResetPassword(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_ResetPassword_MissingFields(t *testing.T) {
tests := []struct {
name string
body map[string]string
}{
{"missing token", map[string]string{"new_password": "123456"}},
{"missing password", map[string]string{"token": "abc"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body, _ := json.Marshal(tt.body)
req := httptest.NewRequest("POST", "/auth/reset", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ResetPassword(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
})
}
}
func TestAuthHandler_ChangePassword_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/auth/change-password", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ChangePassword(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestAuthHandler_ChangePassword_MissingFields(t *testing.T) {
tests := []struct {
name string
body map[string]string
}{
{"missing old password", map[string]string{"new_password": "123456"}},
{"missing new password", map[string]string{"old_password": "abc"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body, _ := json.Marshal(tt.body)
req := httptest.NewRequest("POST", "/auth/change-password", bytes.NewBuffer(body))
rr := httptest.NewRecorder()
h := &AuthHandler{authService: nil}
h.ChangePassword(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
})
}
}

View File

@@ -0,0 +1,21 @@
package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestHabitFreezeHandler_Create_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/habits/1/freeze", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &HabitFreezeHandler{freezeRepo: nil, habitRepo: nil}
h.Create(rr, req)
// No chi URL param → bad request
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}

View File

@@ -0,0 +1,33 @@
package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestHabitHandler_Create_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/habits", bytes.NewBufferString("not json"))
rr := httptest.NewRecorder()
h := &HabitHandler{habitService: nil}
h.Create(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestHabitHandler_Create_EmptyName(t *testing.T) {
body := `{"name":""}`
req := httptest.NewRequest("POST", "/habits", bytes.NewBufferString(body))
rr := httptest.NewRecorder()
h := &HabitHandler{habitService: nil}
h.Create(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}

View File

@@ -0,0 +1,33 @@
package handler
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestInterestHandler_CalculateInterest_Unauthorized(t *testing.T) {
h := &InterestHandler{
service: nil,
secretKey: "test-secret",
}
// No header
req := httptest.NewRequest("POST", "/internal/calculate-interest", nil)
rr := httptest.NewRecorder()
h.CalculateInterest(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
// Wrong header
req2 := httptest.NewRequest("POST", "/internal/calculate-interest", nil)
req2.Header.Set("X-Internal-Key", "wrong-key")
rr2 := httptest.NewRecorder()
h.CalculateInterest(rr2, req2)
if rr2.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr2.Code)
}
}

View File

@@ -0,0 +1,20 @@
package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestProfileHandler_Update_InvalidBody(t *testing.T) {
req := httptest.NewRequest("PUT", "/profile", bytes.NewBufferString("not json"))
rr := httptest.NewRecorder()
h := &ProfileHandler{userRepo: nil}
h.Update(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}

View File

@@ -0,0 +1,97 @@
package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestSavingsHandler_CreateCategory_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/categories", bytes.NewBufferString("not json"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateCategory(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateCategory_EmptyName(t *testing.T) {
body := `{"name":""}`
req := httptest.NewRequest("POST", "/savings/categories", bytes.NewBufferString(body))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateCategory(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateTransaction_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/transactions", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateTransaction(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateTransaction_Validation(t *testing.T) {
tests := []struct {
name string
body string
}{
{"missing category_id", `{"amount": 100, "type": "deposit", "date": "2026-01-01"}`},
{"zero amount", `{"category_id": 1, "amount": 0, "type": "deposit", "date": "2026-01-01"}`},
{"negative amount", `{"category_id": 1, "amount": -10, "type": "deposit", "date": "2026-01-01"}`},
{"invalid type", `{"category_id": 1, "amount": 100, "type": "invalid", "date": "2026-01-01"}`},
{"missing date", `{"category_id": 1, "amount": 100, "type": "deposit", "date": ""}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/transactions", bytes.NewBufferString(tt.body))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateTransaction(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
})
}
}
func TestSavingsHandler_UpdateCategory_InvalidBody(t *testing.T) {
req := httptest.NewRequest("PUT", "/savings/categories/1", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.UpdateCategory(rr, req)
// No chi route params → will fail on ParseInt → 400
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateRecurringPlan_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/categories/1/plans", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateRecurringPlan(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}

View File

@@ -0,0 +1,33 @@
package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestTaskHandler_Create_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/tasks", bytes.NewBufferString("not json"))
rr := httptest.NewRecorder()
h := &TaskHandler{taskService: nil}
h.Create(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestTaskHandler_Create_EmptyTitle(t *testing.T) {
body := `{"title":""}`
req := httptest.NewRequest("POST", "/tasks", bytes.NewBufferString(body))
rr := httptest.NewRecorder()
h := &TaskHandler{taskService: nil}
h.Create(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}