productListLogic_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package product
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. productModel "perms-system-server/internal/model/product"
  7. "perms-system-server/internal/svc"
  8. "perms-system-server/internal/testutil"
  9. "perms-system-server/internal/testutil/ctxhelper"
  10. "perms-system-server/internal/types"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. // TC-0068: 正常分页
  15. func TestProductList_Normal(t *testing.T) {
  16. ctx := context.Background()
  17. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  18. conn := testutil.GetTestSqlConn()
  19. now := time.Now().Unix()
  20. var ids []int64
  21. for i := 0; i < 3; i++ {
  22. code := testutil.UniqueId()
  23. result, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
  24. Code: code, Name: "列表测试产品", AppKey: "k_" + code, AppSecret: "s_" + code,
  25. Status: 1, CreateTime: now, UpdateTime: now,
  26. })
  27. require.NoError(t, err)
  28. id, _ := result.LastInsertId()
  29. ids = append(ids, id)
  30. }
  31. t.Cleanup(func() {
  32. testutil.CleanTable(ctx, conn, "`sys_product`", ids...)
  33. })
  34. logic := NewProductListLogic(ctx, svcCtx)
  35. resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 10})
  36. require.NoError(t, err)
  37. require.NotNil(t, resp)
  38. assert.True(t, resp.Total >= 3)
  39. items, ok := resp.List.([]types.ProductItem)
  40. require.True(t, ok)
  41. assert.True(t, len(items) >= 3)
  42. }
  43. // TC-0069: 默认分页
  44. func TestProductList_DefaultPagination(t *testing.T) {
  45. ctx := context.Background()
  46. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  47. logic := NewProductListLogic(ctx, svcCtx)
  48. resp, err := logic.ProductList(&types.ProductListReq{Page: 0, PageSize: 0})
  49. require.NoError(t, err)
  50. require.NotNil(t, resp)
  51. items, ok := resp.List.([]types.ProductItem)
  52. require.True(t, ok)
  53. assert.True(t, len(items) <= 20, "default pageSize should be 20")
  54. }
  55. // TC-0070: pageSize超过上限
  56. func TestProductList_PageSizeExceedsLimit(t *testing.T) {
  57. ctx := context.Background()
  58. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  59. logic := NewProductListLogic(ctx, svcCtx)
  60. resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 500})
  61. require.NoError(t, err)
  62. require.NotNil(t, resp)
  63. items, ok := resp.List.([]types.ProductItem)
  64. require.True(t, ok)
  65. assert.True(t, len(items) <= 100, "pageSize should be capped at 100")
  66. }
  67. // TC-0071: pageSize=0
  68. func TestProductList_PageSizeZero_DefaultsTo20(t *testing.T) {
  69. ctx := context.Background()
  70. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  71. logic := NewProductListLogic(ctx, svcCtx)
  72. resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 0})
  73. require.NoError(t, err)
  74. require.NotNil(t, resp)
  75. items, ok := resp.List.([]types.ProductItem)
  76. require.True(t, ok)
  77. assert.True(t, len(items) <= 20, "default pageSize should be 20")
  78. }
  79. // TC-0072: page负值
  80. func TestProductList_NegativePage_DefaultsTo1(t *testing.T) {
  81. ctx := context.Background()
  82. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  83. logic := NewProductListLogic(ctx, svcCtx)
  84. resp, err := logic.ProductList(&types.ProductListReq{Page: -1, PageSize: 10})
  85. require.NoError(t, err)
  86. require.NotNil(t, resp)
  87. }
  88. // TC-0075: 非超管AppKey隐藏
  89. func TestProductList_NonSuperAdminAppKeyHidden(t *testing.T) {
  90. ctx := ctxhelper.MemberCtx("test_product")
  91. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  92. conn := testutil.GetTestSqlConn()
  93. now := time.Now().Unix()
  94. code := testutil.UniqueId()
  95. result, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
  96. Code: code, Name: "appkey_test", AppKey: "secret_key_" + code, AppSecret: "s_" + code,
  97. Status: 1, CreateTime: now, UpdateTime: now,
  98. })
  99. require.NoError(t, err)
  100. id, _ := result.LastInsertId()
  101. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product`", id) })
  102. logic := NewProductListLogic(ctx, svcCtx)
  103. resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 100})
  104. require.NoError(t, err)
  105. items, ok := resp.List.([]types.ProductItem)
  106. require.True(t, ok)
  107. for _, item := range items {
  108. if item.Id == id {
  109. assert.Empty(t, item.AppKey, "非超管不应看到AppKey")
  110. return
  111. }
  112. }
  113. t.Fatal("未找到插入的测试产品")
  114. }
  115. // TC-0076: 超管可见AppKey
  116. func TestProductList_SuperAdminAppKeyVisible(t *testing.T) {
  117. ctx := ctxhelper.SuperAdminCtx()
  118. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  119. conn := testutil.GetTestSqlConn()
  120. now := time.Now().Unix()
  121. code := testutil.UniqueId()
  122. result, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
  123. Code: code, Name: "appkey_test_sa", AppKey: "visible_key_" + code, AppSecret: "s_" + code,
  124. Status: 1, CreateTime: now, UpdateTime: now,
  125. })
  126. require.NoError(t, err)
  127. id, _ := result.LastInsertId()
  128. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product`", id) })
  129. logic := NewProductListLogic(ctx, svcCtx)
  130. resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 100})
  131. require.NoError(t, err)
  132. items, ok := resp.List.([]types.ProductItem)
  133. require.True(t, ok)
  134. for _, item := range items {
  135. if item.Id == id {
  136. assert.Equal(t, "visible_key_"+code, item.AppKey, "超管应能看到AppKey")
  137. return
  138. }
  139. }
  140. t.Fatal("未找到插入的测试产品")
  141. }