diff --git a/pawapay_test.go b/pawapay_test.go index f937786..5f69fd9 100644 --- a/pawapay_test.go +++ b/pawapay_test.go @@ -3,6 +3,7 @@ package pawapay_test import ( "bytes" "encoding/json" + "fmt" "io" "log" "net/http" @@ -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, @@ -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) + }) + } +} diff --git a/testdata/get-payout-response.json b/testdata/get-payout-response.json new file mode 100644 index 0000000..11eb2b9 --- /dev/null +++ b/testdata/get-payout-response.json @@ -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" + } + } +] diff --git a/testdata/payout-status-response.json b/testdata/payout-status-response.json new file mode 100644 index 0000000..70b7246 --- /dev/null +++ b/testdata/payout-status-response.json @@ -0,0 +1,4 @@ +{ + "payoutId": "d334c312-6c18-4d7e-a0f1-097d398543d3", + "status": "ACCEPTED" +}