16 |
16 |
from django.utils import timezone
|
17 |
17 |
from django.utils.http import urlencode
|
18 |
18 |
from django.utils.six.moves.urllib import parse as urlparse
|
|
19 |
from django.utils.timezone import utc
|
19 |
20 |
from django.contrib.messages.storage.session import SessionStorage
|
20 |
21 |
from webtest import TestApp
|
21 |
22 |
|
... | ... | |
1302 |
1303 |
resp = login(app).get(basket_page.get_online_url())
|
1303 |
1304 |
resp = resp.form.submit().follow()
|
1304 |
1305 |
assert 'Failed to initiate payment request' in resp.text
|
|
1306 |
|
|
1307 |
|
|
1308 |
@pytest.mark.parametrize('transaction_date', [None, datetime(2020, 1, 1, 12, 00, 00, tzinfo=utc)])
|
|
1309 |
def test_bank_transaction_date(app, key, regie, user, john_doe, caplog, transaction_date):
|
|
1310 |
amount = 12
|
|
1311 |
data = {
|
|
1312 |
'amount': amount,
|
|
1313 |
'display_name': 'test amount',
|
|
1314 |
'url': 'http://example.com/'
|
|
1315 |
}
|
|
1316 |
url = reverse('api-add-basket-item') + '?orig=wcs'
|
|
1317 |
url = sign_url(url, key)
|
|
1318 |
resp = app.post_json(url, params=data)
|
|
1319 |
# check that an unpaid item exists in basket
|
|
1320 |
assert BasketItem.objects.filter(regie=regie, amount=amount, payment_date__isnull=True).exists()
|
|
1321 |
|
|
1322 |
payment_url = resp.json['payment_url']
|
|
1323 |
resp = app.get(payment_url, params={'next_url': 'http://example.net/form/id/'})
|
|
1324 |
|
|
1325 |
assert Transaction.objects.count() == 1
|
|
1326 |
transaction = Transaction.objects.get()
|
|
1327 |
order_id = transaction.order_id
|
|
1328 |
|
|
1329 |
# make sure the redirection is done to the payment backend
|
|
1330 |
assert resp.location.startswith('http://dummy-payment.demo.entrouvert.com/')
|
|
1331 |
qs = urlparse.parse_qs(urlparse.urlparse(resp.location).query)
|
|
1332 |
assert qs['amount'] == ['12.00']
|
|
1333 |
# simulate successful payment response from dummy backend
|
|
1334 |
transaction_id = qs['transaction_id'][0],
|
|
1335 |
data = {
|
|
1336 |
'transaction_id': transaction_id,
|
|
1337 |
'ok': True,
|
|
1338 |
'amount': qs['amount'][0],
|
|
1339 |
'signed': True,
|
|
1340 |
}
|
|
1341 |
mock_response = mock.Mock()
|
|
1342 |
mock_response.status_code = 200
|
|
1343 |
# simulate payment service redirecting the user to /lingo/return/... (eopayment
|
|
1344 |
# dummy module put that URL in return_url query string parameter).
|
|
1345 |
with mock.patch('eopayment.dummy.Payment.response') as eopayment_response:
|
|
1346 |
return_value = eopayment_response.return_value
|
|
1347 |
return_value.result = 1 # eopayment.RECEIVED
|
|
1348 |
return_value.order_id = order_id
|
|
1349 |
return_value.bank_data = {'a': 'b'}
|
|
1350 |
return_value.transaction_id = '1234'
|
|
1351 |
return_value.transaction_date = transaction_date
|
|
1352 |
with mock.patch('combo.utils.requests_wrapper.RequestsSession.request', return_value=mock_response) as request:
|
|
1353 |
resp = app.get(qs['return_url'][0], params=data)
|
|
1354 |
assert request.call_count == 0
|
|
1355 |
# check transaction_date was recorded
|
|
1356 |
transaction.refresh_from_db()
|
|
1357 |
assert transaction.bank_transaction_date == transaction_date
|
|
1358 |
if transaction_date is None:
|
|
1359 |
assert 'no transaction date' in caplog.text
|
|
1360 |
|
|
1361 |
# send another transaction_date
|
|
1362 |
with mock.patch('eopayment.dummy.Payment.response') as eopayment_response:
|
|
1363 |
return_value = eopayment_response.return_value
|
|
1364 |
return_value.result = 3 # eopayment.PAID
|
|
1365 |
return_value.order_id = order_id
|
|
1366 |
return_value.bank_data = {'a': 'b'}
|
|
1367 |
return_value.transaction_id = '1234'
|
|
1368 |
return_value.transaction_date = datetime(2020, 1, 2, 12, 0, tzinfo=utc)
|
|
1369 |
with mock.patch('combo.utils.requests_wrapper.RequestsSession.post', return_value=mock_response) as post:
|
|
1370 |
resp = app.get(qs['return_url'][0], params=data)
|
|
1371 |
if transaction_date is None:
|
|
1372 |
assert json.loads(post.call_args[1]['data'])['bank_transaction_date'] == '2020-01-02T12:00:00'
|
|
1373 |
else:
|
|
1374 |
assert json.loads(post.call_args[1]['data'])['bank_transaction_date'] == '2020-01-01T12:00:00'
|
|
1375 |
assert post.call_args[0][0] == 'http://example.com/jump/trigger/paid'
|
|
1376 |
transaction.refresh_from_db()
|
|
1377 |
if transaction_date is None:
|
|
1378 |
transaction.refresh_from_db()
|
|
1379 |
assert transaction.bank_transaction_date == datetime(2020, 1, 2, 12, 0, tzinfo=utc)
|
|
1380 |
else:
|
|
1381 |
assert transaction.bank_transaction_date == transaction_date
|
|
1382 |
assert 'new transaction_date for transaction' in caplog.text
|
1305 |
|
-
|