Files
iotlcm/server/internal/logic/app/app_customer_test.go
T

126 lines
3.9 KiB
Go

package app
import (
"context"
"testing"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
)
func TestAppCustomerSummary(t *testing.T) {
items := make([]*model.AppCustomerModel, 0, 25)
for index := 0; index < 25; index++ {
items = append(items, &model.AppCustomerModel{Status: "0", RealNameStatus: "已录入实名资料"})
}
items[24].Status = "1"
items[24].RealNameStatus = "实名资料待完善"
items = append(items, nil)
summary := appCustomerSummary(items)
if summary.Total != 25 || summary.NormalCount != 24 || summary.PendingProfileCount != 1 {
t.Fatalf("unexpected summary: %+v", summary)
}
}
func TestAppCustomerSummaryEmpty(t *testing.T) {
summary := appCustomerSummary(nil)
if summary.Total != 0 || summary.NormalCount != 0 || summary.PendingProfileCount != 0 {
t.Fatalf("unexpected empty summary: %+v", summary)
}
}
func TestAppCustomerFilterKeepsAuthorizedScope(t *testing.T) {
depts := []*entity.SysDept{
{DeptId: 101, DeptName: "企业甲", DeptKey: "A101", Status: "0"},
{DeptId: 102, DeptName: "企业乙", DeptKey: "A102", Status: "1"},
nil,
}
cases := []struct {
name, keyword, status string
want int64
}{
{"按编码搜索", "A101", "", 101},
{"按企业名称搜索", "企业乙", "", 102},
{"仅停用客户", "", "1", 102},
}
for _, item := range cases {
t.Run(item.name, func(t *testing.T) {
rows := appFilterEnterpriseDepts(depts, item.keyword, item.status)
if len(rows) != 1 || rows[0].DeptId != item.want {
t.Fatalf("unexpected result: %+v", rows)
}
})
}
if rows := appFilterEnterpriseDepts(depts, "A101", "1"); len(rows) != 0 {
t.Fatalf("combined filters must intersect: %+v", rows)
}
if rows := appFilterEnterpriseDepts(depts, "未授权企业", ""); len(rows) != 0 {
t.Fatalf("search must not expand candidate scope: %+v", rows)
}
}
func TestAppCustomerPage(t *testing.T) {
items := make([]*model.AppCustomerModel, 25)
for index := range items {
items[index] = &model.AppCustomerModel{CustomerId: int64(index + 1)}
}
cases := []struct {
name string
page, size, count int
first int64
}{
{"首页", 1, 20, 20, 1},
{"末页", 2, 20, 5, 21},
{"越界页", 3, 20, 0, 0},
{"最大页码", int(^uint(0) >> 1), 20, 0, 0},
{"最大页容量", 1, int(^uint(0) >> 1), 25, 1},
{"无效页", 0, 20, 0, 0},
{"无效容量", 1, 0, 0, 0},
}
for _, item := range cases {
t.Run(item.name, func(t *testing.T) {
rows := appCustomerPage(items, item.page, item.size)
if len(rows) != item.count {
t.Fatalf("count=%d want=%d", len(rows), item.count)
}
if len(rows) > 0 && rows[0].CustomerId != item.first {
t.Fatalf("first=%d want=%d", rows[0].CustomerId, item.first)
}
})
}
if rows := appCustomerPage(nil, 1, 20); rows == nil || len(rows) != 0 {
t.Fatal("empty result must serialize as an array")
}
}
func TestAppCustomerRoleScope(t *testing.T) {
user := &entity.SysUser{UserId: 7, DeptId: 101, TenantId: "A"}
cases := []struct {
name, scope string
all bool
deptCount, userCount int
}{
{"全部数据", consts.SysRoleDataScopeAll, true, 0, 0},
{"仅本级", consts.SysRoleDataScopeDept, false, 1, 0},
{"仅本人", consts.SysRoleDataScopePersonal, false, 0, 1},
{"未知权限拒绝", "invalid", false, 0, 0},
}
for _, item := range cases {
t.Run(item.name, func(t *testing.T) {
scope, err := appBuildEnterpriseDataScope(context.Background(), user, 1, item.scope, true)
if err != nil {
t.Fatal(err)
}
if scope.All != item.all || len(scope.DeptIds) != item.deptCount || len(scope.UserIds) != item.userCount {
t.Fatalf("unexpected scope: %+v", scope)
}
if !scope.All && !scope.Restricted {
t.Fatal("non-all scope must remain restricted")
}
if len(scope.DeptIds) > 0 && scope.DeptIds[0] != user.DeptId {
t.Fatal("wrong dept scope")
}
})
}
}