Files
zsp-project/backend/test/user_handler_test.go
2026-06-03 20:59:39 +08:00

178 lines
5.3 KiB
Go
Executable File

package test
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/rovina/zsp-backend/internal/dto"
"github.com/rovina/zsp-backend/internal/handler"
)
// mockUserService implements service.UserService for testing
type mockUserService struct {
registerFn func(req *dto.CreateUserRequest) (*dto.UserResponse, error)
loginFn func(req *dto.LoginRequest) (*dto.UserResponse, string, error)
getUserByIDFn func(id uint) (*dto.UserResponse, error)
}
func (m *mockUserService) Register(req *dto.CreateUserRequest) (*dto.UserResponse, error) {
return m.registerFn(req)
}
func (m *mockUserService) Login(req *dto.LoginRequest) (*dto.UserResponse, string, error) {
return m.loginFn(req)
}
func (m *mockUserService) GetUserByID(id uint) (*dto.UserResponse, error) {
return m.getUserByIDFn(id)
}
func setupUserRouter(h *handler.UserHandler) *gin.Engine {
gin.SetMode(gin.TestMode)
r := gin.New()
api := r.Group("/api")
{
auth := api.Group("/auth")
{
auth.POST("/register", h.Register)
auth.POST("/login", h.Login)
}
users := api.Group("/users")
{
users.GET("/:id", h.GetUser)
}
}
return r
}
func TestUserRegister_Success(t *testing.T) {
mock := &mockUserService{
registerFn: func(req *dto.CreateUserRequest) (*dto.UserResponse, error) {
return &dto.UserResponse{ID: 1, Username: "testuser", Email: "test@example.com"}, nil
},
}
h := handler.NewUserHandler(mock)
router := setupUserRouter(h)
body, _ := json.Marshal(dto.CreateUserRequest{Username: "testuser", Email: "test@example.com", Password: "password123"})
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/auth/register", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Errorf("expected status 201, got %d", w.Code)
}
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
if resp["message"] != "User registered successfully" {
t.Errorf("unexpected message: %v", resp["message"])
}
}
func TestUserRegister_DuplicateEmail(t *testing.T) {
mock := &mockUserService{
registerFn: func(req *dto.CreateUserRequest) (*dto.UserResponse, error) {
return nil, errors.New("email already exists")
},
}
h := handler.NewUserHandler(mock)
router := setupUserRouter(h)
body, _ := json.Marshal(dto.CreateUserRequest{Username: "testuser", Email: "existing@example.com", Password: "password123"})
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/auth/register", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("expected status 400, got %d", w.Code)
}
}
func TestUserLogin_Success(t *testing.T) {
mock := &mockUserService{
loginFn: func(req *dto.LoginRequest) (*dto.UserResponse, string, error) {
return &dto.UserResponse{ID: 1, Username: "testuser", Email: "test@example.com"}, "test-token", nil
},
}
h := handler.NewUserHandler(mock)
router := setupUserRouter(h)
body, _ := json.Marshal(dto.LoginRequest{Email: "test@example.com", Password: "password123"})
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/auth/login", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
}
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
if resp["token"] != "test-token" {
t.Errorf("expected token 'test-token', got %v", resp["token"])
}
}
func TestUserLogin_InvalidCredentials(t *testing.T) {
mock := &mockUserService{
loginFn: func(req *dto.LoginRequest) (*dto.UserResponse, string, error) {
return nil, "", errors.New("invalid credentials")
},
}
h := handler.NewUserHandler(mock)
router := setupUserRouter(h)
body, _ := json.Marshal(dto.LoginRequest{Email: "wrong@example.com", Password: "wrong"})
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/auth/login", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != http.StatusUnauthorized {
t.Errorf("expected status 401, got %d", w.Code)
}
}
func TestUserGet_Success(t *testing.T) {
mock := &mockUserService{
getUserByIDFn: func(id uint) (*dto.UserResponse, error) {
return &dto.UserResponse{ID: 1, Username: "testuser", Email: "test@example.com"}, nil
},
}
h := handler.NewUserHandler(mock)
router := setupUserRouter(h)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/users/1", nil)
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
}
}
func TestUserGet_NotFound(t *testing.T) {
mock := &mockUserService{
getUserByIDFn: func(id uint) (*dto.UserResponse, error) {
return nil, errors.New("user not found")
},
}
h := handler.NewUserHandler(mock)
router := setupUserRouter(h)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/users/999", nil)
router.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Errorf("expected status 404, got %d", w.Code)
}
}