perms_mock_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package auth
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "testing"
  7. "time"
  8. "perms-system-server/internal/model/perm"
  9. "perms-system-server/internal/model/productmember"
  10. "perms-system-server/internal/testutil"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. // TC-0271: GetUserPerms 委托到 UserDetailsLoader.Load()
  15. func TestGetUserPerms_DelegatesToLoader(t *testing.T) {
  16. ctx := context.Background()
  17. svcCtx := newTestSvcCtx()
  18. conn := testutil.GetTestSqlConn()
  19. now := time.Now().Unix()
  20. pc := fmt.Sprintf("tp_delegate_%d", rand.Intn(100000))
  21. userId := int64(900000 + rand.Intn(10000))
  22. pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmember.SysProductMember{
  23. ProductCode: pc, UserId: userId, MemberType: "ADMIN", Status: 1, CreateTime: now, UpdateTime: now,
  24. })
  25. require.NoError(t, err)
  26. pmId, _ := pmRes.LastInsertId()
  27. p1Res, err := svcCtx.SysPermModel.Insert(ctx, &perm.SysPerm{
  28. ProductCode: pc, Name: "del_p1", Code: fmt.Sprintf("del_c1_%d", rand.Intn(100000)), Status: 1, CreateTime: now, UpdateTime: now,
  29. })
  30. require.NoError(t, err)
  31. p1Id, _ := p1Res.LastInsertId()
  32. t.Cleanup(func() {
  33. testutil.CleanTable(ctx, conn, "`sys_product_member`", pmId)
  34. testutil.CleanTable(ctx, conn, "`sys_perm`", p1Id)
  35. })
  36. ud := svcCtx.UserDetailsLoader.Load(ctx, userId, pc)
  37. perms, memberType, err := GetUserPerms(ctx, svcCtx, userId, 0, pc, false)
  38. require.NoError(t, err, "GetUserPerms should always return nil error")
  39. assert.Equal(t, ud.Perms, perms, "Perms should match UserDetailsLoader.Load() result")
  40. assert.Equal(t, ud.MemberType, memberType, "MemberType should match UserDetailsLoader.Load() result")
  41. }