2023-03-16 15:57:44 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-04-22 12:57:51 +02:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2023-03-16 15:57:44 +01:00
|
|
|
"testing"
|
2023-04-22 12:57:51 +02:00
|
|
|
"time"
|
2023-03-16 15:57:44 +01:00
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-03-16 15:57:44 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-01 11:11:30 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2023-03-16 15:57:44 +01:00
|
|
|
)
|
|
|
|
|
2023-03-21 13:34:48 +01:00
|
|
|
const (
|
2023-04-22 12:57:51 +02:00
|
|
|
mockAccountID = "accountID"
|
|
|
|
mockUserID = "userID"
|
|
|
|
mockServiceUserID = "serviceUserID"
|
|
|
|
mockRole = "user"
|
|
|
|
mockServiceUserName = "serviceUserName"
|
|
|
|
mockTargetUserId = "targetUserID"
|
|
|
|
mockTokenID1 = "tokenID1"
|
|
|
|
mockToken1 = "SoMeHaShEdToKeN1"
|
|
|
|
mockTokenID2 = "tokenID2"
|
|
|
|
mockToken2 = "SoMeHaShEdToKeN2"
|
|
|
|
mockTokenName = "tokenName"
|
|
|
|
mockEmptyTokenName = ""
|
|
|
|
mockExpiresIn = 7
|
|
|
|
mockWrongExpiresIn = 4506
|
2023-03-21 13:34:48 +01:00
|
|
|
)
|
|
|
|
|
2023-03-30 13:58:44 +02:00
|
|
|
func TestUser_CreatePAT_ForSameUser(t *testing.T) {
|
2023-03-16 15:57:44 +01:00
|
|
|
store := newStore(t)
|
2023-03-21 13:34:48 +01:00
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
2023-03-20 11:44:12 +01:00
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
2023-03-16 15:57:44 +01:00
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-04-01 11:11:30 +02:00
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
2023-03-16 15:57:44 +01:00
|
|
|
}
|
|
|
|
|
2023-03-30 13:58:44 +02:00
|
|
|
pat, err := am.CreatePAT(mockAccountID, mockUserID, mockUserID, mockTokenName, mockExpiresIn)
|
2023-03-20 11:44:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding PAT to user: %s", err)
|
|
|
|
}
|
2023-03-16 15:57:44 +01:00
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
assert.Equal(t, pat.CreatedBy, mockUserID)
|
|
|
|
|
2023-03-16 15:57:44 +01:00
|
|
|
fileStore := am.Store.(*FileStore)
|
2023-03-30 13:58:44 +02:00
|
|
|
tokenID := fileStore.HashedPAT2TokenID[pat.HashedToken]
|
2023-03-16 15:57:44 +01:00
|
|
|
|
2023-03-20 11:44:12 +01:00
|
|
|
if tokenID == "" {
|
2023-03-16 15:57:44 +01:00
|
|
|
t.Fatal("GetTokenIDByHashedToken failed after adding PAT")
|
|
|
|
}
|
|
|
|
|
2023-03-30 13:58:44 +02:00
|
|
|
assert.Equal(t, pat.ID, tokenID)
|
2023-03-16 15:57:44 +01:00
|
|
|
|
2023-03-20 11:44:12 +01:00
|
|
|
userID := fileStore.TokenID2UserID[tokenID]
|
|
|
|
if userID == "" {
|
2023-03-16 15:57:44 +01:00
|
|
|
t.Fatal("GetUserByTokenId failed after adding PAT")
|
|
|
|
}
|
2023-03-21 13:34:48 +01:00
|
|
|
assert.Equal(t, mockUserID, userID)
|
2023-03-16 15:57:44 +01:00
|
|
|
}
|
2023-03-20 16:14:55 +01:00
|
|
|
|
2023-03-30 13:58:44 +02:00
|
|
|
func TestUser_CreatePAT_ForDifferentUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
2023-04-22 12:57:51 +02:00
|
|
|
account.Users[mockTargetUserId] = &User{
|
|
|
|
Id: mockTargetUserId,
|
|
|
|
IsServiceUser: false,
|
|
|
|
}
|
2023-03-30 13:58:44 +02:00
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-04-01 11:11:30 +02:00
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
2023-03-30 13:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = am.CreatePAT(mockAccountID, mockUserID, mockTargetUserId, mockTokenName, mockExpiresIn)
|
|
|
|
assert.Errorf(t, err, "Creating PAT for different user should thorw error")
|
|
|
|
}
|
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
func TestUser_CreatePAT_ForServiceUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockTargetUserId] = &User{
|
|
|
|
Id: mockTargetUserId,
|
|
|
|
IsServiceUser: true,
|
|
|
|
}
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
pat, err := am.CreatePAT(mockAccountID, mockUserID, mockTargetUserId, mockTokenName, mockExpiresIn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding PAT to user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, pat.CreatedBy, mockUserID)
|
|
|
|
}
|
|
|
|
|
2023-03-30 13:58:44 +02:00
|
|
|
func TestUser_CreatePAT_WithWrongExpiration(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-04-01 11:11:30 +02:00
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
2023-03-30 13:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = am.CreatePAT(mockAccountID, mockUserID, mockUserID, mockTokenName, mockWrongExpiresIn)
|
|
|
|
assert.Errorf(t, err, "Wrong expiration should thorw error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_CreatePAT_WithEmptyName(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-04-01 11:11:30 +02:00
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
2023-03-30 13:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = am.CreatePAT(mockAccountID, mockUserID, mockUserID, mockEmptyTokenName, mockExpiresIn)
|
|
|
|
assert.Errorf(t, err, "Wrong expiration should thorw error")
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:14:55 +01:00
|
|
|
func TestUser_DeletePAT(t *testing.T) {
|
|
|
|
store := newStore(t)
|
2023-03-21 13:34:48 +01:00
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockUserID] = &User{
|
|
|
|
Id: mockUserID,
|
2023-03-20 16:14:55 +01:00
|
|
|
PATs: map[string]*PersonalAccessToken{
|
2023-03-30 13:58:44 +02:00
|
|
|
mockTokenID1: {
|
|
|
|
ID: mockTokenID1,
|
|
|
|
HashedToken: mockToken1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-04-01 11:11:30 +02:00
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
2023-03-30 13:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = am.DeletePAT(mockAccountID, mockUserID, mockUserID, mockTokenID1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding PAT to user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, store.Accounts[mockAccountID].Users[mockUserID].PATs[mockTokenID1])
|
|
|
|
assert.Empty(t, store.HashedPAT2TokenID[mockToken1])
|
|
|
|
assert.Empty(t, store.TokenID2UserID[mockTokenID1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetPAT(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockUserID] = &User{
|
|
|
|
Id: mockUserID,
|
|
|
|
PATs: map[string]*PersonalAccessToken{
|
|
|
|
mockTokenID1: {
|
|
|
|
ID: mockTokenID1,
|
|
|
|
HashedToken: mockToken1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-04-01 11:11:30 +02:00
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
2023-03-30 13:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pat, err := am.GetPAT(mockAccountID, mockUserID, mockUserID, mockTokenID1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding PAT to user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, mockTokenID1, pat.ID)
|
|
|
|
assert.Equal(t, mockToken1, pat.HashedToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetAllPATs(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockUserID] = &User{
|
|
|
|
Id: mockUserID,
|
|
|
|
PATs: map[string]*PersonalAccessToken{
|
|
|
|
mockTokenID1: {
|
|
|
|
ID: mockTokenID1,
|
|
|
|
HashedToken: mockToken1,
|
|
|
|
},
|
|
|
|
mockTokenID2: {
|
|
|
|
ID: mockTokenID2,
|
|
|
|
HashedToken: mockToken2,
|
2023-03-20 16:14:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-04-01 11:11:30 +02:00
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
2023-03-20 16:14:55 +01:00
|
|
|
}
|
|
|
|
|
2023-03-30 13:58:44 +02:00
|
|
|
pats, err := am.GetAllPATs(mockAccountID, mockUserID, mockUserID)
|
2023-03-20 16:14:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding PAT to user: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-03-30 13:58:44 +02:00
|
|
|
assert.Equal(t, 2, len(pats))
|
2023-03-20 16:14:55 +01:00
|
|
|
}
|
2023-04-22 12:57:51 +02:00
|
|
|
|
|
|
|
func TestUser_Copy(t *testing.T) {
|
|
|
|
// this is an imaginary case which will never be in DB this way
|
|
|
|
user := User{
|
|
|
|
Id: "userId",
|
|
|
|
Role: "role",
|
|
|
|
IsServiceUser: true,
|
|
|
|
ServiceUserName: "servicename",
|
|
|
|
AutoGroups: []string{"group1", "group2"},
|
|
|
|
PATs: map[string]*PersonalAccessToken{
|
|
|
|
"pat1": {
|
|
|
|
ID: "pat1",
|
|
|
|
Name: "First PAT",
|
|
|
|
HashedToken: "SoMeHaShEdToKeN",
|
|
|
|
ExpirationDate: time.Now().AddDate(0, 0, 7),
|
|
|
|
CreatedBy: "userId",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
LastUsed: time.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := validateStruct(user)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test needs update: dummy struct has not all fields set : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
copiedUser := user.Copy()
|
|
|
|
|
|
|
|
assert.True(t, cmp.Equal(user, *copiedUser))
|
|
|
|
}
|
|
|
|
|
|
|
|
// based on https://medium.com/@anajankow/fast-check-if-all-struct-fields-are-set-in-golang-bba1917213d2
|
|
|
|
func validateStruct(s interface{}) (err error) {
|
|
|
|
|
|
|
|
structType := reflect.TypeOf(s)
|
|
|
|
structVal := reflect.ValueOf(s)
|
|
|
|
fieldNum := structVal.NumField()
|
|
|
|
|
|
|
|
for i := 0; i < fieldNum; i++ {
|
|
|
|
field := structVal.Field(i)
|
|
|
|
fieldName := structType.Field(i).Name
|
|
|
|
|
|
|
|
isSet := field.IsValid() && !field.IsZero()
|
|
|
|
|
|
|
|
if !isSet {
|
|
|
|
err = fmt.Errorf("%v%s in not set; ", err, fieldName)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_CreateServiceUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := am.createServiceUser(mockAccountID, mockUserID, mockRole, mockServiceUserName, []string{"group1", "group2"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating service user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 2, len(store.Accounts[mockAccountID].Users))
|
|
|
|
assert.NotNil(t, store.Accounts[mockAccountID].Users[user.ID])
|
|
|
|
assert.True(t, store.Accounts[mockAccountID].Users[user.ID].IsServiceUser)
|
|
|
|
assert.Equal(t, mockServiceUserName, store.Accounts[mockAccountID].Users[user.ID].ServiceUserName)
|
|
|
|
assert.Equal(t, UserRole(mockRole), store.Accounts[mockAccountID].Users[user.ID].Role)
|
|
|
|
assert.Equal(t, []string{"group1", "group2"}, store.Accounts[mockAccountID].Users[user.ID].AutoGroups)
|
|
|
|
assert.Equal(t, map[string]*PersonalAccessToken{}, store.Accounts[mockAccountID].Users[user.ID].PATs)
|
|
|
|
|
|
|
|
assert.Zero(t, user.Email)
|
|
|
|
assert.True(t, user.IsServiceUser)
|
|
|
|
assert.Equal(t, "active", user.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_CreateUser_ServiceUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := am.CreateUser(mockAccountID, mockUserID, &UserInfo{
|
|
|
|
Name: mockServiceUserName,
|
|
|
|
Role: mockRole,
|
|
|
|
IsServiceUser: true,
|
|
|
|
AutoGroups: []string{"group1", "group2"},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, user.IsServiceUser)
|
|
|
|
assert.Equal(t, 2, len(store.Accounts[mockAccountID].Users))
|
|
|
|
assert.True(t, store.Accounts[mockAccountID].Users[user.ID].IsServiceUser)
|
|
|
|
assert.Equal(t, mockServiceUserName, store.Accounts[mockAccountID].Users[user.ID].ServiceUserName)
|
|
|
|
assert.Equal(t, UserRole(mockRole), store.Accounts[mockAccountID].Users[user.ID].Role)
|
|
|
|
assert.Equal(t, []string{"group1", "group2"}, store.Accounts[mockAccountID].Users[user.ID].AutoGroups)
|
|
|
|
|
|
|
|
assert.Equal(t, mockServiceUserName, user.Name)
|
|
|
|
assert.Equal(t, mockRole, user.Role)
|
|
|
|
assert.Equal(t, []string{"group1", "group2"}, user.AutoGroups)
|
|
|
|
assert.Equal(t, "active", user.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_CreateUser_RegularUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = am.CreateUser(mockAccountID, mockUserID, &UserInfo{
|
|
|
|
Name: mockServiceUserName,
|
|
|
|
Role: mockRole,
|
|
|
|
IsServiceUser: false,
|
|
|
|
AutoGroups: []string{"group1", "group2"},
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Errorf(t, err, "Not configured IDP will throw error but right path used")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_DeleteUser_ServiceUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockServiceUserID] = &User{
|
|
|
|
Id: mockServiceUserID,
|
|
|
|
IsServiceUser: true,
|
|
|
|
ServiceUserName: mockServiceUserName,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.DeleteUser(mockAccountID, mockUserID, mockServiceUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when deleting user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(store.Accounts[mockAccountID].Users))
|
|
|
|
assert.Nil(t, store.Accounts[mockAccountID].Users[mockServiceUserID])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_DeleteUser_regularUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.DeleteUser(mockAccountID, mockUserID, mockUserID)
|
|
|
|
|
|
|
|
assert.Errorf(t, err, "Regular users can not be deleted (yet)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_IsUserAdmin_ForAdmin(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := am.IsUserAdmin(mockUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when checking user role: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_IsUserAdmin_ForUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockUserID] = &User{
|
|
|
|
Id: mockUserID,
|
|
|
|
Role: "user",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := am.IsUserAdmin(mockUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when checking user role: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetUsersFromAccount_ForAdmin(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockServiceUserID] = &User{
|
|
|
|
Id: mockServiceUserID,
|
|
|
|
Role: "user",
|
|
|
|
IsServiceUser: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
users, err := am.GetUsersFromAccount(mockAccountID, mockUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when getting users from account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 2, len(users))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetUsersFromAccount_ForUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockServiceUserID] = &User{
|
|
|
|
Id: mockServiceUserID,
|
|
|
|
Role: "user",
|
|
|
|
IsServiceUser: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
|
|
|
Store: store,
|
|
|
|
eventStore: &activity.InMemoryEventStore{},
|
|
|
|
}
|
|
|
|
|
|
|
|
users, err := am.GetUsersFromAccount(mockAccountID, mockServiceUserID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when getting users from account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
assert.Equal(t, mockServiceUserID, users[0].ID)
|
|
|
|
}
|