This commit is contained in:
Ludovic Stephan 2021-02-28 02:05:41 +01:00
parent 964eec6ab1
commit 4205e0ad0e

View file

@ -15,7 +15,6 @@ from ..auth.utils import hash_password
from ..config import kfet_config
from ..models import (
Account,
AccountNegative,
Article,
ArticleCategory,
Checkout,
@ -1856,7 +1855,10 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"]["operation_group"], ["on_acc"])
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_on_acc", "invalid_formset"],
)
def test_group_on_acc_expects_comment(self):
user_add_perms(self.users["team"], ["kfet.perform_commented_operations"])
@ -1899,7 +1901,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"]["need_comment"], True)
self.assertEqual(json_data["need_comment"], True)
def test_invalid_group_on_acc_needs_comment_requires_perm(self):
self.account.trigramme = "#13"
@ -1922,8 +1924,8 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["missing_perms"],
["[kfet] Enregistrer des commandes avec commentaires"],
json_data["missing_perms"],
["Enregistrer des commandes avec commentaires"],
)
def test_error_on_acc_frozen(self):
@ -1945,7 +1947,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"]["frozen"], [self.account.trigramme])
self.assertEqual([e["code"] for e in json_data["errors"]], ["frozen_acc"])
def test_invalid_group_checkout(self):
self.checkout.valid_from -= timedelta(days=300)
@ -1957,7 +1959,10 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"]["operation_group"], ["checkout"])
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_checkout", "invalid_formset"],
)
def test_invalid_group_expects_one_operation(self):
data = dict(self.base_post_data)
@ -1965,7 +1970,10 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"]["operations"], [])
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_purchase_with_user_is_nof_cof(self):
self.account.cofprofile.is_cof = False
@ -2023,12 +2031,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
# Check response content
self.assertDictEqual(
json_data,
{
"operationgroup": operation_group.pk,
"operations": [operation.pk],
"warnings": {},
"errors": {},
},
{"errors": []},
)
# Check object updates
@ -2179,9 +2182,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"],
[{"__all__": ["Un achat nécessite un article et une quantité"]}],
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_purchase_expects_article_nb(self):
@ -2199,9 +2202,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"],
[{"__all__": ["Un achat nécessite un article et une quantité"]}],
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_purchase_expects_article_nb_greater_than_1(self):
@ -2219,16 +2222,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"],
[
{
"__all__": ["Un achat nécessite un article et une quantité"],
"article_nb": [
"Assurez-vous que cette valeur est supérieure ou " "égale à 1."
],
}
],
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_operation_not_purchase_with_cash(self):
@ -2247,7 +2243,10 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"]["account"], "LIQ")
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_liq"],
)
def test_deposit(self):
user_add_perms(self.users["team"], ["kfet.perform_deposit"])
@ -2300,12 +2299,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertDictEqual(
json_data,
{
"operationgroup": operation_group.pk,
"operations": [operation.pk],
"warnings": {},
"errors": {},
},
{"errors": []},
)
self.account.refresh_from_db()
@ -2364,8 +2358,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"], [{"__all__": ["Bad request"]}]
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_deposit_too_many_params(self):
@ -2383,8 +2378,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"], [{"__all__": ["Bad request"]}]
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_deposit_expects_positive_amount(self):
@ -2402,8 +2398,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"], [{"__all__": ["Charge non positive"]}]
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_deposit_requires_perm(self):
@ -2421,9 +2418,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["missing_perms"], ["[kfet] Effectuer une charge"]
)
self.assertEqual(json_data["missing_perms"], ["Effectuer une charge"])
def test_withdraw(self):
data = dict(
@ -2475,12 +2470,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertDictEqual(
json_data,
{
"operationgroup": operation_group.pk,
"operations": [operation.pk],
"warnings": {},
"errors": {},
},
{"errors": []},
)
self.account.refresh_from_db()
@ -2539,8 +2529,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"], [{"__all__": ["Bad request"]}]
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_withdraw_too_many_params(self):
@ -2558,8 +2549,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"], [{"__all__": ["Bad request"]}]
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_invalid_withdraw_expects_negative_amount(self):
@ -2577,8 +2569,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["operations"], [{"__all__": ["Retrait non négatif"]}]
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_formset"],
)
def test_edit(self):
@ -2634,12 +2627,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertDictEqual(
json_data,
{
"operationgroup": operation_group.pk,
"operations": [operation.pk],
"warnings": {},
"errors": {},
},
{"errors": []},
)
self.account.refresh_from_db()
@ -2700,8 +2688,8 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"]["missing_perms"],
["[kfet] Modifier la balance d'un compte"],
json_data["missing_perms"],
["Modifier la balance d'un compte"],
)
def test_invalid_edit_expects_comment(self):
@ -2721,7 +2709,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"]["need_comment"], True)
self.assertEqual(json_data["need_comment"], True)
def _setup_addcost(self):
self.register_user("addcost", create_user("addcost", "ADD"))
@ -3008,62 +2996,10 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"],
{"missing_perms": ["[kfet] Enregistrer des commandes en négatif"]},
json_data["missing_perms"],
["Enregistrer des commandes en négatif"],
)
def test_invalid_negative_exceeds_allowed_duration_from_config(self):
user_add_perms(self.users["team"], ["kfet.perform_negative_operations"])
kfet_config.set(overdraft_duration=timedelta(days=5))
self.account.balance = Decimal("1.00")
self.account.save()
self.account.negative = AccountNegative.objects.create(
account=self.account, start=timezone.now() - timedelta(days=5, minutes=1)
)
data = dict(
self.base_post_data,
**{
"form-TOTAL_FORMS": "1",
"form-0-type": "purchase",
"form-0-amount": "",
"form-0-article": str(self.article.pk),
"form-0-article_nb": "2",
}
)
resp = self.client.post(self.url, data)
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"], {"negative": ["000"]})
def test_invalid_negative_exceeds_allowed_duration_from_account(self):
user_add_perms(self.users["team"], ["kfet.perform_negative_operations"])
kfet_config.set(overdraft_duration=timedelta(days=5))
self.account.balance = Decimal("1.00")
self.account.save()
self.account.negative = AccountNegative.objects.create(
account=self.account,
start=timezone.now() - timedelta(days=3),
authz_overdraft_until=timezone.now() - timedelta(seconds=1),
)
data = dict(
self.base_post_data,
**{
"form-TOTAL_FORMS": "1",
"form-0-type": "purchase",
"form-0-amount": "",
"form-0-article": str(self.article.pk),
"form-0-article_nb": "2",
}
)
resp = self.client.post(self.url, data)
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"], {"negative": ["000"]})
def test_invalid_negative_exceeds_amount_allowed_from_config(self):
user_add_perms(self.users["team"], ["kfet.perform_negative_operations"])
kfet_config.set(overdraft_amount=Decimal("-1.00"))
@ -3083,38 +3019,13 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
)
resp = self.client.post(self.url, data)
self.assertEqual(resp.status_code, 403)
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"], {"negative": ["000"]})
def test_invalid_negative_exceeds_amount_allowed_from_account(self):
user_add_perms(self.users["team"], ["kfet.perform_negative_operations"])
kfet_config.set(overdraft_amount=Decimal("10.00"))
self.account.balance = Decimal("1.00")
self.account.save()
self.account.update_negative()
self.account.negative = AccountNegative.objects.create(
account=self.account,
start=timezone.now() - timedelta(days=3),
authz_overdraft_amount=Decimal("1.00"),
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["negative"],
)
data = dict(
self.base_post_data,
**{
"form-TOTAL_FORMS": "1",
"form-0-type": "purchase",
"form-0-amount": "",
"form-0-article": str(self.article.pk),
"form-0-article_nb": "2",
}
)
resp = self.client.post(self.url, data)
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"], {"negative": ["000"]})
def test_multi_0(self):
article2 = Article.objects.create(
name="Article 2",
@ -3198,12 +3109,7 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
# Check response content
self.assertDictEqual(
json_data,
{
"operationgroup": operation_group.pk,
"operations": [operation_list[0].pk, operation_list[1].pk],
"warnings": {},
"errors": {},
},
{"errors": []},
)
# Check object updates
@ -3342,7 +3248,10 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"], {})
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["invalid_request"],
)
def test_invalid_operation_not_exist(self):
data = {"operations[]": ["1000"]}
@ -3350,7 +3259,10 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"], {"opes_notexisting": [1000]})
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["cancel_missing"],
)
@mock.patch("django.utils.timezone.now")
def test_purchase(self, now_mock):
@ -3414,7 +3326,7 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
"canceled_by__trigramme": None,
}
],
"errors": {},
"errors": [],
"warnings": {},
"opegroups_to_update": [
{
@ -3602,7 +3514,7 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
"canceled_by__trigramme": None,
}
],
"errors": {},
"errors": [],
"warnings": {},
"opegroups_to_update": [
{
@ -3689,7 +3601,7 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
"canceled_by__trigramme": None,
}
],
"errors": {},
"errors": [],
"warnings": {},
"opegroups_to_update": [
{
@ -3776,7 +3688,7 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
"canceled_by__trigramme": None,
}
],
"errors": {},
"errors": [],
"warnings": {},
"opegroups_to_update": [
{
@ -3839,8 +3751,8 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"],
{"missing_perms": ["[kfet] Annuler des commandes non récentes"]},
json_data["missing_perms"],
["Annuler des commandes non récentes"],
)
def test_already_canceled(self):
@ -3964,9 +3876,12 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
data = {"operations[]": [str(operation.pk)]}
resp = self.client.post(self.url, data)
self.assertEqual(resp.status_code, 403)
self.assertEqual(resp.status_code, 400)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(json_data["errors"], {"negative": [self.account.trigramme]})
self.assertCountEqual(
[e["code"] for e in json_data["errors"]],
["negative"],
)
def test_invalid_negative_requires_perms(self):
kfet_config.set(overdraft_amount=Decimal("40.00"))
@ -3985,8 +3900,8 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(resp.status_code, 403)
json_data = json.loads(resp.content.decode("utf-8"))
self.assertEqual(
json_data["errors"],
{"missing_perms": ["[kfet] Enregistrer des commandes en négatif"]},
json_data["missing_perms"],
["Enregistrer des commandes en négatif"],
)
def test_partial_0(self):
@ -4036,7 +3951,7 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
"canceled_by__trigramme": None,
},
],
"errors": {},
"errors": [],
"warnings": {"already_canceled": [operation3.pk]},
"opegroups_to_update": [
{