mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
added tests before indexes
This commit is contained in:
@@ -7,17 +7,17 @@ import (
|
||||
"github.com/cgrates/rpcclient"
|
||||
)
|
||||
|
||||
type UserData struct {
|
||||
type UserProfile struct {
|
||||
Tenant string
|
||||
UserName string
|
||||
Data map[string]string
|
||||
Profile map[string]string
|
||||
}
|
||||
|
||||
func (ud *UserData) GetId() string {
|
||||
return ud.Tenant + utils.CONCATENATED_KEY_SEP + ud.UserName
|
||||
func (ud *UserProfile) GetId() string {
|
||||
return utils.ConcatenatedKey(ud.Tenant, ud.UserName)
|
||||
}
|
||||
|
||||
func (ud *UserData) SetId(id string) error {
|
||||
func (ud *UserProfile) SetId(id string) error {
|
||||
vals := strings.Split(id, utils.CONCATENATED_KEY_SEP)
|
||||
if len(vals) != 2 {
|
||||
return utils.ErrInvalidKey
|
||||
@@ -28,25 +28,70 @@ func (ud *UserData) SetId(id string) error {
|
||||
}
|
||||
|
||||
type UserService interface {
|
||||
SetUser(UserData, *string) error
|
||||
RemoveUser(UserData, *string) error
|
||||
UpdateUser(UserData, *string) error
|
||||
GetUsers(UserData, *[]UserData) error
|
||||
SetUser(UserProfile, *string) error
|
||||
RemoveUser(UserProfile, *string) error
|
||||
UpdateUser(UserProfile, *string) error
|
||||
GetUsers(UserProfile, *[]*UserProfile) error
|
||||
}
|
||||
|
||||
type UserMap map[string]map[string]string
|
||||
|
||||
func NewUserMap() UserMap {
|
||||
return make(UserMap, 0)
|
||||
}
|
||||
|
||||
func (ud *UserData) SetUser(UserData, *string) error {
|
||||
|
||||
func (um UserMap) SetUser(up UserProfile, reply *string) error {
|
||||
um[up.GetId()] = up.Profile
|
||||
*reply = utils.OK
|
||||
return nil
|
||||
}
|
||||
func (um UserMap) RemoveUser(up UserProfile, reply *string) error {
|
||||
delete(um, up.GetId())
|
||||
*reply = utils.OK
|
||||
return nil
|
||||
}
|
||||
func (um UserMap) UpdateUser(up UserProfile, reply *string) error {
|
||||
m, found := um[up.GetId()]
|
||||
if !found {
|
||||
*reply = utils.ErrNotFound.Error()
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
if m == nil {
|
||||
um[up.GetId()] = make(map[string]string, 0)
|
||||
}
|
||||
for key, value := range up.Profile {
|
||||
um[up.GetId()][key] = value
|
||||
}
|
||||
*reply = utils.OK
|
||||
return nil
|
||||
}
|
||||
func (um UserMap) GetUsers(up UserProfile, results *[]*UserProfile) error {
|
||||
// no index
|
||||
var candidates []*UserProfile
|
||||
for key, values := range um {
|
||||
if up.Tenant != "" && !strings.HasPrefix(key, up.Tenant+utils.CONCATENATED_KEY_SEP) {
|
||||
continue
|
||||
}
|
||||
if up.UserName != "" && !strings.HasSuffix(key, utils.CONCATENATED_KEY_SEP+up.UserName) {
|
||||
continue
|
||||
}
|
||||
valid := true
|
||||
for k, v := range up.Profile {
|
||||
if values[k] != v {
|
||||
valid = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
continue
|
||||
}
|
||||
// all filters passed, add to candidates
|
||||
nup := &UserProfile{Profile: make(map[string]string)}
|
||||
nup.SetId(key)
|
||||
for k, v := range values {
|
||||
nup.Profile[k] = v
|
||||
}
|
||||
candidates = append(candidates, nup)
|
||||
*results = candidates
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (ud *UserData) RemoveUser(UserData, *string) error { return nil }
|
||||
func (ud *UserData) UpdateUser(UserData, *string) error { return nil }
|
||||
func (ud *UserData) GetUsers(UserData, *[]UserData) error { return nil }
|
||||
|
||||
type UserProxy struct{}
|
||||
|
||||
@@ -62,14 +107,14 @@ func NewProxyUserService(addr string, reconnects int) (*ProxyUserService, error)
|
||||
return &ProxyUserService{Client: client}, nil
|
||||
}
|
||||
|
||||
func (ps *ProxyUserService) SetUser(ud UserData, reply *string) error {
|
||||
func (ps *ProxyUserService) SetUser(ud UserProfile, reply *string) error {
|
||||
return ps.Client.Call("UserService.SetUser", ud, reply)
|
||||
}
|
||||
|
||||
func (ps *ProxyUserService) RemoveUser(ud UserData, reply *string) error {
|
||||
func (ps *ProxyUserService) RemoveUser(ud UserProfile, reply *string) error {
|
||||
return ps.Client.Call("UserService.RemoveUser", ud, reply)
|
||||
}
|
||||
|
||||
func (ps *ProxyUserService) GetUsers(ud UserData, users *[]UserData) error {
|
||||
func (ps *ProxyUserService) GetUsers(ud UserProfile, users *[]*UserProfile) error {
|
||||
return ps.Client.Call("UserService.GetUsers", ud, users)
|
||||
}
|
||||
|
||||
253
users/users_test.go
Normal file
253
users/users_test.go
Normal file
@@ -0,0 +1,253 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var testMap = UserMap{
|
||||
"test:user": map[string]string{"t": "v"},
|
||||
":user": map[string]string{"t": "v"},
|
||||
"test:": map[string]string{"t": "v"},
|
||||
"test1:user1": map[string]string{"t": "v", "x": "y"},
|
||||
}
|
||||
|
||||
func TestUsersAdd(t *testing.T) {
|
||||
tm := UserMap{}
|
||||
var r string
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
tm.SetUser(up, &r)
|
||||
p, found := tm[up.GetId()]
|
||||
if r != utils.OK ||
|
||||
!found ||
|
||||
p["t"] != "v" ||
|
||||
len(tm) != 1 ||
|
||||
len(p) != 1 {
|
||||
t.Error("Error setting user: ", tm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersUpdate(t *testing.T) {
|
||||
tm := UserMap{}
|
||||
var r string
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
tm.SetUser(up, &r)
|
||||
p, found := tm[up.GetId()]
|
||||
if r != utils.OK ||
|
||||
!found ||
|
||||
p["t"] != "v" ||
|
||||
len(tm) != 1 ||
|
||||
len(p) != 1 {
|
||||
t.Error("Error setting user: ", tm)
|
||||
}
|
||||
up.Profile["x"] = "y"
|
||||
tm.UpdateUser(up, &r)
|
||||
p, found = tm[up.GetId()]
|
||||
if r != utils.OK ||
|
||||
!found ||
|
||||
p["x"] != "y" ||
|
||||
len(tm) != 1 ||
|
||||
len(p) != 2 {
|
||||
t.Error("Error updating user: ", tm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersUpdateNotFound(t *testing.T) {
|
||||
tm := UserMap{}
|
||||
var r string
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
tm.SetUser(up, &r)
|
||||
up.UserName = "test1"
|
||||
err := tm.UpdateUser(up, &r)
|
||||
if err != utils.ErrNotFound {
|
||||
t.Error("Error detecting user not found on update: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersUpdateInit(t *testing.T) {
|
||||
tm := UserMap{}
|
||||
var r string
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
}
|
||||
tm.SetUser(up, &r)
|
||||
up = UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
tm.UpdateUser(up, &r)
|
||||
p, found := tm[up.GetId()]
|
||||
if r != utils.OK ||
|
||||
!found ||
|
||||
p["t"] != "v" ||
|
||||
len(tm) != 1 ||
|
||||
len(p) != 1 {
|
||||
t.Error("Error updating user: ", tm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersRemove(t *testing.T) {
|
||||
tm := UserMap{}
|
||||
var r string
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
tm.SetUser(up, &r)
|
||||
p, found := tm[up.GetId()]
|
||||
if r != utils.OK ||
|
||||
!found ||
|
||||
p["t"] != "v" ||
|
||||
len(tm) != 1 ||
|
||||
len(p) != 1 {
|
||||
t.Error("Error setting user: ", tm)
|
||||
}
|
||||
tm.RemoveUser(up, &r)
|
||||
p, found = tm[up.GetId()]
|
||||
if r != utils.OK ||
|
||||
found ||
|
||||
len(tm) != 0 {
|
||||
t.Error("Error removing user: ", tm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetFull(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 1 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetTenant(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Tenant: "testX",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetUserName(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "userX",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetNotFoundProfile(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"o": "p",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetMissingTenant(t *testing.T) {
|
||||
up := UserProfile{
|
||||
UserName: "user",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 2 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetMissingUserName(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Tenant: "test",
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 2 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetMissingId(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 4 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetMissingIdTwo(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
"x": "y",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 1 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user