From 6d34f39f12fcb9fc8c0190b9a7d2202f477eaec4 Mon Sep 17 00:00:00 2001 From: Daniel Krol Date: Fri, 24 Jun 2022 13:15:21 -0400 Subject: [PATCH] CreateAccount test and cleanup --- store/store.go | 8 -------- store/store_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/store/store.go b/store/store.go index 6c32346..a48f9b5 100644 --- a/store/store.go +++ b/store/store.go @@ -342,14 +342,6 @@ func (s *Store) CreateAccount(email auth.Email, password auth.Password) (err err var sqliteErr sqlite3.Error if errors.As(err, &sqliteErr) { - // I initially expected to need to check for ErrConstraintUnique. - // Maybe for psql it will be? - // TODO - is this right? Does the above comment explain that it's backwards - // from what I would have expected? Or did I do this backwards? - // Or is this a holdover from when an account was attached to a walletstate? - if errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintPrimaryKey) { - err = ErrDuplicateEmail - } if errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintUnique) { err = ErrDuplicateAccount } diff --git a/store/store_test.go b/store/store_test.go index fd91f9d..3f6f64d 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -453,8 +453,50 @@ func TestStoreGetWallet(t *testing.T) { } } +func expectAccountExists(t *testing.T, s *Store, email auth.Email, password auth.Password) { + _, err := s.GetUserId(email, password) + if err != nil { + t.Fatalf("Unexpected error in GetUserId: %+v", err) + } +} + +func expectAccountNotExists(t *testing.T, s *Store, email auth.Email, password auth.Password) { + _, err := s.GetUserId(email, password) + if err != ErrNoUId { + t.Fatalf("Expected ErrNoUId. err: %+v", err) + } +} + +// Test CreateAccount, using GetUserId as a helper +// Try CreateAccount twice with the same email and different password, error the second time func TestStoreCreateAccount(t *testing.T) { - t.Fatalf("Test me: Account create success and failures") + s, sqliteTmpFile := StoreTestInit(t) + defer StoreTestCleanup(sqliteTmpFile) + + email, password := auth.Email("abc@example.com"), auth.Password("123") + + // Get an account, come back empty + expectAccountNotExists(t, &s, email, password) + + // Create an account + if err := s.CreateAccount(email, password); err != nil { + t.Fatalf("Unexpected error in CreateAccount: %+v", err) + } + + // Get and confirm the account we just put in + expectAccountExists(t, &s, email, password) + + newPassword := auth.Password("xyz") + + // Try to create a new account with the same email and different password, + // fail because email already exists + if err := s.CreateAccount(email, newPassword); err != ErrDuplicateAccount { + t.Fatalf(`CreateAccount err: wanted "%+v", got "%+v"`, ErrDuplicateAccount, err) + } + + // Get the email and same *first* password we successfully put in, but not the second + expectAccountExists(t, &s, email, password) + expectAccountNotExists(t, &s, email, newPassword) } func TestStoreGetUserId(t *testing.T) {