From b9b4c1c38de3f8ddf3c9d0c8cc55928b2fea1ab0 Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Tue, 13 Dec 2022 01:33:41 +0100 Subject: [PATCH] GODT-2181(test): Use [user:NAME] for test user name --- tests/bdd_test.go | 28 ++++++++++++++++++++++++--- tests/ctx_test.go | 32 +++++++++++++++---------------- tests/features/user/login.feature | 32 +++++++++++++++---------------- tests/user_test.go | 2 +- 4 files changed, 57 insertions(+), 37 deletions(-) diff --git a/tests/bdd_test.go b/tests/bdd_test.go index 088e7a03..05f3d3a5 100644 --- a/tests/bdd_test.go +++ b/tests/bdd_test.go @@ -31,10 +31,33 @@ type scenario struct { t *testCtx } +// reset resets the test context for a new scenario. func (s *scenario) reset(tb testing.TB) { s.t = newTestCtx(tb) } +// replace replaces the placeholders in the scenario with the values from the test context. +func (s *scenario) replace(sc *godog.Scenario) { + for _, step := range sc.Steps { + step.Text = s.t.replace(step.Text) + + if arg := step.Argument; arg != nil { + if table := arg.DataTable; table != nil { + for _, row := range table.Rows { + for _, cell := range row.Cells { + cell.Value = s.t.replace(cell.Value) + } + } + } + + if doc := arg.DocString; doc != nil { + doc.Content = s.t.replace(doc.Content) + } + } + } +} + +// close closes the test context. func (s *scenario) close(_ testing.TB) { s.t.close(context.Background()) } @@ -54,8 +77,9 @@ func TestFeatures(testingT *testing.T) { }, ScenarioInitializer: func(ctx *godog.ScenarioContext) { - ctx.Before(func(ctx context.Context, _ *godog.Scenario) (context.Context, error) { + ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) { s.reset(testingT) + s.replace(sc) return ctx, nil }) @@ -66,9 +90,7 @@ func TestFeatures(testingT *testing.T) { ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) { logrus.Debugf("Running step: %s", st.Text) - s.t.beforeStep(st) - return ctx, nil }) diff --git a/tests/ctx_test.go b/tests/ctx_test.go index 606c6401..d1cf9377 100644 --- a/tests/ctx_test.go +++ b/tests/ctx_test.go @@ -37,6 +37,7 @@ import ( "github.com/bradenaw/juniper/xslices" "github.com/cucumber/godog" "github.com/emersion/go-imap/client" + "github.com/google/uuid" "github.com/sirupsen/logrus" "golang.org/x/exp/maps" "google.golang.org/grpc" @@ -69,6 +70,7 @@ type testCtx struct { clientEventCh *queue.QueuedChannel[*frontend.StreamEvent] // These maps hold expected userIDByName, their primary addresses and bridge passwords. + userUUIDByName map[string]string userIDByName map[string]string userAddrByEmail map[string]map[string]string userPassByID map[string]string @@ -111,6 +113,7 @@ func newTestCtx(tb testing.TB) *testCtx { events: newEventCollector(), reporter: newReportRecorder(tb), + userUUIDByName: make(map[string]string), userIDByName: make(map[string]string), userAddrByEmail: make(map[string]map[string]string), userPassByID: make(map[string]string), @@ -137,26 +140,22 @@ func (t *testCtx) replace(value string) string { // Replace [domain] with the domain of the test server. value = strings.ReplaceAll(value, "[domain]", t.api.GetDomain()) + // Replace [user:NAME] with a unique ID for the user NAME. + value = regexp.MustCompile(`\[user:(\w+)\]`).ReplaceAllStringFunc(value, func(match string) string { + name := regexp.MustCompile(`\[user:(\w+)\]`).FindStringSubmatch(match)[1] + + // Create a new user if it doesn't exist yet. + if _, ok := t.userUUIDByName[name]; !ok { + t.userUUIDByName[name] = uuid.NewString() + } + + return t.userUUIDByName[name] + }) + return value } func (t *testCtx) beforeStep(st *godog.Step) { - st.Text = t.replace(st.Text) - - if argument := st.Argument; argument != nil { - if table := argument.DataTable; table != nil { - for _, row := range table.Rows { - for _, cell := range row.Cells { - cell.Value = t.replace(cell.Value) - } - } - } - - if doc := argument.DocString; doc != nil { - doc.Content = t.replace(doc.Content) - } - } - t.callsLock.Lock() defer t.callsLock.Unlock() @@ -165,7 +164,6 @@ func (t *testCtx) beforeStep(st *godog.Step) { t.calls = append(t.calls, nil) t.errors = append(t.errors, nil) - } func (t *testCtx) getName(wantUserID string) string { diff --git a/tests/features/user/login.feature b/tests/features/user/login.feature index ceb9c226..5591bc6a 100644 --- a/tests/features/user/login.feature +++ b/tests/features/user/login.feature @@ -1,36 +1,36 @@ Feature: A user can login Background: - Given there exists an account with username "user" and password "password" + Given there exists an account with username "[user:user]" and password "password" And bridge starts Scenario: Login to account - When the user logs in with username "user" and password "password" - Then user "user" is listed and connected + When the user logs in with username "[user:user]" and password "password" + Then user "[user:user]" is listed and connected Scenario: Login to account with wrong password - When the user logs in with username "user" and password "wrong" - Then user "user" is not listed + When the user logs in with username "[user:user]" and password "wrong" + Then user "[user:user]" is not listed Scenario: Login to nonexistent account - When the user logs in with username "other" and password "unknown" - Then user "other" is not listed + When the user logs in with username "[user:other]" and password "unknown" + Then user "[user:other]" is not listed Scenario: Login to account without internet Given the internet is turned off - When the user logs in with username "user" and password "password" - Then user "user" is not listed + When the user logs in with username "[user:user]" and password "password" + Then user "[user:user]" is not listed Scenario: Login to account without internet but the connection is later restored - When the user logs in with username "user" and password "password" + When the user logs in with username "[user:user]" and password "password" And bridge stops And the internet is turned off And bridge starts And the internet is turned on - Then user "user" is eventually listed and connected + Then user "[user:user]" is eventually listed and connected Scenario: Login to multiple accounts - Given there exists an account with username "additional" and password "other" - When the user logs in with username "user" and password "password" - And the user logs in with username "additional" and password "other" - Then user "user" is listed and connected - And user "additional" is listed and connected \ No newline at end of file + Given there exists an account with username "[user:additional]" and password "password" + When the user logs in with username "[user:user]" and password "password" + And the user logs in with username "[user:additional]" and password "password" + Then user "[user:user]" is listed and connected + And user "[user:additional]" is listed and connected \ No newline at end of file diff --git a/tests/user_test.go b/tests/user_test.go index 61c6d9ef..19da22b4 100644 --- a/tests/user_test.go +++ b/tests/user_test.go @@ -76,10 +76,10 @@ func (s *scenario) theAccountHasAdditionalAddress(username, address string) erro if err := s.t.runQuarkCmd( context.Background(), "user:create:address", + "--gen-keys", "RSA2048", userID, s.t.getUserPass(userID), address, - "--gen-keys", "RSA2048", ); err != nil { return err }