Skip to content

Commit

Permalink
test all payout functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
Uchencho committed Sep 23, 2023
1 parent 19aa2fd commit bf8d8d7
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 1 deletion.
150 changes: 149 additions & 1 deletion pawapay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pawapay_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestCreatePayout(t *testing.T) {
func TestCreateBulkPayout(t *testing.T) {
table := []row{
{
Name: "Creating payout succeeds",
Name: "Creating bulk payout succeeds",
Input: []pawapay.PayoutRequest{
{
PayoutId: testPayoutId,
Expand Down Expand Up @@ -168,3 +169,150 @@ func TestCreateBulkPayout(t *testing.T) {

}
}

func TestGetPayout(t *testing.T) {
table := []row{
{
Name: "Retrieving payout succeeds",
Input: testPayoutId,
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("/payouts/%s", testPayoutId)
assert.Equal(t, http.MethodGet, req.Method)
assert.Equal(t, expectedURL, req.RequestURI)
})

var resp []pawapay.Payout
fileToStruct(filepath.Join("testdata", "get-payout-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)

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

}
}

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

var actualBody, expectedBody pawapay.ResendCallbackRequest
expectedBody.PayoutId = testPayoutId

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 := "/payouts/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.PayoutStatusResponse
fileToStruct(filepath.Join("testdata", "payout-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)

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

}
}

func TestFailEnqueuedPayout(t *testing.T) {
table := []row{
{
Name: "Fail enqueued payout",
Input: testPayoutId,
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("/payouts/fail-enqueued/%s", testPayoutId)
assert.Equal(t, http.MethodPost, req.Method)
assert.Equal(t, expectedURL, req.RequestURI)
})

var resp pawapay.PayoutStatusResponse
fileToStruct(filepath.Join("testdata", "payout-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)

_, err := c.FailEnqueued(req)
t.Run("No error is returned", func(t *testing.T) {
assert.NoError(t, err)
})
}
}
23 changes: 23 additions & 0 deletions testdata/get-payout-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"payoutId": "d334c312-6c18-4d7e-a0f1-097d398543d3",
"status": "COMPLETED",
"amount": "123.45",
"currency": "ZMW",
"country": "ZMB",
"correspondent": "MTN_MOMO_ZMB",
"recipient": {
"type": "MSISDN",
"address": {
"value": "256780334452"
}
},
"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"
}
}
]
4 changes: 4 additions & 0 deletions testdata/payout-status-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"payoutId": "d334c312-6c18-4d7e-a0f1-097d398543d3",
"status": "ACCEPTED"
}

0 comments on commit bf8d8d7

Please sign in to comment.