wallet-sync-server/store/store_test.go

65 lines
1.4 KiB
Go
Raw Normal View History

package store
import (
2022-06-27 17:28:39 +02:00
"io/ioutil"
"os"
"testing"
2022-07-12 04:10:19 +02:00
"lbryio/lbry-id/auth"
)
2022-06-27 17:28:39 +02:00
func StoreTestInit(t *testing.T) (s Store, tmpFile *os.File) {
s = Store{}
2022-06-27 17:28:39 +02:00
tmpFile, err := ioutil.TempFile(os.TempDir(), "sqlite-test-")
if err != nil {
t.Fatalf("DB setup failure: %+v", err)
return
}
2022-06-27 17:28:39 +02:00
s.Init(tmpFile.Name())
2022-06-27 17:28:39 +02:00
err = s.Migrate()
if err != nil {
2022-06-27 17:28:39 +02:00
t.Fatalf("DB setup failure: %+v", err)
}
2022-06-27 17:28:39 +02:00
return
}
2022-06-27 17:28:39 +02:00
func StoreTestCleanup(tmpFile *os.File) {
if tmpFile != nil {
os.Remove(tmpFile.Name())
}
}
func makeTestUser(t *testing.T, s *Store) (userId auth.UserId, email auth.Email, password auth.Password, seed auth.ClientSaltSeed) {
// email with caps to trigger possible problems
email, password = auth.Email("Abc@Example.Com"), auth.Password("123")
normEmail := auth.NormalizedEmail("abc@example.com")
key, salt, err := password.Create()
if err != nil {
t.Fatalf("Error creating password")
}
seed = auth.ClientSaltSeed("abcd1234abcd1234")
rows, err := s.db.Query(
"INSERT INTO accounts (normalized_email, email, key, server_salt, client_salt_seed) values(?,?,?,?,?) returning user_id",
normEmail, email, key, salt, seed,
)
if err != nil {
t.Fatalf("Error setting up account: %+v", err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&userId)
if err != nil {
t.Fatalf("Error setting up account: %+v", err)
}
return
}
t.Fatalf("Error setting up account - no rows found")
return
}