test: add test with changing address order

This commit is contained in:
James Houlahan
2020-05-06 09:57:34 +02:00
parent c61e8bdc71
commit 2d200f6f8c
19 changed files with 202 additions and 23 deletions

View File

@ -146,6 +146,10 @@ func (a *TestAccount) AddressID() string {
return a.addressToBeUsed.ID
}
func (a *TestAccount) GetAddressID(addressTestID string) string {
return a.addressesByBDDAddressID[addressTestID].ID
}
// EnsureAddressID accepts address (simply the address) or bddAddressID used
// in tests (in format [bddAddressID]) and returns always the real address ID.
// If the address is not found, the ID of main address is returned.
@ -163,6 +167,10 @@ func (a *TestAccount) EnsureAddressID(addressOrAddressTestID string) string {
return a.AddressID()
}
func (a *TestAccount) GetAddress(addressTestID string) string {
return a.addressesByBDDAddressID[addressTestID].Email
}
// EnsureAddress accepts address (simply the address) or bddAddressID used
// in tests (in format [bddAddressID]) and returns always the address.
// If the address ID cannot be found, the original value is returned.

View File

@ -55,7 +55,8 @@ func (a *TestAccounts) GetTestAccount(username string) *TestAccount {
return a.GetTestAccountWithAddress(username, "")
}
func (a *TestAccounts) GetTestAccountWithAddress(username, addressID string) *TestAccount {
// GetTestAccount returns the test account with the given username configured to use the given bddAddressID.
func (a *TestAccounts) GetTestAccountWithAddress(username, bddAddressID string) *TestAccount {
// Do lookup by full address and convert to name in tests.
// Used by getting real data to ensure correct address or address ID.
for key, user := range a.Users {
@ -71,7 +72,7 @@ func (a *TestAccounts) GetTestAccountWithAddress(username, addressID string) *Te
return newTestAccount(
user,
a.Addresses[user.Name],
addressID,
bddAddressID,
a.Passwords[user.Name],
a.MailboxPasswords[user.Name],
a.TwoFAs[user.Name],

View File

@ -35,6 +35,7 @@ func BridgeActionsFeatureContext(s *godog.Suite) {
s.Step(`^the internet connection is lost$`, theInternetConnectionIsLost)
s.Step(`^the internet connection is restored$`, theInternetConnectionIsRestored)
s.Step(`^(\d+) seconds pass$`, secondsPass)
s.Step(`^"([^"]*)" swaps address "([^"]*)" with address "([^"]*)"$`, swapsAddressWithAddress)
}
func bridgeStarts() error {
@ -132,3 +133,32 @@ func secondsPass(seconds int) error {
time.Sleep(time.Duration(seconds) * time.Second)
return nil
}
func swapsAddressWithAddress(bddUserID, bddAddressID1, bddAddressID2 string) error {
account := ctx.GetTestAccount(bddUserID)
if account == nil {
return godog.ErrPending
}
address1ID := account.GetAddressID(bddAddressID1)
address2ID := account.GetAddressID(bddAddressID2)
var address1Index, address2Index int
var addressIDs []string
for i, v := range *account.Addresses() {
if v.ID == address1ID {
address1Index = i
}
if v.ID == address2ID {
address2Index = i
}
addressIDs = append(addressIDs, v.ID)
}
addressIDs[address1Index], addressIDs[address2Index] = addressIDs[address2Index], addressIDs[address1Index]
ctx.ReorderAddresses(account.Username(), bddAddressID1, bddAddressID2)
ctx.GetPMAPIController().ReorderAddresses(account.User(), addressIDs)
return nil
}

View File

@ -36,8 +36,8 @@ func (ctx *TestContext) GetTestAccount(bddUserID string) *accounts.TestAccount {
return ctx.testAccounts.GetTestAccount(bddUserID)
}
func (ctx *TestContext) GetTestAccountWithAddress(bddUserID, addressID string) *accounts.TestAccount {
return ctx.testAccounts.GetTestAccountWithAddress(bddUserID, addressID)
func (ctx *TestContext) GetTestAccountWithAddress(bddUserID, bddAddressID string) *accounts.TestAccount {
return ctx.testAccounts.GetTestAccountWithAddress(bddUserID, bddAddressID)
}
func (ctx *TestContext) EnsureAddressID(bddUserID, addressOrAddressTestID string) string {
@ -55,3 +55,9 @@ func (ctx *TestContext) EnsureAddress(bddUserID, addressOrAddressTestID string)
}
return account.EnsureAddress(addressOrAddressTestID)
}
func (ctx *TestContext) ReorderAddresses(userID, addressA, addressB string) {
addresses := ctx.testAccounts.Addresses[userID]
addresses[addressA], addresses[addressB] = addresses[addressB], addresses[addressA]
}

View File

@ -33,6 +33,7 @@ type PMAPIController interface {
GetLabelIDs(username string, labelNames []string) ([]string, error)
AddUserMessage(username string, message *pmapi.Message) error
GetMessageID(username, messageIndex string) string
ReorderAddresses(user *pmapi.User, addressIDs []string) error
PrintCalls()
WasCalled(method, path string, expectedRequest []byte) bool
GetCalls(method, path string) [][]byte

View File

@ -18,6 +18,7 @@
package fakeapi
import (
"errors"
"fmt"
"strings"
@ -44,6 +45,15 @@ func (ctl *Controller) TurnInternetConnectionOn() {
ctl.noInternetConnection = false
}
func (ctl *Controller) ReorderAddresses(user *pmapi.User, addressIDs []string) error {
api := ctl.getFakeAPIForUser(user.ID)
if api == nil {
return errors.New("no such user")
}
return api.ReorderAddresses(addressIDs)
}
func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, password string, twoFAEnabled bool) error {
ctl.usersByUsername[user.Name] = &fakeUser{
user: user,
@ -131,6 +141,15 @@ func (ctl *Controller) AddUserMessage(username string, message *pmapi.Message) e
return nil
}
func (ctl *Controller) getFakeAPIForUser(userID string) *FakePMAPI {
for _, fakeAPI := range ctl.fakeAPIs {
if fakeAPI.userID == userID {
return fakeAPI
}
}
return nil
}
func (ctl *Controller) resetUsers() {
for _, fakeAPI := range ctl.fakeAPIs {
_ = fakeAPI.setUser(fakeAPI.username)

View File

@ -40,20 +40,24 @@ func (api *FakePMAPI) GetEvent(eventID string) (*pmapi.Event, error) {
foundEvent = event
continue
}
if foundEvent != nil {
mergedEvent.EventID = event.EventID
mergedEvent.Refresh |= event.Refresh
mergedEvent.Messages = append(mergedEvent.Messages, event.Messages...)
mergedEvent.MessageCounts = append(mergedEvent.MessageCounts, event.MessageCounts...)
mergedEvent.Labels = append(mergedEvent.Labels, event.Labels...)
mergedEvent.Addresses = append(mergedEvent.Addresses, event.Addresses...)
mergedEvent.Notices = append(mergedEvent.Notices, event.Notices...)
mergedEvent.User = event.User
}
}
// If there isn't next event, return the same one.
if mergedEvent.EventID == "" {
return foundEvent, nil
}
return mergedEvent, nil
}
@ -103,6 +107,19 @@ func (api *FakePMAPI) addEventMessage(action pmapi.EventAction, message *pmapi.M
})
}
func (api *FakePMAPI) addEventAddress(action pmapi.EventAction, address *pmapi.Address) {
api.addEvent(&pmapi.Event{
EventID: api.eventIDGenerator.next("event"),
Addresses: []*pmapi.EventAddress{{
EventItem: pmapi.EventItem{
ID: address.ID,
Action: pmapi.EventUpdate,
},
Address: address,
}},
})
}
func (api *FakePMAPI) addEvent(event *pmapi.Event) {
api.events = append(api.events, event)
}

View File

@ -57,6 +57,29 @@ func (api *FakePMAPI) GetAddresses() (pmapi.AddressList, error) {
return *api.addresses, nil
}
func (api *FakePMAPI) ReorderAddresses(addressIDs []string) error {
if err := api.checkAndRecordCall(PUT, "/addresses/order", nil); err != nil {
return err
}
for wantedIndex, addressID := range addressIDs {
var currentIndex int
for i, v := range *api.addresses {
if v.ID == addressID {
currentIndex = i
break
}
}
(*api.addresses)[wantedIndex], (*api.addresses)[currentIndex] = (*api.addresses)[currentIndex], (*api.addresses)[wantedIndex]
(*api.addresses)[wantedIndex].Order = wantedIndex + 1 // Starts counting from 1.
api.addEventAddress(pmapi.EventUpdate, (*api.addresses)[wantedIndex])
}
return nil
}
func (api *FakePMAPI) Addresses() pmapi.AddressList {
return *api.addresses
}

View File

@ -44,3 +44,12 @@ Feature: Address mode
| from | to | subject |
| john.doe@mail.com | [primary] | foo |
| jane.doe@mail.com | [secondary] | bar |
Scenario: Make secondary address primary in combined mode
Given there is "userMoreAddresses" in "combined" address mode
When "userMoreAddresses" swaps address "primary" with address "secondary"
And "userMoreAddresses" receives an address event
Then mailbox "Folders/mbox" for address "primary" of "userMoreAddresses" has messages
| from | to | subject |
| john.doe@mail.com | [primary] | foo |
| jane.doe@mail.com | [secondary] | bar |

View File

@ -24,7 +24,7 @@ import (
"github.com/pkg/errors"
)
func cleanup(client pmapi.Client) error {
func cleanup(client pmapi.Client, addresses *pmapi.AddressList) error {
if err := cleanSystemFolders(client); err != nil {
return errors.Wrap(err, "failed to clean system folders")
}
@ -34,6 +34,9 @@ func cleanup(client pmapi.Client) error {
if err := cleanTrash(client); err != nil {
return errors.Wrap(err, "failed to clean trash")
}
if err := reorderAddresses(client, addresses); err != nil {
return errors.Wrap(err, "failed to clean trash")
}
return nil
}
@ -130,3 +133,13 @@ func emptyFolder(client pmapi.Client, labelID string) error {
}
return nil
}
func reorderAddresses(client pmapi.Client, addresses *pmapi.AddressList) error {
addressIDs := []string{}
for _, address := range *addresses {
addressIDs = append(addressIDs, address.ID)
}
return client.ReorderAddresses(addressIDs)
}

View File

@ -50,7 +50,7 @@ func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, p
return errors.Wrap(err, "failed to unlock addresses")
}
if err := cleanup(client); err != nil {
if err := cleanup(client, addresses); err != nil {
return errors.Wrap(err, "failed to clean user")
}
@ -58,3 +58,9 @@ func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, p
return nil
}
func (ctl *Controller) ReorderAddresses(user *pmapi.User, addressIDs []string) error {
client := ctl.clientManager.GetClient(user.ID)
return client.ReorderAddresses(addressIDs)
}

View File

@ -18,11 +18,15 @@
package tests
import (
"time"
"github.com/cucumber/godog"
"github.com/stretchr/testify/assert"
)
func StoreActionsFeatureContext(s *godog.Suite) {
s.Step(`^the event loop of "([^"]*)" loops once$`, theEventLoopLoops)
s.Step(`^"([^"]*)" receives an address event$`, receivesAnAddressEvent)
}
func theEventLoopLoops(username string) error {
@ -37,3 +41,19 @@ func theEventLoopLoops(username string) error {
store.TestPollNow()
return nil
}
func receivesAnAddressEvent(username string) error {
acc := ctx.GetTestAccount(username)
if acc == nil {
return godog.ErrPending
}
store, err := ctx.GetStore(acc.Username())
if err != nil {
return internalError(err, "getting store of user %s", username)
}
assert.Eventually(ctx.GetTestingT(), func() bool {
store.TestPollNow()
return len(store.TestGetLastEvent().Addresses) > 0
}, 5*time.Second, time.Second)
return nil
}