mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* remove owner id Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add users endpoint for retrieving users from group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups from things and users Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups into auth Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * separate endpoints for users and things Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix problems with retrieving members Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups from users Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups from things Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename constant Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add new errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unnecessary constants Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix validation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * create groups db mock Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert changes to docker related files Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups endpoints from users openapi Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups endpoints from users openapi Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move constant from postgres to groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move constant from postgres to groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move constant from postgres to groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove testing group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renam typ to groupType Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add error for max level Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove print Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups.Member interface Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix query building and add test cases Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * uncomment tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups package Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove group type, add bulk assign and unassign Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update openapi, remove parentID from create request, reorder endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update openapi Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update openapi for users and things Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix groups test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename assignReq structure Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor mocks, response, remove type from endpoint Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * some refactor, renaming, errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * simplify check Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove package alias Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming and comment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * additional comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add members grpc endpoint test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix retrieving members for different types Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix retrieving members for different types Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unecessary structure Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix api grpc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename const Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactore retrieve parents and children with common function Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small changes for errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix compile error Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix sorting in mock Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove regexp for groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert as change is made by mistake Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert as change is made by mistake Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor groups and keys package Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test for timestamp compare Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix error handling Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove errors not being used Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * var renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add endpoints for groups into nginx Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize endpoints, remove some errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize endpoints, remove some errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small fix Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix group save path problem Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * description constant Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename variables Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix validation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * get back return Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix compile Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
370 lines
9.7 KiB
Go
370 lines
9.7 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package users_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/pkg/uuid"
|
|
"github.com/mainflux/mainflux/users"
|
|
|
|
"github.com/mainflux/mainflux/users/mocks"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const wrong string = "wrong-value"
|
|
|
|
var (
|
|
user = users.User{Email: "user@example.com", Password: "password", Metadata: map[string]interface{}{"role": "user"}}
|
|
nonExistingUser = users.User{Email: "non-ex-user@example.com", Password: "password", Metadata: map[string]interface{}{"role": "user"}}
|
|
host = "example.com"
|
|
|
|
idProvider = uuid.New()
|
|
passRegex = regexp.MustCompile("^.{8,}$")
|
|
)
|
|
|
|
func newService() users.Service {
|
|
userRepo := mocks.NewUserRepository()
|
|
hasher := mocks.NewHasher()
|
|
auth := mocks.NewAuthService(map[string]string{user.Email: user.Email})
|
|
e := mocks.NewEmailer()
|
|
|
|
return users.New(userRepo, hasher, auth, e, idProvider, passRegex)
|
|
}
|
|
|
|
func TestRegister(t *testing.T) {
|
|
svc := newService()
|
|
|
|
cases := []struct {
|
|
desc string
|
|
user users.User
|
|
err error
|
|
}{
|
|
{
|
|
desc: "register new user",
|
|
user: user,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "register existing user",
|
|
user: user,
|
|
err: users.ErrConflict,
|
|
},
|
|
{
|
|
desc: "register new user with weak password",
|
|
user: users.User{
|
|
Email: user.Email,
|
|
Password: "weak",
|
|
},
|
|
err: users.ErrPasswordFormat,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
_, err := svc.Register(context.Background(), tc.user)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestLogin(t *testing.T) {
|
|
svc := newService()
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
noAuthUser := users.User{
|
|
Email: "email@test.com",
|
|
Password: "12345678",
|
|
}
|
|
|
|
cases := map[string]struct {
|
|
user users.User
|
|
err error
|
|
}{
|
|
"login with good credentials": {
|
|
user: user,
|
|
err: nil,
|
|
},
|
|
"login with wrong e-mail": {
|
|
user: users.User{
|
|
Email: wrong,
|
|
Password: user.Password,
|
|
},
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
"login with wrong password": {
|
|
user: users.User{
|
|
Email: user.Email,
|
|
Password: wrong,
|
|
},
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
"login failed auth": {
|
|
user: noAuthUser,
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
_, err := svc.Login(context.Background(), tc.user)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestViewUser(t *testing.T) {
|
|
svc := newService()
|
|
id, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
token, err := svc.Login(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
u := user
|
|
u.Password = ""
|
|
|
|
cases := map[string]struct {
|
|
user users.User
|
|
token string
|
|
userID string
|
|
err error
|
|
}{
|
|
"view user with authorized token": {
|
|
user: u,
|
|
token: token,
|
|
userID: id,
|
|
err: nil,
|
|
},
|
|
"view user with unauthorized token": {
|
|
user: users.User{},
|
|
token: "",
|
|
userID: id,
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
"view user with authorized token and invalid user id": {
|
|
user: users.User{},
|
|
token: token,
|
|
userID: "",
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
_, err := svc.ViewUser(context.Background(), tc.token, tc.userID)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestViewProfile(t *testing.T) {
|
|
svc := newService()
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
token, err := svc.Login(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
u := user
|
|
u.Password = ""
|
|
|
|
cases := map[string]struct {
|
|
user users.User
|
|
token string
|
|
err error
|
|
}{
|
|
"valid token's user info": {
|
|
user: u,
|
|
token: token,
|
|
err: nil,
|
|
},
|
|
"invalid token's user info": {
|
|
user: users.User{},
|
|
token: "",
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
_, err := svc.ViewProfile(context.Background(), tc.token)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
func TestListUsers(t *testing.T) {
|
|
svc := newService()
|
|
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
token, err := svc.Login(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
var nUsers = uint64(10)
|
|
|
|
for i := uint64(1); i < nUsers; i++ {
|
|
email := fmt.Sprintf("TestListUsers%d@example.com", i)
|
|
user := users.User{
|
|
Email: email,
|
|
Password: "passpass",
|
|
}
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
}
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
offset uint64
|
|
limit uint64
|
|
email string
|
|
size uint64
|
|
err error
|
|
}{
|
|
"list users with authorized token": {
|
|
token: token,
|
|
size: 0,
|
|
err: nil,
|
|
},
|
|
"list user with unauthorized token": {
|
|
token: "",
|
|
size: 0,
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
"list users with offset and limit": {
|
|
token: token,
|
|
offset: 6,
|
|
limit: nUsers,
|
|
size: nUsers - 6,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
page, err := svc.ListUsers(context.Background(), tc.token, tc.offset, tc.limit, tc.email, nil)
|
|
size := uint64(len(page.Users))
|
|
assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected size %d got %d\n", desc, tc.size, size))
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestUpdateUser(t *testing.T) {
|
|
svc := newService()
|
|
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
token, err := svc.Login(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
user.Metadata = map[string]interface{}{"role": "test"}
|
|
|
|
cases := map[string]struct {
|
|
user users.User
|
|
token string
|
|
err error
|
|
}{
|
|
"update user with valid token": {
|
|
user: user,
|
|
token: token,
|
|
err: nil,
|
|
},
|
|
"update user with invalid token": {
|
|
user: user,
|
|
token: "non-existent",
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.UpdateUser(context.Background(), tc.token, tc.user)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestGenerateResetToken(t *testing.T) {
|
|
svc := newService()
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
cases := map[string]struct {
|
|
email string
|
|
err error
|
|
}{
|
|
"valid user reset token": {user.Email, nil},
|
|
"invalid user rest token": {nonExistingUser.Email, users.ErrUserNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.GenerateResetToken(context.Background(), tc.email, host)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestChangePassword(t *testing.T) {
|
|
svc := newService()
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("register user error: %s", err))
|
|
token, _ := svc.Login(context.Background(), user)
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
password string
|
|
oldPassword string
|
|
err error
|
|
}{
|
|
"valid user change password ": {token, "newpassword", user.Password, nil},
|
|
"valid user change password with wrong password": {token, "newpassword", "wrongpassword", users.ErrUnauthorizedAccess},
|
|
"valid user change password invalid token": {"", "newpassword", user.Password, users.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.ChangePassword(context.Background(), tc.token, tc.password, tc.oldPassword)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
}
|
|
|
|
func TestResetPassword(t *testing.T) {
|
|
svc := newService()
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
auth := mocks.NewAuthService(map[string]string{user.Email: user.Email})
|
|
resetToken, err := auth.Issue(context.Background(), &mainflux.IssueReq{Id: user.ID, Email: user.Email, Type: 2})
|
|
assert.Nil(t, err, fmt.Sprintf("Generating reset token expected to succeed: %s", err))
|
|
cases := map[string]struct {
|
|
token string
|
|
password string
|
|
err error
|
|
}{
|
|
"valid user reset password ": {resetToken.GetValue(), user.Email, nil},
|
|
"invalid user reset password ": {"", "newpassword", users.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.ResetPassword(context.Background(), tc.token, tc.password)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestSendPasswordReset(t *testing.T) {
|
|
svc := newService()
|
|
_, err := svc.Register(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("register user error: %s", err))
|
|
token, _ := svc.Login(context.Background(), user)
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
email string
|
|
err error
|
|
}{
|
|
"valid user reset password ": {token, user.Email, nil},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.SendPasswordReset(context.Background(), host, tc.email, tc.token)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
}
|