- 添加个人客户微信登录和手机验证码登录接口 - 实现个人客户设备、ICCID、手机号关联管理 - 添加短信发送服务(HTTP 客户端) - 添加微信认证服务(含 mock 实现) - 添加 JWT Token 生成和验证工具 - 创建数据库迁移脚本(personal_customer 关联表) - 修复测试文件中的路由注册参数错误 - 重构 scripts 目录结构(分离独立脚本到子目录) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
321 lines
9.2 KiB
Go
321 lines
9.2 KiB
Go
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/store/postgres"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
|
)
|
|
|
|
// TestPersonalCustomerStore_Create 测试创建个人客户
|
|
func TestPersonalCustomerStore_Create(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
tests := []struct {
|
|
name string
|
|
customer *model.PersonalCustomer
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "创建基本个人客户",
|
|
customer: &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_test_a",
|
|
WxUnionID: "wx_unionid_test_a",
|
|
Nickname: "测试用户A",
|
|
Status: constants.StatusEnabled,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "创建带微信信息的个人客户",
|
|
customer: &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_123456",
|
|
WxUnionID: "wx_unionid_abcdef",
|
|
Nickname: "测试用户B",
|
|
AvatarURL: "https://example.com/avatar.jpg",
|
|
Status: constants.StatusEnabled,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := store.Create(ctx, tt.customer)
|
|
if tt.wantErr {
|
|
assert.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, tt.customer.ID)
|
|
assert.NotZero(t, tt.customer.CreatedAt)
|
|
assert.NotZero(t, tt.customer.UpdatedAt)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestPersonalCustomerStore_GetByID 测试根据 ID 查询个人客户
|
|
func TestPersonalCustomerStore_GetByID(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试客户
|
|
customer := &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_test_getbyid",
|
|
WxUnionID: "wx_unionid_test_getbyid",
|
|
Nickname: "测试客户",
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("查询存在的客户", func(t *testing.T) {
|
|
found, err := store.GetByID(ctx, customer.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, customer.WxOpenID, found.WxOpenID)
|
|
assert.Equal(t, customer.Nickname, found.Nickname)
|
|
})
|
|
|
|
t.Run("查询不存在的客户", func(t *testing.T) {
|
|
_, err := store.GetByID(ctx, 99999)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestPersonalCustomerStore_GetByPhone 测试根据手机号查询
|
|
func TestPersonalCustomerStore_GetByPhone(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试客户
|
|
customer := &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_test_phone",
|
|
WxUnionID: "wx_unionid_test_phone",
|
|
Nickname: "测试客户",
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
// 创建手机号绑定记录
|
|
customerPhone := &model.PersonalCustomerPhone{
|
|
CustomerID: customer.ID,
|
|
Phone: "13800000001",
|
|
IsPrimary: true,
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err = db.Create(customerPhone).Error
|
|
require.NoError(t, err)
|
|
|
|
t.Run("根据手机号查询", func(t *testing.T) {
|
|
found, err := store.GetByPhone(ctx, "13800000001")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, customer.ID, found.ID)
|
|
assert.Equal(t, customer.Nickname, found.Nickname)
|
|
})
|
|
|
|
t.Run("查询不存在的手机号", func(t *testing.T) {
|
|
_, err := store.GetByPhone(ctx, "99900000000")
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestPersonalCustomerStore_GetByWxOpenID 测试根据微信 OpenID 查询
|
|
func TestPersonalCustomerStore_GetByWxOpenID(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试客户
|
|
customer := &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_unique",
|
|
WxUnionID: "wx_unionid_unique",
|
|
Nickname: "测试客户",
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("根据微信OpenID查询", func(t *testing.T) {
|
|
found, err := store.GetByWxOpenID(ctx, "wx_openid_unique")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, customer.ID, found.ID)
|
|
assert.Equal(t, customer.WxOpenID, found.WxOpenID)
|
|
})
|
|
|
|
t.Run("查询不存在的OpenID", func(t *testing.T) {
|
|
_, err := store.GetByWxOpenID(ctx, "nonexistent_openid")
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestPersonalCustomerStore_Update 测试更新个人客户
|
|
func TestPersonalCustomerStore_Update(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试客户
|
|
customer := &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_test_update",
|
|
WxUnionID: "wx_unionid_test_update",
|
|
Nickname: "原昵称",
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("更新客户信息", func(t *testing.T) {
|
|
customer.Nickname = "新昵称"
|
|
customer.AvatarURL = "https://example.com/new_avatar.jpg"
|
|
|
|
err := store.Update(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
// 验证更新
|
|
found, err := store.GetByID(ctx, customer.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "新昵称", found.Nickname)
|
|
assert.Equal(t, "https://example.com/new_avatar.jpg", found.AvatarURL)
|
|
})
|
|
|
|
t.Run("绑定微信信息", func(t *testing.T) {
|
|
customer.WxOpenID = "wx_openid_new"
|
|
customer.WxUnionID = "wx_unionid_new"
|
|
err := store.Update(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
found, err := store.GetByID(ctx, customer.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "wx_openid_new", found.WxOpenID)
|
|
assert.Equal(t, "wx_unionid_new", found.WxUnionID)
|
|
})
|
|
|
|
t.Run("更新客户状态", func(t *testing.T) {
|
|
customer.Status = constants.StatusDisabled
|
|
err := store.Update(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
found, err := store.GetByID(ctx, customer.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, constants.StatusDisabled, found.Status)
|
|
})
|
|
}
|
|
|
|
// TestPersonalCustomerStore_Delete 测试软删除个人客户
|
|
func TestPersonalCustomerStore_Delete(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试客户
|
|
customer := &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_test_delete",
|
|
WxUnionID: "wx_unionid_test_delete",
|
|
Nickname: "待删除客户",
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("软删除客户", func(t *testing.T) {
|
|
err := store.Delete(ctx, customer.ID)
|
|
require.NoError(t, err)
|
|
|
|
// 验证已被软删除
|
|
_, err = store.GetByID(ctx, customer.ID)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestPersonalCustomerStore_List 测试查询客户列表
|
|
func TestPersonalCustomerStore_List(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建多个测试客户
|
|
for i := 1; i <= 5; i++ {
|
|
customer := &model.PersonalCustomer{
|
|
WxOpenID: testutils.GenerateUsername("wx_openid_list_", i),
|
|
WxUnionID: testutils.GenerateUsername("wx_unionid_list_", i),
|
|
Nickname: testutils.GenerateUsername("客户", i),
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, customer)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
t.Run("分页查询", func(t *testing.T) {
|
|
customers, total, err := store.List(ctx, nil, nil)
|
|
require.NoError(t, err)
|
|
assert.GreaterOrEqual(t, len(customers), 5)
|
|
assert.GreaterOrEqual(t, total, int64(5))
|
|
})
|
|
|
|
t.Run("带过滤条件查询", func(t *testing.T) {
|
|
filters := map[string]interface{}{
|
|
"status": constants.StatusEnabled,
|
|
}
|
|
customers, _, err := store.List(ctx, nil, filters)
|
|
require.NoError(t, err)
|
|
for _, c := range customers {
|
|
assert.Equal(t, constants.StatusEnabled, c.Status)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestPersonalCustomerStore_UniqueConstraints 测试唯一约束
|
|
func TestPersonalCustomerStore_UniqueConstraints(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试客户
|
|
customer := &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_unique_test",
|
|
WxUnionID: "wx_unionid_unique_test",
|
|
Nickname: "唯一测试客户",
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, customer)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("重复微信OpenID应失败", func(t *testing.T) {
|
|
duplicate := &model.PersonalCustomer{
|
|
WxOpenID: "wx_openid_unique_test", // 重复
|
|
WxUnionID: "wx_unionid_different",
|
|
Nickname: "另一个客户",
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
err := store.Create(ctx, duplicate)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|