refac: password and code hashing & checking go through a domain hasher interface used by the use cases instead of bcrypt in the user repository and the auth handler
This commit is contained in:
1 parent
8460a078e3
commit
ff8cd5f0ec
16 files changed
+725
-88
No files matched your search
+21
-2
@@ -3,12 +3,31 @@ package test
|
||||
import (
|
||||
"RenewCMS/internal/domain/user"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"errors"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type stubHasher struct {
|
||||
hashErr error
|
||||
}
|
||||
|
||||
func (s *stubHasher) Hash(value string) (string, error) {
|
||||
if s.hashErr != nil {
|
||||
return "", s.hashErr
|
||||
}
|
||||
return "hashed:" + value, nil
|
||||
}
|
||||
|
||||
func (s *stubHasher) Compare(hashed string, value string) error {
|
||||
if hashed == "hashed:"+value {
|
||||
return nil
|
||||
}
|
||||
return errors.New("hash mismatch")
|
||||
}
|
||||
|
||||
type mockUserRepo struct {
|
||||
users map[uint32]user.User
|
||||
nextID uint32
|
||||
@@ -134,7 +153,7 @@ var _ user.Repository = &mockUserRepo{}
|
||||
|
||||
func TestCreateUser(t *testing.T) {
|
||||
repo := newMockUserRepo()
|
||||
uc := useCases.NewCreateUserUseCase(repo)
|
||||
uc := useCases.NewCreateUserUseCase(repo, &stubHasher{})
|
||||
|
||||
cmd := useCases.CreateUserCommand{
|
||||
Username: "testuser",
|
||||
@@ -157,7 +176,7 @@ func TestCreateUser(t *testing.T) {
|
||||
func TestUpdateUserVerification(t *testing.T) {
|
||||
repo := newMockUserRepo()
|
||||
repo.users[1] = user.User{ID: 1, IsVerified: false}
|
||||
uc := useCases.NewUpdateUserUseCase(repo)
|
||||
uc := useCases.NewUpdateUserUseCase(repo, &stubHasher{})
|
||||
|
||||
res, err := uc.UpdateVerificationStatus(1, true)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user