package unit import ( "context" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/break/junhong_cmp_fiber/internal/model" "github.com/break/junhong_cmp_fiber/internal/service/customer_account" "github.com/break/junhong_cmp_fiber/internal/store/postgres" "github.com/break/junhong_cmp_fiber/pkg/constants" "github.com/break/junhong_cmp_fiber/tests/testutils" ) func createCustomerAccountTestContext(userID uint) context.Context { ctx := context.Background() ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID) ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform) return ctx } func TestCustomerAccountService_List(t *testing.T) { db, redisClient := testutils.SetupTestDB(t) defer testutils.TeardownTestDB(t, db, redisClient) accountStore := postgres.NewAccountStore(db, redisClient) shopStore := postgres.NewShopStore(db, redisClient) enterpriseStore := postgres.NewEnterpriseStore(db, redisClient) service := customer_account.New(db, accountStore, shopStore, enterpriseStore) t.Run("查询账号列表-空结果", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) req := &model.CustomerAccountListReq{ Page: 1, PageSize: 20, } result, err := service.List(ctx, req) require.NoError(t, err) assert.NotNil(t, result) assert.GreaterOrEqual(t, result.Total, int64(0)) }) t.Run("查询账号列表-按用户名筛选", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "列表测试店铺", ShopCode: "SHOP_LIST_001", Level: 1, ContactName: "联系人", ContactPhone: "13800000001", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) createReq := &model.CreateCustomerAccountReq{ Username: "测试账号用户", Phone: "13900000001", Password: "Test123456", ShopID: shop.ID, } _, err = service.Create(ctx, createReq) require.NoError(t, err) req := &model.CustomerAccountListReq{ Page: 1, PageSize: 20, Username: "测试账号", } result, err := service.List(ctx, req) require.NoError(t, err) assert.NotNil(t, result) assert.GreaterOrEqual(t, result.Total, int64(1)) }) t.Run("查询账号列表-按店铺筛选", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "筛选测试店铺", ShopCode: "SHOP_FILTER_001", Level: 1, ContactName: "联系人", ContactPhone: "13800000002", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) createReq := &model.CreateCustomerAccountReq{ Username: "店铺筛选账号", Phone: "13900000002", Password: "Test123456", ShopID: shop.ID, } _, err = service.Create(ctx, createReq) require.NoError(t, err) req := &model.CustomerAccountListReq{ Page: 1, PageSize: 20, ShopID: &shop.ID, } result, err := service.List(ctx, req) require.NoError(t, err) assert.NotNil(t, result) assert.GreaterOrEqual(t, result.Total, int64(1)) }) } func TestCustomerAccountService_Create(t *testing.T) { db, redisClient := testutils.SetupTestDB(t) defer testutils.TeardownTestDB(t, db, redisClient) accountStore := postgres.NewAccountStore(db, redisClient) shopStore := postgres.NewShopStore(db, redisClient) enterpriseStore := postgres.NewEnterpriseStore(db, redisClient) service := customer_account.New(db, accountStore, shopStore, enterpriseStore) t.Run("新增代理商账号", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "新增账号测试店铺", ShopCode: "SHOP_CREATE_001", Level: 1, ContactName: "联系人", ContactPhone: "13800000010", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) req := &model.CreateCustomerAccountReq{ Username: "新代理账号", Phone: "13900000010", Password: "Test123456", ShopID: shop.ID, } result, err := service.Create(ctx, req) require.NoError(t, err) assert.NotNil(t, result) assert.Equal(t, "新代理账号", result.Username) assert.Equal(t, "13900000010", result.Phone) assert.Equal(t, constants.UserTypeAgent, result.UserType) assert.Equal(t, constants.StatusEnabled, result.Status) }) t.Run("新增账号-手机号已存在应失败", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "手机号测试店铺", ShopCode: "SHOP_CREATE_002", Level: 1, ContactName: "联系人", ContactPhone: "13800000011", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) req1 := &model.CreateCustomerAccountReq{ Username: "账号一", Phone: "13900000011", Password: "Test123456", ShopID: shop.ID, } _, err = service.Create(ctx, req1) require.NoError(t, err) req2 := &model.CreateCustomerAccountReq{ Username: "账号二", Phone: "13900000011", Password: "Test123456", ShopID: shop.ID, } _, err = service.Create(ctx, req2) assert.Error(t, err) }) t.Run("新增账号-店铺不存在应失败", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) req := &model.CreateCustomerAccountReq{ Username: "无效店铺账号", Phone: "13900000012", Password: "Test123456", ShopID: 99999, } _, err := service.Create(ctx, req) assert.Error(t, err) }) t.Run("新增账号-未授权用户应失败", func(t *testing.T) { ctx := context.Background() req := &model.CreateCustomerAccountReq{ Username: "未授权账号", Phone: "13900000013", Password: "Test123456", ShopID: 1, } _, err := service.Create(ctx, req) assert.Error(t, err) }) } func TestCustomerAccountService_Update(t *testing.T) { db, redisClient := testutils.SetupTestDB(t) defer testutils.TeardownTestDB(t, db, redisClient) accountStore := postgres.NewAccountStore(db, redisClient) shopStore := postgres.NewShopStore(db, redisClient) enterpriseStore := postgres.NewEnterpriseStore(db, redisClient) service := customer_account.New(db, accountStore, shopStore, enterpriseStore) t.Run("编辑账号", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "编辑账号测试店铺", ShopCode: "SHOP_UPDATE_001", Level: 1, ContactName: "联系人", ContactPhone: "13800000020", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) createReq := &model.CreateCustomerAccountReq{ Username: "待编辑账号", Phone: "13900000020", Password: "Test123456", ShopID: shop.ID, } created, err := service.Create(ctx, createReq) require.NoError(t, err) newName := "编辑后账号" updateReq := &model.UpdateCustomerAccountReq{ Username: &newName, } updated, err := service.Update(ctx, created.ID, updateReq) require.NoError(t, err) assert.Equal(t, "编辑后账号", updated.Username) }) t.Run("编辑账号-不存在应失败", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) newName := "不存在账号" updateReq := &model.UpdateCustomerAccountReq{ Username: &newName, } _, err := service.Update(ctx, 99999, updateReq) assert.Error(t, err) }) } func TestCustomerAccountService_UpdatePassword(t *testing.T) { db, redisClient := testutils.SetupTestDB(t) defer testutils.TeardownTestDB(t, db, redisClient) accountStore := postgres.NewAccountStore(db, redisClient) shopStore := postgres.NewShopStore(db, redisClient) enterpriseStore := postgres.NewEnterpriseStore(db, redisClient) service := customer_account.New(db, accountStore, shopStore, enterpriseStore) t.Run("修改密码", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "密码测试店铺", ShopCode: "SHOP_PWD_001", Level: 1, ContactName: "联系人", ContactPhone: "13800000030", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) createReq := &model.CreateCustomerAccountReq{ Username: "密码测试账号", Phone: "13900000030", Password: "OldPass123", ShopID: shop.ID, } created, err := service.Create(ctx, createReq) require.NoError(t, err) err = service.UpdatePassword(ctx, created.ID, "NewPass456") require.NoError(t, err) var account model.Account err = db.First(&account, created.ID).Error require.NoError(t, err) assert.NotEqual(t, "OldPass123", account.Password) assert.NotEqual(t, "NewPass456", account.Password) }) t.Run("修改不存在账号密码应失败", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) err := service.UpdatePassword(ctx, 99999, "NewPass789") assert.Error(t, err) }) } func TestCustomerAccountService_UpdateStatus(t *testing.T) { db, redisClient := testutils.SetupTestDB(t) defer testutils.TeardownTestDB(t, db, redisClient) accountStore := postgres.NewAccountStore(db, redisClient) shopStore := postgres.NewShopStore(db, redisClient) enterpriseStore := postgres.NewEnterpriseStore(db, redisClient) service := customer_account.New(db, accountStore, shopStore, enterpriseStore) t.Run("禁用账号", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "状态测试店铺", ShopCode: "SHOP_STATUS_001", Level: 1, ContactName: "联系人", ContactPhone: "13800000040", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) createReq := &model.CreateCustomerAccountReq{ Username: "状态测试账号", Phone: "13900000040", Password: "Test123456", ShopID: shop.ID, } created, err := service.Create(ctx, createReq) require.NoError(t, err) err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled) require.NoError(t, err) var account model.Account err = db.First(&account, created.ID).Error require.NoError(t, err) assert.Equal(t, constants.StatusDisabled, account.Status) }) t.Run("启用账号", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) shop := &model.Shop{ ShopName: "启用测试店铺", ShopCode: "SHOP_STATUS_002", Level: 1, ContactName: "联系人", ContactPhone: "13800000041", Status: constants.StatusEnabled, } shop.Creator = 1 shop.Updater = 1 err := db.Create(shop).Error require.NoError(t, err) createReq := &model.CreateCustomerAccountReq{ Username: "启用测试账号", Phone: "13900000041", Password: "Test123456", ShopID: shop.ID, } created, err := service.Create(ctx, createReq) require.NoError(t, err) err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled) require.NoError(t, err) err = service.UpdateStatus(ctx, created.ID, constants.StatusEnabled) require.NoError(t, err) var account model.Account err = db.First(&account, created.ID).Error require.NoError(t, err) assert.Equal(t, constants.StatusEnabled, account.Status) }) t.Run("更新不存在账号状态应失败", func(t *testing.T) { ctx := createCustomerAccountTestContext(1) err := service.UpdateStatus(ctx, 99999, constants.StatusDisabled) assert.Error(t, err) }) }