mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* NOISSUE - Add admin method in users service to return users list Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix loggings and metrics Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add email and metadata filters Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Retrieve User infos by ID if Admin Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Remove admin checks and fix comments Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix missing query Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use generic funccs to create email and metadata queries Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add /users/profile endpoint Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Simplify db helpers Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix View, List, Retrieve prefix methods naming Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix tracer endpoints naming Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add tests and remove TODO comments Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
134 lines
2.8 KiB
Go
134 lines
2.8 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package mocks
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/mainflux/mainflux/users"
|
|
)
|
|
|
|
var _ users.UserRepository = (*userRepositoryMock)(nil)
|
|
|
|
type userRepositoryMock struct {
|
|
mu sync.Mutex
|
|
users map[string]users.User
|
|
usersByID map[string]users.User
|
|
usersByGroupID map[string]users.User
|
|
}
|
|
|
|
// NewUserRepository creates in-memory user repository
|
|
func NewUserRepository() users.UserRepository {
|
|
return &userRepositoryMock{
|
|
users: make(map[string]users.User),
|
|
usersByID: make(map[string]users.User),
|
|
usersByGroupID: make(map[string]users.User),
|
|
}
|
|
}
|
|
|
|
func (urm *userRepositoryMock) Save(ctx context.Context, user users.User) (string, error) {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
if _, ok := urm.users[user.Email]; ok {
|
|
return "", users.ErrConflict
|
|
}
|
|
|
|
urm.users[user.Email] = user
|
|
urm.usersByID[user.ID] = user
|
|
return user.ID, nil
|
|
}
|
|
|
|
func (urm *userRepositoryMock) Update(ctx context.Context, user users.User) error {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
if _, ok := urm.users[user.Email]; !ok {
|
|
return users.ErrUserNotFound
|
|
}
|
|
|
|
urm.users[user.Email] = user
|
|
return nil
|
|
}
|
|
|
|
func (urm *userRepositoryMock) UpdateUser(ctx context.Context, user users.User) error {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
if _, ok := urm.users[user.Email]; !ok {
|
|
return users.ErrUserNotFound
|
|
}
|
|
|
|
urm.users[user.Email] = user
|
|
return nil
|
|
}
|
|
|
|
func (urm *userRepositoryMock) RetrieveByEmail(ctx context.Context, email string) (users.User, error) {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
val, ok := urm.users[email]
|
|
if !ok {
|
|
return users.User{}, users.ErrNotFound
|
|
}
|
|
|
|
return val, nil
|
|
}
|
|
|
|
func (urm *userRepositoryMock) RetrieveByID(ctx context.Context, id string) (users.User, error) {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
val, ok := urm.usersByID[id]
|
|
if !ok {
|
|
return users.User{}, users.ErrNotFound
|
|
}
|
|
|
|
return val, nil
|
|
}
|
|
|
|
func (urm *userRepositoryMock) RetrieveAll(ctx context.Context, offset, limit uint64, email string, um users.Metadata) (users.UserPage, error) {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
up := users.UserPage{}
|
|
i := uint64(0)
|
|
|
|
for _, u := range urm.users {
|
|
if i >= offset && i < (limit+offset) {
|
|
up.Users = append(up.Users, u)
|
|
}
|
|
i++
|
|
}
|
|
|
|
up.Offset = offset
|
|
up.Limit = limit
|
|
up.Total = uint64(i)
|
|
|
|
return up, nil
|
|
}
|
|
|
|
func (urm *userRepositoryMock) RetrieveMembers(ctx context.Context, groupID string, offset, limit uint64, um users.Metadata) (users.UserPage, error) {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
_, ok := urm.usersByGroupID[groupID]
|
|
if !ok {
|
|
return users.UserPage{}, users.ErrNotFound
|
|
}
|
|
|
|
return users.UserPage{}, nil
|
|
}
|
|
|
|
func (urm *userRepositoryMock) UpdatePassword(_ context.Context, token, password string) error {
|
|
urm.mu.Lock()
|
|
defer urm.mu.Unlock()
|
|
|
|
if _, ok := urm.users[token]; !ok {
|
|
return users.ErrUserNotFound
|
|
}
|
|
return nil
|
|
}
|