2021-12-25 02:16:58 +01:00
|
|
|
package store
|
2021-12-19 23:24:43 +01:00
|
|
|
|
|
|
|
import (
|
2022-06-27 17:28:39 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-12-19 23:24:43 +01:00
|
|
|
"testing"
|
2022-07-30 21:06:27 +02:00
|
|
|
"time"
|
2022-06-29 06:06:43 +02:00
|
|
|
|
2022-08-22 18:05:53 +02:00
|
|
|
"lbryio/wallet-sync-server/auth"
|
2021-12-19 23:24:43 +01:00
|
|
|
)
|
|
|
|
|
2022-06-27 17:28:39 +02:00
|
|
|
func StoreTestInit(t *testing.T) (s Store, tmpFile *os.File) {
|
|
|
|
s = Store{}
|
2021-12-19 23:24:43 +01:00
|
|
|
|
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
|
2021-12-19 23:24:43 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 17:28:39 +02:00
|
|
|
s.Init(tmpFile.Name())
|
2022-06-07 19:25:14 +02:00
|
|
|
|
2022-06-27 17:28:39 +02:00
|
|
|
err = s.Migrate()
|
2021-12-19 23:24:43 +01:00
|
|
|
if err != nil {
|
2022-06-27 17:28:39 +02:00
|
|
|
t.Fatalf("DB setup failure: %+v", err)
|
2021-12-19 23:24:43 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 17:28:39 +02:00
|
|
|
return
|
|
|
|
}
|
2021-12-19 23:24:43 +01:00
|
|
|
|
2022-06-27 17:28:39 +02:00
|
|
|
func StoreTestCleanup(tmpFile *os.File) {
|
|
|
|
if tmpFile != nil {
|
|
|
|
os.Remove(tmpFile.Name())
|
2021-12-19 23:24:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-29 06:06:43 +02:00
|
|
|
|
2022-07-30 21:06:27 +02:00
|
|
|
func makeTestUser(
|
|
|
|
t *testing.T,
|
|
|
|
s *Store,
|
2022-08-14 04:15:19 +02:00
|
|
|
verifyToken *auth.VerifyTokenString,
|
2022-07-30 21:06:27 +02:00
|
|
|
verifyExpiration *time.Time,
|
|
|
|
) (userId auth.UserId, email auth.Email, password auth.Password, seed auth.ClientSaltSeed) {
|
2022-07-22 03:19:24 +02:00
|
|
|
// email with caps to trigger possible problems
|
|
|
|
email, password = auth.Email("Abc@Example.Com"), auth.Password("123")
|
|
|
|
normEmail := auth.NormalizedEmail("abc@example.com")
|
2022-07-13 18:32:48 +02:00
|
|
|
key, salt, err := password.Create()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating password")
|
|
|
|
}
|
2022-06-29 06:06:43 +02:00
|
|
|
|
2022-07-15 21:36:11 +02:00
|
|
|
seed = auth.ClientSaltSeed("abcd1234abcd1234")
|
|
|
|
|
2022-06-29 06:06:43 +02:00
|
|
|
rows, err := s.db.Query(
|
2022-08-23 19:34:31 +02:00
|
|
|
"INSERT INTO accounts (normalized_email, email, key, server_salt, client_salt_seed, verify_token, verify_expiration, updated) values(?,?,?,?,?,?,?, datetime('now')) returning user_id",
|
2022-07-30 21:06:27 +02:00
|
|
|
normEmail, email, key, salt, seed, verifyToken, verifyExpiration,
|
2022-06-29 06:06:43 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-07-13 18:32:48 +02:00
|
|
|
t.Fatalf("Error setting up account: %+v", err)
|
2022-06-29 06:06:43 +02:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&userId)
|
|
|
|
if err != nil {
|
2022-07-13 18:32:48 +02:00
|
|
|
t.Fatalf("Error setting up account: %+v", err)
|
2022-06-29 06:06:43 +02:00
|
|
|
}
|
2022-07-01 00:44:59 +02:00
|
|
|
return
|
2022-06-29 06:06:43 +02:00
|
|
|
}
|
2022-07-13 18:32:48 +02:00
|
|
|
t.Fatalf("Error setting up account - no rows found")
|
2022-07-01 00:44:59 +02:00
|
|
|
return
|
2022-06-29 06:06:43 +02:00
|
|
|
}
|