|
| 1 | +package sales |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/erply/api-go-wrapper/internal/common" |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestRecurringBilling(t *testing.T) { |
| 13 | + |
| 14 | + t.Run("success", func(t *testing.T) { |
| 15 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 16 | + common.AssertFormValues(t, r, map[string]interface{}{ |
| 17 | + "clientCode": "someclient", |
| 18 | + "sessionKey": "somesess", |
| 19 | + }) |
| 20 | + _, err := w.Write([]byte(`{ |
| 21 | + "status": { |
| 22 | + "request": "processRecurringBilling", |
| 23 | + "requestUnixTime": 1728482531, |
| 24 | + "responseStatus": "ok", |
| 25 | + "errorCode": 0, |
| 26 | + "generationTime": 0.3931558132171630859375, |
| 27 | + "recordsTotal": 0, |
| 28 | + "recordsInResponse": 0 |
| 29 | + }, |
| 30 | + "records": [ |
| 31 | + { |
| 32 | + "processedInvoices": [ |
| 33 | + { |
| 34 | + "id": 89, |
| 35 | + "created": true, |
| 36 | + "updated": true |
| 37 | + } |
| 38 | + ] |
| 39 | + } |
| 40 | + ] |
| 41 | + }`)) |
| 42 | + assert.NoError(t, err) |
| 43 | + })) |
| 44 | + defer srv.Close() |
| 45 | + |
| 46 | + cli := common.NewClient("somesess", "someclient", "", nil, nil) |
| 47 | + cli.Url = srv.URL |
| 48 | + cl := NewClient(cli) |
| 49 | + bulkResp, err := cl.ProcessRecurringBilling( |
| 50 | + context.Background(), |
| 51 | + map[string]string{ |
| 52 | + "billingStatementIDs": "1,2", |
| 53 | + }, |
| 54 | + ) |
| 55 | + assert.NoError(t, err) |
| 56 | + assert.Equal(t, 1, len(bulkResp)) |
| 57 | + assert.Equal(t, 89, bulkResp[0].ID) |
| 58 | + assert.Equal(t, true, bulkResp[0].Created) |
| 59 | + assert.Equal(t, true, bulkResp[0].Updated) |
| 60 | + }) |
| 61 | + t.Run("empty", func(t *testing.T) { |
| 62 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 63 | + common.AssertFormValues(t, r, map[string]interface{}{ |
| 64 | + "clientCode": "someclient", |
| 65 | + "sessionKey": "somesess", |
| 66 | + }) |
| 67 | + _, err := w.Write([]byte(`{ |
| 68 | + "status": { |
| 69 | + "request": "processRecurringBilling", |
| 70 | + "requestUnixTime": 1728485864, |
| 71 | + "responseStatus": "ok", |
| 72 | + "errorCode": 0, |
| 73 | + "generationTime": 0.1155679225921630859375, |
| 74 | + "recordsTotal": 0, |
| 75 | + "recordsInResponse": 0 |
| 76 | + }, |
| 77 | + "records": [ |
| 78 | + { |
| 79 | + "processedInvoices": [] |
| 80 | + } |
| 81 | + ] |
| 82 | + }`)) |
| 83 | + assert.NoError(t, err) |
| 84 | + })) |
| 85 | + defer srv.Close() |
| 86 | + |
| 87 | + cli := common.NewClient("somesess", "someclient", "", nil, nil) |
| 88 | + cli.Url = srv.URL |
| 89 | + cl := NewClient(cli) |
| 90 | + bulkResp, err := cl.ProcessRecurringBilling( |
| 91 | + context.Background(), |
| 92 | + map[string]string{ |
| 93 | + "billingStatementIDs": "1,2", |
| 94 | + }, |
| 95 | + ) |
| 96 | + assert.NoError(t, err) |
| 97 | + assert.Equal(t, 0, len(bulkResp)) |
| 98 | + }) |
| 99 | + t.Run("error", func(t *testing.T) { |
| 100 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 101 | + common.AssertFormValues(t, r, map[string]interface{}{ |
| 102 | + "clientCode": "someclient", |
| 103 | + "sessionKey": "somesess", |
| 104 | + }) |
| 105 | + _, err := w.Write([]byte(`{ |
| 106 | + "status": { |
| 107 | + "request": "processRecurringBilling", |
| 108 | + "requestUnixTime": 1728489814, |
| 109 | + "responseStatus": "error", |
| 110 | + "errorCode": 1016, |
| 111 | + "errorField": "billingStatementIDs", |
| 112 | + "generationTime": 0.0900490283966064453125, |
| 113 | + "recordsTotal": 0, |
| 114 | + "recordsInResponse": 0 |
| 115 | + }, |
| 116 | + "records": [] |
| 117 | + }`)) |
| 118 | + assert.NoError(t, err) |
| 119 | + })) |
| 120 | + defer srv.Close() |
| 121 | + |
| 122 | + cli := common.NewClient("somesess", "someclient", "", nil, nil) |
| 123 | + cli.Url = srv.URL |
| 124 | + cl := NewClient(cli) |
| 125 | + bulkResp, err := cl.ProcessRecurringBilling( |
| 126 | + context.Background(), |
| 127 | + map[string]string{ |
| 128 | + "billingStatementIDs": "a", |
| 129 | + }, |
| 130 | + ) |
| 131 | + assert.Error(t, err) |
| 132 | + assert.Equal(t, "ERPLY API: processRecurringBilling: error, status: [1016] Invalid value.(Attribute \"errorField\" indicates the field that contains an invalid value.), error field: billingStatementIDs, code: 1016", err.Error()) |
| 133 | + assert.Equal(t, 0, len(bulkResp)) |
| 134 | + }) |
| 135 | +} |
0 commit comments