Skip to content

Commit

Permalink
add test for refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
Uchencho committed Sep 24, 2023
1 parent a288a27 commit e250bb1
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
177 changes: 177 additions & 0 deletions pawapay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,180 @@ func TestResendDepositCallBack(t *testing.T) {

}
}

func TestRequestRefund(t *testing.T) {

type refundPayload struct {
DepositId string
Amount pawapay.Amount
RefundId string
}

table := []row{
{
Name: "refund is requested successfully",
Input: refundPayload{
DepositId: testDepositId,
Amount: pawapay.Amount{Currency: "GHS", Value: "1000"},
RefundId: testDepositId,
},
CustomServerURL: func(t *testing.T) string {
pawapayService := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

var actualBody, expectedBody pawapay.RefundRequest

if err := json.NewDecoder(req.Body).Decode(&actualBody); err != nil {
log.Printf("error in unmarshalling %+v", err)
w.WriteHeader(http.StatusBadRequest)
return
}

t.Run("URL and request method is as expected", func(t *testing.T) {
expectedURL := "/refunds"
assert.Equal(t, http.MethodPost, req.Method)
assert.Equal(t, expectedURL, req.RequestURI)
})

t.Run("Request is as expected", func(t *testing.T) {
fileToStruct(filepath.Join("testdata", "refund-request.json"), &expectedBody)
assert.Equal(t, expectedBody, actualBody)
})

var resp pawapay.InitiateRefundResponse
fileToStruct(filepath.Join("testdata", "refund-response.json"), &resp)

w.WriteHeader(http.StatusOK)
bb, _ := json.Marshal(resp)
w.Write(bb)

}))
return pawapayService.URL
},
},
}

for _, row := range table {

c := pawapay.NewService(pawapay.Config{
BaseURL: row.CustomServerURL(t),
})

req := row.Input.(refundPayload)

log.Printf("======== Running row: %s ==========", row.Name)

_, err := c.RequestRefund(req.RefundId, req.DepositId, req.Amount)
t.Run("No error is returned", func(t *testing.T) {
assert.NoError(t, err)
})

}
}

func TestGetRefund(t *testing.T) {
table := []row{
{
Name: "Retrieving refund succeeds",
Input: testDepositId,
CustomServerURL: func(t *testing.T) string {
pawapayService := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

t.Run("URL and request method is as expected", func(t *testing.T) {
expectedURL := fmt.Sprintf("/refunds/%s", testDepositId)
assert.Equal(t, http.MethodGet, req.Method)
assert.Equal(t, expectedURL, req.RequestURI)
})

var resp []pawapay.Refund
fileToStruct(filepath.Join("testdata", "get-refund-response.json"), &resp)

w.WriteHeader(http.StatusOK)
bb, _ := json.Marshal(resp)
w.Write(bb)

}))
return pawapayService.URL
},
},
}

for _, row := range table {

c := pawapay.NewService(pawapay.Config{
BaseURL: row.CustomServerURL(t),
})

req := row.Input.(string)

log.Printf("======== Running row: %s ==========", row.Name)

result, err := c.GetRefund(req)
t.Run("No error is returned", func(t *testing.T) {
assert.NoError(t, err)
})

t.Run("Annotation response code is as expected", func(t *testing.T) {
assert.Equal(t, http.StatusOK, result.Annotation.ResponseCode)
})
}
}

func TestResendRefundCallBack(t *testing.T) {
table := []row{
{
Name: "Resend refund callback succeeds",
Input: testDepositId,
CustomServerURL: func(t *testing.T) string {
pawapayService := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

var actualBody, expectedBody pawapay.ResendCallbackRequest
expectedBody.RefundId = testDepositId

if err := json.NewDecoder(req.Body).Decode(&actualBody); err != nil {
log.Printf("error in unmarshalling %+v", err)
w.WriteHeader(http.StatusBadRequest)
return
}

t.Run("URL and request method is as expected", func(t *testing.T) {
expectedURL := "/refunds/resend-callback"
assert.Equal(t, http.MethodPost, req.Method)
assert.Equal(t, expectedURL, req.RequestURI)
})

t.Run("Request payload is as expected", func(t *testing.T) {
assert.Equal(t, expectedBody, actualBody)
})

var resp pawapay.RefundStatusResponse
fileToStruct(filepath.Join("testdata", "refund-status-response.json"), &resp)

w.WriteHeader(http.StatusOK)
bb, _ := json.Marshal(resp)
w.Write(bb)

}))
return pawapayService.URL
},
},
}

for _, row := range table {

c := pawapay.NewService(pawapay.Config{
BaseURL: row.CustomServerURL(t),
})

req := row.Input.(string)

log.Printf("======== Running row: %s ==========", row.Name)

result, err := c.ResendRefundCallback(req)
t.Run("No error is returned", func(t *testing.T) {
assert.NoError(t, err)
})
t.Run("Annotation response code is as expected", func(t *testing.T) {
assert.Equal(t, http.StatusOK, result.Annotation.ResponseCode)
})
}
}
23 changes: 23 additions & 0 deletions testdata/get-refund-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"refundId": "d334c312-6c18-4d7e-a0f1-097d398543d3",
"status": "COMPLETED",
"amount": "123.45",
"currency": "ZMW",
"country": "ZMB",
"correspondent": "MTN_MOMO_ZMB",
"recipient": {
"type": "MSISDN",
"address": {
"value": "260961234567"
}
},
"customerTimestamp": "2020-10-19T08:17:00Z",
"statementDescription": "From ACME company",
"created": "2020-10-19T08:17:01Z",
"receivedByRecipient": "2020-10-19T08:17:02Z",
"correspondentIds": {
"SOME_CORRESPONDENT_ID": "12356789"
}
}
]
5 changes: 5 additions & 0 deletions testdata/refund-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"refundId": "d334c312-6c18-4d7e-a0f1-097d398543d3",
"depositId": "d334c312-6c18-4d7e-a0f1-097d398543d3",
"amount": "1000"
}
5 changes: 5 additions & 0 deletions testdata/refund-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"refundId": "d334c312-6c18-4d7e-a0f1-097d398543d3",
"status": "ACCEPTED",
"created": "2020-10-19T11:17:01Z"
}
4 changes: 4 additions & 0 deletions testdata/refund-status-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"refundId": "f4401bd2-1568-4140-bf2d-eb77d2b2b639",
"status": "ACCEPTED"
}

0 comments on commit e250bb1

Please sign in to comment.