tsk-18: Create service user (#22)
Closes #18 Reviewed-on: phoenix/textsender-auth#22 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
||||
)
|
||||
|
||||
type MockServiceUserStore struct {
|
||||
ServiceUsers map[uuid.UUID]*user.ServiceUser
|
||||
ServiceUsersByUsername map[string]*user.ServiceUser
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockServiceUserStore() *MockServiceUserStore {
|
||||
return &MockServiceUserStore{
|
||||
ServiceUsers: make(map[uuid.UUID]*user.ServiceUser),
|
||||
ServiceUsersByUsername: make(map[string]*user.ServiceUser),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockServiceUserStore) Create(ctx context.Context, user *user.ServiceUser) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if user.Id == uuid.Nil {
|
||||
user.Id = uuid.New()
|
||||
}
|
||||
|
||||
if _, exists := m.ServiceUsersByUsername[user.Username]; exists {
|
||||
return errors.New("service User with username already exists")
|
||||
}
|
||||
|
||||
m.ServiceUsers[user.Id] = user
|
||||
m.ServiceUsersByUsername[user.Username] = user
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockServiceUserStore) CheckWithUsername(ctx context.Context, username string) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
var exists bool
|
||||
|
||||
for _, serviceUser := range m.ServiceUsers {
|
||||
if serviceUser.Username == username {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return exists, nil
|
||||
} else {
|
||||
return exists, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user