add: keystore tests

This commit is contained in:
Mohamed Sohail 2023-01-10 11:09:54 +00:00
parent e53cb8a6c1
commit 952535ca2f
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
3 changed files with 27 additions and 15 deletions

View File

@ -30,4 +30,4 @@ minconn = 5
[asynq] [asynq]
concurrency = 25 concurrency = 25
debug = false debug = false
dsn = "redis://localhost:6379/0" dsn = "redis://redis:6379/0"

View File

@ -7,47 +7,59 @@ import (
"github.com/grassrootseconomics/cic-custodial/pkg/keypair" "github.com/grassrootseconomics/cic-custodial/pkg/keypair"
"github.com/grassrootseconomics/cic-custodial/pkg/logg" "github.com/grassrootseconomics/cic-custodial/pkg/logg"
"github.com/grassrootseconomics/cic-custodial/pkg/postgres" "github.com/grassrootseconomics/cic-custodial/pkg/postgres"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/zerodha/logf"
) )
const ( const (
testDsn = "postgres://postgres:postgres@localhost:5432/cic_custodial" testDsn = "postgres://postgres:postgres@localhost:5432/cic_custodial"
) )
type ItKeystoreSuite struct { type itKeystoreSuite struct {
suite.Suite suite.Suite
Keystore Keystore keystore Keystore
pgPool *pgxpool.Pool
logg logf.Logger
} }
func TestItKeystoreSuite(t *testing.T) { func TestItKeystoreSuite(t *testing.T) {
suite.Run(t, new(ItKeystoreSuite)) suite.Run(t, new(itKeystoreSuite))
} }
func (s *ItKeystoreSuite) SetupSuite() { func (s *itKeystoreSuite) SetupSuite() {
logg := logg.NewLogg(logg.LoggOpts{
Debug: true,
Caller: true,
})
pgPool, err := postgres.NewPostgresPool(postgres.PostgresPoolOpts{ pgPool, err := postgres.NewPostgresPool(postgres.PostgresPoolOpts{
DSN: testDsn, DSN: testDsn,
}) })
s.Require().NoError(err) s.Require().NoError(err)
s.pgPool = pgPool
s.logg = logg
ks, err := NewPostgresKeytore(Opts{ s.keystore, err = NewPostgresKeytore(Opts{
PostgresPool: pgPool, PostgresPool: pgPool,
Logg: logg.NewLogg(logg.LoggOpts{ Logg: logg,
Debug: true,
Caller: true,
}),
}) })
s.Require().NoError(err) s.Require().NoError(err)
s.Keystore = ks
} }
func (s *ItKeystoreSuite) Test_Write_And_Load_KeyPair() { func (s *itKeystoreSuite) TearDownSuite() {
_, err := s.pgPool.Exec(context.Background(), "DROP TABLE IF EXISTS keystore")
s.Require().NoError(err)
}
func (s *itKeystoreSuite) Test_Write_And_Load_KeyPair() {
ctx := context.Background() ctx := context.Background()
keypair, err := keypair.Generate() keypair, err := keypair.Generate()
s.NoError(err) s.NoError(err)
err = s.Keystore.WriteKeyPair(ctx, keypair) err = s.keystore.WriteKeyPair(ctx, keypair)
s.NoError(err) s.NoError(err)
_, err = s.Keystore.LoadPrivateKey(ctx, keypair.Public) _, err = s.keystore.LoadPrivateKey(ctx, keypair.Public)
s.NoError(err) s.NoError(err)
} }

View File

@ -8,7 +8,7 @@ import (
) )
func applyMigration(dbPool *pgxpool.Pool) error { func applyMigration(dbPool *pgxpool.Pool) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel() defer cancel()
_, err := dbPool.Exec(ctx, ` _, err := dbPool.Exec(ctx, `