cic-custodial/internal/keystore/keystore_pg_test.go

66 lines
1.4 KiB
Go
Raw Permalink Normal View History

package keystore
import (
"context"
"testing"
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
"github.com/grassrootseconomics/cic-custodial/pkg/logg"
"github.com/grassrootseconomics/cic-custodial/pkg/postgres"
2023-01-10 12:09:54 +01:00
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/suite"
2023-01-10 12:09:54 +01:00
"github.com/zerodha/logf"
)
const (
testDsn = "postgres://postgres:postgres@localhost:5432/cic_custodial"
)
2023-01-10 12:09:54 +01:00
type itKeystoreSuite struct {
suite.Suite
2023-01-10 12:09:54 +01:00
keystore Keystore
pgPool *pgxpool.Pool
logg logf.Logger
}
func TestItKeystoreSuite(t *testing.T) {
2023-01-10 12:09:54 +01:00
suite.Run(t, new(itKeystoreSuite))
}
2023-01-10 12:09:54 +01:00
func (s *itKeystoreSuite) SetupSuite() {
logg := logg.NewLogg(logg.LoggOpts{
Debug: true,
Caller: true,
})
pgPool, err := postgres.NewPostgresPool(postgres.PostgresPoolOpts{
DSN: testDsn,
})
s.Require().NoError(err)
2023-01-10 12:09:54 +01:00
s.pgPool = pgPool
s.logg = logg
2023-01-10 12:09:54 +01:00
s.keystore, err = NewPostgresKeytore(Opts{
PostgresPool: pgPool,
2023-01-10 12:09:54 +01:00
Logg: logg,
})
s.Require().NoError(err)
}
2023-01-10 12:09:54 +01:00
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()
keypair, err := keypair.Generate()
s.NoError(err)
2023-01-10 12:09:54 +01:00
err = s.keystore.WriteKeyPair(ctx, keypair)
s.NoError(err)
2023-01-10 12:09:54 +01:00
_, err = s.keystore.LoadPrivateKey(ctx, keypair.Public)
s.NoError(err)
}