summaryrefslogtreecommitdiffstats
path: root/tests/test_lingo_remote_regie.py
blob: 6ae7087880ef57521054de6a8de164c6fc303039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# -*- coding: utf-8 -*-

import json
import pytest
import mock
from decimal import Decimal
from requests.exceptions import ConnectionError

from django.apps import apps
from django.test.client import RequestFactory
from django.test import override_settings
from django.core.urlresolvers import reverse
from django.conf import settings
from django.core.management import call_command
from django.utils.encoding import force_text
from django.utils.six.moves.urllib import parse as urlparse
from django.utils.timezone import timedelta, now
from django.contrib.auth.models import User

from combo.utils import check_query, aes_hex_encrypt
from combo.data.models import Page
from combo.apps.lingo.models import (Regie, ActiveItems, ItemsHistory, SelfDeclaredInvoicePayment,
                                     Transaction, BasketItem)

pytestmark = pytest.mark.django_db


INVOICES = [
    {
        'id': 'F201601',
        'display_id': 'F-2016-One',
        'label': 'invoice-one',
        'regie': 'remote',
        'created': '2016-02-02',
        'pay_limit_date': '2999-12-31',
        'total_amount': '123.45',
        'amount': '123.45',
        'has_pdf': True,
        'online_payment': True,
        'paid': False,
        'payment_date': '1970-01-01',
        'no_online_payment_reason': '',
    },
]

@pytest.fixture
def admin():
    try:
        admin = User.objects.get(username='foo')
    except User.DoesNotExist:
        admin = User.objects.create_user('foo', email=None, password='bar')
    admin.email = 'foo@example.net'
    admin.save()
    return admin

@pytest.fixture
def remote_regie():
    try:
        regie = Regie.objects.get(slug='remote')
    except Regie.DoesNotExist:
        regie = Regie()
        regie.label = 'Remote'
        regie.slug = 'remote'
        regie.description = 'remote'
        regie.payment_min_amount = Decimal(2.0)
        regie.service = 'dummy'
        regie.service_options = {'siret': '1234'}
        regie.webservice_url = 'http://example.org/regie' # is_remote
        regie.save()
    return regie


class MockUser(object):
    email = 'foo@example.net'
    def is_authenticated(self):
        return True

    def get_name_id(self):
        return 'r2d2'


@mock.patch('combo.utils.requests_wrapper.RequestsSession.request')
def test_remote_regie_active_invoices_cell(mock_request, remote_regie):
    assert remote_regie.is_remote() == True

    page = Page(title='xxx', slug='test_basket_cell', template_name='standard')
    page.save()
    cell = ActiveItems(regie='remote', page=page, placeholder='content', order=0)
    context = {'request': RequestFactory().get('/')}
    context['synchronous'] = True # to get fresh content

    user = MockUser()
    context['user'] = user
    context['request'].user = user

    assert cell.is_relevant(context) is True

    # show regie with an invoice
    ws_invoices = {'err': 0, 'data': INVOICES}
    mock_response = mock.Mock(status_code=200, content=json.dumps(ws_invoices))
    mock_response.json.return_value = ws_invoices
    mock_request.return_value = mock_response
    content = cell.render(context)
    assert 'F-2016-One' in content
    assert '123.45' in content

    assert '?page=%s' % page.pk in content
    # check if regie webservice has been correctly called
    assert mock_request.call_args[0][0] == 'GET'
    url = mock_request.call_args[0][1]
    scheme, netloc, path, params, querystring, fragment = urlparse.urlparse(url)
    assert scheme == 'http'
    assert netloc == 'example.org'
    assert path == '/regie/invoices/'
    query = urlparse.parse_qs(querystring, keep_blank_values=True)
    assert query['NameID'][0] == 'r2d2'
    assert query['orig'][0] == 'combo'
    assert check_query(querystring, 'combo') == True

    # with no invoice
    ws_invoices = {'err': 0, 'data': []}
    mock_response = mock.Mock(status_code=200, content=json.dumps(ws_invoices))
    mock_response.json.return_value = ws_invoices
    mock_request.return_value = mock_response
    content = cell.render(context)
    assert 'No items yet' in content

@mock.patch('combo.utils.requests_wrapper.RequestsSession.request')
def test_remote_regie_past_invoices_cell(mock_request, remote_regie):
    assert remote_regie.is_remote() == True

    page = Page(title='xxx', slug='test_basket_cell', template_name='standard')
    page.save()
    cell = ItemsHistory(regie='remote', page=page, placeholder='content', order=0)
    context = {'request': RequestFactory().get('/')}
    context['synchronous'] = True # to get fresh content

    user = MockUser()
    context['user'] = user
    context['request'].user = user

    assert cell.is_relevant(context) is True

    # show regie with an invoice
    ws_invoices = {'err': 0, 'data': INVOICES}
    mock_response = mock.Mock(status_code=200, content=json.dumps(ws_invoices))
    mock_response.json.return_value = ws_invoices
    mock_request.return_value = mock_response
    content = cell.render(context)
    assert 'F-2016-One' in content
    assert '123.45' in content

    # check if regie webservice has been correctly called
    assert mock_request.call_args[0][0] == 'GET'
    url = mock_request.call_args[0][1]
    scheme, netloc, path, params, querystring, fragment = urlparse.urlparse(url)
    assert scheme == 'http'
    assert netloc == 'example.org'
    assert path == '/regie/invoices/history/'
    query = urlparse.parse_qs(querystring, keep_blank_values=True)
    assert query['NameID'][0] == 'r2d2'
    assert query['orig'][0] == 'combo'
    assert check_query(querystring, 'combo') == True

    # with no invoice
    ws_invoices = {'err': 0, 'data': []}
    mock_response = mock.Mock(status_code=200, content=json.dumps(ws_invoices))
    mock_response.json.return_value = ws_invoices
    mock_request.return_value = mock_response
    content = cell.render(context)
    assert 'No items yet' in content

@mock.patch('combo.apps.lingo.models.Regie.pay_invoice')
@mock.patch('combo.apps.lingo.models.requests.get')
def test_anonymous_successful_item_payment(mock_get, mock_pay_invoice, app, remote_regie):
    assert remote_regie.is_remote() == True
    encrypt_id = aes_hex_encrypt(settings.SECRET_KEY, 'F201601')
    mock_json = mock.Mock()
    mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
    mock_get.return_value = mock_json
    mock_pay_invoice.return_value = mock.Mock(status_code=200)
    resp = app.get('/lingo/item/%s/%s/' % (remote_regie.id, encrypt_id))
    form = resp.form

    assert 'email' in form.fields
    assert form['email'].value == ''
    assert 'item_url' in form.fields
    assert form['item_url'].value == '/lingo/item/%s/%s/' % (remote_regie.id, encrypt_id)
    assert 'item' in form.fields
    assert form['item'].value == 'F201601'
    assert 'regie' in form.fields
    assert form['regie'].value == force_text(remote_regie.pk)

    form['email'] = 'ghost@buster.com'
    resp = form.submit()

    assert resp.status_code == 302
    location = resp.location
    assert 'dummy-payment' in location
    parsed = urlparse.urlparse(location)
    # get return_url and transaction id from location
    qs = urlparse.parse_qs(parsed.query)
    args = {'transaction_id': qs['transaction_id'][0], 'signed': True,
            'ok': True, 'reason': 'Paid'}
    # make sure return url is the user return URL
    assert urlparse.urlparse(qs['return_url'][0]).path.startswith(
        reverse('lingo-return', kwargs={'regie_pk': remote_regie.id}))
    # simulate successful return URL
    resp = app.get(qs['return_url'][0], params=args)
    assert resp.status_code == 302
    assert urlparse.urlparse(resp.url).path == '/'
    # simulate successful call to callback URL
    resp = app.get(reverse('lingo-callback', kwargs={'regie_pk': remote_regie.id}), params=args)
    trans = Transaction.objects.all()
    b_item = BasketItem.objects.all()

    assert trans
    assert b_item
    trans = trans[0]
    b_item = b_item[0]
    assert b_item.subject == 'Invoice #%s' % INVOICES[0]['display_id']
    assert b_item.amount == Decimal(INVOICES[0]['amount'])
    assert b_item in trans.items.all()

    assert resp.status_code == 200

@mock.patch('combo.apps.lingo.models.requests.get')
def test_anonymous_item_payment_email_error(mock_get, app, remote_regie):
    assert remote_regie.is_remote() == True
    encrypt_id = aes_hex_encrypt(settings.SECRET_KEY, 'F201601')
    mock_json = mock.Mock()
    mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
    mock_get.return_value = mock_json
    resp = app.get('/lingo/item/%s/%s/' % (remote_regie.id, encrypt_id))
    form = resp.form
    resp = form.submit()

    assert resp.status_code == 302
    path = urlparse.urlparse(resp.location).path
    assert path == '/lingo/item/%s/%s/' % (remote_regie.id, encrypt_id)

@mock.patch('combo.apps.lingo.models.requests.get')
def test_wrong_crypted_item(mock_get, remote_regie, app):
    assert remote_regie.is_remote() == True
    mock_json = mock.Mock()
    mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
    mock_get.return_value = mock_json
    resp = app.get('/lingo/item/%s/%s/' % (remote_regie.id, 'zrzer854sfaear45e6rzerzerzef'), status=404)

@mock.patch('combo.apps.lingo.models.requests.get')
def test_self_declared_invoice(mock_get, app, remote_regie):
    mock_json = mock.Mock()
    mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
    mock_get.return_value = mock_json

    page = Page(title='xxx', slug='test-self-invoice', template_name='standard')
    page.save()
    cell = SelfDeclaredInvoicePayment(regie='remote', page=page, placeholder='content', order=0)
    cell.save()

    resp = app.get('/test-self-invoice/')
    resp = resp.form.submit().follow()
    assert 'Sorry, no invoice were found with that number and amount.'

    resp = app.get('/test-self-invoice/')
    resp.form['invoice-number'] = 'F201601'
    resp.form['invoice-amount'] = 'FOOBAR' # wrong format
    resp = resp.form.submit().follow()
    assert 'Sorry, the provided amount is invalid.'

    resp = app.get('/test-self-invoice/')
    resp.form['invoice-number'] = 'F201602' # invalid number
    resp.form['invoice-amount'] = '123.45'
    resp = resp.form.submit().follow()
    assert 'Sorry, no invoice were found with that number and amount.'

    resp = app.get('/test-self-invoice/')
    resp.form['invoice-number'] = 'F201601'
    resp.form['invoice-amount'] = '123.46' # invalid amount
    resp = resp.form.submit().follow()
    assert 'Sorry, no invoice were found with that number and amount.'

    resp = app.get('/test-self-invoice/')
    resp.form['invoice-number'] = 'F201601'
    resp.form['invoice-amount'] = '123.45'
    resp = resp.form.submit()
    path = urlparse.urlparse(resp.location).path
    assert path.startswith('/lingo/item/%s/' % remote_regie.id)
    resp = resp.follow()


@mock.patch('combo.apps.lingo.models.Regie.pay_invoice')
@mock.patch('combo.apps.lingo.models.requests.get')
@mock.patch('combo.apps.lingo.models.requests.post')
def test_remote_item_payment_failure(mock_post, mock_get, mock_pay_invoice, app, remote_regie):
    page = Page(title='xxx', slug='active-remote-invoices-page', template_name='standard')
    page.save()
    assert remote_regie.is_remote()
    encrypt_id = aes_hex_encrypt(settings.SECRET_KEY, 'F201601')
    mock_json = mock.Mock()
    mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
    mock_get.return_value = mock_json
    mock_pay_invoice.return_value = mock.Mock(status_code=200)
    resp = app.get('/lingo/item/%s/%s/?page=%s' % (remote_regie.id, encrypt_id, page.pk))
    form = resp.form

    assert 'email' in form.fields
    assert form['email'].value == ''
    assert 'item_url' in form.fields
    assert form['item_url'].value == '/lingo/item/%s/%s/?page=%s' % (remote_regie.id, encrypt_id, page.pk)
    assert 'item' in form.fields
    assert form['item'].value == 'F201601'
    assert 'regie' in form.fields
    assert form['regie'].value == force_text(remote_regie.pk)

    form['email'] = 'test@example.net'
    resp = form.submit()

    assert resp.status_code == 302
    location = resp.location
    assert 'dummy-payment' in location
    parsed = urlparse.urlparse(location)
    # get return_url and transaction id from location
    qs = urlparse.parse_qs(parsed.query)
    args = {'transaction_id': qs['transaction_id'][0], 'signed': True,
            'ok': True, 'reason': 'Paid'}
    # make sure return url is the user return URL
    assert urlparse.urlparse(qs['return_url'][0]).path.startswith(
        reverse('lingo-return', kwargs={'regie_pk': remote_regie.id}))
    # simulate payment failure
    mock_get.side_effect = ConnectionError('where is my hostname?')
    resp = app.get(qs['return_url'][0], params=args)
    assert resp.status_code == 302
    assert urlparse.urlparse(resp.url).path == '/active-remote-invoices-page/'
    # simulate successful call to callback URL
    resp = app.get(reverse('lingo-callback', kwargs={'regie_pk': remote_regie.id}), params=args)
    trans = Transaction.objects.all()
    b_item = BasketItem.objects.all()

    assert trans.count() == 1
    assert not b_item
    assert trans[0].to_be_paid_remote_items
    assert resp.status_code == 200

    mock_get.side_effect = None
    appconfig = apps.get_app_config('lingo')
    appconfig.update_transactions()

    assert Transaction.objects.count() == 1
    assert BasketItem.objects.count() == 1
    assert Transaction.objects.all()[0].to_be_paid_remote_items is None

    appconfig.update_transactions()


@mock.patch('combo.apps.lingo.models.Regie.pay_invoice')
@mock.patch('combo.apps.lingo.models.requests.get')
def test_remote_invoice_successfull_payment_redirect(mock_get, mock_pay_invoice, app, remote_regie):
    page = Page(title='xxx', slug='active-remote-invoices-page', template_name='standard')
    page.save()
    assert remote_regie.is_remote()
    encrypt_id = aes_hex_encrypt(settings.SECRET_KEY, 'F201601')
    mock_json = mock.Mock()
    mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
    mock_get.return_value = mock_json
    mock_pay_invoice.return_value = mock.Mock(status_code=200)
    resp = app.get('/lingo/item/%s/%s/?page=%s' % (remote_regie.id, encrypt_id, page.pk))
    form = resp.form
    assert form['next_url'].value == '/active-remote-invoices-page/'
    form['email'] = 'test@example.net'
    resp = form.submit()

    assert resp.status_code == 302
    location = resp.location
    assert 'dummy-payment' in location
    parsed = urlparse.urlparse(location)
    # get return_url and transaction id from location
    qs = urlparse.parse_qs(parsed.query)
    args = {'transaction_id': qs['transaction_id'][0], 'signed': True,
            'ok': True, 'reason': 'Paid'}

    resp = app.get(qs['return_url'][0], params=args)
    assert resp.status_code == 302
    assert urlparse.urlparse(resp.location).path == '/active-remote-invoices-page/'


@mock.patch('combo.apps.lingo.models.UserSAMLIdentifier')
@mock.patch('combo.apps.lingo.models.requests.get')
def test_send_new_remote_invoices_by_email(mock_get, user_saml, admin, app, remote_regie, mailoutbox):
    mocked_objects = mock.Mock()
    mocked_objects.get.return_value = mock.Mock(user=admin)
    user_saml.objects = mocked_objects
    invoice_now = now()
    creation_date = (invoice_now - timedelta(days=1)).date().isoformat()
    pay_limit_date = (invoice_now + timedelta(days=30)).date().isoformat()
    FAKE_PENDING_INVOICES = {
        'data' :  {'foo': {'invoices': [{'id': '01', 'label': '010101', 'paid': False,
                        'amount': '37.26',  'total_amount': '37.26', 'online_payment': False,
                        'has_pdf': True, 'created': creation_date,
                        'pay_limit_date': pay_limit_date}]},
        }
    }
    mock_response = mock.Mock(status_code=200, content=json.dumps(FAKE_PENDING_INVOICES))
    mock_response.json.return_value = FAKE_PENDING_INVOICES
    mock_get.return_value = mock_response
    with override_settings(LANGUAGE_CODE='fr'):
        call_command('notify_new_remote_invoices')

    assert len(mailoutbox) == 1
    assert mailoutbox[0].recipients() == ['foo@example.net']
    assert mailoutbox[0].from_email == settings.DEFAULT_FROM_EMAIL
    assert mailoutbox[0].subject == u'Nouvelle facture numéro 01 disponible'
    html_message = mailoutbox[0].alternatives[0][0]
    assert 'http://localhost' in mailoutbox[0].body
    assert 'http://localhost' in html_message
    assert mailoutbox[0].attachments[0][0] == '01.pdf'
    assert mailoutbox[0].attachments[0][2] == 'application/pdf'