Files
im_manager/server/internal/logic/sys/im_admin_scope_test.go
T

76 lines
2.0 KiB
Go

package sys
import (
"context"
"testing"
"hotgo/internal/consts"
_ "hotgo/internal/logic/admin"
"hotgo/internal/model"
)
func TestBoundCompanyDepartmentUsesBoundTenant(t *testing.T) {
ctx := context.WithValue(context.Background(), consts.ContextHTTPKey, &model.Context{
User: &model.Identity{Id: 14, TenantId: 1, DeptType: consts.DeptTypeCompany, RoleKey: "imManage"},
})
if isImPlatformAdmin(ctx, 14) {
t.Fatal("expected bound company department IM manager to use its bound tenant")
}
}
func TestUnboundCompanyDepartmentIsImPlatformAdmin(t *testing.T) {
ctx := context.WithValue(context.Background(), consts.ContextHTTPKey, &model.Context{
User: &model.Identity{Id: 14, DeptType: consts.DeptTypeCompany, RoleKey: "imManage"},
})
if !isImPlatformAdmin(ctx, 14) {
t.Fatal("expected unbound company department IM manager to use platform tenant selection")
}
}
func TestResolveImAdminBoundTenant(t *testing.T) {
tests := []struct {
name string
identity *model.Identity
want int64
}{
{
name: "tenant department with tenant binding",
identity: &model.Identity{
TenantId: 2,
DeptType: consts.DeptTypeTenant,
},
want: 2,
},
{
name: "company department with tenant binding",
identity: &model.Identity{
TenantId: 1,
DeptType: consts.DeptTypeCompany,
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), consts.ContextHTTPKey, &model.Context{User: tt.identity})
got, err := resolveImAdminBoundTenant(ctx)
if err != nil {
t.Fatal(err)
}
if got != tt.want {
t.Fatalf("tenant binding mismatch: got %d, want %d", got, tt.want)
}
})
}
}
func TestResolveImAdminBoundTenantRejectsUnboundAccount(t *testing.T) {
ctx := context.WithValue(context.Background(), consts.ContextHTTPKey, &model.Context{
User: &model.Identity{DeptType: consts.DeptTypeTenant, RoleKey: "tenantManager"},
})
if _, err := resolveImAdminBoundTenant(ctx); err == nil {
t.Fatal("expected unbound account to be rejected")
}
}