ark-member/internal/logic/bind_account_logic_test.go

180 lines
4.5 KiB
Go
Raw Permalink Normal View History

2024-08-02 02:10:25 +00:00
package logic
import (
"context"
"errors"
"fmt"
"member/gen_result/pb/member"
"member/internal/domain"
libMock "member/internal/mock/lib"
modelMock "member/internal/mock/model"
svcMock "member/internal/mock/svc"
"member/internal/model"
"member/internal/svc"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/bwmarrin/snowflake"
"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)
mockSnackFlow := svcMock.NewMockSnackFlow(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,
SnackFlowGen: mockSnackFlow,
Redis: *newRedis,
Validate: mockValidate,
}
l := NewBindAccountLogic(context.Background(), &sc)
tests := []struct {
name string
input *member.BindingUserReq
setupMocks func()
expectError bool
expected *member.Response
}{
{
name: "ok",
input: &member.BindingUserReq{
LoginId: "cat1@30cm.net",
Uid: "12345",
},
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.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
},
},
{
name: "valid account without UID",
input: &member.BindingUserReq{
LoginId: "cat1@30cm.net",
Uid: "",
},
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)
mockSnackFlow.EXPECT().
Generate().
Return(snowflake.ID(202408013345678))
mockAccountToUidModel.EXPECT().
Insert(gomock.Any(), &model.AccountToUid{Account: "cat1@30cm.net", Uid: "202408013345678"}).
Return(nil, nil)
},
expectError: false,
expected: &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
},
},
{
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: "202408013345679",
},
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: "202408013345679"}).
Return(nil, errors.New("insert failed"))
},
expectError: true,
},
{
name: "failed to validate param",
input: &member.BindingUserReq{
LoginId: "cat3",
Uid: "202408013345679",
},
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)
}
})
}
}