159 lines
3.8 KiB
Go
159 lines
3.8 KiB
Go
|
package accountlogic
|
||
|
|
||
|
import (
|
||
|
"app-cloudep-member-server/gen_result/pb/member"
|
||
|
libMock "app-cloudep-member-server/internal/mock/lib"
|
||
|
modelMock "app-cloudep-member-server/internal/mock/model"
|
||
|
usecaseMock "app-cloudep-member-server/internal/mock/usecase"
|
||
|
"app-cloudep-member-server/internal/svc"
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/alicebob/miniredis/v2"
|
||
|
"github.com/go-sql-driver/mysql"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
||
|
"go.uber.org/mock/gomock"
|
||
|
)
|
||
|
|
||
|
func TestCreateUserAccountLogic_CreateUserAccount(t *testing.T) {
|
||
|
// mock
|
||
|
ctrl := gomock.NewController(t)
|
||
|
defer ctrl.Finish()
|
||
|
|
||
|
modelAccountModel := modelMock.NewMockAccountModel(ctrl)
|
||
|
mockGenUIDModel := usecaseMock.NewMockUIDGenerateUseCase(ctrl)
|
||
|
mockValidate := libMock.NewMockValidate(ctrl)
|
||
|
|
||
|
r1, err := miniredis.Run()
|
||
|
assert.NoError(t, err)
|
||
|
defer r1.Close()
|
||
|
|
||
|
newRedis, err := redis.NewRedis(redis.RedisConf{
|
||
|
Host: r1.Addr(),
|
||
|
Type: redis.ClusterType,
|
||
|
Pass: "",
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
sc := svc.ServiceContext{
|
||
|
AccountModel: modelAccountModel,
|
||
|
GenUIDUseCase: mockGenUIDModel,
|
||
|
Redis: *newRedis,
|
||
|
Validate: mockValidate,
|
||
|
}
|
||
|
|
||
|
l := NewCreateUserAccountLogic(context.Background(), &sc)
|
||
|
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
input *member.CreateLoginUserReq
|
||
|
setupMocks func()
|
||
|
expectError bool
|
||
|
expected *member.OKResp
|
||
|
}{
|
||
|
{
|
||
|
name: "ok",
|
||
|
input: &member.CreateLoginUserReq{
|
||
|
LoginId: "test_user",
|
||
|
Platform: 1,
|
||
|
Token: "password123",
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(nil)
|
||
|
modelAccountModel.EXPECT().
|
||
|
Insert(gomock.Any(), gomock.Any()).
|
||
|
Return(nil, nil)
|
||
|
},
|
||
|
expectError: false,
|
||
|
expected: &member.OKResp{},
|
||
|
},
|
||
|
{
|
||
|
name: "validation failure",
|
||
|
input: &member.CreateLoginUserReq{
|
||
|
LoginId: "test_user",
|
||
|
Platform: 1,
|
||
|
Token: "password123",
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(fmt.Errorf("validation error"))
|
||
|
},
|
||
|
expectError: true,
|
||
|
},
|
||
|
{
|
||
|
name: "password hash failure",
|
||
|
input: &member.CreateLoginUserReq{
|
||
|
LoginId: "test_user",
|
||
|
Platform: 1,
|
||
|
Token: "password123",
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(nil)
|
||
|
HasPasswordFunc = func(password string, cost int) (string, error) {
|
||
|
return "", fmt.Errorf("hash error")
|
||
|
}
|
||
|
},
|
||
|
expectError: true,
|
||
|
},
|
||
|
{
|
||
|
name: "duplicate entry error",
|
||
|
input: &member.CreateLoginUserReq{
|
||
|
LoginId: "test_user",
|
||
|
Platform: 1,
|
||
|
Token: "password123",
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(nil)
|
||
|
HasPasswordFunc = func(password string, cost int) (string, error) {
|
||
|
return "hashedpassword", nil
|
||
|
}
|
||
|
modelAccountModel.EXPECT().
|
||
|
Insert(gomock.Any(), gomock.Any()).
|
||
|
Return(nil, &mysql.MySQLError{Number: 1062, Message: "Duplicate entry"})
|
||
|
},
|
||
|
expectError: true,
|
||
|
},
|
||
|
{
|
||
|
name: "database error",
|
||
|
input: &member.CreateLoginUserReq{
|
||
|
LoginId: "test_user",
|
||
|
Platform: 1,
|
||
|
Token: "password123",
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(nil)
|
||
|
HasPasswordFunc = func(password string, cost int) (string, error) {
|
||
|
return "hashedpassword", nil
|
||
|
}
|
||
|
modelAccountModel.EXPECT().
|
||
|
Insert(gomock.Any(), gomock.Any()).
|
||
|
Return(nil, fmt.Errorf("database error"))
|
||
|
},
|
||
|
expectError: true,
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
tt.setupMocks()
|
||
|
resp, err := l.CreateUserAccount(tt.input)
|
||
|
if tt.expectError {
|
||
|
assert.Error(t, err)
|
||
|
} else {
|
||
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, tt.expected, resp)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|