From 93d3c124fdc98c79a42dcab58267ba47cca0271b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 1 Oct 2018 16:45:48 +0200 Subject: [PATCH 1/4] kfet -- Add fixme related to available checkouts in K-Psul --- kfet/forms.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kfet/forms.py b/kfet/forms.py index 522f20de..5fcd30a6 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -273,6 +273,10 @@ class ArticleRestrictForm(ArticleForm): # ----- class KPsulOperationGroupForm(forms.ModelForm): + # FIXME(AD): Use timezone.now instead of timezone.now() to avoid using a + # fixed datetime (application boot here). + # One may even use: Checkout.objects.is_valid() if changing + # to now = timezone.now is ok in 'is_valid' definition. checkout = forms.ModelChoiceField( queryset = Checkout.objects.filter( is_protected=False, valid_from__lte=timezone.now(), From 0f688a8f1c6aaefee0c6a29c0be2dff0bb255e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 1 Oct 2018 23:03:45 +0200 Subject: [PATCH 2/4] kfet -- Stack errors of KPsulOperationForm Delete an error never raised, and avoid duplicate messages. --- kfet/forms.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/kfet/forms.py b/kfet/forms.py index 5fcd30a6..9d0fadd8 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -3,6 +3,7 @@ from decimal import Decimal from django import forms from django.core.exceptions import ValidationError +from django.core import validators from django.contrib.auth.models import User from django.forms import modelformset_factory from django.utils import timezone @@ -321,13 +322,18 @@ class KPsulOperationForm(forms.ModelForm): queryset=Article.objects.select_related('category').all(), required=False, widget = forms.HiddenInput()) + article_nb = forms.IntegerField( + required=False, + initial=None, + validators=[validators.MinValueValidator(1)], + widget=forms.HiddenInput(), + ) class Meta: model = Operation fields = ['type', 'amount', 'article', 'article_nb'] widgets = { 'type': forms.HiddenInput(), 'amount': forms.HiddenInput(), - 'article_nb': forms.HiddenInput(), } def clean(self): @@ -336,22 +342,26 @@ class KPsulOperationForm(forms.ModelForm): amount = self.cleaned_data.get('amount') article = self.cleaned_data.get('article') article_nb = self.cleaned_data.get('article_nb') + errors = [] if type_ope and type_ope == Operation.PURCHASE: - if not article or not article_nb: - raise ValidationError( - "Un achat nécessite un article et une quantité") - if article_nb < 1: - raise ValidationError("Impossible d'acheter moins de 1 article") + if not article or article_nb is None or article_nb < 1: + errors.append(ValidationError( + "Un achat nécessite un article et une quantité")) elif type_ope and type_ope in [Operation.DEPOSIT, Operation.WITHDRAW]: if not amount or article or article_nb: - raise ValidationError("Bad request") - if type_ope == Operation.DEPOSIT and amount <= 0: - raise ValidationError("Charge non positive") - if type_ope == Operation.WITHDRAW and amount >= 0: - raise ValidationError("Retrait non négatif") + errors.append(ValidationError("Bad request")) + else: + if type_ope == Operation.DEPOSIT and amount <= 0: + errors.append(ValidationError("Charge non positive")) + elif type_ope == Operation.WITHDRAW and amount >= 0: + errors.append(ValidationError("Retrait non négatif")) self.cleaned_data['article'] = None self.cleaned_data['article_nb'] = None + if errors: + raise ValidationError(errors) + + KPsulOperationFormSet = modelformset_factory( Operation, form = KPsulOperationForm, From 22011faba965a98768874e200d8b6a088f5084b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Tue, 2 Oct 2018 12:02:24 +0200 Subject: [PATCH 3/4] kfet -- Init KFetConfig, even without request, for easy testing --- kfet/config.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/kfet/config.py b/kfet/config.py index f248b370..452c2a09 100644 --- a/kfet/config.py +++ b/kfet/config.py @@ -1,25 +1,39 @@ from django.core.exceptions import ValidationError from django.db import models -from djconfig import config +import djconfig class KFetConfig(object): """kfet app configuration. Enhance isolation with backend used to store config. - Usable after DjConfig middleware was called. """ prefix = 'kfet_' + def __init__(self): + # Set this to False again to reload the config, e.g for testing + # purposes. + self._conf_init = False + + def _check_init(self): + # For initialization purposes, we call 'reload_maybe' directly + # (normaly done once per request in middleware). + # Note it should be called only once across requests, if you use + # kfet_config instance below. + if not self._conf_init: + djconfig.reload_maybe() + self._conf_init = True + def __getattr__(self, key): + self._check_init() if key == 'subvention_cof': # Allows accessing to the reduction as a subvention # Other reason: backward compatibility reduction_mult = 1 - self.reduction_cof/100 return (1/reduction_mult - 1) * 100 - return getattr(config, self._get_dj_key(key)) + return getattr(djconfig.config, self._get_dj_key(key)) def list(self): """Get list of kfet app configuration. @@ -30,7 +44,8 @@ class KFetConfig(object): """ # prevent circular imports from kfet.forms import KFetConfigForm - return [(field.label, getattr(config, name), ) + self._check_init() + return [(field.label, getattr(djconfig.config, name), ) for name, field in KFetConfigForm.base_fields.items()] def _get_dj_key(self, key): @@ -47,6 +62,7 @@ class KFetConfig(object): # prevent circular imports from kfet.forms import KFetConfigForm + self._check_init() # get old config new_cfg = KFetConfigForm().initial # update to new config From 507a59c9141c57fa0ab50f8050b6f1bc168c09c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Wed, 3 Oct 2018 18:01:19 +0200 Subject: [PATCH 4/4] kfet.tests -- Add tests for perform_operations view --- kfet/tests/test_views.py | 1434 +++++++++++++++++++++++++++++++++++++- kfet/tests/testcases.py | 11 +- 2 files changed, 1437 insertions(+), 8 deletions(-) diff --git a/kfet/tests/test_views.py b/kfet/tests/test_views.py index 40f895a1..28599937 100644 --- a/kfet/tests/test_views.py +++ b/kfet/tests/test_views.py @@ -10,12 +10,12 @@ from django.utils import timezone from ..config import kfet_config from ..models import ( - Account, Article, ArticleCategory, Checkout, CheckoutStatement, Inventory, - InventoryArticle, Operation, OperationGroup, Order, OrderArticle, Supplier, - SupplierArticle, Transfer, TransferGroup, + Account, AccountNegative, Article, ArticleCategory, Checkout, + CheckoutStatement, Inventory, InventoryArticle, Operation, OperationGroup, + Order, OrderArticle, Supplier, SupplierArticle, Transfer, TransferGroup, ) from .testcases import ViewTestCaseMixin -from .utils import create_team, create_user, get_perms +from .utils import create_team, create_user, get_perms, user_add_perms class AccountListViewTests(ViewTestCaseMixin, TestCase): @@ -1452,6 +1452,42 @@ class KPsulCheckoutDataViewTests(ViewTestCaseMixin, TestCase): class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase): + """ + Test cases for kpsul_perform_operations view. + + Below is the test ordering, try to keep this organized ;-) + + * OperationGroup: + - test_group... + - test_invalid_group... + * Operation: + - test_purchase... + - test_invalid_purchase... + - test_deposit... + - test_invalid_deposit... + - test_withdraw... + - test_invalid_withdraw... + - test_edit... + - test_invalid_edit... + * Addcost: + - test_addcost... + * Negative: + - test_negative... + - test_invalid_negative... + * More concrete examples: + - test_multi... + + To test valid requests, one should use '_assertResponseOk(response)' to get + hints about failure reasons, if any. + + At least one test per operation type should test the complete response and + behavior (HTTP, WebSocket, object updates, and object creations) + Other tests of the same operation type can only assert the specific + behavior differences. + + For invalid requests, response errors should be tested. + + """ url_name = 'kfet.kpsul.perform_operations' url_expected = '/k-fet/k-psul/perform_operations' @@ -1460,8 +1496,1394 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team' auth_forbidden = [None, 'user'] - def test_ok(self): - pass + with_liq = True + + def setUp(self): + super(KPsulPerformOperationsViewTests, self).setUp() + + # A Checkout, curently usable, balance=100 + self.checkout = Checkout.objects.create( + created_by=self.accounts["team"], + name="Checkout", + valid_from=timezone.now() - timedelta(days=7), + valid_to=timezone.now() + timedelta(days=7), + balance=Decimal("100.00"), + ) + # An Article, price=2.5, stock=20 + self.article = Article.objects.create( + category=ArticleCategory.objects.create(name="Category"), + name="Article", + price=Decimal("2.5"), + stock=20, + ) + # An Account, trigramme=000, balance=50 + # Do not assume user is cof, nor not cof. + self.account = self.accounts['user'] + self.account.balance = Decimal("50.00") + self.account.save() + + # Mock consumer of K-Psul websocket to catch what we're sending + kpsul_consumer_patcher = mock.patch("kfet.consumers.KPsul") + self.kpsul_consumer_mock = kpsul_consumer_patcher.start() + self.addCleanup(kpsul_consumer_patcher.stop) + + # Reset cache of kfet config + kfet_config._conf_init = False + + def _assertResponseOk(self, response): + """ + Asserts that status code of 'response' is 200, and returns the + deserialized content of the JSONResponse. + + In case status code is not 200, it prints the content of "errors" of + the response. + + """ + json_data = ( + json.loads(getattr(response, "content", b"{}").decode("utf-8")) + ) + try: + self.assertEqual(response.status_code, 200) + except AssertionError as exc: + msg = ( + "Expected response is 200, got {}. Errors: {}" + .format(response.status_code, json_data.get("errors")) + ) + raise AssertionError(msg) from exc + return json_data + + def get_base_post_data(self): + return { + # OperationGroup form + 'on_acc': str(self.account.pk), + 'checkout': str(self.checkout.pk), + # Operation formset + 'form-TOTAL_FORMS': '0', + 'form-INITIAL_FORMS': '0', + 'form-MIN_NUM_FORMS': '1', + 'form-MAX_NUM_FORMS': '1000', + } + + base_post_data = property(get_base_post_data) + + def test_invalid_group_on_acc(self): + data = dict(self.base_post_data, **{"on_acc": "GNR"}) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual(json_data["errors"]["operation_group"], ["on_acc"]) + + def test_group_on_acc_expects_comment(self): + user_add_perms( + self.users["team"], ["kfet.perform_commented_operations"] + ) + self.account.trigramme = "#13" + self.account.save() + self.assertTrue(self.account.need_comment) + + data = dict(self.base_post_data, **{ + "comment": "A comment to explain it", + "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._assertResponseOk(resp) + + def test_invalid_group_on_acc_expects_comment(self): + user_add_perms( + self.users["team"], ["kfet.perform_commented_operations"] + ) + self.account.trigramme = "#13" + self.account.save() + self.assertTrue(self.account.need_comment) + + 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, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual(json_data["errors"]["need_comment"], True) + + def test_invalid_group_on_acc_needs_comment_requires_perm(self): + self.account.trigramme = "#13" + self.account.save() + self.assertTrue(self.account.need_comment) + + data = dict(self.base_post_data, **{ + "comment": "A comment to explain it", + "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"]["missing_perms"], + ["Enregistrer des commandes avec commentaires"], + ) + + def test_group_on_acc_frozen(self): + user_add_perms( + self.users["team"], ["kfet.override_frozen_protection"] + ) + self.account.is_frozen = True + self.account.save() + + data = dict(self.base_post_data, **{ + "comment": "A comment to explain it", + "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._assertResponseOk(resp) + + def test_invalid_group_on_acc_frozen_requires_perm(self): + self.account.is_frozen = True + self.account.save() + + data = dict(self.base_post_data, **{ + "comment": "A comment to explain it", + "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"]["missing_perms"], + ["Forcer le gel d'un compte"], + ) + + def test_invalid_group_checkout(self): + self.checkout.valid_from -= timedelta(days=300) + self.checkout.valid_to -= timedelta(days=300) + self.checkout.save() + + data = dict(self.base_post_data) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual(json_data["errors"]["operation_group"], ["checkout"]) + + def test_invalid_group_expects_one_operation(self): + data = dict(self.base_post_data) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual(json_data["errors"]["operations"], []) + + def test_purchase_with_user_is_nof_cof(self): + self.account.cofprofile.is_cof = False + self.account.cofprofile.save() + + 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) + + # Check response status + json_data = self._assertResponseOk(resp) + + # Check object creations + operation_group = OperationGroup.objects.get() + self.assertDictEqual(operation_group.__dict__, { + "_state": mock.ANY, + "at": mock.ANY, + "amount": Decimal("-5.00"), + "checkout_id": self.checkout.pk, + "comment": "", + "id": mock.ANY, + "is_cof": False, + "on_acc_id": self.account.pk, + "valid_by_id": None, + }) + operation = Operation.objects.get() + self.assertDictEqual(operation.__dict__, { + "_state": mock.ANY, + "addcost_amount": None, + "addcost_for_id": None, + "amount": Decimal("-5.00"), + "article_id": self.article.pk, + "article_nb": 2, + "canceled_at": None, + "canceled_by_id": None, + "group_id": operation_group.pk, + "id": mock.ANY, + "type": "purchase", + }) + + # Check response content + self.assertDictEqual(json_data, { + "operationgroup": operation_group.pk, + "operations": [operation.pk], + "warnings": {}, + "errors": {}, + }) + + # Check object updates + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("45.00")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("100.00")) + self.article.refresh_from_db() + self.assertEqual(self.article.stock, 18) + + # Check websocket data + self.kpsul_consumer_mock.group_send.assert_called_once_with( + "kfet.kpsul", + { + "opegroups": [ + { + "add": True, + "at": mock.ANY, + "amount": Decimal("-5.00"), + "checkout__name": "Checkout", + "comment": "", + "id": operation_group.pk, + "is_cof": False, + "on_acc__trigramme": "000", + "valid_by__trigramme": None, + "opes": [ + { + "id": operation.pk, + "addcost_amount": None, + "addcost_for__trigramme": None, + "amount": Decimal("-5.00"), + "article__name": "Article", + "article_nb": 2, + "canceled_at": None, + "canceled_by__trigramme": None, + "group_id": operation_group.pk, + "type": "purchase", + }, + ], + }, + ], + "checkouts": [ + { + "id": self.checkout.pk, + "balance": Decimal("100.00"), + }, + ], + "articles": [ + { + "id": self.article.pk, + "stock": 18, + }, + ], + }, + ) + + def test_purchase_with_user_is_cof(self): + kfet_config.set(kfet_reduction_cof=Decimal("20")) + self.account.cofprofile.is_cof = True + self.account.cofprofile.save() + + 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._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertEqual(operation_group.amount, Decimal("-4.00")) + self.assertEqual(operation_group.is_cof, True) + operation = Operation.objects.get() + self.assertEqual(operation.amount, Decimal("-4.00")) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("46.00")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("100.00")) + self.article.refresh_from_db() + self.assertEqual(self.article.stock, 18) + + def test_purchase_with_cash(self): + data = dict(self.base_post_data, **{ + "on_acc": str(self.accounts["liq"].pk), + "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._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertEqual(operation_group.on_acc, self.accounts["liq"]) + self.assertEqual(operation_group.is_cof, False) + + self.accounts["liq"].refresh_from_db() + self.assertEqual(self.accounts["liq"].balance, 0) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("105.00")) + self.article.refresh_from_db() + self.assertEqual(self.article.stock, 18) + + def test_invalid_purchase_expects_article(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "purchase", + "form-0-amount": "", + "form-0-article": "", + "form-0-article_nb": "1", + }) + resp = self.client.post(self.url, data) + + 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é"]}, + ], + ) + + def test_invalid_purchase_expects_article_nb(self): + 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": "", + }) + resp = self.client.post(self.url, data) + + 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é"]}, + ], + ) + + def test_invalid_purchase_expects_article_nb_greater_than_1( + self + ): + 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": "-1", + }) + resp = self.client.post(self.url, data) + + 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.", + ], + }, + ], + ) + + def test_invalid_operation_not_purchase_with_cash(self): + data = dict(self.base_post_data, **{ + "on_acc": str(self.accounts["liq"].pk), + "form-TOTAL_FORMS": "1", + "form-0-type": "deposit", + "form-0-amount": "10.00", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual(json_data["errors"]["account"], "LIQ") + + def test_deposit(self): + user_add_perms(self.users["team"], ["kfet.perform_deposit"]) + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "deposit", + "form-0-amount": "10.75", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + json_data = self._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertDictEqual(operation_group.__dict__, { + "_state": mock.ANY, + "at": mock.ANY, + "amount": Decimal("10.75"), + "checkout_id": self.checkout.pk, + "comment": "", + "id": mock.ANY, + "is_cof": False, + "on_acc_id": self.account.pk, + "valid_by_id": self.accounts["team"].pk, + }) + operation = Operation.objects.get() + self.assertDictEqual(operation.__dict__, { + "_state": mock.ANY, + "addcost_amount": None, + "addcost_for_id": None, + "amount": Decimal("10.75"), + "article_id": None, + "article_nb": None, + "canceled_at": None, + "canceled_by_id": None, + "group_id": operation_group.pk, + "id": mock.ANY, + "type": "deposit", + }) + + self.assertDictEqual(json_data, { + "operationgroup": operation_group.pk, + "operations": [operation.pk], + "warnings": {}, + "errors": {}, + }) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("60.75")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("110.75")) + + self.kpsul_consumer_mock.group_send.assert_called_once_with( + "kfet.kpsul", + { + "opegroups": [ + { + "add": True, + "at": mock.ANY, + "amount": Decimal("10.75"), + "checkout__name": "Checkout", + "comment": "", + "id": operation_group.pk, + "is_cof": False, + "on_acc__trigramme": "000", + "valid_by__trigramme": "100", + "opes": [ + { + "id": operation.pk, + "addcost_amount": None, + "addcost_for__trigramme": None, + "amount": Decimal("10.75"), + "article__name": None, + "article_nb": None, + "canceled_at": None, + "canceled_by__trigramme": None, + "group_id": operation_group.pk, + "type": "deposit", + }, + ], + }, + ], + "checkouts": [ + { + "id": self.checkout.pk, + "balance": Decimal("110.75"), + }, + ], + "articles": [], + }, + ) + + def test_invalid_deposit_expects_amount(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "deposit", + "form-0-amount": "", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual( + json_data["errors"]["operations"], [{"__all__": ["Bad request"]}] + ) + + def test_invalid_deposit_too_many_params(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "deposit", + "form-0-amount": "10", + "form-0-article": str(self.article.pk), + "form-0-article_nb": "3", + }) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual( + json_data["errors"]["operations"], [{"__all__": ["Bad request"]}] + ) + + def test_invalid_deposit_expects_positive_amount(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "deposit", + "form-0-amount": "-10", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + 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"]}] + ) + + def test_invalid_deposit_requires_perm(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "deposit", + "form-0-amount": "10.75", + "form-0-article": "", + "form-0-article_nb": "", + }) + 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"]["missing_perms"], ["Effectuer une charge"] + ) + + def test_withdraw(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "withdraw", + "form-0-amount": "-10.75", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + json_data = self._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertDictEqual(operation_group.__dict__, { + "_state": mock.ANY, + "at": mock.ANY, + "amount": Decimal("-10.75"), + "checkout_id": self.checkout.pk, + "comment": "", + "id": mock.ANY, + "is_cof": False, + "on_acc_id": self.account.pk, + "valid_by_id": None, + }) + operation = Operation.objects.get() + self.assertDictEqual(operation.__dict__, { + "_state": mock.ANY, + "addcost_amount": None, + "addcost_for_id": None, + "amount": Decimal("-10.75"), + "article_id": None, + "article_nb": None, + "canceled_at": None, + "canceled_by_id": None, + "group_id": operation_group.pk, + "id": mock.ANY, + "type": "withdraw", + }) + + self.assertDictEqual(json_data, { + "operationgroup": operation_group.pk, + "operations": [operation.pk], + "warnings": {}, + "errors": {}, + }) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("39.25")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("89.25")) + + self.kpsul_consumer_mock.group_send.assert_called_once_with( + "kfet.kpsul", + { + "opegroups": [ + { + "add": True, + "at": mock.ANY, + "amount": Decimal("-10.75"), + "checkout__name": "Checkout", + "comment": "", + "id": operation_group.pk, + "is_cof": False, + "on_acc__trigramme": "000", + "valid_by__trigramme": None, + "opes": [ + { + "id": operation.pk, + "addcost_amount": None, + "addcost_for__trigramme": None, + "amount": Decimal("-10.75"), + "article__name": None, + "article_nb": None, + "canceled_at": None, + "canceled_by__trigramme": None, + "group_id": operation_group.pk, + "type": "withdraw", + }, + ], + }, + ], + "checkouts": [ + { + "id": self.checkout.pk, + "balance": Decimal("89.25"), + }, + ], + "articles": [], + }, + ) + + def test_invalid_withdraw_expects_amount(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "withdraw", + "form-0-amount": "", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual( + json_data["errors"]["operations"], [{"__all__": ["Bad request"]}] + ) + + def test_invalid_withdraw_too_many_params(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "withdraw", + "form-0-amount": "-10", + "form-0-article": str(self.article.pk), + "form-0-article_nb": "3", + }) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual( + json_data["errors"]["operations"], [{"__all__": ["Bad request"]}] + ) + + def test_invalid_withdraw_expects_negative_amount(self): + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "withdraw", + "form-0-amount": "10", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + 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"]}] + ) + + def test_edit(self): + user_add_perms(self.users["team"], ["kfet.edit_balance_account"]) + + data = dict(self.base_post_data, **{ + "comment": "A comment to explain it", + "form-TOTAL_FORMS": "1", + "form-0-type": "edit", + "form-0-amount": "10.75", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + json_data = self._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertDictEqual(operation_group.__dict__, { + "_state": mock.ANY, + "at": mock.ANY, + "amount": Decimal("10.75"), + "checkout_id": self.checkout.pk, + "comment": "A comment to explain it", + "id": mock.ANY, + "is_cof": False, + "on_acc_id": self.account.pk, + "valid_by_id": self.accounts["team"].pk, + }) + operation = Operation.objects.get() + self.assertDictEqual(operation.__dict__, { + "_state": mock.ANY, + "addcost_amount": None, + "addcost_for_id": None, + "amount": Decimal("10.75"), + "article_id": None, + "article_nb": None, + "canceled_at": None, + "canceled_by_id": None, + "group_id": operation_group.pk, + "id": mock.ANY, + "type": "edit", + }) + + self.assertDictEqual(json_data, { + "operationgroup": operation_group.pk, + "operations": [operation.pk], + "warnings": {}, + "errors": {}, + }) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("60.75")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("100.00")) + + self.kpsul_consumer_mock.group_send.assert_called_once_with( + "kfet.kpsul", + { + "opegroups": [ + { + "add": True, + "at": mock.ANY, + "amount": Decimal("10.75"), + "checkout__name": "Checkout", + "comment": "A comment to explain it", + "id": operation_group.pk, + "is_cof": False, + "on_acc__trigramme": "000", + "valid_by__trigramme": "100", + "opes": [ + { + "id": operation.pk, + "addcost_amount": None, + "addcost_for__trigramme": None, + "amount": Decimal("10.75"), + "article__name": None, + "article_nb": None, + "canceled_at": None, + "canceled_by__trigramme": None, + "group_id": operation_group.pk, + "type": "edit", + }, + ], + }, + ], + "checkouts": [ + { + "id": self.checkout.pk, + "balance": Decimal("100.00"), + }, + ], + "articles": [], + }, + ) + + def test_invalid_edit_requires_perm(self): + data = dict(self.base_post_data, **{ + "comment": "A comment to explain it", + "form-TOTAL_FORMS": "1", + "form-0-type": "edit", + "form-0-amount": "10.75", + "form-0-article": "", + "form-0-article_nb": "", + }) + 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"]["missing_perms"], + ["Modifier la balance d'un compte"], + ) + + def test_invalid_edit_expects_comment(self): + user_add_perms(self.users["team"], ["kfet.edit_balance_account"]) + + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "1", + "form-0-type": "edit", + "form-0-amount": "10.75", + "form-0-article": "", + "form-0-article_nb": "", + }) + resp = self.client.post(self.url, data) + + self.assertEqual(resp.status_code, 400) + json_data = json.loads(resp.content.decode("utf-8")) + self.assertEqual(json_data["errors"]["need_comment"], True) + + def _setup_addcost(self): + self.register_user("addcost", create_user("addcost", "ADD")) + kfet_config.set( + addcost_amount=Decimal("0.50"), + addcost_for=self.accounts["addcost"], + ) + + def test_addcost_user_is_not_cof(self): + self.account.cofprofile.is_cof = False + self.account.cofprofile.save() + self._setup_addcost() + + 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._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertEqual(operation_group.amount, Decimal("-6.00")) + operation = Operation.objects.get() + self.assertEqual(operation.addcost_for, self.accounts["addcost"]) + self.assertEqual(operation.addcost_amount, Decimal("1.00")) + self.assertEqual(operation.amount, Decimal("-6.00")) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("44.00")) + self.accounts["addcost"].refresh_from_db() + self.assertEqual(self.accounts["addcost"].balance, Decimal("1.00")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("100.00")) + + ws_data_ope = ( + self.kpsul_consumer_mock.group_send + .call_args[0][1]["opegroups"][0]["opes"][0] + ) + self.assertEqual(ws_data_ope["addcost_amount"], Decimal("1.00")) + self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD") + + def test_addcost_user_is_cof(self): + kfet_config.set(reduction_cof=Decimal("20")) + self.account.cofprofile.is_cof = True + self.account.cofprofile.save() + self._setup_addcost() + + 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._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertEqual(operation_group.amount, Decimal("-4.80")) + operation = Operation.objects.get() + self.assertEqual(operation.addcost_for, self.accounts["addcost"]) + self.assertEqual(operation.addcost_amount, Decimal("0.80")) + self.assertEqual(operation.amount, Decimal("-4.80")) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("45.20")) + self.accounts["addcost"].refresh_from_db() + self.assertEqual(self.accounts["addcost"].balance, Decimal("0.80")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("100.00")) + + ws_data_ope = ( + self.kpsul_consumer_mock.group_send + .call_args[0][1]["opegroups"][0]["opes"][0] + ) + self.assertEqual(ws_data_ope["addcost_amount"], Decimal("0.80")) + self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD") + + def test_addcost_user_is_cash(self): + self.account.cofprofile.is_cof = True + self.account.cofprofile.save() + self._setup_addcost() + + data = dict(self.base_post_data, **{ + "on_acc": str(self.accounts["liq"].pk), + "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._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertEqual(operation_group.amount, Decimal("-6.00")) + operation = Operation.objects.get() + self.assertEqual(operation.addcost_for, self.accounts["addcost"]) + self.assertEqual(operation.addcost_amount, Decimal("1.00")) + self.assertEqual(operation.amount, Decimal("-6.00")) + + self.accounts["addcost"].refresh_from_db() + self.assertEqual(self.accounts["addcost"].balance, Decimal("1.00")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("106.00")) + + ws_data_ope = ( + self.kpsul_consumer_mock.group_send + .call_args[0][1]["opegroups"][0]["opes"][0] + ) + self.assertEqual(ws_data_ope["addcost_amount"], Decimal("1.00")) + self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD") + + def test_addcost_to_self(self): + self._setup_addcost() + self.accounts["addcost"].balance = Decimal("20.00") + self.accounts["addcost"].save() + + data = dict(self.base_post_data, **{ + "on_acc": str(self.accounts["addcost"].pk), + "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._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertEqual(operation_group.amount, Decimal("-5.00")) + operation = Operation.objects.get() + self.assertEqual(operation.addcost_for, None) + self.assertEqual(operation.addcost_amount, None) + self.assertEqual(operation.amount, Decimal("-5.00")) + + self.accounts["addcost"].refresh_from_db() + self.assertEqual(self.accounts["addcost"].balance, Decimal("15.00")) + + ws_data_ope = ( + self.kpsul_consumer_mock.group_send + .call_args[0][1]["opegroups"][0]["opes"][0] + ) + self.assertEqual(ws_data_ope["addcost_amount"], None) + self.assertEqual(ws_data_ope["addcost_for__trigramme"], None) + + def test_addcost_category_disabled(self): + self._setup_addcost() + self.article.category.has_addcost = False + self.article.category.save() + + 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._assertResponseOk(resp) + + operation_group = OperationGroup.objects.get() + self.assertEqual(operation_group.amount, Decimal("-5.00")) + operation = Operation.objects.get() + self.assertEqual(operation.addcost_for, None) + self.assertEqual(operation.addcost_amount, None) + self.assertEqual(operation.amount, Decimal("-5.00")) + + self.accounts["addcost"].refresh_from_db() + self.assertEqual(self.accounts["addcost"].balance, Decimal("0.00")) + + ws_data_ope = ( + self.kpsul_consumer_mock.group_send + .call_args[0][1]["opegroups"][0]["opes"][0] + ) + self.assertEqual(ws_data_ope["addcost_amount"], None) + self.assertEqual(ws_data_ope["addcost_for__trigramme"], None) + + def test_negative_new(self): + user_add_perms( + self.users["team"], ["kfet.perform_negative_operations"] + ) + self.account.balance = Decimal("1.00") + self.account.save() + + 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._assertResponseOk(resp) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("-4.00")) + + def test_negative_exists(self): + user_add_perms( + self.users["team"], ["kfet.perform_negative_operations"] + ) + self.account.balance = Decimal("-10.00") + self.account.save() + self.account.update_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._assertResponseOk(resp) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("-15.00")) + + def test_negative_exists_balance_higher_than_initial(self): + user_add_perms(self.users["team"], ["kfet.perform_deposit"]) + self.account.balance = Decimal("-10.00") + self.account.save() + self.account.update_negative() + + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "2", + "form-0-type": "purchase", + "form-0-amount": "", + "form-0-article": str(self.article.pk), + "form-0-article_nb": "1", + "form-1-type": "deposit", + "form-1-amount": "5.00", + "form-1-article": "", + "form-1-article_nb": "", + }) + resp = self.client.post(self.url, data) + + self._assertResponseOk(resp) + + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("-7.50")) + + def test_invalid_negative_new_requires_perm(self): + self.account.balance = Decimal("1.00") + self.account.save() + + 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"], + { + "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")) + self.account.balance = Decimal("1.00") + self.account.save() + self.account.update_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_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"), + ) + + 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", + price=Decimal("4"), + stock=-5, + category=ArticleCategory.objects.first(), + ) + self.account.cofprofile.is_cof = False + self.account.cofprofile.save() + + data = dict(self.base_post_data, **{ + "form-TOTAL_FORMS": "2", + "form-0-type": "purchase", + "form-0-amount": "", + "form-0-article": str(self.article.pk), + "form-0-article_nb": "2", + "form-1-type": "purchase", + "form-1-amount": "", + "form-1-article": str(article2.pk), + "form-1-article_nb": "1", + }) + resp = self.client.post(self.url, data) + + # Check response status + json_data = self._assertResponseOk(resp) + + # Check object creations + operation_group = OperationGroup.objects.get() + self.assertDictEqual(operation_group.__dict__, { + "_state": mock.ANY, + "at": mock.ANY, + "amount": Decimal("-9.00"), + "checkout_id": self.checkout.pk, + "comment": "", + "id": mock.ANY, + "is_cof": False, + "on_acc_id": self.account.pk, + "valid_by_id": None, + }) + operation_list = Operation.objects.all() + self.assertEqual(len(operation_list), 2) + self.assertDictEqual(operation_list[0].__dict__, { + "_state": mock.ANY, + "addcost_amount": None, + "addcost_for_id": None, + "amount": Decimal("-5.00"), + "article_id": self.article.pk, + "article_nb": 2, + "canceled_at": None, + "canceled_by_id": None, + "group_id": operation_group.pk, + "id": mock.ANY, + "type": "purchase", + }) + self.assertDictEqual(operation_list[1].__dict__, { + "_state": mock.ANY, + "addcost_amount": None, + "addcost_for_id": None, + "amount": Decimal("-4.00"), + "article_id": article2.pk, + "article_nb": 1, + "canceled_at": None, + "canceled_by_id": None, + "group_id": operation_group.pk, + "id": mock.ANY, + "type": "purchase", + }) + + # Check response content + self.assertDictEqual(json_data, { + "operationgroup": operation_group.pk, + "operations": [operation_list[0].pk, operation_list[1].pk], + "warnings": {}, + "errors": {}, + }) + + # Check object updates + self.account.refresh_from_db() + self.assertEqual(self.account.balance, Decimal("41.00")) + self.checkout.refresh_from_db() + self.assertEqual(self.checkout.balance, Decimal("100.00")) + self.article.refresh_from_db() + self.assertEqual(self.article.stock, 18) + article2.refresh_from_db() + self.assertEqual(article2.stock, -6) + + # Check websocket data + self.kpsul_consumer_mock.group_send.assert_called_once_with( + "kfet.kpsul", + { + "opegroups": [ + { + "add": True, + "at": mock.ANY, + "amount": Decimal("-9.00"), + "checkout__name": "Checkout", + "comment": "", + "id": operation_group.pk, + "is_cof": False, + "on_acc__trigramme": "000", + "valid_by__trigramme": None, + "opes": [ + { + "id": operation_list[0].pk, + "addcost_amount": None, + "addcost_for__trigramme": None, + "amount": Decimal("-5.00"), + "article__name": "Article", + "article_nb": 2, + "canceled_at": None, + "canceled_by__trigramme": None, + "group_id": operation_group.pk, + "type": "purchase", + }, + { + "id": operation_list[1].pk, + "addcost_amount": None, + "addcost_for__trigramme": None, + "amount": Decimal("-4.00"), + "article__name": "Article 2", + "article_nb": 1, + "canceled_at": None, + "canceled_by__trigramme": None, + "group_id": operation_group.pk, + "type": "purchase", + }, + ], + }, + ], + "checkouts": [ + { + "id": self.checkout.pk, + "balance": Decimal("100.00"), + }, + ], + "articles": [ + { + "id": self.article.pk, + "stock": 18, + }, + { + "id": article2.pk, + "stock": -6, + }, + ], + }, + ) class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase): diff --git a/kfet/tests/testcases.py b/kfet/tests/testcases.py index aa2fb1b6..3a69e9ca 100644 --- a/kfet/tests/testcases.py +++ b/kfet/tests/testcases.py @@ -178,7 +178,9 @@ class ViewTestCaseMixin(TestCaseMixin): During setup, three users are created with their kfet account: - 'user': a basic user without any permission, account trigramme: 000, - 'team': a user with kfet.is_team permission, account trigramme: 100, - - 'root': a superuser, account trigramme: 200. + - 'root': a superuser, account trigramme: 200, + - 'liq': if class attribute 'with_liq' is 'True', account + trigramme: LIQ. Their password is their username. One can create additionnal users with 'get_users_extra' method, or prevent @@ -221,6 +223,8 @@ class ViewTestCaseMixin(TestCaseMixin): auth_user = None auth_forbidden = [] + with_liq = False + def setUp(self): """ Warning: Do not forget to call super().setUp() in subclasses. @@ -262,7 +266,7 @@ class ViewTestCaseMixin(TestCaseMixin): """ # Format desc: username, password, trigramme - return { + users_base = { # user, user, 000 'user': create_user(), # team, team, 100 @@ -270,6 +274,9 @@ class ViewTestCaseMixin(TestCaseMixin): # root, root, 200 'root': create_root(), } + if self.with_liq: + users_base['liq'] = create_user('liq', 'LIQ') + return users_base @cached_property def users_base(self):