refactor(account): 移除卡类型字段、优化账号列表查询和权限检查
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m18s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m18s
- 移除 IoT 卡和号卡的 card_type 字段(数据库迁移) - 优化账号列表查询,支持按店铺和企业筛选 - 账号响应增加店铺名称和企业名称字段 - 实现批量加载店铺和企业名称,避免 N+1 查询 - 更新权限检查中间件,完善权限验证逻辑 - 更新相关测试用例,确保功能正确性
This commit is contained in:
@@ -834,3 +834,103 @@ func TestAccount_InvalidID(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, errors.CodeInvalidParam, result.Code)
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 关联查询测试
|
||||
// =============================================================================
|
||||
|
||||
func TestAccountList_FilterByShopID_WithShopName(t *testing.T) {
|
||||
env := integ.NewIntegrationTestEnv(t)
|
||||
|
||||
shop1 := env.CreateTestShop("测试店铺A", 1, nil)
|
||||
shop2 := env.CreateTestShop("测试店铺B", 1, nil)
|
||||
|
||||
account1 := env.CreateTestAccount("shop_account_1", "password123", constants.UserTypeAgent, &shop1.ID, nil)
|
||||
account2 := env.CreateTestAccount("shop_account_2", "password123", constants.UserTypeAgent, &shop1.ID, nil)
|
||||
account3 := env.CreateTestAccount("shop_account_3", "password123", constants.UserTypeAgent, &shop2.ID, nil)
|
||||
|
||||
url := fmt.Sprintf("/api/admin/accounts?shop_id=%d&page=1&page_size=10", shop1.ID)
|
||||
resp, err := env.AsSuperAdmin().Request("GET", url, nil)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var result response.Response
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, result.Code)
|
||||
|
||||
data := result.Data.(map[string]interface{})
|
||||
items := data["items"].([]interface{})
|
||||
assert.GreaterOrEqual(t, len(items), 2)
|
||||
|
||||
foundAccount1 := false
|
||||
foundAccount2 := false
|
||||
for _, item := range items {
|
||||
accountData := item.(map[string]interface{})
|
||||
accountID := uint(accountData["id"].(float64))
|
||||
|
||||
if accountID == account1.ID || accountID == account2.ID {
|
||||
assert.Equal(t, float64(shop1.ID), accountData["shop_id"])
|
||||
assert.Equal(t, shop1.ShopName, accountData["shop_name"])
|
||||
|
||||
if accountID == account1.ID {
|
||||
foundAccount1 = true
|
||||
}
|
||||
if accountID == account2.ID {
|
||||
foundAccount2 = true
|
||||
}
|
||||
}
|
||||
|
||||
if accountID == account3.ID {
|
||||
t.Errorf("不应该返回 shop2 的账号,但返回了账号 %d", account3.ID)
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, foundAccount1, "应该返回 account1")
|
||||
assert.True(t, foundAccount2, "应该返回 account2")
|
||||
}
|
||||
|
||||
func TestAccountList_FilterByEnterpriseID_WithEnterpriseName(t *testing.T) {
|
||||
env := integ.NewIntegrationTestEnv(t)
|
||||
|
||||
shop := env.CreateTestShop("归属店铺", 1, nil)
|
||||
enterprise1 := env.CreateTestEnterprise("测试企业A", &shop.ID)
|
||||
enterprise2 := env.CreateTestEnterprise("测试企业B", &shop.ID)
|
||||
|
||||
account1 := env.CreateTestAccount("enterprise_account_1", "password123", constants.UserTypeEnterprise, nil, &enterprise1.ID)
|
||||
account2 := env.CreateTestAccount("enterprise_account_2", "password123", constants.UserTypeEnterprise, nil, &enterprise2.ID)
|
||||
|
||||
url := fmt.Sprintf("/api/admin/accounts?enterprise_id=%d&page=1&page_size=10", enterprise1.ID)
|
||||
resp, err := env.AsSuperAdmin().Request("GET", url, nil)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var result response.Response
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, result.Code)
|
||||
|
||||
data := result.Data.(map[string]interface{})
|
||||
items := data["items"].([]interface{})
|
||||
assert.GreaterOrEqual(t, len(items), 1)
|
||||
|
||||
foundAccount1 := false
|
||||
for _, item := range items {
|
||||
accountData := item.(map[string]interface{})
|
||||
accountID := uint(accountData["id"].(float64))
|
||||
|
||||
if accountID == account1.ID {
|
||||
foundAccount1 = true
|
||||
assert.Equal(t, float64(enterprise1.ID), accountData["enterprise_id"])
|
||||
assert.Equal(t, enterprise1.EnterpriseName, accountData["enterprise_name"])
|
||||
}
|
||||
|
||||
if accountID == account2.ID {
|
||||
t.Errorf("不应该返回 enterprise2 的账号,但返回了账号 %d", account2.ID)
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, foundAccount1, "应该返回 account1")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user