Sith/accounting/tests.py

293 lines
10 KiB
Python
Raw Normal View History

#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
2024-09-23 08:25:27 +00:00
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
2024-06-24 11:07:36 +00:00
from datetime import date, timedelta
2017-06-12 06:49:03 +00:00
from django.test import TestCase
from django.urls import reverse
2016-01-28 15:53:37 +00:00
2018-10-04 19:29:19 +00:00
from accounting.models import (
2024-06-24 11:07:36 +00:00
AccountingType,
2018-10-04 19:29:19 +00:00
GeneralJournal,
Label,
2024-06-24 11:07:36 +00:00
Operation,
2018-10-04 19:29:19 +00:00
SimplifiedAccountingType,
)
2024-06-24 11:07:36 +00:00
from core.models import User
2016-11-30 01:41:25 +00:00
2024-07-23 22:39:26 +00:00
class TestRefoundAccount(TestCase):
@classmethod
def setUpTestData(cls):
cls.skia = User.objects.get(username="skia")
2016-11-30 01:41:25 +00:00
# reffil skia's account
cls.skia.customer.amount = 800
cls.skia.customer.save()
cls.refound_account_url = reverse("accounting:refound_account")
2016-11-30 01:41:25 +00:00
def test_permission_denied(self):
self.client.force_login(User.objects.get(username="guy"))
2018-10-04 19:29:19 +00:00
response_post = self.client.post(
self.refound_account_url, {"user": self.skia.id}
2018-10-04 19:29:19 +00:00
)
response_get = self.client.get(self.refound_account_url)
assert response_get.status_code == 403
assert response_post.status_code == 403
2016-11-30 01:41:25 +00:00
def test_root_granteed(self):
self.client.force_login(User.objects.get(username="root"))
response = self.client.post(self.refound_account_url, {"user": self.skia.id})
self.assertRedirects(response, self.refound_account_url)
self.skia.refresh_from_db()
response = self.client.get(self.refound_account_url)
assert response.status_code == 200
assert '<form action="" method="post">' in str(response.content)
assert self.skia.customer.amount == 0
2016-11-30 01:41:25 +00:00
def test_comptable_granteed(self):
self.client.force_login(User.objects.get(username="comptable"))
response = self.client.post(self.refound_account_url, {"user": self.skia.id})
self.assertRedirects(response, self.refound_account_url)
self.skia.refresh_from_db()
response = self.client.get(self.refound_account_url)
assert response.status_code == 200
assert '<form action="" method="post">' in str(response.content)
assert self.skia.customer.amount == 0
2016-12-22 00:02:32 +00:00
2017-06-12 06:49:03 +00:00
2024-07-23 22:39:26 +00:00
class TestJournal(TestCase):
@classmethod
def setUpTestData(cls):
cls.journal = GeneralJournal.objects.get(id=1)
2016-12-22 00:02:32 +00:00
def test_permission_granted(self):
self.client.force_login(User.objects.get(username="comptable"))
2018-10-04 19:29:19 +00:00
response_get = self.client.get(
reverse("accounting:journal_details", args=[self.journal.id])
)
2016-12-22 00:02:32 +00:00
assert response_get.status_code == 200
assert "<td>M\\xc3\\xa9thode de paiement</td>" in str(response_get.content)
2016-12-22 00:02:32 +00:00
def test_permission_not_granted(self):
self.client.force_login(User.objects.get(username="skia"))
2018-10-04 19:29:19 +00:00
response_get = self.client.get(
reverse("accounting:journal_details", args=[self.journal.id])
)
2016-12-22 00:02:32 +00:00
assert response_get.status_code == 403
assert "<td>M\xc3\xa9thode de paiement</td>" not in str(response_get.content)
2016-12-22 00:02:32 +00:00
2017-06-12 06:49:03 +00:00
2024-07-23 22:39:26 +00:00
class TestOperation(TestCase):
2016-12-22 00:02:32 +00:00
def setUp(self):
self.tomorrow_formatted = (date.today() + timedelta(days=1)).strftime(
"%d/%m/%Y"
)
2017-06-12 06:49:03 +00:00
self.journal = GeneralJournal.objects.filter(id=1).first()
2018-10-04 19:29:19 +00:00
self.skia = User.objects.filter(username="skia").first()
at = AccountingType(
code="443", label="Ce code n'existe pas", movement_type="CREDIT"
)
2016-12-25 11:09:32 +00:00
at.save()
label = Label.objects.create(club_account=self.journal.club_account, name="bob")
self.client.force_login(User.objects.get(username="comptable"))
2018-10-04 19:29:19 +00:00
self.op1 = Operation(
journal=self.journal,
date=date.today(),
amount=1,
remark="Test bilan",
mode="CASH",
done=True,
label=label,
2018-10-04 19:29:19 +00:00
accounting_type=at,
target_type="USER",
target_id=self.skia.id,
)
2016-12-25 11:09:32 +00:00
self.op1.save()
2018-10-04 19:29:19 +00:00
self.op2 = Operation(
journal=self.journal,
date=date.today(),
amount=2,
remark="Test bilan",
mode="CASH",
done=True,
label=label,
2018-10-04 19:29:19 +00:00
accounting_type=at,
target_type="USER",
target_id=self.skia.id,
)
2016-12-25 11:09:32 +00:00
self.op2.save()
2016-12-22 00:02:32 +00:00
def test_new_operation(self):
at = AccountingType.objects.get(code="604")
2018-10-04 19:29:19 +00:00
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": self.tomorrow_formatted,
2018-10-04 19:29:19 +00:00
"mode": "CASH",
"cheque_number": "",
"invoice": "",
"simpleaccounting_type": "",
"accounting_type": at.id,
"label": "",
"done": False,
},
)
2016-12-22 00:02:32 +00:00
self.assertFalse(response.status_code == 403)
2018-10-04 19:29:19 +00:00
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))
2016-12-24 15:15:22 +00:00
def test_bad_new_operation(self):
AccountingType.objects.get(code="604")
2018-10-04 19:29:19 +00:00
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": self.tomorrow_formatted,
2018-10-04 19:29:19 +00:00
"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)
)
2016-12-22 00:02:32 +00:00
def test_new_operation_not_authorized(self):
self.client.force_login(self.skia)
2018-10-04 19:29:19 +00:00
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": self.tomorrow_formatted,
2018-10-04 19:29:19 +00:00
"mode": "CASH",
"cheque_number": "",
"invoice": "",
"simpleaccounting_type": "",
"accounting_type": at.id,
"label": "",
"done": False,
},
)
2016-12-24 15:15:22 +00:00
self.assertTrue(response.status_code == 403)
2018-10-04 19:29:19 +00:00
self.assertFalse(
self.journal.operations.filter(target_label="Le fantome du jour").exists()
)
2016-12-24 15:15:22 +00:00
def test_operation_simple_accounting(self):
2016-12-24 15:15:22 +00:00
sat = SimplifiedAccountingType.objects.all().first()
2018-10-04 19:29:19 +00:00
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": self.tomorrow_formatted,
2018-10-04 19:29:19 +00:00
"mode": "CASH",
"cheque_number": "",
"invoice": "",
"simpleaccounting_type": sat.id,
"accounting_type": "",
"label": "",
"done": False,
},
)
assert response.status_code != 403
assert self.journal.operations.filter(amount=23).exists()
2018-10-04 19:29:19 +00:00
response_get = self.client.get(
reverse("accounting:journal_details", args=[self.journal.id])
)
assert "<td>Le fantome de l&#39;aurore</td>" in str(response_get.content)
assert (
2018-10-04 19:29:19 +00:00
self.journal.operations.filter(amount=23)
.values("accounting_type")
.first()["accounting_type"]
== AccountingType.objects.filter(code=6).values("id").first()["id"]
)
2016-12-25 11:09:32 +00:00
def test_nature_statement(self):
2019-10-06 15:44:30 +00:00
response = self.client.get(
2018-10-04 19:29:19 +00:00
reverse("accounting:journal_nature_statement", args=[self.journal.id])
)
2019-10-06 15:44:30 +00:00
self.assertContains(response, "bob (Troll Penché) : 3.00", status_code=200)
2016-12-25 11:09:32 +00:00
def test_person_statement(self):
2019-10-06 15:44:30 +00:00
response = self.client.get(
2018-10-04 19:29:19 +00:00
reverse("accounting:journal_person_statement", args=[self.journal.id])
)
2019-10-06 15:44:30 +00:00
self.assertContains(response, "Total : 5575.72", status_code=200)
self.assertContains(response, "Total : 71.42")
content = response.content.decode()
self.assertInHTML(
"""<td><a href="/user/1/">S&#39; Kia</a></td><td>3.00</td>""", content
2019-10-06 15:44:30 +00:00
)
self.assertInHTML(
"""<td><a href="/user/1/">S&#39; Kia</a></td><td>823.00</td>""", content
2018-10-04 19:29:19 +00:00
)
2016-12-25 11:09:32 +00:00
def test_accounting_statement(self):
2019-10-06 15:44:30 +00:00
response = self.client.get(
2018-10-04 19:29:19 +00:00
reverse("accounting:journal_accounting_statement", args=[self.journal.id])
)
assert response.status_code == 200
self.assertInHTML(
2019-10-06 15:44:30 +00:00
"""
<tr>
<td>443 - Crédit - Ce code n&#39;existe pas</td>
<td>3.00</td>
</tr>""",
response.content.decode(),
2019-10-06 15:44:30 +00:00
)
self.assertContains(
response,
"""
<p><strong>Montant : </strong>-5504.30 </p>
<p><strong>Montant effectif: </strong>-5504.30 </p>""",
2018-10-04 19:29:19 +00:00
)