CreateAccount test and cleanup

This commit is contained in:
Daniel Krol 2022-06-24 13:15:21 -04:00
parent 55b4db0fe8
commit 6d34f39f12
2 changed files with 43 additions and 9 deletions

View file

@ -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
}

View file

@ -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) {