Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit e225593

Browse files
authored
Merge pull request #1682 from ozamiatin/master
Extract GETOrder behavior into unit testable method #1483
2 parents 3fd0924 + 2107015 commit e225593

File tree

3 files changed

+96
-50
lines changed

3 files changed

+96
-50
lines changed

api/jsonapi.go

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,53 +1637,12 @@ func (i *jsonAPIHandler) POSTResyncBlockchain(w http.ResponseWriter, r *http.Req
16371637

16381638
func (i *jsonAPIHandler) GETOrder(w http.ResponseWriter, r *http.Request) {
16391639
_, orderID := path.Split(r.URL.Path)
1640-
var (
1641-
err error
1642-
isSale bool
1643-
contract *pb.RicardianContract
1644-
state pb.OrderState
1645-
funded bool
1646-
records []*wallet.TransactionRecord
1647-
read bool
1648-
paymentCoin *repo.CurrencyCode
1649-
)
1650-
contract, state, funded, records, read, paymentCoin, err = i.node.Datastore.Purchases().GetByOrderId(orderID)
1651-
if err != nil {
1652-
contract, state, funded, records, read, paymentCoin, err = i.node.Datastore.Sales().GetByOrderId(orderID)
1653-
if err != nil {
1654-
ErrorResponse(w, http.StatusNotFound, "Order not found")
1655-
return
1656-
}
1657-
isSale = true
1658-
}
1659-
resp := new(pb.OrderRespApi)
1660-
resp.Contract = contract
1661-
resp.Funded = funded
1662-
resp.Read = read
1663-
resp.State = state
16641640

1665-
// TODO: Remove once broken contracts are migrated
1666-
lookupCoin := contract.BuyerOrder.Payment.Coin
1667-
_, err = repo.LoadCurrencyDefinitions().Lookup(lookupCoin)
1668-
if err != nil {
1669-
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, orderID)
1670-
contract.BuyerOrder.Payment.Coin = paymentCoin.String()
1671-
}
1672-
1673-
paymentTxs, refundTx, err := i.node.BuildTransactionRecords(contract, records, state)
1641+
resp, err := i.node.GetOrder(orderID)
16741642
if err != nil {
1675-
ErrorResponse(w, http.StatusInternalServerError, err.Error())
1676-
return
1677-
}
1678-
resp.PaymentAddressTransactions = paymentTxs
1679-
resp.RefundAddressTransaction = refundTx
1680-
1681-
unread, err := i.node.Datastore.Chat().GetUnreadCount(orderID)
1682-
if err != nil {
1683-
ErrorResponse(w, http.StatusInternalServerError, err.Error())
1643+
ErrorResponse(w, http.StatusNotFound, "Order not found")
16841644
return
16851645
}
1686-
resp.UnreadChatMessages = uint64(unread)
16871646

16881647
m := jsonpb.Marshaler{
16891648
EnumsAsInts: false,
@@ -1696,11 +1655,7 @@ func (i *jsonAPIHandler) GETOrder(w http.ResponseWriter, r *http.Request) {
16961655
ErrorResponse(w, http.StatusInternalServerError, err.Error())
16971656
return
16981657
}
1699-
if isSale {
1700-
i.node.Datastore.Sales().MarkAsRead(orderID)
1701-
} else {
1702-
i.node.Datastore.Purchases().MarkAsRead(orderID)
1703-
}
1658+
17041659
SanitizedResponseM(w, out, new(pb.OrderRespApi))
17051660
}
17061661

core/order.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,64 @@ const (
7878
CryptocurrencyPurchasePaymentAddressMaxLength = 512
7979
)
8080

81+
// GetOrder - provide API response order object by orderID
82+
func (n *OpenBazaarNode) GetOrder(orderID string) (*pb.OrderRespApi, error) {
83+
var (
84+
err error
85+
isSale bool
86+
contract *pb.RicardianContract
87+
state pb.OrderState
88+
funded bool
89+
records []*wallet.TransactionRecord
90+
read bool
91+
paymentCoin *repo.CurrencyCode
92+
)
93+
contract, state, funded, records, read, paymentCoin, err = n.Datastore.Purchases().GetByOrderId(orderID)
94+
if err != nil {
95+
contract, state, funded, records, read, paymentCoin, err = n.Datastore.Sales().GetByOrderId(orderID)
96+
if err != nil {
97+
return nil, errors.New("order not found")
98+
}
99+
isSale = true
100+
}
101+
resp := new(pb.OrderRespApi)
102+
resp.Contract = contract
103+
resp.Funded = funded
104+
resp.Read = read
105+
resp.State = state
106+
107+
// TODO: Remove once broken contracts are migrated
108+
lookupCoin := contract.BuyerOrder.Payment.Coin
109+
_, err = repo.LoadCurrencyDefinitions().Lookup(lookupCoin)
110+
if err != nil {
111+
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, orderID)
112+
contract.BuyerOrder.Payment.Coin = paymentCoin.String()
113+
}
114+
115+
paymentTxs, refundTx, err := n.BuildTransactionRecords(contract, records, state)
116+
if err != nil {
117+
log.Errorf(err.Error())
118+
return nil, err
119+
}
120+
resp.PaymentAddressTransactions = paymentTxs
121+
resp.RefundAddressTransaction = refundTx
122+
123+
unread, err := n.Datastore.Chat().GetUnreadCount(orderID)
124+
if err != nil {
125+
log.Errorf(err.Error())
126+
return nil, err
127+
}
128+
resp.UnreadChatMessages = uint64(unread)
129+
130+
if isSale {
131+
n.Datastore.Sales().MarkAsRead(orderID)
132+
} else {
133+
n.Datastore.Purchases().MarkAsRead(orderID)
134+
}
135+
136+
return resp, nil
137+
}
138+
81139
// Purchase - add ricardian contract
82140
func (n *OpenBazaarNode) Purchase(data *PurchaseData) (orderID string, paymentAddress string, paymentAmount uint64, vendorOnline bool, err error) {
83141
contract, err := n.createContractWithOrder(data)

core/order_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package core_test
22

33
import (
4-
"testing"
5-
64
"github.com/OpenBazaar/openbazaar-go/core"
75
"github.com/OpenBazaar/openbazaar-go/pb"
86
"github.com/OpenBazaar/openbazaar-go/test"
7+
"github.com/OpenBazaar/openbazaar-go/test/factory"
98
"github.com/golang/protobuf/proto"
9+
10+
"fmt"
11+
"testing"
1012
)
1113

1214
func TestOpenBazaarNode_CalculateOrderTotal(t *testing.T) {
@@ -356,3 +358,34 @@ func TestOpenBazaarNode_CalculateOrderTotal(t *testing.T) {
356358
t.Error("Calculated wrong order total")
357359
}
358360
}
361+
362+
func TestOpenBazaarNode_GetOrder(t *testing.T) {
363+
node, err := test.NewNode()
364+
if err != nil {
365+
t.Fatal(err)
366+
}
367+
368+
contract := factory.NewContract()
369+
370+
orderID, err := node.CalcOrderID(contract.BuyerOrder)
371+
if err != nil {
372+
t.Fatal(err)
373+
}
374+
375+
state := pb.OrderState_AWAITING_PAYMENT
376+
err = node.Datastore.Purchases().Put(orderID, *contract, state, false)
377+
if err != nil {
378+
t.Fatal(err)
379+
}
380+
381+
orderResponse, err := node.GetOrder(orderID)
382+
if err != nil {
383+
t.Fatal(err)
384+
}
385+
386+
if orderResponse.State != state {
387+
t.Fatal(fmt.Errorf("expected order state to be %s, but was %s",
388+
pb.OrderState_name[int32(state)],
389+
pb.OrderState_name[int32(orderResponse.State)]))
390+
}
391+
}

0 commit comments

Comments
 (0)