将状态码改成自定义码
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/user"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/token"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
@@ -106,7 +107,7 @@ func TestCreateUser(t *testing.T) {
|
||||
// 在成功创建用户的路径下,FindByUsername 不会被调用,因此这里不需要设置其期望
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusCreated), // 修改这里:从 http.StatusOK 改为 http.StatusCreated
|
||||
"code": float64(controller.CodeCreated), // 修改这里:使用自定义状态码
|
||||
"message": "用户创建成功",
|
||||
"data": map[string]interface{}{
|
||||
"username": "testuser",
|
||||
@@ -124,7 +125,7 @@ func TestCreateUser(t *testing.T) {
|
||||
// 不会调用 Create 或 FindByUsername
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusBadRequest),
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
"message": "Key: 'CreateUserRequest.Password' Error:Field validation for 'Password' failed on the 'min' tag",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -138,7 +139,7 @@ func TestCreateUser(t *testing.T) {
|
||||
// 不会调用 Create 或 FindByUsername
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusBadRequest),
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
"message": "Key: 'CreateUserRequest.Username' Error:Field validation for 'Username' failed on the 'required' tag",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -156,7 +157,7 @@ func TestCreateUser(t *testing.T) {
|
||||
m.On("FindByUsername", "existinguser").Return(&models.User{Username: "existinguser"}, nil).Once()
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusConflict),
|
||||
"code": float64(controller.CodeConflict),
|
||||
"message": "用户名已存在",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -174,7 +175,7 @@ func TestCreateUser(t *testing.T) {
|
||||
m.On("FindByUsername", "db_error_user").Return(nil, gorm.ErrRecordNotFound).Once()
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusInternalServerError),
|
||||
"code": float64(controller.CodeInternalError),
|
||||
"message": "创建用户失败",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -200,10 +201,10 @@ func TestCreateUser(t *testing.T) {
|
||||
tt.mockRepoSetup(mockRepo)
|
||||
|
||||
// 创建控制器实例,使用静默日志器
|
||||
controller := user.NewController(mockRepo, silentLogger, nil) // tokenService 在 CreateUser 中未使用,设为 nil
|
||||
userController := user.NewController(mockRepo, silentLogger, nil) // tokenService 在 CreateUser 中未使用,设为 nil
|
||||
|
||||
// 调用被测试的方法
|
||||
controller.CreateUser(ctx)
|
||||
userController.CreateUser(ctx)
|
||||
|
||||
// 解析响应体
|
||||
var responseBody map[string]interface{}
|
||||
@@ -214,7 +215,7 @@ func TestCreateUser(t *testing.T) {
|
||||
assert.Equal(t, tt.expectedResponse["code"], responseBody["code"])
|
||||
|
||||
// 断言响应内容 (除了 code 字段)
|
||||
if tt.expectedResponse["code"] == float64(http.StatusCreated) { // 修改这里:从 http.StatusOK 改为 http.StatusCreated
|
||||
if tt.expectedResponse["code"] == float64(controller.CodeCreated) {
|
||||
// 确保 data 字段存在且是 map[string]interface{} 类型
|
||||
data, ok := responseBody["data"].(map[string]interface{})
|
||||
assert.True(t, ok, "响应体中的 data 字段应为 map[string]interface{}")
|
||||
@@ -282,7 +283,7 @@ func TestLogin(t *testing.T) {
|
||||
m.On("GenerateToken", uint(1)).Return("mocked_token", nil).Once()
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusOK),
|
||||
"code": float64(controller.CodeSuccess),
|
||||
"message": "登录成功",
|
||||
"data": map[string]interface{}{
|
||||
"username": "loginuser",
|
||||
@@ -300,7 +301,7 @@ func TestLogin(t *testing.T) {
|
||||
mockRepoSetup: func(m *MockUserRepository) {},
|
||||
mockTokenServiceSetup: func(m *MockTokenService) {},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusBadRequest),
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
"message": "Key: 'LoginRequest.Username' Error:Field validation for 'Username' failed on the 'required' tag",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -314,7 +315,7 @@ func TestLogin(t *testing.T) {
|
||||
mockRepoSetup: func(m *MockUserRepository) {},
|
||||
mockTokenServiceSetup: func(m *MockTokenService) {},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusBadRequest),
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
"message": "Key: 'LoginRequest.Password' Error:Field validation for 'Password' failed on the 'required' tag",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -330,7 +331,7 @@ func TestLogin(t *testing.T) {
|
||||
},
|
||||
mockTokenServiceSetup: func(m *MockTokenService) {},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusUnauthorized),
|
||||
"code": float64(controller.CodeUnauthorized),
|
||||
"message": "用户名或密码不正确",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -345,7 +346,7 @@ func TestLogin(t *testing.T) {
|
||||
m.On("FindByUsername", "dberroruser").Return(nil, errors.New("database connection error")).Once()
|
||||
},
|
||||
mockTokenServiceSetup: func(m *MockTokenService) {}, expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusInternalServerError),
|
||||
"code": float64(controller.CodeInternalError),
|
||||
"message": "登录失败",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -368,7 +369,7 @@ func TestLogin(t *testing.T) {
|
||||
},
|
||||
mockTokenServiceSetup: func(m *MockTokenService) {},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusUnauthorized),
|
||||
"code": float64(controller.CodeUnauthorized),
|
||||
"message": "用户名或密码不正确",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -393,7 +394,7 @@ func TestLogin(t *testing.T) {
|
||||
m.On("GenerateToken", uint(1)).Return("", errors.New("jwt error")).Once()
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(http.StatusInternalServerError),
|
||||
"code": float64(controller.CodeInternalError),
|
||||
"message": "登录失败,无法生成认证信息",
|
||||
"data": nil,
|
||||
},
|
||||
@@ -421,10 +422,10 @@ func TestLogin(t *testing.T) {
|
||||
tt.mockTokenServiceSetup(mockTokenService)
|
||||
|
||||
// 创建控制器实例
|
||||
controller := user.NewController(mockRepo, silentLogger, mockTokenService)
|
||||
userController := user.NewController(mockRepo, silentLogger, mockTokenService)
|
||||
|
||||
// 调用被测试的方法
|
||||
controller.Login(ctx)
|
||||
userController.Login(ctx)
|
||||
|
||||
// 解析响应体
|
||||
var responseBody map[string]interface{}
|
||||
@@ -435,9 +436,15 @@ func TestLogin(t *testing.T) {
|
||||
assert.Equal(t, tt.expectedResponse["code"], responseBody["code"])
|
||||
|
||||
// 断言响应内容 (除了 code 字段)
|
||||
if tt.expectedResponse["code"] == float64(http.StatusOK) {
|
||||
assert.NotNil(t, responseBody["data"].(map[string]interface{})["id"])
|
||||
assert.NotNil(t, responseBody["data"].(map[string]interface{})["token"])
|
||||
if tt.expectedResponse["code"] == float64(controller.CodeSuccess) {
|
||||
// 确保 data 字段存在且是 map[string]interface{} 类型
|
||||
data, ok := responseBody["data"].(map[string]interface{})
|
||||
assert.True(t, ok, "响应体中的 data 字段应为 map[string]interface{}")
|
||||
|
||||
// 验证 id 和 token 存在
|
||||
assert.NotNil(t, data["id"])
|
||||
assert.NotNil(t, data["token"])
|
||||
|
||||
// 移除 ID 和 Token 字段以便进行通用断言
|
||||
delete(responseBody["data"].(map[string]interface{}), "id")
|
||||
delete(tt.expectedResponse["data"].(map[string]interface{}), "id")
|
||||
|
||||
Reference in New Issue
Block a user