diff --git a/apps/wallet/tests/application.py b/apps/wallet/tests/application.py index b7fbc7f..abdf815 100755 --- a/apps/wallet/tests/application.py +++ b/apps/wallet/tests/application.py @@ -273,40 +273,37 @@ class ApplicationApiFlowsTests(APITestCase): self.assertEqual(response.status_code, 400) def test_deposit_flow_prevent_double_spending(self): - # TODO: check this test 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_account_balance_api'), HTTP_AUTHORIZATION=auth) - self.assertContains(response, 'balance') - self.assertEqual(response.json()['balance'], 300) + wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] + response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + self.assertGreater(response.json()['count'], 1) + from_wallet = response.json()['results'][0] + self.assertEqual(from_wallet['balance'], 300) params = { - 'uuid': self.payee_uuid, - 'wallet_uuid': self.wallet_rial_uuid, + 'user': 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) + response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + self.assertGreater(response.json()['count'], 1) + to_wallet = response.json()['results'][0] + self.assertEqual(to_wallet['balance'], 0) # Deposit process data = { - 'uuid': "b14e8b86-8f4a-44d9-b29d-badceb470053", 'amount': 200, 'payee': self.payee_uuid, 'payee_type': TypeChoices.USER, - 'payee_account': self.wallet_rial_uuid, - + 'payee_wallet': self.wallet_rial_uuid, } - - kwargs_wallet_uuid_payee_uuid = { - 'wallet_uuid': self.wallet_rial_uuid, + kwargs_payer_wallet = { + 'payer_wallet': self.wallet_rial_uuid, } - response = self.client.post(reverse('wallet:application_deposit_submit_api', kwargs=kwargs_wallet_uuid_payee_uuid), data=data, HTTP_AUTHORIZATION=auth) + response = self.client.post(reverse('wallet:application_deposit_submit_api', kwargs=kwargs_payer_wallet), data=data, HTTP_AUTHORIZATION=auth) self.assertEqual(response.status_code, 201) - response = self.client.post(reverse('wallet:application_deposit_submit_api', kwargs=kwargs_wallet_uuid_payee_uuid), data=data, HTTP_AUTHORIZATION=auth) - # self.assertEqual(response.status_code, 422) - self.assertEqual(response.status_code, 201) + response = self.client.post(reverse('wallet:application_deposit_submit_api', kwargs=kwargs_payer_wallet), data=data, HTTP_AUTHORIZATION=auth) + self.assertEqual(response.status_code, 422) def test_withdraw_flow_success(self): auth = self._create_authorization_header(self.payer_access_token.token) diff --git a/apps/wallet/views/application.py b/apps/wallet/views/application.py index ef3888a..64a15a6 100755 --- a/apps/wallet/views/application.py +++ b/apps/wallet/views/application.py @@ -8,6 +8,7 @@ from rest_framework.exceptions import APIException from rest_framework.generics import get_object_or_404 from rest_framework.request import Request from rest_framework.response import Response +import uuid from apps.users.models import User from apps.wallet.constans import TypeChoices, StateChoices @@ -215,26 +216,22 @@ class DepositSubmitAPIView(generics.CreateAPIView): payee_account, _ = Account.objects.get_or_create(owner_uuid=payee_id, owner_type=payee_type, wallet=payee_wallet) logger.debug(f"Payee account: {payee_account.uuid}") - uuid = serializer.validated_data.pop('uuid', None) + uuid_param = serializer.validated_data.pop('uuid', uuid.uuid4()) # if uuid not provided generate new one to handle get_or_create details = serializer.validated_data.get('details', {}) reference_id = details.get('reference_id', None) - logger.debug(f"Transaction UUID: {uuid}, Reference ID: {reference_id}") + logger.debug(f"Transaction UUID: {uuid_param}, Reference ID: {reference_id}") transaction_params = { + 'uuid': uuid_param, 'application': application, 'payer_account': payer_account, 'payee_account': payee_account, 'amount': serializer.validated_data.get('amount'), } - if uuid: - transaction_params['uuid'] = uuid + # TODO: checking reference_id + # if reference_id: + # transaction_params['details__reference_id'] = reference_id - if reference_id: - transaction_params['details__reference_id'] = reference_id - - # else: - # logger.error('Missing UUID and Reference ID') - # raise UnprocessableEntity('Missing UUID or reference_id') instance, created = Transaction.objects.get_or_create(**transaction_params, defaults={ 'details': details })