Skip to content

Commit

Permalink
Refactor tests, code
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke-Rogerson committed Dec 3, 2023
1 parent b51d86c commit 549a531
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 25 deletions.
5 changes: 4 additions & 1 deletion cmd/order-book/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func setup() {
log.Fatalf("error creating repository: %v", err)
}

service, err := service.New(repository)
// TODO: add CLI flag to easily switch between blockchains
ethClient := &service.EthereumClient{}

service, err := service.New(repository, ethClient)
if err != nil {
log.Fatalf("error creating service: %v", err)
}
Expand Down
16 changes: 16 additions & 0 deletions mocks/blockchain_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mocks

import (
"context"

"github.com/orbs-network/order-book/service"
)

type MockBcClient struct {
IsVerified bool
Error error
}

func (m *MockBcClient) VerifySignature(ctx context.Context, input service.VerifySignatureInput) (bool, error) {
return m.IsVerified, m.Error
}
4 changes: 3 additions & 1 deletion service/cancel_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestService_CancelOrder(t *testing.T) {
clientOId := uuid.MustParse("f577273e-12de-4acc-a4f8-de7fb5b86e37")
order := &models.Order{Id: orderId, UserId: userId, Status: models.STATUS_OPEN, ClientOId: clientOId}

mockBcClient := &mocks.MockBcClient{IsVerified: true}

testCases := []struct {
name string
order *models.Order
Expand All @@ -41,7 +43,7 @@ func TestService_CancelOrder(t *testing.T) {
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
fmt.Print(c.name)
svc, _ := service.New(&mocks.MockOrderBookStore{Order: c.order, Error: c.err})
svc, _ := service.New(&mocks.MockOrderBookStore{Order: c.order, Error: c.err}, mockBcClient)

input := service.CancelOrderInput{Id: orderId, IsClientOId: false, UserId: userId}

Expand Down
5 changes: 3 additions & 2 deletions service/cancel_orders_for_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (

func TestService_CancelOrdersForUser(t *testing.T) {
ctx := context.Background()
mockBcClient := &mocks.MockBcClient{IsVerified: true}

t.Run("should successfully cancel all orders for a user", func(t *testing.T) {
store := &mocks.MockOrderBookStore{User: &mocks.User}

s, _ := service.New(store)
s, _ := service.New(store, mockBcClient)

err := s.CancelOrdersForUser(ctx, mocks.UserId)
assert.Equal(t, err, nil)
Expand All @@ -24,7 +25,7 @@ func TestService_CancelOrdersForUser(t *testing.T) {
t.Run("should return error on unexpected error", func(t *testing.T) {
store := &mocks.MockOrderBookStore{User: nil, Error: assert.AnError}

s, _ := service.New(store)
s, _ := service.New(store, mockBcClient)

err := s.CancelOrdersForUser(ctx, mocks.UserId)
assert.ErrorContains(t, err, "could not cancel orders for user")
Expand Down
58 changes: 58 additions & 0 deletions service/ethereum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package service

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

var sig = "0xf577460acb728646ceb2f561a96db6c84aeb99334bcc938e7d21fb1fa83c90f75efb5e8c64c53b98b4d1d97b693a89dbd7606225f68cddffbc075f402494b7331b"

var pubKey = "0x6a04ab98d9e4774ad806e302dddeb63bea16b5cb5f223ee77478e861bb583eb336b6fbcb60b5b3d4f1551ac45e5ffc4936466e7d98f6c7c0ec736539f74691a6"

var message = `{"permitted": {"token": "0x3c499c542cef5e3811e1192ce70d8cc03d5c3359", "amount": 10000000}, "spender": "0x21Da9737764527e75C17F1AB26Cb668b66dEE0a0", "nonce": 2576040678, "deadline": 1709382304, "witness": {"info": {"reactor": "0x21Da9737764527e75C17F1AB26Cb668b66dEE0a0", "swapper": "0xE3682CCecefBb3C3fe524BbFF1598B2BBaC0d6E3", "nonce": 2576040678, "deadline": 1709382304, "additionalValidationContract": "0x1a08D64Fb4a7D0b6DA5606A1e4619c147C3fB95e", "additionalValidationData": "0x"}, "decayStartTime": 1709382304, "decayEndTime": 1709382304, "exclusiveFiller": "0x1a08D64Fb4a7D0b6DA5606A1e4619c147C3fB95e", "exclusivityOverrideBps": "0", "inputToken": "0x3c499c542cef5e3811e1192ce70d8cc03d5c3359", "inputStartAmount": 10000000, "inputEndAmount": 10000000}}`

func TestEthereumClient_VerifySignature(t *testing.T) {
ctx := context.TODO()
ethereumClient := &EthereumClient{}

t.Run("successfully verify signature - should return true", func(t *testing.T) {
input := VerifySignatureInput{
PublicKey: pubKey,
Signature: sig,
MessageData: message,
}

result, err := ethereumClient.VerifySignature(ctx, input)

assert.NoError(t, err)
assert.True(t, result)
})

t.Run("invalid public key - should return false", func(t *testing.T) {
input := VerifySignatureInput{
PublicKey: "0x123",
Signature: sig,
MessageData: message,
}

result, err := ethereumClient.VerifySignature(ctx, input)

assert.ErrorContains(t, err, "error decoding hex public key")
assert.False(t, result)
})

t.Run("failed to unmarshal message - should return false", func(t *testing.T) {
input := VerifySignatureInput{
PublicKey: pubKey,
Signature: sig,
MessageData: "invalid message",
}

result, err := ethereumClient.VerifySignature(ctx, input)

assert.ErrorContains(t, err, "failed to unmarshal message")
assert.False(t, result)
})
}
8 changes: 5 additions & 3 deletions service/get_order_by_client_oid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func TestService_GetOrderByClientOId(t *testing.T) {

clientOId := uuid.MustParse("e577273e-12de-4acc-a4f8-de7fb5b86e37")

mockBcClient := &mocks.MockBcClient{IsVerified: true}

t.Run("successfully retrieve order by client order ID - should return order", func(t *testing.T) {
o := &models.Order{ClientOId: clientOId}
svc, _ := service.New(&mocks.MockOrderBookStore{Order: o})
svc, _ := service.New(&mocks.MockOrderBookStore{Order: o}, mockBcClient)

order, err := svc.GetOrderByClientOId(ctx, clientOId)

Expand All @@ -28,7 +30,7 @@ func TestService_GetOrderByClientOId(t *testing.T) {
})

t.Run("order not found - should return nil", func(t *testing.T) {
svc, _ := service.New(&mocks.MockOrderBookStore{Error: models.ErrOrderNotFound})
svc, _ := service.New(&mocks.MockOrderBookStore{Error: models.ErrOrderNotFound}, mockBcClient)

order, err := svc.GetOrderByClientOId(ctx, clientOId)

Expand All @@ -37,7 +39,7 @@ func TestService_GetOrderByClientOId(t *testing.T) {
})

t.Run("unexpected error - should return error", func(t *testing.T) {
svc, _ := service.New(&mocks.MockOrderBookStore{Error: assert.AnError})
svc, _ := service.New(&mocks.MockOrderBookStore{Error: assert.AnError}, mockBcClient)

order, err := svc.GetOrderByClientOId(ctx, clientOId)

Expand Down
2 changes: 1 addition & 1 deletion service/get_symbols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestService_GetSymbols(t *testing.T) {
ctx := context.Background()
svc, _ := service.New(&mocks.MockOrderBookStore{})
svc, _ := service.New(&mocks.MockOrderBookStore{}, &mocks.MockBcClient{})

symbols, err := svc.GetSymbols(ctx)

Expand Down
9 changes: 5 additions & 4 deletions service/get_user_by_public_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
func TestService_GetUserByPublicKey(t *testing.T) {

ctx := context.Background()
mockBcClient := &mocks.MockBcClient{IsVerified: true}

t.Run("should get a user by their public key", func(t *testing.T) {
store := &mocks.MockOrderBookStore{User: &mocks.User}

svc, _ := service.New(store)
svc, _ := service.New(store, mockBcClient)

user, _ := svc.GetUserByPublicKey(ctx, mocks.PubKey)

Expand All @@ -27,7 +28,7 @@ func TestService_GetUserByPublicKey(t *testing.T) {
t.Run("should return error if user not found", func(t *testing.T) {
store := &mocks.MockOrderBookStore{User: nil, ErrUser: models.ErrUserNotFound}

svc, _ := service.New(store)
svc, _ := service.New(store, mockBcClient)

user, err := svc.GetUserByPublicKey(ctx, mocks.PubKey)

Expand All @@ -38,7 +39,7 @@ func TestService_GetUserByPublicKey(t *testing.T) {
t.Run("should return error on unexpected error getting user by public key", func(t *testing.T) {
store := &mocks.MockOrderBookStore{User: nil, ErrUser: assert.AnError}

svc, _ := service.New(store)
svc, _ := service.New(store, mockBcClient)

user, err := svc.GetUserByPublicKey(ctx, mocks.PubKey)

Expand All @@ -49,7 +50,7 @@ func TestService_GetUserByPublicKey(t *testing.T) {
t.Run("should return error if user is nil but no error", func(t *testing.T) {
store := &mocks.MockOrderBookStore{User: nil, ErrUser: nil}

svc, _ := service.New(store)
svc, _ := service.New(store, mockBcClient)

user, err := svc.GetUserByPublicKey(ctx, mocks.PubKey)

Expand Down
9 changes: 5 additions & 4 deletions service/process_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func TestService_ProcessOrder(t *testing.T) {

ctx := context.Background()
mockBcClient := &mocks.MockBcClient{IsVerified: true}

symbol, _ := models.StrToSymbol("USDC-ETH")
userId := uuid.MustParse("a577273e-12de-4acc-a4f8-de7fb5b86e37")
Expand All @@ -40,7 +41,7 @@ func TestService_ProcessOrder(t *testing.T) {
ClientOrderID: orderId,
}

svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Error: assert.AnError})
svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Error: assert.AnError}, mockBcClient)

order, err := svc.ProcessOrder(ctx, input)

Expand All @@ -58,7 +59,7 @@ func TestService_ProcessOrder(t *testing.T) {
ClientOrderID: orderId,
}

svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: nil})
svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: nil}, mockBcClient)

newOrder, err := svc.ProcessOrder(ctx, input)

Expand All @@ -85,7 +86,7 @@ func TestService_ProcessOrder(t *testing.T) {
ClientOrderID: orderId,
}

svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: &models.Order{UserId: uuid.MustParse("b577273e-12de-4acc-a4f8-de7fb5b86e37")}})
svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: &models.Order{UserId: uuid.MustParse("b577273e-12de-4acc-a4f8-de7fb5b86e37")}}, mockBcClient)

order, err := svc.ProcessOrder(ctx, input)

Expand All @@ -103,7 +104,7 @@ func TestService_ProcessOrder(t *testing.T) {
ClientOrderID: orderId,
}

svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: &models.Order{ClientOId: orderId, UserId: userId}})
svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: &models.Order{ClientOId: orderId, UserId: userId}}, mockBcClient)

order, err := svc.ProcessOrder(ctx, input)

Expand Down
16 changes: 8 additions & 8 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ type OrderBookService interface {
}

type BlockChainService interface {
VerifySignature(message, signature string, publicKey []byte) (bool, error)
VerifySignature(ctx context.Context, input VerifySignatureInput) (bool, error)
}

// Service contains methods that implement the business logic for the application.
type Service struct {
orderBookStore store.OrderBookStore
// blockchainClient BlockChainService
orderBookStore store.OrderBookStore
blockchainClient BlockChainService
}

// New creates a new Service with injected dependencies.
func New(store store.OrderBookStore) (*Service, error) {
func New(store store.OrderBookStore, bcClient BlockChainService) (*Service, error) {
if store == nil {
return nil, errors.New("store cannot be nil")
}

// if blockchainClient == nil {
// return nil, errors.New("blockchainClient cannot be nil")
// }
if bcClient == nil {
return nil, errors.New("bcClient cannot be nil")
}

return &Service{orderBookStore: store}, nil
return &Service{orderBookStore: store, blockchainClient: bcClient}, nil
}
3 changes: 2 additions & 1 deletion transport/rest/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var httpServer *rest.HTTPServer

func runAuctionServer(t *testing.T) {
repository := mocks.CreateAuctionMock()
mockBcClient := &mocks.MockBcClient{IsVerified: true}

service, err := service.New(repository)
service, err := service.New(repository, mockBcClient)
if err != nil {
log.Fatalf("error creating service: %v", err)
}
Expand Down

0 comments on commit 549a531

Please sign in to comment.