Skip to content

Commit

Permalink
Split up service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhuaaa committed Sep 12, 2024
1 parent 1875117 commit 58c6ad6
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 159 deletions.
99 changes: 99 additions & 0 deletions pkg/api/publish_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package api_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/xmtp/xmtpd/pkg/db/queries"
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/message_api"
"github.com/xmtp/xmtpd/pkg/testutils"
"google.golang.org/protobuf/proto"
)

func TestPublishEnvelope(t *testing.T) {
svc, db, cleanup := testutils.NewTestService(t)
defer cleanup()

resp, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: testutils.CreatePayerEnvelope(t),
},
)
require.NoError(t, err)
require.NotNil(t, resp)

unsignedEnv := &message_api.UnsignedOriginatorEnvelope{}
require.NoError(
t,
proto.Unmarshal(resp.GetOriginatorEnvelope().GetUnsignedOriginatorEnvelope(), unsignedEnv),
)
clientEnv := &message_api.ClientEnvelope{}
require.NoError(
t,
proto.Unmarshal(unsignedEnv.GetPayerEnvelope().GetUnsignedClientEnvelope(), clientEnv),
)
require.Equal(t, uint8(0x5), clientEnv.Aad.GetTargetTopic()[0])

// Check that the envelope was published to the database after a delay
require.Eventually(t, func() bool {
envs, err := queries.New(db).
SelectGatewayEnvelopes(context.Background(), queries.SelectGatewayEnvelopesParams{})
require.NoError(t, err)

if len(envs) != 1 {
return false
}

originatorEnv := &message_api.OriginatorEnvelope{}
require.NoError(t, proto.Unmarshal(envs[0].OriginatorEnvelope, originatorEnv))
return proto.Equal(originatorEnv, resp.GetOriginatorEnvelope())
}, 500*time.Millisecond, 50*time.Millisecond)
}

func TestUnmarshalErrorOnPublish(t *testing.T) {
svc, _, cleanup := testutils.NewTestService(t)
defer cleanup()

envelope := testutils.CreatePayerEnvelope(t)
envelope.UnsignedClientEnvelope = []byte("invalidbytes")
_, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: envelope,
},
)
require.ErrorContains(t, err, "unmarshal")
}

func TestMismatchingOriginatorOnPublish(t *testing.T) {
svc, _, cleanup := testutils.NewTestService(t)
defer cleanup()

clientEnv := testutils.CreateClientEnvelope()
clientEnv.Aad.TargetOriginator = 2
_, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: testutils.CreatePayerEnvelope(t, clientEnv),
},
)
require.ErrorContains(t, err, "originator")
}

func TestMissingTopicOnPublish(t *testing.T) {
svc, _, cleanup := testutils.NewTestService(t)
defer cleanup()

clientEnv := testutils.CreateClientEnvelope()
clientEnv.Aad.TargetTopic = nil
_, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: testutils.CreatePayerEnvelope(t, clientEnv),
},
)
require.ErrorContains(t, err, "topic")
}
129 changes: 7 additions & 122 deletions pkg/api/service_test.go → pkg/api/query_test.go
Original file line number Diff line number Diff line change
@@ -1,131 +1,16 @@
package api
package api_test

import (
"context"
"database/sql"
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
"github.com/xmtp/xmtpd/pkg/db/queries"
mocks "github.com/xmtp/xmtpd/pkg/mocks/registry"
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/message_api"
"github.com/xmtp/xmtpd/pkg/registrant"
"github.com/xmtp/xmtpd/pkg/registry"
"github.com/xmtp/xmtpd/pkg/testutils"
"google.golang.org/protobuf/proto"
)

func newTestService(t *testing.T) (*Service, *sql.DB, func()) {
ctx := context.Background()
log := testutils.NewLog(t)
db, _, dbCleanup := testutils.NewDB(t, ctx)
privKey, err := crypto.GenerateKey()
require.NoError(t, err)
privKeyStr := "0x" + testutils.HexEncode(crypto.FromECDSA(privKey))
mockRegistry := mocks.NewMockNodeRegistry(t)
mockRegistry.EXPECT().GetNodes().Return([]registry.Node{
{NodeID: 1, SigningKey: &privKey.PublicKey},
}, nil)
registrant, err := registrant.NewRegistrant(ctx, queries.New(db), mockRegistry, privKeyStr)
require.NoError(t, err)

svc, err := NewReplicationApiService(ctx, log, registrant, db)
require.NoError(t, err)

return svc, db, func() {
svc.Close()
dbCleanup()
}
}

func TestPublishEnvelope(t *testing.T) {
svc, db, cleanup := newTestService(t)
defer cleanup()

resp, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: testutils.CreatePayerEnvelope(t),
},
)
require.NoError(t, err)
require.NotNil(t, resp)

unsignedEnv := &message_api.UnsignedOriginatorEnvelope{}
require.NoError(
t,
proto.Unmarshal(resp.GetOriginatorEnvelope().GetUnsignedOriginatorEnvelope(), unsignedEnv),
)
clientEnv := &message_api.ClientEnvelope{}
require.NoError(
t,
proto.Unmarshal(unsignedEnv.GetPayerEnvelope().GetUnsignedClientEnvelope(), clientEnv),
)
require.Equal(t, uint8(0x5), clientEnv.Aad.GetTargetTopic()[0])

// Check that the envelope was published to the database after a delay
require.Eventually(t, func() bool {
envs, err := queries.New(db).
SelectGatewayEnvelopes(context.Background(), queries.SelectGatewayEnvelopesParams{})
require.NoError(t, err)

if len(envs) != 1 {
return false
}

originatorEnv := &message_api.OriginatorEnvelope{}
require.NoError(t, proto.Unmarshal(envs[0].OriginatorEnvelope, originatorEnv))
return proto.Equal(originatorEnv, resp.GetOriginatorEnvelope())
}, 500*time.Millisecond, 50*time.Millisecond)
}

func TestUnmarshalErrorOnPublish(t *testing.T) {
svc, _, cleanup := newTestService(t)
defer cleanup()

envelope := testutils.CreatePayerEnvelope(t)
envelope.UnsignedClientEnvelope = []byte("invalidbytes")
_, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: envelope,
},
)
require.ErrorContains(t, err, "unmarshal")
}

func TestMismatchingOriginatorOnPublish(t *testing.T) {
svc, _, cleanup := newTestService(t)
defer cleanup()

clientEnv := testutils.CreateClientEnvelope()
clientEnv.Aad.TargetOriginator = 2
_, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: testutils.CreatePayerEnvelope(t, clientEnv),
},
)
require.ErrorContains(t, err, "originator")
}

func TestMissingTopicOnPublish(t *testing.T) {
svc, _, cleanup := newTestService(t)
defer cleanup()

clientEnv := testutils.CreateClientEnvelope()
clientEnv.Aad.TargetTopic = nil
_, err := svc.PublishEnvelope(
context.Background(),
&message_api.PublishEnvelopeRequest{
PayerEnvelope: testutils.CreatePayerEnvelope(t, clientEnv),
},
)
require.ErrorContains(t, err, "topic")
}

func setupQueryTest(t *testing.T, db *sql.DB) []queries.InsertGatewayEnvelopeParams {
db_rows := []queries.InsertGatewayEnvelopeParams{
{
Expand Down Expand Up @@ -179,7 +64,7 @@ func setupQueryTest(t *testing.T, db *sql.DB) []queries.InsertGatewayEnvelopePar
}

func TestQueryAllEnvelopes(t *testing.T) {
svc, db, cleanup := newTestService(t)
svc, db, cleanup := testutils.NewTestService(t)
defer cleanup()
db_rows := setupQueryTest(t, db)

Expand All @@ -195,7 +80,7 @@ func TestQueryAllEnvelopes(t *testing.T) {
}

func TestQueryPagedEnvelopes(t *testing.T) {
svc, db, cleanup := newTestService(t)
svc, db, cleanup := testutils.NewTestService(t)
defer cleanup()
db_rows := setupQueryTest(t, db)

Expand All @@ -211,7 +96,7 @@ func TestQueryPagedEnvelopes(t *testing.T) {
}

func TestQueryEnvelopesByOriginator(t *testing.T) {
svc, db, cleanup := newTestService(t)
svc, db, cleanup := testutils.NewTestService(t)
defer cleanup()
db_rows := setupQueryTest(t, db)

Expand All @@ -232,7 +117,7 @@ func TestQueryEnvelopesByOriginator(t *testing.T) {
}

func TestQueryEnvelopesByTopic(t *testing.T) {
svc, db, cleanup := newTestService(t)
svc, db, cleanup := testutils.NewTestService(t)
defer cleanup()
db_rows := setupQueryTest(t, db)

Expand All @@ -251,7 +136,7 @@ func TestQueryEnvelopesByTopic(t *testing.T) {
}

func TestQueryEnvelopesFromLastSeen(t *testing.T) {
svc, db, cleanup := newTestService(t)
svc, db, cleanup := testutils.NewTestService(t)
defer cleanup()
db_rows := setupQueryTest(t, db)

Expand All @@ -270,7 +155,7 @@ func TestQueryEnvelopesFromLastSeen(t *testing.T) {
}

func TestQueryEnvelopesWithEmptyResult(t *testing.T) {
svc, db, cleanup := newTestService(t)
svc, db, cleanup := testutils.NewTestService(t)
defer cleanup()
db_rows := setupQueryTest(t, db)

Expand Down
Loading

0 comments on commit 58c6ad6

Please sign in to comment.