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

* adding group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding user group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add retrieve methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default admin user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default admin user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * changes signature for AssignUser Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * bug fixing retrieving groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unused code Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * bug fixing retrieving groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * retrieve groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change environment for admin Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change environment for admin Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * retrieve groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove adding default group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * expose port for debugging purposes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix tests, and linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add prefix Users for groups endpoint Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter problems Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix endpoint prefix url Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix endpoint test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add group features in cli Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove println Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * when user is created return id in response Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * when user is created return id in response Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding default admin env Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * proper alignment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * proper alignment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename method Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * return user id when created Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * return user id when created Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unused variable Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix to retrieve whole tree starting from parent Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add endpoint to list groups for user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add readme for groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fixing bugs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fixing bugs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add group commands for add and remove user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * replace default email, use example.com Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix capital letters beginning of sentence Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove warning for deprecated api, mistakenly copied Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * simplify repo methods, rely on db driver rather than the check before operation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * check if group is valid Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * openapi spec 3.0 Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove check for existing users in groups before delete Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change func signature Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change func signature Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix bugs, resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix bugs, resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix alignment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add missing command Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize envs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix doc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix compile Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize cli commands Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor corrections Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments, minor changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
349 lines
9.7 KiB
Go
349 lines
9.7 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package postgres_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/pkg/uuid"
|
|
uuidProvider "github.com/mainflux/mainflux/pkg/uuid"
|
|
"github.com/mainflux/mainflux/users"
|
|
"github.com/mainflux/mainflux/users/postgres"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
groupName = "Mainflux"
|
|
password = "12345678"
|
|
)
|
|
|
|
func TestGroupSave(t *testing.T) {
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.NewGroupRepo(dbMiddleware)
|
|
userRepo := postgres.NewUserRepo(dbMiddleware)
|
|
uid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("user id unexpected error: %s", err))
|
|
user := users.User{
|
|
ID: uid,
|
|
Email: "TestGroupSave@mainflux.com",
|
|
Password: password,
|
|
}
|
|
_, err = userRepo.Save(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("save got unexpected error: %s", err))
|
|
|
|
user, err = userRepo.RetrieveByEmail(context.Background(), user.Email)
|
|
require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err))
|
|
|
|
uid, err = uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group := users.Group{
|
|
ID: uid,
|
|
Name: "TestGroupSave",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
cases := []struct {
|
|
desc string
|
|
group users.Group
|
|
err error
|
|
}{
|
|
{
|
|
desc: "create new group",
|
|
group: group,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "create group that already exist",
|
|
group: group,
|
|
err: users.ErrGroupConflict,
|
|
},
|
|
{
|
|
desc: "create thing with invalid name",
|
|
group: users.Group{
|
|
Name: "x^%",
|
|
},
|
|
err: users.ErrMalformedEntity,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
_, err := repo.Save(context.Background(), tc.group)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestGroupRetrieveByID(t *testing.T) {
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.NewGroupRepo(dbMiddleware)
|
|
userRepo := postgres.NewUserRepo(dbMiddleware)
|
|
uid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
user := users.User{
|
|
ID: uid,
|
|
Email: "TestGroupRetrieveByID@mainflux.com",
|
|
Password: password,
|
|
}
|
|
_, err = userRepo.Save(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("save got unexpected error: %s", err))
|
|
|
|
user, err = userRepo.RetrieveByEmail(context.Background(), user.Email)
|
|
require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err))
|
|
|
|
gid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group1 := users.Group{
|
|
ID: gid,
|
|
Name: groupName + "TestGroupRetrieveByID1",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
gid, err = uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group2 := users.Group{
|
|
ID: gid,
|
|
Name: groupName + "TestGroupRetrieveByID2",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
g1, err := repo.Save(context.Background(), group1)
|
|
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
|
|
|
|
g2, err := repo.Save(context.Background(), group2)
|
|
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
|
|
|
|
g2.ID, err = uuidProvider.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("failed to generate id error: %s", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
group users.Group
|
|
err error
|
|
}{
|
|
{
|
|
desc: "retrieve group for valid id",
|
|
group: g1,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "retrieve group for invalid id",
|
|
group: g2,
|
|
err: users.ErrNotFound,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
_, err := repo.RetrieveByID(context.Background(), tc.group.ID)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestGroupDelete(t *testing.T) {
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.NewGroupRepo(dbMiddleware)
|
|
userRepo := postgres.NewUserRepo(dbMiddleware)
|
|
uid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
user := users.User{
|
|
ID: uid,
|
|
Email: "TestGroupDelete@mainflux.com",
|
|
Password: password,
|
|
}
|
|
_, err = userRepo.Save(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("save got unexpected error: %s", err))
|
|
|
|
user, err = userRepo.RetrieveByEmail(context.Background(), user.Email)
|
|
require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err))
|
|
|
|
gid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group1 := users.Group{
|
|
ID: gid,
|
|
Name: groupName + "TestGroupDelete1",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
g1, err := repo.Save(context.Background(), group1)
|
|
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
|
|
|
|
err = repo.Assign(context.Background(), user.ID, g1.ID)
|
|
require.Nil(t, err, fmt.Sprintf("failed to assign user to a group: %s", err))
|
|
|
|
gid, err = uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group2 := users.Group{
|
|
ID: gid,
|
|
Name: groupName + "TestGroupDelete2",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
g2, err := repo.Save(context.Background(), group2)
|
|
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
group users.Group
|
|
err error
|
|
}{
|
|
{
|
|
desc: "delete group for existing id",
|
|
group: g2,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "delete group for non-existing id",
|
|
group: g2,
|
|
err: users.ErrDeleteGroupMissing,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := repo.Delete(context.Background(), tc.group.ID)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestAssignUser(t *testing.T) {
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.NewGroupRepo(dbMiddleware)
|
|
userRepo := postgres.NewUserRepo(dbMiddleware)
|
|
uid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
user := users.User{
|
|
ID: uid,
|
|
Email: "TestAssignUser@mainflux.com",
|
|
Password: password,
|
|
}
|
|
|
|
_, err = userRepo.Save(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("save got unexpected error: %s", err))
|
|
|
|
user, err = userRepo.RetrieveByEmail(context.Background(), user.Email)
|
|
require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err))
|
|
|
|
gid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group1 := users.Group{
|
|
ID: gid,
|
|
Name: groupName + "TestAssignUser1",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
g1, err := repo.Save(context.Background(), group1)
|
|
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
|
|
|
|
gid, err = uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group2 := users.Group{
|
|
ID: gid,
|
|
Name: groupName + "TestAssignUser2",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
g2, err := repo.Save(context.Background(), group2)
|
|
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
|
|
|
|
gid, err = uuidProvider.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id generating error: %s", err))
|
|
g3 := users.Group{
|
|
ID: gid,
|
|
}
|
|
|
|
cases := []struct {
|
|
desc string
|
|
group users.Group
|
|
err error
|
|
}{
|
|
{
|
|
desc: "assign user to existing group",
|
|
group: g1,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "assign user to another existing group",
|
|
group: g2,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "assign user to non existing group",
|
|
group: g3,
|
|
err: users.ErrNotFound,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := repo.Assign(context.Background(), user.ID, tc.group.ID)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
|
|
}
|
|
|
|
func TestUnassignUser(t *testing.T) {
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.NewGroupRepo(dbMiddleware)
|
|
userRepo := postgres.NewUserRepo(dbMiddleware)
|
|
|
|
uid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
user := users.User{
|
|
ID: uid,
|
|
Email: "UnassignUser1@mainflux.com",
|
|
Password: password,
|
|
}
|
|
|
|
_, err = userRepo.Save(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("save got unexpected error: %s", err))
|
|
|
|
user1, err := userRepo.RetrieveByEmail(context.Background(), user.Email)
|
|
require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err))
|
|
|
|
uid, err = uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
user = users.User{
|
|
ID: uid,
|
|
Email: "UnassignUser2@mainflux.com",
|
|
Password: password,
|
|
}
|
|
|
|
_, err = userRepo.Save(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("save got unexpected error: %s", err))
|
|
|
|
user2, err := userRepo.RetrieveByEmail(context.Background(), user.Email)
|
|
require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err))
|
|
|
|
gid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err))
|
|
group1 := users.Group{
|
|
ID: gid,
|
|
Name: groupName + "UnassignUser1",
|
|
OwnerID: user.ID,
|
|
}
|
|
|
|
g1, err := repo.Save(context.Background(), group1)
|
|
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
|
|
|
|
err = repo.Assign(context.Background(), user1.ID, group1.ID)
|
|
require.Nil(t, err, fmt.Sprintf("failed to assign user: %s", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
group users.Group
|
|
user users.User
|
|
err error
|
|
}{
|
|
{desc: "remove user from a group", group: g1, user: user1, err: nil},
|
|
{desc: "remove already removed user from a group", group: g1, user: user1, err: nil},
|
|
{desc: "remove non existing user from a group", group: g1, user: user2, err: nil},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := repo.Unassign(context.Background(), tc.user.ID, tc.group.ID)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
|
|
}
|