wallet application api flows tests

This commit is contained in:
hgh 2025-09-03 10:22:48 +03:30
parent 8f28b2c981
commit 3274a5d0e6
3 changed files with 115 additions and 3 deletions

View file

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

View file

115
apps/wallet/tests/application.py Executable file
View file

@ -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)