| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- package product
- import (
- "context"
- "testing"
- "time"
- productModel "perms-system-server/internal/model/product"
- "perms-system-server/internal/svc"
- "perms-system-server/internal/testutil"
- "perms-system-server/internal/testutil/ctxhelper"
- "perms-system-server/internal/types"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- // TC-0068: 正常分页
- func TestProductList_Normal(t *testing.T) {
- ctx := context.Background()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- conn := testutil.GetTestSqlConn()
- now := time.Now().Unix()
- var ids []int64
- for i := 0; i < 3; i++ {
- code := testutil.UniqueId()
- result, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
- Code: code, Name: "列表测试产品", AppKey: "k_" + code, AppSecret: "s_" + code,
- Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- id, _ := result.LastInsertId()
- ids = append(ids, id)
- }
- t.Cleanup(func() {
- testutil.CleanTable(ctx, conn, "`sys_product`", ids...)
- })
- logic := NewProductListLogic(ctx, svcCtx)
- resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 10})
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.True(t, resp.Total >= 3)
- items, ok := resp.List.([]types.ProductItem)
- require.True(t, ok)
- assert.True(t, len(items) >= 3)
- }
- // TC-0069: 默认分页
- func TestProductList_DefaultPagination(t *testing.T) {
- ctx := context.Background()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- logic := NewProductListLogic(ctx, svcCtx)
- resp, err := logic.ProductList(&types.ProductListReq{Page: 0, PageSize: 0})
- require.NoError(t, err)
- require.NotNil(t, resp)
- items, ok := resp.List.([]types.ProductItem)
- require.True(t, ok)
- assert.True(t, len(items) <= 20, "default pageSize should be 20")
- }
- // TC-0070: pageSize超过上限
- func TestProductList_PageSizeExceedsLimit(t *testing.T) {
- ctx := context.Background()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- logic := NewProductListLogic(ctx, svcCtx)
- resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 500})
- require.NoError(t, err)
- require.NotNil(t, resp)
- items, ok := resp.List.([]types.ProductItem)
- require.True(t, ok)
- assert.True(t, len(items) <= 100, "pageSize should be capped at 100")
- }
- // TC-0071: pageSize=0
- func TestProductList_PageSizeZero_DefaultsTo20(t *testing.T) {
- ctx := context.Background()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- logic := NewProductListLogic(ctx, svcCtx)
- resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 0})
- require.NoError(t, err)
- require.NotNil(t, resp)
- items, ok := resp.List.([]types.ProductItem)
- require.True(t, ok)
- assert.True(t, len(items) <= 20, "default pageSize should be 20")
- }
- // TC-0072: page负值
- func TestProductList_NegativePage_DefaultsTo1(t *testing.T) {
- ctx := context.Background()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- logic := NewProductListLogic(ctx, svcCtx)
- resp, err := logic.ProductList(&types.ProductListReq{Page: -1, PageSize: 10})
- require.NoError(t, err)
- require.NotNil(t, resp)
- }
- // TC-0075: 非超管AppKey隐藏
- func TestProductList_NonSuperAdminAppKeyHidden(t *testing.T) {
- ctx := ctxhelper.MemberCtx("test_product")
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- conn := testutil.GetTestSqlConn()
- now := time.Now().Unix()
- code := testutil.UniqueId()
- result, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
- Code: code, Name: "appkey_test", AppKey: "secret_key_" + code, AppSecret: "s_" + code,
- Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- id, _ := result.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product`", id) })
- logic := NewProductListLogic(ctx, svcCtx)
- resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 100})
- require.NoError(t, err)
- items, ok := resp.List.([]types.ProductItem)
- require.True(t, ok)
- for _, item := range items {
- if item.Id == id {
- assert.Empty(t, item.AppKey, "非超管不应看到AppKey")
- return
- }
- }
- t.Fatal("未找到插入的测试产品")
- }
- // TC-0076: 超管可见AppKey
- func TestProductList_SuperAdminAppKeyVisible(t *testing.T) {
- ctx := ctxhelper.SuperAdminCtx()
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- conn := testutil.GetTestSqlConn()
- now := time.Now().Unix()
- code := testutil.UniqueId()
- result, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
- Code: code, Name: "appkey_test_sa", AppKey: "visible_key_" + code, AppSecret: "s_" + code,
- Status: 1, CreateTime: now, UpdateTime: now,
- })
- require.NoError(t, err)
- id, _ := result.LastInsertId()
- t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product`", id) })
- logic := NewProductListLogic(ctx, svcCtx)
- resp, err := logic.ProductList(&types.ProductListReq{Page: 1, PageSize: 100})
- require.NoError(t, err)
- items, ok := resp.List.([]types.ProductItem)
- require.True(t, ok)
- for _, item := range items {
- if item.Id == id {
- assert.Equal(t, "visible_key_"+code, item.AppKey, "超管应能看到AppKey")
- return
- }
- }
- t.Fatal("未找到插入的测试产品")
- }
|