app-cloudep-member-server/internal/logic/account/bind_account_logic_test.go

178 lines
4.7 KiB
Go
Raw Permalink Normal View History

2024-08-22 14:27:13 +00:00
package accountlogic
import (
"app-cloudep-member-server/gen_result/pb/member"
"app-cloudep-member-server/internal/domain"
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/model"
"app-cloudep-member-server/internal/svc"
"context"
"errors"
"fmt"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/stores/redis"
"go.uber.org/mock/gomock"
)
func TestBindAccountLogic_BindAccount(t *testing.T) {
// mock
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockAccountModel := modelMock.NewMockAccountModel(ctrl)
mockAccountToUidModel := modelMock.NewMockAccountToUidModel(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: mockAccountModel,
AccountToUidModel: mockAccountToUidModel,
GenUIDUseCase: mockGenUIDModel,
Redis: *newRedis,
Validate: mockValidate,
}
l := NewBindAccountLogic(context.Background(), &sc)
tests := []struct {
name string
input *member.BindingUserReq
setupMocks func()
expectError bool
expected *member.BindingUserResp
}{
{
name: "ok",
input: &member.BindingUserReq{
LoginId: "cat1@30cm.net",
Uid: "12345",
Type: domain.AccountTypeMail.ToInt64(),
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat1@30cm.net").
Return(&model.Account{Account: "cat1@30cm.net"}, nil)
mockAccountToUidModel.EXPECT().
Insert(gomock.Any(), gomock.Any()).
Return(nil, nil)
},
expectError: false,
expected: &member.BindingUserResp{
LoginId: "cat1@30cm.net",
Uid: "12345",
Type: domain.AccountTypeMail.ToInt64(),
},
},
{
name: "valid account without UID",
input: &member.BindingUserReq{
LoginId: "cat1@30cm.net",
Uid: "",
Type: domain.AccountTypeMail.ToInt64(),
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat1@30cm.net").
Return(&model.Account{Account: "cat1@30cm.net"}, nil)
mockGenUIDModel.EXPECT().
Generate(gomock.Any()).
Return("OOOOOOOD", nil)
mockAccountToUidModel.EXPECT().
Insert(gomock.Any(), &model.AccountToUid{Account: "cat1@30cm.net", Uid: "OOOOOOOD", Type: domain.AccountTypeMail.ToInt64()}).
Return(nil, nil)
},
expectError: false,
expected: &member.BindingUserResp{
LoginId: "cat1@30cm.net",
Uid: "OOOOOOOD",
Type: domain.AccountTypeMail.ToInt64(),
},
},
{
name: "account not found",
input: &member.BindingUserReq{
LoginId: "cat2@30cm.net",
Uid: "12345",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat2@30cm.net").
Return(nil, errors.New("account not found"))
},
expectError: true,
},
{
name: "insert account to UID failed",
input: &member.BindingUserReq{
LoginId: "cat2@30cm.net",
Uid: "OOOOOOJ",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat2@30cm.net").
Return(&model.Account{Account: "cat2@30cm.net"}, nil)
mockAccountToUidModel.EXPECT().
Insert(gomock.Any(), &model.AccountToUid{Account: "cat2@30cm.net", Uid: "OOOOOOJ"}).
Return(nil, errors.New("insert failed"))
},
expectError: true,
},
{
name: "failed to validate param",
input: &member.BindingUserReq{
LoginId: "cat3",
Uid: "OOOOOOOL",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(fmt.Errorf("invalid format: failed to get correct formate loginID "))
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupMocks()
resp, err := l.BindAccount(tt.input)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, resp)
}
})
}
}