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