少提交的东西

This commit is contained in:
2026-01-06 11:07:29 +08:00
parent 2d566a9820
commit 4df3c12328
10 changed files with 328 additions and 100 deletions

View File

@@ -22,7 +22,7 @@ import (
"gorm.io/gorm/logger"
"github.com/break/junhong_cmp_fiber/internal/bootstrap"
"github.com/break/junhong_cmp_fiber/internal/handler"
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/routes"
accountService "github.com/break/junhong_cmp_fiber/internal/service/account"
@@ -107,7 +107,7 @@ func setupTestEnv(t *testing.T) *testEnv {
accService := accountService.New(accountStore, roleStore, accountRoleStore)
// 初始化 Handler
accountHandler := handler.NewAccountHandler(accService)
accountHandler := admin.NewAccountHandler(accService)
// 创建 Fiber App
app := fiber.New(fiber.Config{
@@ -191,7 +191,7 @@ func TestAccountAPI_Create(t *testing.T) {
}
jsonBody, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/v1/accounts", bytes.NewReader(jsonBody))
req := httptest.NewRequest("POST", "/api/admin/accounts", bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "application/json")
resp, err := env.app.Test(req)
@@ -231,7 +231,7 @@ func TestAccountAPI_Create(t *testing.T) {
}
jsonBody, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/v1/accounts", bytes.NewReader(jsonBody))
req := httptest.NewRequest("POST", "/api/admin/accounts", bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "application/json")
resp, err := env.app.Test(req)
@@ -253,7 +253,7 @@ func TestAccountAPI_Create(t *testing.T) {
}
jsonBody, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/v1/accounts", bytes.NewReader(jsonBody))
req := httptest.NewRequest("POST", "/api/admin/accounts", bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "application/json")
resp, err := env.app.Test(req)
@@ -290,7 +290,7 @@ func TestAccountAPI_Get(t *testing.T) {
createTestAccount(t, env.db, testAccount)
t.Run("成功获取账号详情", func(t *testing.T) {
req := httptest.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d", testAccount.ID), nil)
req := httptest.NewRequest("GET", fmt.Sprintf("/api/admin/accounts/%d", testAccount.ID), nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
@@ -302,7 +302,7 @@ func TestAccountAPI_Get(t *testing.T) {
})
t.Run("账号不存在时返回错误", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/v1/accounts/99999", nil)
req := httptest.NewRequest("GET", "/api/admin/accounts/99999", nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
@@ -313,7 +313,7 @@ func TestAccountAPI_Get(t *testing.T) {
})
t.Run("无效ID返回错误", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/v1/accounts/invalid", nil)
req := httptest.NewRequest("GET", "/api/admin/accounts/invalid", nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
@@ -354,7 +354,7 @@ func TestAccountAPI_Update(t *testing.T) {
}
jsonBody, _ := json.Marshal(reqBody)
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d", testAccount.ID), bytes.NewReader(jsonBody))
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/admin/accounts/%d", testAccount.ID), bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "application/json")
resp, err := env.app.Test(req)
@@ -392,7 +392,7 @@ func TestAccountAPI_Delete(t *testing.T) {
}
createTestAccount(t, env.db, testAccount)
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d", testAccount.ID), nil)
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/admin/accounts/%d", testAccount.ID), nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
@@ -431,7 +431,7 @@ func TestAccountAPI_List(t *testing.T) {
}
t.Run("成功获取账号列表", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/v1/accounts?page=1&page_size=10", nil)
req := httptest.NewRequest("GET", "/api/admin/accounts?page=1&page_size=10", nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
@@ -443,7 +443,7 @@ func TestAccountAPI_List(t *testing.T) {
})
t.Run("分页功能正常", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/v1/accounts?page=1&page_size=2", nil)
req := httptest.NewRequest("GET", "/api/admin/accounts?page=1&page_size=2", nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
@@ -487,7 +487,7 @@ func TestAccountAPI_AssignRoles(t *testing.T) {
}
jsonBody, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/roles", testAccount.ID), bytes.NewReader(jsonBody))
req := httptest.NewRequest("POST", fmt.Sprintf("/api/admin/accounts/%d/roles", testAccount.ID), bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "application/json")
resp, err := env.app.Test(req)
@@ -542,7 +542,7 @@ func TestAccountAPI_GetRoles(t *testing.T) {
env.db.Create(accountRole)
t.Run("成功获取账号角色", func(t *testing.T) {
req := httptest.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/roles", testAccount.ID), nil)
req := httptest.NewRequest("GET", fmt.Sprintf("/api/admin/accounts/%d/roles", testAccount.ID), nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
@@ -595,7 +595,7 @@ func TestAccountAPI_RemoveRole(t *testing.T) {
env.db.Create(accountRole)
t.Run("成功移除角色", func(t *testing.T) {
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/roles/%d", testAccount.ID, testRole.ID), nil)
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/admin/accounts/%d/roles/%d", testAccount.ID, testRole.ID), nil)
resp, err := env.app.Test(req)
require.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)