Fix DefaultAccountManager GetGroupsFromTheToken false positive tests (#1019)

This fixes the test logic creates copy of account with empty id and
re-pointing the indices to it.

Also, adds additional check for empty ID in SaveAccount method of FileStore.
This commit is contained in:
Yury Gargay 2023-07-22 13:54:08 +02:00 committed by GitHub
parent 6ad3847615
commit 97b6e79809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -470,11 +470,15 @@ func TestDefaultAccountManager_GetGroupsFromTheToken(t *testing.T) {
require.NoError(t, err, "unable to create account manager") require.NoError(t, err, "unable to create account manager")
accountID := initAccount.Id accountID := initAccount.Id
_, err = manager.GetAccountByUserOrAccountID(userId, accountID, domain) acc, err := manager.GetAccountByUserOrAccountID(userId, accountID, domain)
require.NoError(t, err, "create init user failed") require.NoError(t, err, "create init user failed")
// as initAccount was created without account id we have to take the id after account initialization
// that happens inside the GetAccountByUserOrAccountID where the id is getting generated
// it is important to set the id as it help to avoid creating additional account with empty Id and re-pointing indices to it
initAccount.Id = acc.Id
claims := jwtclaims.AuthorizationClaims{ claims := jwtclaims.AuthorizationClaims{
AccountId: accountID, AccountId: accountID, // is empty as it is based on accountID right after initialization of initAccount
Domain: domain, Domain: domain,
UserId: userId, UserId: userId,
DomainCategory: "test-category", DomainCategory: "test-category",
@ -491,6 +495,7 @@ func TestDefaultAccountManager_GetGroupsFromTheToken(t *testing.T) {
initAccount.Settings.JWTGroupsEnabled = true initAccount.Settings.JWTGroupsEnabled = true
err := manager.Store.SaveAccount(initAccount) err := manager.Store.SaveAccount(initAccount)
require.NoError(t, err, "save account failed") require.NoError(t, err, "save account failed")
require.Len(t, manager.Store.GetAllAccounts(), 1, "only one account should exist")
account, _, err := manager.GetAccountFromToken(claims) account, _, err := manager.GetAccountFromToken(claims)
require.NoError(t, err, "get account by token failed") require.NoError(t, err, "get account by token failed")
@ -502,6 +507,7 @@ func TestDefaultAccountManager_GetGroupsFromTheToken(t *testing.T) {
initAccount.Settings.JWTGroupsClaimName = "idp-groups" initAccount.Settings.JWTGroupsClaimName = "idp-groups"
err := manager.Store.SaveAccount(initAccount) err := manager.Store.SaveAccount(initAccount)
require.NoError(t, err, "save account failed") require.NoError(t, err, "save account failed")
require.Len(t, manager.Store.GetAllAccounts(), 1, "only one account should exist")
account, _, err := manager.GetAccountFromToken(claims) account, _, err := manager.GetAccountFromToken(claims)
require.NoError(t, err, "get account by token failed") require.NoError(t, err, "get account by token failed")

View File

@ -289,6 +289,10 @@ func (s *FileStore) SaveAccount(account *Account) error {
s.mux.Lock() s.mux.Lock()
defer s.mux.Unlock() defer s.mux.Unlock()
if account.Id == "" {
return status.Errorf(status.InvalidArgument, "account id should not be empty")
}
accountCopy := account.Copy() accountCopy := account.Copy()
s.Accounts[accountCopy.Id] = accountCopy s.Accounts[accountCopy.Id] = accountCopy