test: Refactor account management, fix map-random-order race condition

Some SMTP tests made use of disabled addresses. We stored addresses
in a map, meaning the order was randomized. This lead to tests sometimes
attempting to authenticate over SMTP using a disabled address, failing.
This commit is contained in:
James Houlahan
2023-02-07 17:32:44 +01:00
parent 31dce41276
commit c9d496956c
7 changed files with 154 additions and 128 deletions

View File

@ -32,7 +32,6 @@ import (
"github.com/bradenaw/juniper/xslices"
"github.com/cucumber/godog"
"github.com/google/uuid"
"golang.org/x/exp/slices"
)
func (s *scenario) thereExistsAnAccountWithUsernameAndPassword(username, password string) error {
@ -52,7 +51,7 @@ func (s *scenario) theAccountHasAdditionalDisabledAddress(username, address stri
}
func (s *scenario) theAccountHasAdditionalAddressWithoutKeys(username, address string) error {
userID := s.t.getUserID(username)
userID := s.t.getUserByName(username).getUserID()
// Decrypt the user's encrypted ID for use with quark.
userDecID, err := s.t.runQuarkCmd(context.Background(), "encryption:id", "--decrypt", userID)
@ -65,7 +64,8 @@ func (s *scenario) theAccountHasAdditionalAddressWithoutKeys(username, address s
context.Background(),
"user:create:address",
string(userDecID),
s.t.getUserPass(userID),
s.t.getUserByID(userID).getUserPass(),
address,
); err != nil {
return err
@ -78,15 +78,15 @@ func (s *scenario) theAccountHasAdditionalAddressWithoutKeys(username, address s
}
// Set the new address of the user.
s.t.setUserAddr(userID, addr[len(addr)-1].ID, address)
s.t.getUserByID(userID).addAddress(addr[len(addr)-1].ID, address)
return nil
})
}
func (s *scenario) theAccountNoLongerHasAdditionalAddress(username, address string) error {
userID := s.t.getUserID(username)
addrID := s.t.getUserAddrID(userID, address)
userID := s.t.getUserByName(username).getUserID()
addrID := s.t.getUserByName(username).getAddrID(address)
if err := s.t.withClient(context.Background(), username, func(ctx context.Context, c *proton.Client) error {
if err := c.DisableAddress(ctx, addrID); err != nil {
@ -98,7 +98,7 @@ func (s *scenario) theAccountNoLongerHasAdditionalAddress(username, address stri
return err
}
s.t.unsetUserAddr(userID, addrID)
s.t.getUserByID(userID).remAddress(addrID)
return nil
}
@ -184,8 +184,8 @@ func (s *scenario) theAddressOfAccountHasTheFollowingMessagesInMailbox(address,
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
userID := s.t.getUserID(username)
addrID := s.t.getUserAddrID(userID, address)
userID := s.t.getUserByName(username).getUserID()
addrID := s.t.getUserByName(username).getAddrID(address)
mboxID := s.t.getMBoxID(userID, mailbox)
wantMessages, err := unmarshalTable[Message](table)
@ -219,8 +219,8 @@ func (s *scenario) theAddressOfAccountHasMessagesInMailbox(address, username str
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
userID := s.t.getUserID(username)
addrID := s.t.getUserAddrID(userID, address)
userID := s.t.getUserByName(username).getUserID()
addrID := s.t.getUserByName(username).getAddrID(address)
mboxID := s.t.getMBoxID(userID, mailbox)
return s.t.createMessages(ctx, username, addrID, iterator.Collect(iterator.Map(iterator.Counter(count), func(idx int) proton.ImportReq {
@ -262,7 +262,7 @@ func (s *scenario) theFollowingFieldsWereChangedInDraftForAddressOfAccount(draft
defer cancel()
return s.t.withClient(ctx, username, func(ctx context.Context, c *proton.Client) error {
return s.t.withAddrKR(ctx, c, username, s.t.getUserAddrID(s.t.getUserID(username), address), func(_ context.Context, addrKR *crypto.KeyRing) error {
return s.t.withAddrKR(ctx, c, username, s.t.getUserByName(username).getAddrID(address), func(_ context.Context, addrKR *crypto.KeyRing) error {
var changes proton.DraftTemplate
if wantMessages[0].From != "" {
@ -311,7 +311,7 @@ func (s *scenario) drafAtIndexWasMovedToTrashForAddressOfAccount(draftIndex int,
defer cancel()
return s.t.withClient(ctx, username, func(ctx context.Context, c *proton.Client) error {
return s.t.withAddrKR(ctx, c, username, s.t.getUserAddrID(s.t.getUserID(username), address), func(_ context.Context, addrKR *crypto.KeyRing) error {
return s.t.withAddrKR(ctx, c, username, s.t.getUserByName(username).getAddrID(address), func(_ context.Context, addrKR *crypto.KeyRing) error {
if err := c.UnlabelMessages(ctx, []string{draftID}, proton.DraftsLabel); err != nil {
return fmt.Errorf("failed to unlabel draft")
}
@ -329,7 +329,7 @@ func (s *scenario) userLogsInWithUsernameAndPassword(username, password string)
if err != nil {
s.t.pushError(err)
} else {
if userID != s.t.getUserID(username) {
if userID != s.t.getUserByName(username).getUserID() {
return errors.New("user ID mismatch")
}
@ -338,18 +338,18 @@ func (s *scenario) userLogsInWithUsernameAndPassword(username, password string)
return err
}
s.t.setUserBridgePass(userID, info.BridgePass)
s.t.getUserByID(userID).setBridgePass(string(info.BridgePass))
}
return nil
}
func (s *scenario) userLogsOut(username string) error {
return s.t.bridge.LogoutUser(context.Background(), s.t.getUserID(username))
return s.t.bridge.LogoutUser(context.Background(), s.t.getUserByName(username).getUserID())
}
func (s *scenario) userIsDeleted(username string) error {
return s.t.bridge.DeleteUser(context.Background(), s.t.getUserID(username))
return s.t.bridge.DeleteUser(context.Background(), s.t.getUserByName(username).getUserID())
}
func (s *scenario) theAuthOfUserIsRevoked(username string) error {
@ -359,7 +359,7 @@ func (s *scenario) theAuthOfUserIsRevoked(username string) error {
}
func (s *scenario) userIsListedAndConnected(username string) error {
user, err := s.t.bridge.GetUserInfo(s.t.getUserID(username))
user, err := s.t.bridge.GetUserInfo(s.t.getUserByName(username).getUserID())
if err != nil {
return err
}
@ -382,7 +382,7 @@ func (s *scenario) userIsEventuallyListedAndConnected(username string) error {
}
func (s *scenario) userIsListedButNotConnected(username string) error {
user, err := s.t.bridge.GetUserInfo(s.t.getUserID(username))
user, err := s.t.bridge.GetUserInfo(s.t.getUserByName(username).getUserID())
if err != nil {
return err
}
@ -399,7 +399,7 @@ func (s *scenario) userIsListedButNotConnected(username string) error {
}
func (s *scenario) userIsNotListed(username string) error {
if slices.Contains(s.t.bridge.GetUserIDs(), s.t.getUserID(username)) {
if _, err := s.t.bridge.QueryUserInfo(username); !errors.Is(err, bridge.ErrNoSuchUser) {
return errors.New("user listed")
}
@ -411,7 +411,7 @@ func (s *scenario) userFinishesSyncing(username string) error {
}
func (s *scenario) addAdditionalAddressToAccount(username, address string, disabled bool) error {
userID := s.t.getUserID(username)
userID := s.t.getUserByName(username).getUserID()
// Decrypt the user's encrypted ID for use with quark.
userDecID, err := s.t.runQuarkCmd(context.Background(), "encryption:id", "--decrypt", userID)
@ -429,7 +429,7 @@ func (s *scenario) addAdditionalAddressToAccount(username, address string, disab
args = append(args,
string(userDecID),
s.t.getUserPass(userID),
s.t.getUserByID(userID).getUserPass(),
address,
)
@ -449,7 +449,7 @@ func (s *scenario) addAdditionalAddressToAccount(username, address string, disab
}
// Set the new address of the user.
s.t.setUserAddr(userID, addr[len(addr)-1].ID, address)
s.t.getUserByID(userID).addAddress(addr[len(addr)-1].ID, address)
return nil
})
@ -503,14 +503,11 @@ func (s *scenario) createUserAccount(username, password string, disabled bool) e
return err
}
// Set the ID of the user.
s.t.setUserID(username, user.ID)
// Set the password of the user.
s.t.setUserPass(user.ID, password)
// Add the test user.
s.t.addUser(user.ID, username, password)
// Set the address of the user.
s.t.setUserAddr(user.ID, addr[0].ID, addr[0].Email)
s.t.getUserByID(user.ID).addAddress(addr[0].ID, addr[0].Email)
return nil
})