refac: reamaining auth surface rewrite, T0
This commit is contained in:
1 parent
6d510d2805
commit
aa865af2e9
50 files changed
+3723
-149
No files matched your search
+64
-5
@@ -6,13 +6,17 @@ import (
|
||||
"errors"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type stubHasher struct {
|
||||
hashErr error
|
||||
hashErr error
|
||||
hashCalls int
|
||||
compareCalls int
|
||||
}
|
||||
|
||||
func (s *stubHasher) Hash(value string) (string, error) {
|
||||
s.hashCalls++
|
||||
if s.hashErr != nil {
|
||||
return "", s.hashErr
|
||||
}
|
||||
@@ -20,6 +24,7 @@ func (s *stubHasher) Hash(value string) (string, error) {
|
||||
}
|
||||
|
||||
func (s *stubHasher) Compare(hashed string, value string) error {
|
||||
s.compareCalls++
|
||||
if hashed == "hashed:"+value {
|
||||
return nil
|
||||
}
|
||||
@@ -27,15 +32,30 @@ func (s *stubHasher) Compare(hashed string, value string) error {
|
||||
}
|
||||
|
||||
type mockUserRepo struct {
|
||||
users map[uint32]user.User
|
||||
nextID uint32
|
||||
fail failSwitch
|
||||
users map[uint32]user.User
|
||||
nextID uint32
|
||||
fail failSwitch
|
||||
clearVerificationCodeCalls int
|
||||
clearVerificationCodeUserIds []uint32
|
||||
updatePasswordResetCodeCalls int
|
||||
lastUpdatePasswordResetCodeUserId uint32
|
||||
lastUpdatePasswordResetCodeCode string
|
||||
lastUpdatePasswordResetCodeExpiration time.Time
|
||||
resetPasswordCalls int
|
||||
lastResetPasswordUserId uint32
|
||||
lastResetPasswordHash string
|
||||
}
|
||||
|
||||
func newMockUserRepo() *mockUserRepo {
|
||||
return &mockUserRepo{users: make(map[uint32]user.User)}
|
||||
}
|
||||
|
||||
func newVerifiedUserRepo() *mockUserRepo {
|
||||
repo := newMockUserRepo()
|
||||
repo.users[1] = user.User{ID: 1, IsVerified: true}
|
||||
return repo
|
||||
}
|
||||
|
||||
func (m *mockUserRepo) sortedIds() []uint32 {
|
||||
ids := make([]uint32, 0, len(m.users))
|
||||
for id := range m.users {
|
||||
@@ -137,7 +157,11 @@ func (m *mockUserRepo) UpdatePassword(userId uint32, password string) (user.User
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (m *mockUserRepo) UpdatePasswordResetCode(userId uint32, code string) (user.User, error) {
|
||||
func (m *mockUserRepo) UpdatePasswordResetCode(userId uint32, code string, expiration time.Time) (user.User, error) {
|
||||
m.updatePasswordResetCodeCalls++
|
||||
m.lastUpdatePasswordResetCodeUserId = userId
|
||||
m.lastUpdatePasswordResetCodeCode = code
|
||||
m.lastUpdatePasswordResetCodeExpiration = expiration
|
||||
if err := m.fail.trigger("UpdatePasswordResetCode"); err != nil {
|
||||
return user.User{}, err
|
||||
}
|
||||
@@ -146,6 +170,41 @@ func (m *mockUserRepo) UpdatePasswordResetCode(userId uint32, code string) (user
|
||||
return user.User{}, user.ErrUserNotFound
|
||||
}
|
||||
u.PasswordResetCode = code
|
||||
u.PasswordResetExpiration = expiration
|
||||
m.users[userId] = u
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (m *mockUserRepo) ClearVerificationCode(userId uint32) (user.User, error) {
|
||||
m.clearVerificationCodeCalls++
|
||||
m.clearVerificationCodeUserIds = append(m.clearVerificationCodeUserIds, userId)
|
||||
if err := m.fail.trigger("ClearVerificationCode"); err != nil {
|
||||
return user.User{}, err
|
||||
}
|
||||
u, ok := m.users[userId]
|
||||
if !ok {
|
||||
return user.User{}, user.ErrUserNotFound
|
||||
}
|
||||
u.IsVerified = true
|
||||
u.VerificationCode = ""
|
||||
m.users[userId] = u
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (m *mockUserRepo) ResetPassword(userId uint32, hashedPassword string) (user.User, error) {
|
||||
m.resetPasswordCalls++
|
||||
m.lastResetPasswordUserId = userId
|
||||
m.lastResetPasswordHash = hashedPassword
|
||||
if err := m.fail.trigger("ResetPassword"); err != nil {
|
||||
return user.User{}, err
|
||||
}
|
||||
u, ok := m.users[userId]
|
||||
if !ok {
|
||||
return user.User{}, user.ErrUserNotFound
|
||||
}
|
||||
u.Password = hashedPassword
|
||||
u.PasswordResetCode = ""
|
||||
u.PasswordResetExpiration = time.Time{}
|
||||
m.users[userId] = u
|
||||
return u, nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user