Skip to content

Commit

Permalink
test: add fetchcurrentuser test
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanarya0 committed Apr 21, 2024
1 parent 448fb15 commit 84c46a7
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions core/user/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,64 @@ func TestService_GetByID(t *testing.T) {
})
}
}

func TestService_FetchCurrentUser(t *testing.T) {
t.Parallel()

tests := []struct {
name string
email string
setup func(t *testing.T) *user.Service
urn string
want user.User
wantErr error
}{
{
name: "FetchCurrentUser",
email: "[email protected]",
setup: func(t *testing.T) *user.Service {
t.Helper()
repository := &mocks.Repository{}
activityService := &mocks.ActivityService{}
logger := shieldlogger.InitLogger(logger.Config{})
repository.EXPECT().
GetByEmail(mock.Anything, "[email protected]").
Return(user.User{
ID: "qwer-1234-tyui-5678-opas-90",
Name: "John Doe",
Email: "[email protected]"}, nil).Once()

activityService.EXPECT().
Log(mock.Anything, user.AuditKeyUserUpdate, "", user.UserLogData{Entity: "user", Name: "John Doe", Email: "[email protected]"}).Return(nil).Once()
return user.NewService(logger, repository, activityService)
},
want: user.User{
ID: "qwer-1234-tyui-5678-opas-90",
Name: "John Doe",
Email: "[email protected]",
},
wantErr: nil,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

assert.NotNil(t, svc)

ctx := user.SetContextWithEmail(context.Background(), tt.email)

got, err := svc.FetchCurrentUser(ctx)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 84c46a7

Please sign in to comment.