wallet-sync-server/store/account_test.go
2022-06-27 11:39:44 -04:00

82 lines
2.7 KiB
Go

package store
import (
"testing"
"orblivion/lbry-id/auth"
)
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) {
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)
}
// Test GetUserId, using CreateAccount as a helper
// Try GetUserId before creating an account (fail), and after (succeed)
func TestStoreGetUserId(t *testing.T) {
s, sqliteTmpFile := StoreTestInit(t)
defer StoreTestCleanup(sqliteTmpFile)
email, password := auth.Email("abc@example.com"), auth.Password("123")
// Check that there's no user id for email and password first
if userId, err := s.GetUserId(email, password); err != ErrNoUId || userId != 0 {
t.Fatalf(`CreateAccount err: wanted "%+v", got "%+v. userId: %v"`, ErrNoUId, err, userId)
}
// Create the account
_ = s.CreateAccount(email, password)
// Check that there's now a user id for the email and password
if userId, err := s.GetUserId(email, password); err != nil || userId == 0 {
t.Fatalf("Unexpected error in GetUserId: err: %+v userId: %v", err, userId)
}
}
// TODO - Tests each db method. Check for missing "NOT NULL" fields. Do the loop thing, and always just check for null error.
func TestStoreAccountEmptyFields(t *testing.T) {
// Make sure expiration doesn't get set if sanitization fails
t.Fatalf("Test me")
}