From 3274a5d0e6fca4f43bf26f3a402fee75c2bfdc82 Mon Sep 17 00:00:00 2001 From: hgh Date: Wed, 3 Sep 2025 10:22:48 +0330 Subject: [PATCH] wallet application api flows tests --- apps/wallet/tests.py | 3 - apps/wallet/tests/__init__.py | 0 apps/wallet/tests/application.py | 115 +++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) delete mode 100755 apps/wallet/tests.py create mode 100644 apps/wallet/tests/__init__.py create mode 100755 apps/wallet/tests/application.py diff --git a/apps/wallet/tests.py b/apps/wallet/tests.py deleted file mode 100755 index 7ce503c..0000000 --- a/apps/wallet/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/apps/wallet/tests/__init__.py b/apps/wallet/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/wallet/tests/application.py b/apps/wallet/tests/application.py new file mode 100755 index 0000000..b6b8d85 --- /dev/null +++ b/apps/wallet/tests/application.py @@ -0,0 +1,115 @@ +import uuid +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 apps.users.models import User +from apps.wallet.constans import TypeChoices +from apps.wallet.models import Wallet + +AccessToken = get_access_token_model() +Application = get_application_model() + + +class ApplicationApiFlowsTests(APITestCase): + access_token = 'u4naVsdKCbKNOhnElPyXcrwSnqqFbm' + payer_uuid = uuid.UUID('b14e8b86-8f4a-44d9-b29d-badceb470051') + payee_uuid = uuid.UUID('b14e8b86-8f4a-44d9-b29d-badceb470052') + application_uuid = uuid.UUID('b14e8b86-8f4a-44d9-b29d-badceb470053') + client_id = '4INGOCMoulE0fNY1SQlTbPtsWqqxGj2DdqjADq6u' + + def setUp(self): + self.application_user, _ = User.objects.get_or_create(pk=self.application_uuid) + self.application_wallet, _ = Wallet.objects.get_or_create(owner_uuid=self.application_uuid, owner_type=TypeChoices.APPLICATION, balance=300) + self.payee_wallet, _ = Wallet.objects.get_or_create(owner_uuid=self.payee_uuid, owner_type=1) + expire_datetime = timezone.now() + timedelta(seconds=3600) + expire_datetime.isoformat() + + scopes = [ + 'wallet.application.deposit:verify', + 'wallet.application:get_balance', + 'wallet.application.deposit:submit', + 'wallet.deposit:submit', + 'wallet.wallet:get_balance', + 'wallet.application:get_user_balance', + ] + + self.access_token, _created = AccessToken.objects.update_or_create( + token=self.access_token, + defaults={ + "user": self.application_user, + "client_id": self.client_id, + # "client_owner": owner, + "application_id": self.application_uuid, + "scope": ' '.join(scopes), + "expires": expire_datetime.isoformat(), + }, + ) + + self.application, _created = Application.objects.get_or_create( + client_id=self.client_id, + uuid=self.application_uuid, + ) + + def tearDown(self): + super().tearDown() + + def _create_authorization_header(self, token): + return "Bearer {0}".format(token) + + def test_authentication_allow(self): + auth = self._create_authorization_header(self.access_token.token) + response = self.client.get(reverse('wallet:user_wallet_balance_api'), HTTP_AUTHORIZATION=auth) + self.assertContains(response, 'balance') + + # def test_authentication_disallow(self): + # auth = self._create_authorization_header('fake_token') + # response = self.client.get(reverse('wallet:user_wallet_balance_api'), HTTP_AUTHORIZATION=auth) + # self.assertEqual(response.status_code, 401) + + def test_deposit_flow_success(self): + auth = self._create_authorization_header(self.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': 100, + '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) + json_response = response.json() + + params = { + 'uuid': json_response['uuid'] + } + response = self.client.get(reverse('wallet:application_deposit_verify_api', kwargs=params), HTTP_AUTHORIZATION=auth) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json()['state'], 5) + + # Desired results + response = self.client.get(reverse('wallet:application_wallet_balance_api'), HTTP_AUTHORIZATION=auth) + self.assertContains(response, 'balance') + self.assertEqual(response.json()['balance'], 200) + + 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'], 100) +