tests.py 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#

Krophil's avatar
Krophil committed
25
from django.test import TestCase
26
from django.urls import reverse
Sli's avatar
Sli committed
27
from django.core.management import call_command
Krophil's avatar
Krophil committed
28
from datetime import date
Skia's avatar
Skia committed
29

Sli's avatar
Sli committed
30
from core.models import User
Sli's avatar
Sli committed
31
32
33
34
35
36
37
from accounting.models import (
    GeneralJournal,
    Operation,
    Label,
    AccountingType,
    SimplifiedAccountingType,
)
Sli's avatar
Sli committed
38
39
40
41
42
43
44
45
46
47
48


class RefoundAccountTest(TestCase):
    def setUp(self):
        call_command("populate")
        self.skia = User.objects.filter(username="skia").first()
        # reffil skia's account
        self.skia.customer.amount = 800
        self.skia.customer.save()

    def test_permission_denied(self):
Sli's avatar
Sli committed
49
50
51
52
        self.client.login(username="guy", password="plop")
        response_post = self.client.post(
            reverse("accounting:refound_account"), {"user": self.skia.id}
        )
Sli's avatar
Sli committed
53
54
55
56
57
        response_get = self.client.get(reverse("accounting:refound_account"))
        self.assertTrue(response_get.status_code == 403)
        self.assertTrue(response_post.status_code == 403)

    def test_root_granteed(self):
Sli's avatar
Sli committed
58
59
60
61
62
        self.client.login(username="root", password="plop")
        response_post = self.client.post(
            reverse("accounting:refound_account"), {"user": self.skia.id}
        )
        self.skia = User.objects.filter(username="skia").first()
Sli's avatar
Sli committed
63
64
65
66
67
68
69
        response_get = self.client.get(reverse("accounting:refound_account"))
        self.assertFalse(response_get.status_code == 403)
        self.assertTrue('<form action="" method="post">' in str(response_get.content))
        self.assertFalse(response_post.status_code == 403)
        self.assertTrue(self.skia.customer.amount == 0)

    def test_comptable_granteed(self):
Sli's avatar
Sli committed
70
71
72
73
74
        self.client.login(username="comptable", password="plop")
        response_post = self.client.post(
            reverse("accounting:refound_account"), {"user": self.skia.id}
        )
        self.skia = User.objects.filter(username="skia").first()
Sli's avatar
Sli committed
75
76
77
78
79
        response_get = self.client.get(reverse("accounting:refound_account"))
        self.assertFalse(response_get.status_code == 403)
        self.assertTrue('<form action="" method="post">' in str(response_get.content))
        self.assertFalse(response_post.status_code == 403)
        self.assertTrue(self.skia.customer.amount == 0)
Krophil's avatar
Krophil committed
80

Krophil's avatar
Krophil committed
81

Krophil's avatar
Krophil committed
82
83
84
class JournalTest(TestCase):
    def setUp(self):
        call_command("populate")
Krophil's avatar
Krophil committed
85
        self.journal = GeneralJournal.objects.filter(id=1).first()
Krophil's avatar
Krophil committed
86
87

    def test_permission_granted(self):
Sli's avatar
Sli committed
88
89
90
91
        self.client.login(username="comptable", password="plop")
        response_get = self.client.get(
            reverse("accounting:journal_details", args=[self.journal.id])
        )
Krophil's avatar
Krophil committed
92
93

        self.assertTrue(response_get.status_code == 200)
Sli's avatar
Sli committed
94
95
96
        self.assertTrue(
            "<td>M\\xc3\\xa9thode de paiement</td>" in str(response_get.content)
        )
Krophil's avatar
Krophil committed
97
98

    def test_permission_not_granted(self):
Sli's avatar
Sli committed
99
100
101
102
        self.client.login(username="skia", password="plop")
        response_get = self.client.get(
            reverse("accounting:journal_details", args=[self.journal.id])
        )
Krophil's avatar
Krophil committed
103
104

        self.assertTrue(response_get.status_code == 403)
Sli's avatar
Sli committed
105
106
107
        self.assertFalse(
            "<td>M\xc3\xa9thode de paiement</td>" in str(response_get.content)
        )
Krophil's avatar
Krophil committed
108

Krophil's avatar
Krophil committed
109

Krophil's avatar
Krophil committed
110
111
112
class OperationTest(TestCase):
    def setUp(self):
        call_command("populate")
Krophil's avatar
Krophil committed
113
        self.journal = GeneralJournal.objects.filter(id=1).first()
Sli's avatar
Sli committed
114
115
116
117
        self.skia = User.objects.filter(username="skia").first()
        at = AccountingType(
            code="443", label="Ce code n'existe pas", movement_type="CREDIT"
        )
Krophil's avatar
Krophil committed
118
        at.save()
Sli's avatar
Sli committed
119
        l = Label(club_account=self.journal.club_account, name="bob")
Krophil's avatar
Krophil committed
120
        l.save()
Sli's avatar
Sli committed
121
122
123
124
125
126
127
128
129
130
131
132
133
        self.client.login(username="comptable", password="plop")
        self.op1 = Operation(
            journal=self.journal,
            date=date.today(),
            amount=1,
            remark="Test bilan",
            mode="CASH",
            done=True,
            label=l,
            accounting_type=at,
            target_type="USER",
            target_id=self.skia.id,
        )
Krophil's avatar
Krophil committed
134
        self.op1.save()
Sli's avatar
Sli committed
135
136
137
138
139
140
141
142
143
144
145
146
        self.op2 = Operation(
            journal=self.journal,
            date=date.today(),
            amount=2,
            remark="Test bilan",
            mode="CASH",
            done=True,
            label=l,
            accounting_type=at,
            target_type="USER",
            target_id=self.skia.id,
        )
Krophil's avatar
Krophil committed
147
        self.op2.save()
Krophil's avatar
Krophil committed
148
149

    def test_new_operation(self):
Sli's avatar
Sli committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
        self.client.login(username="comptable", password="plop")
        at = AccountingType.objects.filter(code="604").first()
        response = self.client.post(
            reverse("accounting:op_new", args=[self.journal.id]),
            {
                "amount": 30,
                "remark": "Un gros test",
                "journal": self.journal.id,
                "target_type": "OTHER",
                "target_id": "",
                "target_label": "Le fantome de la nuit",
                "date": "04/12/2020",
                "mode": "CASH",
                "cheque_number": "",
                "invoice": "",
                "simpleaccounting_type": "",
                "accounting_type": at.id,
                "label": "",
                "done": False,
            },
        )
Krophil's avatar
Krophil committed
171
        self.assertFalse(response.status_code == 403)
Sli's avatar
Sli committed
172
173
174
175
176
177
178
179
180
        self.assertTrue(
            self.journal.operations.filter(
                target_label="Le fantome de la nuit"
            ).exists()
        )
        response_get = self.client.get(
            reverse("accounting:journal_details", args=[self.journal.id])
        )
        self.assertTrue("<td>Le fantome de la nuit</td>" in str(response_get.content))
Krophil's avatar
Krophil committed
181
182

    def test_bad_new_operation(self):
Sli's avatar
Sli committed
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
        self.client.login(username="comptable", password="plop")
        AccountingType.objects.filter(code="604").first()
        response = self.client.post(
            reverse("accounting:op_new", args=[self.journal.id]),
            {
                "amount": 30,
                "remark": "Un gros test",
                "journal": self.journal.id,
                "target_type": "OTHER",
                "target_id": "",
                "target_label": "Le fantome de la nuit",
                "date": "04/12/2020",
                "mode": "CASH",
                "cheque_number": "",
                "invoice": "",
                "simpleaccounting_type": "",
                "accounting_type": "",
                "label": "",
                "done": False,
            },
        )
        self.assertTrue(
            "Vous devez fournir soit un type comptable simplifi\\xc3\\xa9 ou un type comptable standard"
            in str(response.content)
        )
Krophil's avatar
Krophil committed
208
209

    def test_new_operation_not_authorized(self):
Sli's avatar
Sli committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        self.client.login(username="skia", password="plop")
        at = AccountingType.objects.filter(code="604").first()
        response = self.client.post(
            reverse("accounting:op_new", args=[self.journal.id]),
            {
                "amount": 30,
                "remark": "Un gros test",
                "journal": self.journal.id,
                "target_type": "OTHER",
                "target_id": "",
                "target_label": "Le fantome du jour",
                "date": "04/12/2020",
                "mode": "CASH",
                "cheque_number": "",
                "invoice": "",
                "simpleaccounting_type": "",
                "accounting_type": at.id,
                "label": "",
                "done": False,
            },
        )
Krophil's avatar
Krophil committed
231
        self.assertTrue(response.status_code == 403)
Sli's avatar
Sli committed
232
233
234
        self.assertFalse(
            self.journal.operations.filter(target_label="Le fantome du jour").exists()
        )
Krophil's avatar
Krophil committed
235
236

    def test__operation_simple_accounting(self):
Sli's avatar
Sli committed
237
        self.client.login(username="comptable", password="plop")
Krophil's avatar
Krophil committed
238
        sat = SimplifiedAccountingType.objects.all().first()
Sli's avatar
Sli committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
        response = self.client.post(
            reverse("accounting:op_new", args=[self.journal.id]),
            {
                "amount": 23,
                "remark": "Un gros test",
                "journal": self.journal.id,
                "target_type": "OTHER",
                "target_id": "",
                "target_label": "Le fantome de l'aurore",
                "date": "04/12/2020",
                "mode": "CASH",
                "cheque_number": "",
                "invoice": "",
                "simpleaccounting_type": sat.id,
                "accounting_type": "",
                "label": "",
                "done": False,
            },
        )
Krophil's avatar
Krophil committed
258
259
        self.assertFalse(response.status_code == 403)
        self.assertTrue(self.journal.operations.filter(amount=23).exists())
Sli's avatar
Sli committed
260
261
262
263
264
265
266
267
268
269
270
271
        response_get = self.client.get(
            reverse("accounting:journal_details", args=[self.journal.id])
        )
        self.assertTrue(
            "<td>Le fantome de l&#39;aurore</td>" in str(response_get.content)
        )
        self.assertTrue(
            self.journal.operations.filter(amount=23)
            .values("accounting_type")
            .first()["accounting_type"]
            == AccountingType.objects.filter(code=6).values("id").first()["id"]
        )
Krophil's avatar
Krophil committed
272
273

    def test_nature_statement(self):
Sli's avatar
Sli committed
274
        self.client.login(username="comptable", password="plop")
Sli's avatar
Sli committed
275
        response = self.client.get(
Sli's avatar
Sli committed
276
277
            reverse("accounting:journal_nature_statement", args=[self.journal.id])
        )
Sli's avatar
Sli committed
278
        self.assertContains(response, "bob (Troll Penché) : 3.00", status_code=200)
Krophil's avatar
Krophil committed
279
280

    def test_person_statement(self):
Sli's avatar
Sli committed
281
        self.client.login(username="comptable", password="plop")
Sli's avatar
Sli committed
282
        response = self.client.get(
Sli's avatar
Sli committed
283
284
            reverse("accounting:journal_person_statement", args=[self.journal.id])
        )
Sli's avatar
Sli committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
        self.assertContains(response, "Total : 5575.72", status_code=200)
        self.assertContains(response, "Total : 71.42")
        self.assertContains(
            response,
            """
                <td><a href="/user/1/">S&#39; Kia</a></td>
                
                <td>3.00</td>""",
        )
        self.assertContains(
            response,
            """
                <td><a href="/user/1/">S&#39; Kia</a></td>
                
                <td>823.00</td>""",
Sli's avatar
Sli committed
300
        )
Krophil's avatar
Krophil committed
301
302

    def test_accounting_statement(self):
Sli's avatar
Sli committed
303
        self.client.login(username="comptable", password="plop")
Sli's avatar
Sli committed
304
        response = self.client.get(
Sli's avatar
Sli committed
305
306
            reverse("accounting:journal_accounting_statement", args=[self.journal.id])
        )
Sli's avatar
Sli committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
        self.assertContains(
            response,
            """
            <tr>
                <td>443 - Crédit - Ce code n&#39;existe pas</td>
                <td>3.00</td>
            </tr>""",
            status_code=200,
        )
        self.assertContains(
            response,
            """
    <p><strong>Montant : </strong>-5504.30 €</p>
    <p><strong>Montant effectif: </strong>-5504.30 €</p>""",
Sli's avatar
Sli committed
321
        )