summaryrefslogtreecommitdiffstats
path: root/tests/test_lingo_manager.py
blob: a66a1786561fef24ab9eeda81f27bb0c113dd9ea (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
# -*- coding: utf-8 -*-

import datetime

from django.contrib.auth.models import User
from django.core.wsgi import get_wsgi_application
from django.utils.timezone import now
from webtest import TestApp
import pytest

import eopayment
from combo.data.models import Page
from combo.apps.lingo.models import (Regie, BasketItem, Transaction, ActiveItems,
                                     TipiPaymentFormCell, PaymentBackend)
from decimal import Decimal

pytestmark = pytest.mark.django_db


@pytest.fixture
def payment_backend():
    return PaymentBackend.objects.create(
            label='test1', slug='test1', service='dummy', service_options={'siret': '1234'})


def login(app, username='admin', password='admin'):
    login_page = app.get('/login/')
    login_form = login_page.forms[0]
    login_form['username'] = username
    login_form['password'] = password
    resp = login_form.submit()
    assert resp.status_int == 302
    return app

def test_access(app, admin_user):
    app = login(app)
    resp = app.get('/manage/', status=200)
    assert '/manage/lingo/' in resp.text

def test_add_regie(app, admin_user, payment_backend):
    Regie.objects.all().delete()
    app = login(app)
    resp = app.get('/manage/lingo/regies/', status=200)
    resp = resp.click('New')
    resp.forms[0]['label'] = 'Test'
    resp.forms[0]['slug'] = 'test'
    resp.forms[0]['description'] = 'description'
    resp.forms[0]['payment_backend'] = payment_backend.pk
    assert resp.form['is_default'].checked is True
    resp = resp.forms[0].submit()
    assert resp.location.endswith('/manage/lingo/regies/')
    assert Regie.objects.count() == 1
    regie = Regie.objects.all()[0]
    assert regie.label == 'Test'
    assert regie.is_default is True

def test_edit_regie(app, admin_user, payment_backend):
    test_add_regie(app, admin_user, payment_backend)
    app = login(app)
    resp = app.get('/manage/lingo/regies/', status=200)
    resp = resp.click('Test')
    resp.forms[0]['description'] = 'other description'
    resp = resp.forms[0].submit()
    assert resp.location.endswith('/manage/lingo/regies/')
    assert Regie.objects.count() == 1
    regie = Regie.objects.all()[0]
    assert regie.description == 'other description'

def test_delete_regie(app, admin_user, payment_backend):
    test_add_regie(app, admin_user, payment_backend)
    app = login(app)
    resp = app.get('/manage/lingo/regies/', status=200)
    resp = resp.click('remove')
    assert 'Are you sure you want to delete this?' in resp.text
    resp = resp.forms[0].submit()
    assert Regie.objects.count() == 0

def test_add_second_regie(app, admin_user, payment_backend):
    test_add_regie(app, admin_user, payment_backend)
    regie = Regie.objects.all()[0]

    app = login(app)
    resp = app.get('/manage/lingo/regies/', status=200)
    resp = resp.click('New')
    resp.forms[0]['label'] = 'Test2'
    resp.forms[0]['slug'] = 'test2'
    resp.forms[0]['description'] = 'description'
    resp.forms[0]['payment_backend'] = payment_backend.pk
    assert resp.form['is_default'].checked is False
    resp = resp.forms[0].submit()
    assert resp.location.endswith('/manage/lingo/regies/')

    assert Regie.objects.count() == 2
    assert Regie.objects.get(id=regie.id).is_default is True
    assert Regie.objects.exclude(id=regie.id)[0].is_default is False

def test_download_transaction(app, admin_user, payment_backend):
    test_add_regie(app, admin_user, payment_backend)
    regie = Regie.objects.filter(slug='test')[0]
    user = User.objects.create_user('dimebag', 'dime@bag.pan', 'pwd')
    user.last_name = u'Darëll'
    user.first_name = 'Dimebag'
    user.save()

    b_item = BasketItem.objects.create(user=user, regie=regie, subject='it\'s a subject',
                                       source_url='http://example.net', amount=18.5)
    trans1 = Transaction.objects.create(regie=regie, remote_items='remote items lol', order_id='1', user=user,
                                        bank_transaction_id='567', status=eopayment.PAID)
    trans2 = Transaction.objects.create(regie=regie, remote_items='remote items omg', order_id='2', user=user,
                                        bank_transaction_id='136', status=eopayment.PAID)
    trans1.items = [b_item,]
    trans1.save()
    trans2.items = [b_item,]
    trans2.save()
    app = login(app)
    resp = app.get('/manage/lingo/transactions/download-csv/', status=200)
    content = [i for i in [item.split(',') for item in resp.text.split('\r\n')]]
    for row in content[:-1]:
        if row[0] == '2':
            assert row[1] == trans2.bank_transaction_id
            assert row[3] == '%s %s' % (user.first_name, user.last_name)
            assert Decimal(row[4]) == Decimal(trans2.amount)
            assert row[5] == b_item.subject
            assert Decimal(row[6]) == b_item.amount
        else:
            assert row[1] == trans1.bank_transaction_id
            assert row[3] == '%s %s' % (user.first_name, user.last_name)
            assert Decimal(row[4]) == Decimal(trans1.amount)
            assert row[5] == b_item.subject
            assert Decimal(row[6]) == b_item.amount

def test_transactions_search(app, admin_user):
    for i in range(50):
        Transaction(status=eopayment.PAID,
                order_id='order id %s' % (i+1),
                bank_transaction_id='bank id %s' % (i+1),
                amount=1+i).save()

    app = login(app)
    resp = app.get('/manage/lingo/', status=200)
    assert resp.text.count('<tr') == 11

    resp.form['q'] = 'order id 16'
    resp = resp.form.submit()
    assert resp.text.count('<tr') == 2

    resp.form['q'] = now().strftime('%d/%m/%Y')
    resp = resp.form.submit()
    assert resp.text.count('<tr') == 11

    resp.form['q'] = (now() + datetime.timedelta(days=2)).strftime('%d/%m%/Y')
    resp = resp.form.submit()
    assert resp.text.count('<tr') == 0
    assert 'No transactions found matching' in resp.text

def test_configure_tipi_cell(app, admin_user):
    page = Page(title='tipi', slug='tipi', template_name='standard')
    page.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id, status=200)

    cell = TipiPaymentFormCell(title='Test payment', page=page, placeholder='content', order=0)
    cell.save()
    resp = app.get('/manage/pages/%s/' % page.id, status=200)
    assert resp.text.count('Exer:') == 1
    assert resp.text.count('IDPCE:') == 1
    assert resp.text.count('IDLIGNE:') == 1
    assert resp.text.count('ROLREC:') == 1
    assert resp.text.count('ROLDEB:') == 1
    assert resp.text.count('ROLDET:') == 1

def test_configure_invoices_cell(app, admin_user, payment_backend):
    page = Page(title='xxx', slug='test', template_name='standard')
    page.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id, status=200)

    # 2 occurences of tipi payment form should be present, and no other cells
    assert resp.text.count('lingo') == 2
    assert resp.text.count('lingo_tipipaymentformcell') == 2

    regie = Regie()
    regie.label = 'Test'
    regie.slug = 'test'
    regie.description = 'test'
    regie.payment_backend = payment_backend
    regie.save()

    resp = app.get('/manage/pages/%s/' % page.id, status=200)
    assert 'lingorecenttransactionscell' in resp.text  # because there's a regie
    assert not 'lingo_activeitems' in resp.text  # because there's no webservice

    regie.webservice_url = 'http://example.net/'
    regie.save()

    resp = app.get('/manage/pages/%s/' % page.id, status=200)
    assert 'lingo_activeitems' in resp.text

    cell = ActiveItems(regie='remote', page=page, placeholder='content', order=0)
    cell.save()
    resp = app.get('/manage/pages/%s/' % page.id, status=200)
    assert 'clingo_activeitems-%s-regie' % cell.id not in resp.form.fields
    assert 'clingo_activeitems-%s-title' % cell.id in resp.form.fields

    regie2 = Regie()
    regie2.label = 'Test2'
    regie2.slug = 'test2'
    regie2.description = 'test2'
    regie2.webservice_url = 'http://example.net/'
    regie2.payment_backend = payment_backend
    regie2.save()

    resp = app.get('/manage/pages/%s/' % page.id, status=200)
    assert 'clingo_activeitems-%s-regie' % cell.id in resp.form.fields
    assert 'clingo_activeitems-%s-title' % cell.id in resp.form.fields
    options = [x[2] for x in resp.form['clingo_activeitems-%s-regie' % cell.id].options]
    assert 'All' in options
    assert regie.label in options
    assert regie2.label in options

    resp.form['clingo_activeitems-%s-regie' % cell.id].value = 'test2'
    resp.form.submit()
    assert ActiveItems.objects.get(id=cell.id).regie == regie2.slug


def test_payment_backend_list(app, admin_user):
    PaymentBackend.objects.create(label='label1', slug='slug1')
    PaymentBackend.objects.create(label='label2', slug='slug2')
    app = login(app)
    resp = app.get('/manage/lingo/paymentbackends/', status=200)

    assert '/manage/lingo/paymentbackends/add' in resp.text
    assert 'label1' in resp.content
    assert 'label2' in resp.content

    for payment_backend in PaymentBackend.objects.all():
        assert '/manage/lingo/paymentbackends/%s' % payment_backend.pk in resp.text
        assert '/manage/lingo/paymentbackends/%s/delete' % payment_backend.pk in resp.text


def test_add_payment_backend(app, admin_user):
    assert not PaymentBackend.objects.count()
    app = login(app)
    resp = app.get('/manage/lingo/paymentbackends/add/', status=200)
    assert '/manage/lingo/paymentbackends/' in resp.text

    resp.forms[0]['label'] = 'Test'
    resp.forms[0]['slug'] = 'test-add'
    resp.forms[0]['service'] = 'dummy'
    resp.forms[0]['service_options'] = '{"siret": "1234"}'
    resp = resp.forms[0].submit()

    assert PaymentBackend.objects.count() == 1
    payment_backend = PaymentBackend.objects.get(slug='test-add')
    assert payment_backend.label == 'Test'
    assert payment_backend.service_options == {'siret': '1234'}

    assert resp.location.endswith('/manage/lingo/paymentbackends/')


def test_edit_payment_backend(app, admin_user):
    payment_backend = PaymentBackend.objects.create(label='label1', slug='slug1')
    app = login(app)
    resp = app.get('/manage/lingo/paymentbackends/%s/edit' % payment_backend.pk, status=200)
    assert '/manage/lingo/paymentbackends/' in resp.text

    resp.forms[0]['label'] = 'label1-modified'
    resp.forms[0]['slug'] = 'slug1'
    resp.forms[0]['service'] = 'dummy'
    resp.forms[0]['service_options'] = '{"siret": "1234"}'
    resp = resp.forms[0].submit()

    assert resp.location.endswith('/manage/lingo/paymentbackends/')
    payment_backend = PaymentBackend.objects.get(slug='slug1')
    assert payment_backend.label == 'label1-modified'