From 9de126d5859d779f1a5ec5d0c0d0b860238efce0 Mon Sep 17 00:00:00 2001 From: hgh Date: Sat, 6 Sep 2025 16:51:33 +0330 Subject: [PATCH] skip insufficient balance for allowed negative balance applications test --- apps/wallet/models.py | 4 +-- apps/wallet/tests/application.py | 49 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/apps/wallet/models.py b/apps/wallet/models.py index 49d1b49..4308c43 100755 --- a/apps/wallet/models.py +++ b/apps/wallet/models.py @@ -312,8 +312,8 @@ class Transaction(BaseModel): ######################## category def category_withdraw_from_payer_balance(self): - payer_is_allowed_negative_balance = self.payer_type == TypeChoices.APPLICATION.value and str( - self.payer_id) in settings.ALLOWED_NEGATIVE_BALANCE_APPLICATIONS + payer_is_allowed_negative_balance = self.payer_wallet.owner_type == TypeChoices.APPLICATION.value and str( + self.payer_wallet.owner_uuid) in settings.ALLOWED_NEGATIVE_BALANCE_APPLICATIONS if self.amount > self.payer_wallet.balance and not payer_is_allowed_negative_balance: raise APIException('not enough balance') self.payer_wallet.balance = F('balance') - self.amount diff --git a/apps/wallet/tests/application.py b/apps/wallet/tests/application.py index 1c58092..00e969d 100755 --- a/apps/wallet/tests/application.py +++ b/apps/wallet/tests/application.py @@ -4,7 +4,7 @@ from datetime import timedelta from django.urls import reverse from django.utils import timezone from oauth2_provider.models import get_access_token_model, get_application_model -from rest_framework.test import APITestCase +from rest_framework.test import APITestCase, override_settings from apps.users.models import User from apps.wallet.constans import TypeChoices @@ -234,6 +234,30 @@ class ApplicationApiFlowsTests(APITestCase): response = self.client.post(reverse('wallet:application_deposit_submit_api'), data=data, HTTP_AUTHORIZATION=auth) self.assertEqual(response.status_code, 500) + @override_settings(ALLOWED_NEGATIVE_BALANCE_APPLICATIONS=['b14e8b86-8f4a-44d9-b29d-badceb470053']) + def test_deposit_flow_skip_insufficient_balance_for_allowed_negative_balance_applications(self): + auth = self._create_authorization_header(self.application_access_token.token) + + # Checking wallets initials values(application balance = 300 and payee balance = zero) + response = self.client.get(reverse('wallet:application_wallet_balance_api'), HTTP_AUTHORIZATION=auth) + self.assertContains(response, 'balance') + self.assertEqual(response.json()['balance'], 300) + params = { + 'uuid': self.payee_uuid, + } + response = self.client.get(reverse('wallet:application_user_wallet_balance_api', kwargs=params), HTTP_AUTHORIZATION=auth) + self.assertContains(response, 'balance') + self.assertEqual(response.json()['balance'], 0) + + # Deposit process + data = { + 'amount': 500, + 'payee_id': self.payee_uuid, + 'payee_type': TypeChoices.USER + } + response = self.client.post(reverse('wallet:application_deposit_submit_api'), data=data, HTTP_AUTHORIZATION=auth) + self.assertEqual(response.status_code, 201) + def test_deposit_flow_zero_amount(self): auth = self._create_authorization_header(self.application_access_token.token) @@ -381,29 +405,6 @@ class ApplicationApiFlowsTests(APITestCase): self.assertContains(response, 'balance') self.assertEqual(response.json()['balance'], 300) - def test_withdraw_flow_insufficient_balance(self): - auth = self._create_authorization_header(self.payer_access_token.token) - - # Checking wallets initials values(application balance = 300 and payee balance = zero) - response = self.client.get(reverse('wallet:application_wallet_balance_api'), HTTP_AUTHORIZATION=auth) - self.assertContains(response, 'balance') - self.assertEqual(response.json()['balance'], 300) - params = { - 'uuid': self.payer_uuid, - } - response = self.client.get(reverse('wallet:application_user_wallet_balance_api', kwargs=params), HTTP_AUTHORIZATION=auth) - self.assertContains(response, 'balance') - self.assertEqual(response.json()['balance'], 300) - - # Deposit process - data = { - 'amount': 500, - 'payer_id': self.payer_uuid, - 'payer_type': TypeChoices.USER - } - response = self.client.post(reverse('wallet:application_withdraw_submit_api'), data=data, HTTP_AUTHORIZATION=auth) - self.assertEqual(response.status_code, 500) - def test_withdraw_flow_zero_amount(self): auth = self._create_authorization_header(self.payer_access_token.token)