lab2
This commit is contained in:
@@ -49,7 +49,7 @@ func (_m *MockRepository) EXPECT() *MockRepository_Expecter {
|
||||
}
|
||||
|
||||
// Create provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) Create(ctx context.Context, draft domain.UserDraft) (domain.User, error) {
|
||||
func (_mock *MockRepository) Create(ctx context.Context, draft domain.NewUser) (domain.User, error) {
|
||||
ret := _mock.Called(ctx, draft)
|
||||
|
||||
if len(ret) == 0 {
|
||||
@@ -58,15 +58,15 @@ func (_mock *MockRepository) Create(ctx context.Context, draft domain.UserDraft)
|
||||
|
||||
var r0 domain.User
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.UserDraft) (domain.User, error)); ok {
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.NewUser) (domain.User, error)); ok {
|
||||
return returnFunc(ctx, draft)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.UserDraft) domain.User); ok {
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.NewUser) domain.User); ok {
|
||||
r0 = returnFunc(ctx, draft)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.User)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, domain.UserDraft) error); ok {
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, domain.NewUser) error); ok {
|
||||
r1 = returnFunc(ctx, draft)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
@@ -81,20 +81,20 @@ type MockRepository_Create_Call struct {
|
||||
|
||||
// Create is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - draft domain.UserDraft
|
||||
// - draft domain.NewUser
|
||||
func (_e *MockRepository_Expecter) Create(ctx any, draft any) *MockRepository_Create_Call {
|
||||
return &MockRepository_Create_Call{Call: _e.mock.On("Create", ctx, draft)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Create_Call) Run(run func(ctx context.Context, draft domain.UserDraft)) *MockRepository_Create_Call {
|
||||
func (_c *MockRepository_Create_Call) Run(run func(ctx context.Context, draft domain.NewUser)) *MockRepository_Create_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 domain.UserDraft
|
||||
var arg1 domain.NewUser
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(domain.UserDraft)
|
||||
arg1 = args[1].(domain.NewUser)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
@@ -109,7 +109,7 @@ func (_c *MockRepository_Create_Call) Return(user domain.User, err error) *MockR
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Create_Call) RunAndReturn(run func(ctx context.Context, draft domain.UserDraft) (domain.User, error)) *MockRepository_Create_Call {
|
||||
func (_c *MockRepository_Create_Call) RunAndReturn(run func(ctx context.Context, draft domain.NewUser) (domain.User, error)) *MockRepository_Create_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
@@ -7,12 +7,11 @@ import (
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/platform/password"
|
||||
)
|
||||
|
||||
//go:generate go tool mockery
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, draft domain.UserDraft) (domain.User, error)
|
||||
Create(ctx context.Context, draft domain.NewUser) (domain.User, error)
|
||||
GetByID(ctx context.Context, id uuid.UUID) (domain.User, error)
|
||||
}
|
||||
|
||||
@@ -29,7 +28,16 @@ func (s *Service) Register(ctx context.Context, draft domain.UserDraft) (domain.
|
||||
return domain.User{}, err
|
||||
}
|
||||
|
||||
user, err := s.users.Create(ctx, draft)
|
||||
hash, err := password.Hash(draft.Password)
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("hash password: %w", err)
|
||||
}
|
||||
|
||||
user, err := s.users.Create(ctx, domain.NewUser{
|
||||
Username: draft.Username,
|
||||
DisplayName: draft.DisplayName,
|
||||
PasswordHash: hash,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("create user: %w", err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
@@ -34,8 +35,8 @@ func (s *ServiceSuite) SetupTest() {
|
||||
s.now = time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestRegisterReturnsStoredUser() {
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: "Алиса"}
|
||||
func (s *ServiceSuite) TestRegisterHashesPasswordAndReturnsStoredUser() {
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: "Алиса", Password: "very-secret-password"}
|
||||
stored := domain.User{
|
||||
ID: uuid.New(),
|
||||
Username: draft.Username,
|
||||
@@ -43,7 +44,15 @@ func (s *ServiceSuite) TestRegisterReturnsStoredUser() {
|
||||
CreatedAt: s.now,
|
||||
}
|
||||
|
||||
s.users.EXPECT().Create(s.T().Context(), draft).Return(stored, nil).Once()
|
||||
s.users.EXPECT().
|
||||
Create(s.T().Context(), mock.MatchedBy(func(candidate domain.NewUser) bool {
|
||||
return candidate.Username == draft.Username &&
|
||||
candidate.DisplayName == draft.DisplayName &&
|
||||
candidate.PasswordHash != "" &&
|
||||
candidate.PasswordHash != draft.Password
|
||||
})).
|
||||
Return(stored, nil).
|
||||
Once()
|
||||
|
||||
got, err := s.service.Register(s.T().Context(), draft)
|
||||
s.Require().NoError(err)
|
||||
@@ -51,16 +60,26 @@ func (s *ServiceSuite) TestRegisterReturnsStoredUser() {
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestRegisterRejectsBlankDisplayNameWithoutRepositoryCall() {
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: " "}
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: " ", Password: "very-secret-password"}
|
||||
|
||||
_, err := s.service.Register(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrValidation)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestRegisterRejectsShortPasswordWithoutRepositoryCall() {
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: "Алиса", Password: "too-short"}
|
||||
|
||||
_, err := s.service.Register(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrValidation)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestRegisterPropagatesUsernameConflict() {
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: "Алиса"}
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: "Алиса", Password: "very-secret-password"}
|
||||
|
||||
s.users.EXPECT().Create(s.T().Context(), draft).Return(domain.User{}, domain.ErrUsernameTaken).Once()
|
||||
s.users.EXPECT().
|
||||
Create(s.T().Context(), mock.Anything).
|
||||
Return(domain.User{}, domain.ErrUsernameTaken).
|
||||
Once()
|
||||
|
||||
_, err := s.service.Register(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrUsernameTaken)
|
||||
|
||||
Reference in New Issue
Block a user