Skip to content

Commit

Permalink
test create user rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Jan 15, 2024
1 parent 735d8e8 commit 78d6586
Showing 1 changed file with 82 additions and 3 deletions.
85 changes: 82 additions & 3 deletions gapi/rpc_create_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gapi

import (
"context"
"database/sql"
"fmt"
"reflect"
"testing"
Expand All @@ -14,6 +15,8 @@ import (
mockwk "github.com/aradwann/eenergy/worker/mock"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type eqCreateUserTxParamsMatcher struct {
Expand Down Expand Up @@ -64,7 +67,7 @@ func randomUser(t *testing.T) (user db.User, password string) {
return
}

func TestCreateUserRPC(t *testing.T) {
func TestCreateUserAPI(t *testing.T) {
user, password := randomUser(t)

testCases := []struct {
Expand Down Expand Up @@ -111,8 +114,85 @@ func TestCreateUserRPC(t *testing.T) {
require.Equal(t, user.Email, createdUser.Email)
},
},
{
name: "InternalError",
req: &pb.CreateUserRequest{
Username: user.Username,
Password: password,
FullName: user.FullName,
Email: user.Email,
},
buildStubs: func(store *mockdb.MockStore, taskDistributor *mockwk.MockTaskDistributor) {
store.EXPECT().
CreateUserTx(gomock.Any(), gomock.Any()).
Times(1).
Return(db.CreateUserTxResult{}, sql.ErrConnDone)

taskDistributor.EXPECT().
DistributeTaskSendVerifyEmail(gomock.Any(), gomock.Any(), gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, res *pb.CreateUserResponse, err error) {
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
require.Equal(t, codes.Internal, st.Code())
},
},
{
name: "DuplicateUsername",
req: &pb.CreateUserRequest{
Username: user.Username,
Password: password,
FullName: user.FullName,
Email: user.Email,
},
buildStubs: func(store *mockdb.MockStore, taskDistributor *mockwk.MockTaskDistributor) {
store.EXPECT().
CreateUserTx(gomock.Any(), gomock.Any()).
Times(1).
Return(db.CreateUserTxResult{}, db.ErrUniqueViolation)

taskDistributor.EXPECT().
DistributeTaskSendVerifyEmail(gomock.Any(), gomock.Any(), gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, res *pb.CreateUserResponse, err error) {
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
require.Equal(t, codes.AlreadyExists, st.Code())
},
},
{
name: "InvalidEmail",
req: &pb.CreateUserRequest{
Username: user.Username,
Password: password,
FullName: user.FullName,
Email: "invalid-email",
},
buildStubs: func(store *mockdb.MockStore, taskDistributor *mockwk.MockTaskDistributor) {
store.EXPECT().
CreateUserTx(gomock.Any(), gomock.Any()).
Times(0)

taskDistributor.EXPECT().
DistributeTaskSendVerifyEmail(gomock.Any(), gomock.Any(), gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, res *pb.CreateUserResponse, err error) {
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
require.Equal(t, codes.InvalidArgument, st.Code())
},
},
}
for _, tc := range testCases {

for i := range testCases {
tc := testCases[i]

t.Run(tc.name, func(t *testing.T) {
storeCtrl := gomock.NewController(t)
defer storeCtrl.Finish()
Expand All @@ -127,7 +207,6 @@ func TestCreateUserRPC(t *testing.T) {

res, err := server.CreateUser(context.Background(), tc.req)
tc.checkResponse(t, res, err)

})
}
}

0 comments on commit 78d6586

Please sign in to comment.