mangopay 3.0.23 → 3.0.25.pre.alpha.pre.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +28 -26
  3. data/.rspec +2 -2
  4. data/.travis.yml +13 -0
  5. data/Gemfile +2 -2
  6. data/LICENSE +20 -20
  7. data/README.md +126 -123
  8. data/bin/mangopay +9 -9
  9. data/lib/generators/mangopay/install_generator.rb +60 -60
  10. data/lib/generators/templates/mangopay.rb.erb +5 -5
  11. data/lib/mangopay.rb +225 -158
  12. data/lib/mangopay/authorization_token.rb +88 -88
  13. data/lib/mangopay/bank_account.rb +38 -38
  14. data/lib/mangopay/card.rb +8 -8
  15. data/lib/mangopay/card_registration.rb +9 -9
  16. data/lib/mangopay/client.rb +74 -74
  17. data/lib/mangopay/dispute.rb +130 -130
  18. data/lib/mangopay/errors.rb +61 -61
  19. data/lib/mangopay/event.rb +18 -18
  20. data/lib/mangopay/filter_parameters.rb +46 -0
  21. data/lib/mangopay/hook.rb +9 -9
  22. data/lib/mangopay/http_calls.rb +85 -85
  23. data/lib/mangopay/json.rb +14 -14
  24. data/lib/mangopay/kyc_document.rb +70 -70
  25. data/lib/mangopay/legal_user.rb +15 -15
  26. data/lib/mangopay/mandate.rb +32 -32
  27. data/lib/mangopay/natural_user.rb +14 -14
  28. data/lib/mangopay/pay_in.rb +85 -73
  29. data/lib/mangopay/pay_out.rb +14 -14
  30. data/lib/mangopay/pre_authorization.rb +13 -13
  31. data/lib/mangopay/refund.rb +7 -7
  32. data/lib/mangopay/report.rb +17 -17
  33. data/lib/mangopay/resource.rb +21 -21
  34. data/lib/mangopay/temp.rb +74 -74
  35. data/lib/mangopay/transaction.rb +24 -24
  36. data/lib/mangopay/transfer.rb +9 -9
  37. data/lib/mangopay/user.rb +43 -35
  38. data/lib/mangopay/version.rb +3 -3
  39. data/lib/mangopay/wallet.rb +17 -17
  40. data/mangopay.gemspec +31 -30
  41. data/spec/mangopay/authorization_token_spec.rb +70 -70
  42. data/spec/mangopay/bank_account_spec.rb +97 -97
  43. data/spec/mangopay/card_registration_spec.rb +73 -73
  44. data/spec/mangopay/client_spec.rb +110 -110
  45. data/spec/mangopay/configuration_spec.rb +95 -18
  46. data/spec/mangopay/dispute_spec.rb +262 -262
  47. data/spec/mangopay/event_spec.rb +31 -31
  48. data/spec/mangopay/fetch_filters_spec.rb +63 -63
  49. data/spec/mangopay/hook_spec.rb +37 -37
  50. data/spec/mangopay/idempotency_spec.rb +41 -41
  51. data/spec/mangopay/kyc_document_spec.rb +103 -103
  52. data/spec/mangopay/log_requests_filter_spec.rb +25 -0
  53. data/spec/mangopay/mandate_spec.rb +92 -92
  54. data/spec/mangopay/payin_bankwire_direct_spec.rb +74 -74
  55. data/spec/mangopay/payin_card_direct_spec.rb +68 -68
  56. data/spec/mangopay/payin_card_web_spec.rb +38 -38
  57. data/spec/mangopay/payin_directdebit_direct_spec.rb +37 -37
  58. data/spec/mangopay/payin_directdebit_web_spec.rb +38 -38
  59. data/spec/mangopay/payin_paypal_web_spec.rb +38 -0
  60. data/spec/mangopay/payin_preauthorized_direct_spec.rb +68 -68
  61. data/spec/mangopay/payout_bankwire_spec.rb +54 -54
  62. data/spec/mangopay/preauthorization_spec.rb +42 -42
  63. data/spec/mangopay/refund_spec.rb +21 -21
  64. data/spec/mangopay/report_spec.rb +39 -39
  65. data/spec/mangopay/shared_resources.rb +381 -365
  66. data/spec/mangopay/temp_paymentcard_spec.rb +31 -31
  67. data/spec/mangopay/transaction_spec.rb +54 -54
  68. data/spec/mangopay/transfer_spec.rb +69 -69
  69. data/spec/mangopay/user_spec.rb +137 -122
  70. data/spec/mangopay/wallet_spec.rb +80 -80
  71. data/spec/spec_helper.rb +31 -31
  72. metadata +11 -5
@@ -1,262 +1,262 @@
1
- describe MangoPay::Dispute do
2
-
3
- =begin
4
- comment out all Dispute related unit tests please
5
- these require manual actions on our side
6
- and it's infact not suitable like that
7
-
8
- # IMPORTANT NOTE!
9
- #
10
- # Due to the fact the disputes CANNOT be created on user's side,
11
- # a special approach in testing is needed.
12
- # In order to get the tests below pass, a bunch of disputes have
13
- # to be prepared on the API server side - if they're not, you can
14
- # just skip these tests, as they won't pass.
15
- before(:each) do
16
- @disputes = MangoPay::Dispute.fetch({'page' => 1, 'per_page' => 100})
17
- end
18
-
19
- describe 'FETCH' do
20
- it 'fetches disputes' do
21
- expect(@disputes).to be_kind_of(Array)
22
- expect(@disputes).not_to be_empty
23
- end
24
- it 'fetches a dispute' do
25
- id = @disputes.first['Id']
26
- dispute = MangoPay::Dispute.fetch(id)
27
- expect(dispute['Id']).to eq(id)
28
- end
29
- end
30
-
31
- describe 'TRANSACTIONS' do
32
- it 'fetches transactions for dispute' do
33
- dispute = @disputes.find { |d| d['DisputeType'] == 'NOT_CONTESTABLE' }
34
- id = dispute['Id']
35
- transactions = MangoPay::Dispute.transactions(id, {'per_page' => 1})
36
- expect(transactions).to be_kind_of(Array)
37
- expect(transactions).not_to be_empty
38
- end
39
- end
40
-
41
- describe 'FETCH FOR USER AND WALLET' do
42
- it 'fetches disputes for user' do
43
- dispute = @disputes.find { |d| d['DisputeType'] == 'NOT_CONTESTABLE' }
44
- id = dispute['Id']
45
- transactions = MangoPay::Dispute.transactions(id, {'per_page' => 1})
46
- user_id = transactions[0]['AuthorId']
47
- disputes = MangoPay::Dispute.fetch_for_user(user_id, {'per_page' => 1})
48
- expect(disputes).to be_kind_of(Array)
49
- expect(disputes).not_to be_empty
50
- end
51
- it 'fetches disputes for wallet' do
52
- dispute = @disputes.find {|disp| disp['InitialTransactionId'] != nil}
53
- expect(dispute).not_to be_nil, "Cannot test fetching disputes for wallet because there's no disputes with transaction ID in the disputes list."
54
- payin = MangoPay::PayIn.fetch(dispute['InitialTransactionId'])
55
- wallet_id = payin['CreditedWalletId']
56
- disputes = MangoPay::Dispute.fetch_for_wallet(wallet_id, {'per_page' => 1})
57
- expect(disputes).to be_kind_of(Array)
58
- expect(disputes).not_to be_empty
59
- end
60
- end
61
-
62
- describe 'UPDATE' do
63
- it 'updates a dispute' do
64
- dispute = @disputes.first
65
- id = dispute['Id']
66
- new_tag = dispute['Tag'] + '.com'
67
- changed_dispute = MangoPay::Dispute.update(id, {Tag: new_tag})
68
- expect(changed_dispute['Tag']).to eq(new_tag)
69
- end
70
- end
71
-
72
- describe 'FETCH REPUDIATION' do
73
- it 'fetches a repudiation' do
74
- dispute = @disputes.find {|disp| disp['InitialTransactionId'] != nil && disp['DisputeType'] == 'NOT_CONTESTABLE'}
75
- expect(dispute).not_to be_nil, "Cannot test closing dispute because there's no not contestable disputes with transaction ID in the disputes list."
76
- transactions = MangoPay::Dispute.transactions(dispute['Id'], {'per_page' => 1})
77
- repudiation_id = transactions[0]['Id']
78
- repudiation = MangoPay::Dispute.fetch_repudiation(repudiation_id)
79
- expect(repudiation['Id']).to eq(repudiation_id)
80
- expect(repudiation['Nature']).to eq('REPUDIATION')
81
- end
82
- end
83
-
84
- describe 'CREATE AND FETCH SETTLEMENT TRANSFER' do
85
- it 'creates and fetches settlement transfer' do
86
- dispute = @disputes.find {|disp| disp['Status'] == 'CLOSED' && disp['DisputeType'] == 'NOT_CONTESTABLE'}
87
- expect(dispute).not_to be_nil, "Cannot test creating settlement transfer because there's no closed, not contestable disputes in the disputes list."
88
- transactions = MangoPay::Dispute.transactions(dispute['Id'], {'per_page' => 1})
89
- repudiation_id = transactions[0]['Id']
90
- repudiation = MangoPay::Dispute.fetch_repudiation(repudiation_id)
91
- params = {
92
- AuthorId: repudiation['AuthorId'],
93
- DebitedFunds: {Currency: 'EUR', Amount: 1},
94
- Fees: {Currency: 'EUR', Amount: 0},
95
- Tag: 'Custom tag data'
96
- }
97
-
98
- transfer = MangoPay::Dispute.create_settlement_transfer(repudiation_id, params)
99
- expect(transfer['Type']).to eq('TRANSFER')
100
- expect(transfer['Nature']).to eq('SETTLEMENT')
101
-
102
- fetched_transfer = MangoPay::Dispute.fetch_settlement_transfer(transfer['Id'])
103
- expect(fetched_transfer['Id']).to eq(transfer['Id'])
104
- expect(fetched_transfer['CreationDate']).to eq(transfer['CreationDate'])
105
- end
106
- end
107
-
108
- describe 'DISPUTE DOCUMENTS API' do
109
-
110
- def find_dispute
111
- dispute = @disputes.find {|disp| ['PENDING_CLIENT_ACTION', 'REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status'])}
112
- expect(dispute).not_to be_nil, "Cannot test dispute document API because there's no dispute with expected status in the disputes list."
113
- dispute
114
- end
115
-
116
- def create_doc(dispute = nil)
117
- no_dispute_passed = dispute.nil?
118
- dispute = find_dispute if no_dispute_passed
119
- params = { Type: 'DELIVERY_PROOF', Tag: 'Custom tag data' }
120
- doc = MangoPay::Dispute.create_document(dispute['Id'], params)
121
- doc['dispute'] = dispute if no_dispute_passed # add it for testing purposes
122
- doc
123
- end
124
-
125
- it 'creates dispute document' do
126
- doc = create_doc
127
- expect(doc['Type']).to eq('DELIVERY_PROOF')
128
- expect(doc['Tag']).to eq('Custom tag data')
129
- expect(doc['Status']).to eq('CREATED')
130
- end
131
-
132
- it 'fetches dispute document' do
133
- created_doc = create_doc
134
- fetched_doc = MangoPay::Dispute.fetch_document(created_doc['Id'])
135
- fields = ['Id', 'Type', 'Tag', 'Status', 'CreationDate', 'RefusedReasonType', 'RefusedReasonMessage']
136
- fields.each do |field|
137
- expect(fetched_doc[field]).to eq(created_doc[field])
138
- end
139
- end
140
-
141
- it 'updates dispute document' do
142
- created_doc = create_doc
143
- changed_doc = MangoPay::Dispute.update_document(created_doc['dispute']['Id'], created_doc['Id'], {
144
- Status: 'VALIDATION_ASKED'
145
- })
146
- expect(changed_doc['Id']).to eq(created_doc['Id'])
147
- expect(changed_doc['Status']).to eq('VALIDATION_ASKED')
148
- end
149
-
150
- it 'fetches a list of documents' do
151
- disp = @disputes.find {|disp| disp['Status'] == 'SUBMITTED'}
152
- disp = test_contest_dispute if disp == nil
153
- expect(disp).not_to be_nil, "Cannot test fetching dispute documents because there's no dispute with expected status in the disputes list."
154
-
155
- doc1 = create_doc(disp)
156
- doc2 = create_doc(disp) # for the same dispute
157
-
158
- filters = {'per_page' => 2, 'sort' => 'CreationDate:desc'}
159
-
160
- # fetch last 2 docs for the dispute
161
- docs = MangoPay::Dispute.fetch_documents(disp['Id'], filters)
162
- expect(docs).to be_kind_of(Array)
163
- expect(docs.count).to eq 2 # exactly 2 as pagiantion requested
164
- # all 2 are at top as lastly created
165
- # but may share the same CreationDate
166
- # so the order between them is undetermined
167
- expect(docs).to include(doc1, doc2)
168
-
169
- # fetch all docs ever
170
- docs = MangoPay::Dispute.fetch_documents()
171
- expect(docs).to be_kind_of(Array)
172
- expect(docs.count).to be >= 2
173
-
174
- # fetch last 2 docs ever (sorting by date descending)
175
- docs = MangoPay::Dispute.fetch_documents(nil, filters)
176
- expect(docs).to be_kind_of(Array)
177
- expect(docs.count).to eq 2 # exactly 2 as pagiantion requested
178
- expect(docs).to include(doc1, doc2)
179
- end
180
-
181
- def create_doc_page(file)
182
- disp = find_dispute
183
- doc = create_doc(disp)
184
- MangoPay::Dispute.create_document_page(disp['Id'], doc['Id'], file)
185
- end
186
-
187
- it 'create_document_page accepts Base64 encoded file content' do
188
- fnm = __FILE__.sub('.rb', '.png')
189
- bts = File.open(fnm, 'rb') { |f| f.read }
190
- b64 = Base64.encode64(bts)
191
- ret = create_doc_page(b64)
192
- expect(ret).to be_nil
193
- end
194
-
195
- it 'create_document_page accepts file path' do
196
- fnm = __FILE__.sub('.rb', '.png')
197
- disp = find_dispute
198
- doc = create_doc(disp)
199
- ret = MangoPay::Dispute.create_document_page(disp['Id'], doc['Id'], nil, fnm)
200
- expect(ret).to be_nil
201
- end
202
-
203
- it 'create_document_page fails when input string is not base64-encoded' do
204
- file = 'any file content...'
205
- expect { create_doc_page(file) }.to raise_error { |err|
206
- expect(err).to be_a MangoPay::ResponseError
207
- expect(err.code).to eq '400'
208
- expect(err.type).to eq 'param_error'
209
- }
210
- end
211
- end
212
-
213
- def test_contest_dispute
214
- dispute = @disputes.find do |disp|
215
- ['PENDING_CLIENT_ACTION', 'REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status']) &&
216
- ['CONTESTABLE', 'RETRIEVAL'].include?(disp['DisputeType'])
217
- end
218
- expect(dispute).not_to be_nil, "Cannot test contesting dispute because there's no available disputes with expected status and type in the disputes list."
219
- id = dispute['Id']
220
- contested_funds = dispute['Status'] == 'PENDING_CLIENT_ACTION' ? { Amount: 10, Currency: 'EUR' } : nil;
221
- changed_dispute = MangoPay::Dispute.contest(id, contested_funds)
222
- expect(changed_dispute['Id']).to eq(id)
223
- expect(changed_dispute['Status']).to eq('SUBMITTED')
224
- changed_dispute
225
- end
226
-
227
- describe 'CONTEST' do
228
- it 'contests a dispute' do
229
- test_contest_dispute
230
- end
231
- end
232
-
233
- describe 'RESUBMIT' do
234
- it 'resubmits a dispute' do
235
- dispute = @disputes.find do |disp|
236
- ['REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status'])
237
- end
238
- expect(dispute).not_to be_nil, "Cannot test resubmiting dispute because there's no available disputes with expected status in the disputes list."
239
- id = dispute['Id']
240
- changed_dispute = MangoPay::Dispute.resubmit(id)
241
- expect(changed_dispute['Id']).to eq(id)
242
- expect(changed_dispute['Status']).to eq('SUBMITTED')
243
- end
244
- end
245
-
246
- describe 'CLOSE' do
247
- it 'closes a dispute' do
248
- dispute = @disputes.find do |disp|
249
- ['PENDING_CLIENT_ACTION', 'REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status']) &&
250
- ['CONTESTABLE', 'RETRIEVAL'].include?(disp['DisputeType'])
251
- end
252
- expect(dispute).not_to be_nil, "Cannot test closing dispute because there's no available disputes with expected status in the disputes list."
253
- id = dispute['Id']
254
- changed_dispute = MangoPay::Dispute.close(id)
255
- expect(changed_dispute['Id']).to eq(id)
256
- expect(changed_dispute['Status']).to eq('CLOSED')
257
- end
258
- end
259
-
260
- =end
261
-
262
- end
1
+ describe MangoPay::Dispute do
2
+
3
+ =begin
4
+ comment out all Dispute related unit tests please
5
+ these require manual actions on our side
6
+ and it's infact not suitable like that
7
+
8
+ # IMPORTANT NOTE!
9
+ #
10
+ # Due to the fact the disputes CANNOT be created on user's side,
11
+ # a special approach in testing is needed.
12
+ # In order to get the tests below pass, a bunch of disputes have
13
+ # to be prepared on the API server side - if they're not, you can
14
+ # just skip these tests, as they won't pass.
15
+ before(:each) do
16
+ @disputes = MangoPay::Dispute.fetch({'page' => 1, 'per_page' => 100})
17
+ end
18
+
19
+ describe 'FETCH' do
20
+ it 'fetches disputes' do
21
+ expect(@disputes).to be_kind_of(Array)
22
+ expect(@disputes).not_to be_empty
23
+ end
24
+ it 'fetches a dispute' do
25
+ id = @disputes.first['Id']
26
+ dispute = MangoPay::Dispute.fetch(id)
27
+ expect(dispute['Id']).to eq(id)
28
+ end
29
+ end
30
+
31
+ describe 'TRANSACTIONS' do
32
+ it 'fetches transactions for dispute' do
33
+ dispute = @disputes.find { |d| d['DisputeType'] == 'NOT_CONTESTABLE' }
34
+ id = dispute['Id']
35
+ transactions = MangoPay::Dispute.transactions(id, {'per_page' => 1})
36
+ expect(transactions).to be_kind_of(Array)
37
+ expect(transactions).not_to be_empty
38
+ end
39
+ end
40
+
41
+ describe 'FETCH FOR USER AND WALLET' do
42
+ it 'fetches disputes for user' do
43
+ dispute = @disputes.find { |d| d['DisputeType'] == 'NOT_CONTESTABLE' }
44
+ id = dispute['Id']
45
+ transactions = MangoPay::Dispute.transactions(id, {'per_page' => 1})
46
+ user_id = transactions[0]['AuthorId']
47
+ disputes = MangoPay::Dispute.fetch_for_user(user_id, {'per_page' => 1})
48
+ expect(disputes).to be_kind_of(Array)
49
+ expect(disputes).not_to be_empty
50
+ end
51
+ it 'fetches disputes for wallet' do
52
+ dispute = @disputes.find {|disp| disp['InitialTransactionId'] != nil}
53
+ expect(dispute).not_to be_nil, "Cannot test fetching disputes for wallet because there's no disputes with transaction ID in the disputes list."
54
+ payin = MangoPay::PayIn.fetch(dispute['InitialTransactionId'])
55
+ wallet_id = payin['CreditedWalletId']
56
+ disputes = MangoPay::Dispute.fetch_for_wallet(wallet_id, {'per_page' => 1})
57
+ expect(disputes).to be_kind_of(Array)
58
+ expect(disputes).not_to be_empty
59
+ end
60
+ end
61
+
62
+ describe 'UPDATE' do
63
+ it 'updates a dispute' do
64
+ dispute = @disputes.first
65
+ id = dispute['Id']
66
+ new_tag = dispute['Tag'] + '.com'
67
+ changed_dispute = MangoPay::Dispute.update(id, {Tag: new_tag})
68
+ expect(changed_dispute['Tag']).to eq(new_tag)
69
+ end
70
+ end
71
+
72
+ describe 'FETCH REPUDIATION' do
73
+ it 'fetches a repudiation' do
74
+ dispute = @disputes.find {|disp| disp['InitialTransactionId'] != nil && disp['DisputeType'] == 'NOT_CONTESTABLE'}
75
+ expect(dispute).not_to be_nil, "Cannot test closing dispute because there's no not contestable disputes with transaction ID in the disputes list."
76
+ transactions = MangoPay::Dispute.transactions(dispute['Id'], {'per_page' => 1})
77
+ repudiation_id = transactions[0]['Id']
78
+ repudiation = MangoPay::Dispute.fetch_repudiation(repudiation_id)
79
+ expect(repudiation['Id']).to eq(repudiation_id)
80
+ expect(repudiation['Nature']).to eq('REPUDIATION')
81
+ end
82
+ end
83
+
84
+ describe 'CREATE AND FETCH SETTLEMENT TRANSFER' do
85
+ it 'creates and fetches settlement transfer' do
86
+ dispute = @disputes.find {|disp| disp['Status'] == 'CLOSED' && disp['DisputeType'] == 'NOT_CONTESTABLE'}
87
+ expect(dispute).not_to be_nil, "Cannot test creating settlement transfer because there's no closed, not contestable disputes in the disputes list."
88
+ transactions = MangoPay::Dispute.transactions(dispute['Id'], {'per_page' => 1})
89
+ repudiation_id = transactions[0]['Id']
90
+ repudiation = MangoPay::Dispute.fetch_repudiation(repudiation_id)
91
+ params = {
92
+ AuthorId: repudiation['AuthorId'],
93
+ DebitedFunds: {Currency: 'EUR', Amount: 1},
94
+ Fees: {Currency: 'EUR', Amount: 0},
95
+ Tag: 'Custom tag data'
96
+ }
97
+
98
+ transfer = MangoPay::Dispute.create_settlement_transfer(repudiation_id, params)
99
+ expect(transfer['Type']).to eq('TRANSFER')
100
+ expect(transfer['Nature']).to eq('SETTLEMENT')
101
+
102
+ fetched_transfer = MangoPay::Dispute.fetch_settlement_transfer(transfer['Id'])
103
+ expect(fetched_transfer['Id']).to eq(transfer['Id'])
104
+ expect(fetched_transfer['CreationDate']).to eq(transfer['CreationDate'])
105
+ end
106
+ end
107
+
108
+ describe 'DISPUTE DOCUMENTS API' do
109
+
110
+ def find_dispute
111
+ dispute = @disputes.find {|disp| ['PENDING_CLIENT_ACTION', 'REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status'])}
112
+ expect(dispute).not_to be_nil, "Cannot test dispute document API because there's no dispute with expected status in the disputes list."
113
+ dispute
114
+ end
115
+
116
+ def create_doc(dispute = nil)
117
+ no_dispute_passed = dispute.nil?
118
+ dispute = find_dispute if no_dispute_passed
119
+ params = { Type: 'DELIVERY_PROOF', Tag: 'Custom tag data' }
120
+ doc = MangoPay::Dispute.create_document(dispute['Id'], params)
121
+ doc['dispute'] = dispute if no_dispute_passed # add it for testing purposes
122
+ doc
123
+ end
124
+
125
+ it 'creates dispute document' do
126
+ doc = create_doc
127
+ expect(doc['Type']).to eq('DELIVERY_PROOF')
128
+ expect(doc['Tag']).to eq('Custom tag data')
129
+ expect(doc['Status']).to eq('CREATED')
130
+ end
131
+
132
+ it 'fetches dispute document' do
133
+ created_doc = create_doc
134
+ fetched_doc = MangoPay::Dispute.fetch_document(created_doc['Id'])
135
+ fields = ['Id', 'Type', 'Tag', 'Status', 'CreationDate', 'RefusedReasonType', 'RefusedReasonMessage']
136
+ fields.each do |field|
137
+ expect(fetched_doc[field]).to eq(created_doc[field])
138
+ end
139
+ end
140
+
141
+ it 'updates dispute document' do
142
+ created_doc = create_doc
143
+ changed_doc = MangoPay::Dispute.update_document(created_doc['dispute']['Id'], created_doc['Id'], {
144
+ Status: 'VALIDATION_ASKED'
145
+ })
146
+ expect(changed_doc['Id']).to eq(created_doc['Id'])
147
+ expect(changed_doc['Status']).to eq('VALIDATION_ASKED')
148
+ end
149
+
150
+ it 'fetches a list of documents' do
151
+ disp = @disputes.find {|disp| disp['Status'] == 'SUBMITTED'}
152
+ disp = test_contest_dispute if disp == nil
153
+ expect(disp).not_to be_nil, "Cannot test fetching dispute documents because there's no dispute with expected status in the disputes list."
154
+
155
+ doc1 = create_doc(disp)
156
+ doc2 = create_doc(disp) # for the same dispute
157
+
158
+ filters = {'per_page' => 2, 'sort' => 'CreationDate:desc'}
159
+
160
+ # fetch last 2 docs for the dispute
161
+ docs = MangoPay::Dispute.fetch_documents(disp['Id'], filters)
162
+ expect(docs).to be_kind_of(Array)
163
+ expect(docs.count).to eq 2 # exactly 2 as pagiantion requested
164
+ # all 2 are at top as lastly created
165
+ # but may share the same CreationDate
166
+ # so the order between them is undetermined
167
+ expect(docs).to include(doc1, doc2)
168
+
169
+ # fetch all docs ever
170
+ docs = MangoPay::Dispute.fetch_documents()
171
+ expect(docs).to be_kind_of(Array)
172
+ expect(docs.count).to be >= 2
173
+
174
+ # fetch last 2 docs ever (sorting by date descending)
175
+ docs = MangoPay::Dispute.fetch_documents(nil, filters)
176
+ expect(docs).to be_kind_of(Array)
177
+ expect(docs.count).to eq 2 # exactly 2 as pagiantion requested
178
+ expect(docs).to include(doc1, doc2)
179
+ end
180
+
181
+ def create_doc_page(file)
182
+ disp = find_dispute
183
+ doc = create_doc(disp)
184
+ MangoPay::Dispute.create_document_page(disp['Id'], doc['Id'], file)
185
+ end
186
+
187
+ it 'create_document_page accepts Base64 encoded file content' do
188
+ fnm = __FILE__.sub('.rb', '.png')
189
+ bts = File.open(fnm, 'rb') { |f| f.read }
190
+ b64 = Base64.encode64(bts)
191
+ ret = create_doc_page(b64)
192
+ expect(ret).to be_nil
193
+ end
194
+
195
+ it 'create_document_page accepts file path' do
196
+ fnm = __FILE__.sub('.rb', '.png')
197
+ disp = find_dispute
198
+ doc = create_doc(disp)
199
+ ret = MangoPay::Dispute.create_document_page(disp['Id'], doc['Id'], nil, fnm)
200
+ expect(ret).to be_nil
201
+ end
202
+
203
+ it 'create_document_page fails when input string is not base64-encoded' do
204
+ file = 'any file content...'
205
+ expect { create_doc_page(file) }.to raise_error { |err|
206
+ expect(err).to be_a MangoPay::ResponseError
207
+ expect(err.code).to eq '400'
208
+ expect(err.type).to eq 'param_error'
209
+ }
210
+ end
211
+ end
212
+
213
+ def test_contest_dispute
214
+ dispute = @disputes.find do |disp|
215
+ ['PENDING_CLIENT_ACTION', 'REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status']) &&
216
+ ['CONTESTABLE', 'RETRIEVAL'].include?(disp['DisputeType'])
217
+ end
218
+ expect(dispute).not_to be_nil, "Cannot test contesting dispute because there's no available disputes with expected status and type in the disputes list."
219
+ id = dispute['Id']
220
+ contested_funds = dispute['Status'] == 'PENDING_CLIENT_ACTION' ? { Amount: 10, Currency: 'EUR' } : nil;
221
+ changed_dispute = MangoPay::Dispute.contest(id, contested_funds)
222
+ expect(changed_dispute['Id']).to eq(id)
223
+ expect(changed_dispute['Status']).to eq('SUBMITTED')
224
+ changed_dispute
225
+ end
226
+
227
+ describe 'CONTEST' do
228
+ it 'contests a dispute' do
229
+ test_contest_dispute
230
+ end
231
+ end
232
+
233
+ describe 'RESUBMIT' do
234
+ it 'resubmits a dispute' do
235
+ dispute = @disputes.find do |disp|
236
+ ['REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status'])
237
+ end
238
+ expect(dispute).not_to be_nil, "Cannot test resubmiting dispute because there's no available disputes with expected status in the disputes list."
239
+ id = dispute['Id']
240
+ changed_dispute = MangoPay::Dispute.resubmit(id)
241
+ expect(changed_dispute['Id']).to eq(id)
242
+ expect(changed_dispute['Status']).to eq('SUBMITTED')
243
+ end
244
+ end
245
+
246
+ describe 'CLOSE' do
247
+ it 'closes a dispute' do
248
+ dispute = @disputes.find do |disp|
249
+ ['PENDING_CLIENT_ACTION', 'REOPENED_PENDING_CLIENT_ACTION'].include?(disp['Status']) &&
250
+ ['CONTESTABLE', 'RETRIEVAL'].include?(disp['DisputeType'])
251
+ end
252
+ expect(dispute).not_to be_nil, "Cannot test closing dispute because there's no available disputes with expected status in the disputes list."
253
+ id = dispute['Id']
254
+ changed_dispute = MangoPay::Dispute.close(id)
255
+ expect(changed_dispute['Id']).to eq(id)
256
+ expect(changed_dispute['Status']).to eq('CLOSED')
257
+ end
258
+ end
259
+
260
+ =end
261
+
262
+ end