wallet-sync-server/store/store_test.go

71 lines
1.6 KiB
Go
Raw Permalink Normal View History

package store
import (
2022-06-27 17:28:39 +02:00
"io/ioutil"
"os"
"testing"
"time"
"lbryio/wallet-sync-server/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,
verifyToken *auth.VerifyTokenString,
verifyExpiration *time.Time,
) (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, verify_token, verify_expiration, updated) values(?,?,?,?,?,?,?, datetime('now')) returning user_id",
normEmail, email, key, salt, seed, verifyToken, verifyExpiration,
)
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
}