Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,16 @@ func (s Service) UpdateByEmail(ctx context.Context, toUpdate User) (User, error)
}

func (s Service) Enable(ctx context.Context, id string) error {
if !utils.IsValidUUID(id) {
return ErrInvalidID
}
return s.repository.SetState(ctx, id, Enabled)
}

func (s Service) Disable(ctx context.Context, id string) error {
if !utils.IsValidUUID(id) {
return ErrInvalidID
}
return s.repository.SetState(ctx, id, Disabled)
}

Expand Down
122 changes: 122 additions & 0 deletions core/user/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,128 @@ func TestService_Update(t *testing.T) {
}
}

func TestService_Disable(t *testing.T) {
validID := uuid.New().String()
tests := []struct {
name string
id string
setup func() *user.Service
wantErr error
}{
{
name: "disable user with valid uuid",
id: validID,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
repo.EXPECT().SetState(mock.Anything, validID, user.Disabled).Return(nil)
return user.NewService(repo, relationService, policyService, roleService)
},
},
{
name: "return invalid id error for non-uuid string",
id: "not-a-uuid",
wantErr: user.ErrInvalidID,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
return user.NewService(repo, relationService, policyService, roleService)
},
},
{
name: "return invalid id error for empty string",
id: "",
wantErr: user.ErrInvalidID,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
return user.NewService(repo, relationService, policyService, roleService)
},
},
{
name: "return not exist error if user not found",
id: validID,
wantErr: user.ErrNotExist,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
repo.EXPECT().SetState(mock.Anything, validID, user.Disabled).Return(user.ErrNotExist)
return user.NewService(repo, relationService, policyService, roleService)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := tt.setup()
err := s.Disable(context.Background(), tt.id)
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Errorf("Disable() error = %v, wantErr %v", err, tt.wantErr)
}
} else if err != nil {
t.Errorf("Disable() unexpected error = %v", err)
}
})
}
}

func TestService_Enable(t *testing.T) {
validID := uuid.New().String()
tests := []struct {
name string
id string
setup func() *user.Service
wantErr error
}{
{
name: "enable user with valid uuid",
id: validID,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
repo.EXPECT().SetState(mock.Anything, validID, user.Enabled).Return(nil)
return user.NewService(repo, relationService, policyService, roleService)
},
},
{
name: "return invalid id error for non-uuid string",
id: "not-a-uuid",
wantErr: user.ErrInvalidID,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
return user.NewService(repo, relationService, policyService, roleService)
},
},
{
name: "return invalid id error for empty string",
id: "",
wantErr: user.ErrInvalidID,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
return user.NewService(repo, relationService, policyService, roleService)
},
},
{
name: "return not exist error if user not found",
id: validID,
wantErr: user.ErrNotExist,
setup: func() *user.Service {
repo, relationService, policyService, roleService := mockService(t)
repo.EXPECT().SetState(mock.Anything, validID, user.Enabled).Return(user.ErrNotExist)
return user.NewService(repo, relationService, policyService, roleService)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := tt.setup()
err := s.Enable(context.Background(), tt.id)
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Errorf("Enable() error = %v, wantErr %v", err, tt.wantErr)
}
} else if err != nil {
t.Errorf("Enable() unexpected error = %v", err)
}
})
}
}

func TestService_Delete(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading