269 lines
7.1 KiB
Go
269 lines
7.1 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|