Skip to content

Commit

Permalink
test orders paid state should match transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Aug 11, 2017
1 parent 04edc2a commit b254e93
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion api/order_test.go
Expand Up @@ -366,6 +366,10 @@ func TestOrderSetUserIDLogic(t *testing.T) {
func TestOrderUpdate(t *testing.T) {
t.Run("FieldsUpdate", func(t *testing.T) {
test := NewRouteTest(t)
test.Data.firstOrder.PaymentState = models.PendingState
rsp := test.DB.Save(test.Data.firstOrder)
require.NoError(t, rsp.Error, "Failed to update email")

op := &orderRequestParams{
Email: "mrfreeze@dc.com",
Currency: "monopoly-dollars",
Expand All @@ -379,7 +383,7 @@ func TestOrderUpdate(t *testing.T) {
extractPayload(t, http.StatusOK, recorder, rspOrder)

saved := new(models.Order)
rsp := test.DB.Preload("LineItems").First(saved, "id = ?", test.Data.firstOrder.ID)
rsp = test.DB.Preload("LineItems").First(saved, "id = ?", test.Data.firstOrder.ID)
require.False(t, rsp.RecordNotFound())

assert.Equal("mrfreeze@dc.com", rspOrder.Email)
Expand Down
22 changes: 15 additions & 7 deletions api/payments_test.go
Expand Up @@ -326,6 +326,10 @@ func TestPaymentCreate(t *testing.T) {
t.Run("PayPal", func(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
test := NewRouteTest(t)
test.Data.secondOrder.PaymentState = models.PendingState
rsp := test.DB.Save(test.Data.secondOrder)
require.NoError(t, rsp.Error, "Failed to update order")

var loginCount, paymentCount int
paymentID := "4CF18861HF410323V"
amtString := fmt.Sprintf("%.2f", float64(test.Data.secondOrder.Total)/100)
Expand Down Expand Up @@ -367,10 +371,10 @@ func TestPaymentCreate(t *testing.T) {

recorder := test.TestEndpoint(http.MethodPost, "/orders/second-order/payments", bytes.NewBuffer(body), test.Data.testUserToken)

rsp := models.Transaction{}
extractPayload(t, http.StatusOK, recorder, &rsp)
assert.Equal(t, paymentID, rsp.ProcessorID)
assert.Equal(t, models.PaidState, rsp.Status)
trans := models.Transaction{}
extractPayload(t, http.StatusOK, recorder, &trans)
assert.Equal(t, paymentID, trans.ProcessorID)
assert.Equal(t, models.PaidState, trans.Status)
assert.Equal(t, 1, loginCount, "too many login calls")
assert.Equal(t, 2, paymentCount, "too many payment calls")
})
Expand All @@ -388,6 +392,10 @@ func TestPaymentCreate(t *testing.T) {
defer stripe.SetBackend(stripe.APIBackend, nil)

test := NewRouteTest(t)
test.Data.firstOrder.PaymentState = models.PendingState
rsp := test.DB.Save(test.Data.firstOrder)
require.NoError(t, rsp.Error, "Failed to update order")

params := &stripePaymentParams{
Amount: test.Data.firstOrder.Total,
Currency: test.Data.firstOrder.Currency,
Expand All @@ -400,9 +408,9 @@ func TestPaymentCreate(t *testing.T) {

recorder := test.TestEndpoint(http.MethodPost, "/orders/first-order/payments", bytes.NewBuffer(body), test.Data.testUserToken)

rsp := models.Transaction{}
extractPayload(t, http.StatusOK, recorder, &rsp)
assert.Equal(t, models.PaidState, rsp.Status)
trans := models.Transaction{}
extractPayload(t, http.StatusOK, recorder, &trans)
assert.Equal(t, models.PaidState, trans.Status)
assert.Equal(t, 1, callCount)
})
}
Expand Down
2 changes: 2 additions & 0 deletions api/utils_test.go
Expand Up @@ -147,6 +147,7 @@ func setupTestData() *TestData {
firstOrder := models.NewOrder("session1", testUser.Email, "usd")
firstOrder.UserID = testUser.ID
firstOrder.PaymentProcessor = "stripe"
firstOrder.PaymentState = models.PaidState
firstTransaction := models.NewTransaction(firstOrder)
firstTransaction.ProcessorID = "stripe"
firstTransaction.Amount = 100
Expand Down Expand Up @@ -174,6 +175,7 @@ func setupTestData() *TestData {
secondOrder := models.NewOrder("session2", testUser.Email, "usd")
secondOrder.UserID = testUser.ID
secondOrder.PaymentProcessor = "paypal"
secondOrder.PaymentState = models.PaidState
secondTransaction := models.NewTransaction(secondOrder)
secondTransaction.ProcessorID = "paypal"
secondLineItem1 := &models.LineItem{
Expand Down

0 comments on commit b254e93

Please sign in to comment.