You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
6.4 KiB
198 lines
6.4 KiB
import json
|
|
|
|
import mock
|
|
import pytest
|
|
|
|
from django.urls import reverse
|
|
|
|
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from yandex_money.models import Payment
|
|
|
|
from finance import models
|
|
from progress.models import Progress, ProgressLesson
|
|
|
|
from tests.fixtures.finance import PRICE, METHOD_CASH, STATUS_WAIT, STATUS_PAID
|
|
|
|
DUMMY_COMMENT = 'test comment'
|
|
DUMMY_DESCRIPTION = 'test description'
|
|
|
|
mock.patch('lms.global_decorators.transaction_decorator', lambda x: x).start()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_list_bills(api_client, student_client, student, manager, admin_client,
|
|
manager_client, lead_manager_client, bill_cash):
|
|
"""
|
|
Test for get list of bills
|
|
"""
|
|
assert api_client.get(
|
|
reverse('finance:bills'),
|
|
status=status.HTTP_403_FORBIDDEN
|
|
)
|
|
response = student_client.get(
|
|
reverse('finance:bills'),
|
|
status=status.HTTP_200_OK
|
|
)
|
|
results_student = json.loads(response.content)
|
|
assert len(results_student) is 1
|
|
assert results_student[0]['opener'] == manager.email
|
|
assert results_student[0]['user'] == student.email
|
|
response_admin = admin_client.get(
|
|
reverse('finance:bills'),
|
|
status=status.HTTP_200_OK
|
|
)
|
|
results_admin = json.loads(response_admin.content)
|
|
assert len(results_admin) is 0
|
|
response_manager = manager_client.get(
|
|
reverse('finance:bills'),
|
|
status=status.HTTP_200_OK
|
|
)
|
|
results_manager = json.loads(response_manager.content)
|
|
assert results_manager[0]['opener'] == manager.email
|
|
assert results_manager[0]['user'] == student.email
|
|
response_lead_manager = lead_manager_client.get(
|
|
reverse('finance:bills'),
|
|
status=status.HTTP_200_OK
|
|
)
|
|
results_lead_manager = json.loads(response_lead_manager.content)
|
|
assert len(results_lead_manager) is 0
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_create_bill(api_client, student_client, manager_client,
|
|
manager, student, course):
|
|
"""
|
|
Test create bill
|
|
"""
|
|
data = {
|
|
'bill': {
|
|
'course_token': course.token.hex,
|
|
'opener': manager.email,
|
|
'user': student.email,
|
|
'comment': DUMMY_COMMENT,
|
|
'description': DUMMY_DESCRIPTION
|
|
},
|
|
'children': [{
|
|
'status': STATUS_WAIT,
|
|
'method': METHOD_CASH,
|
|
'price': PRICE,
|
|
'real_price': PRICE,
|
|
}]
|
|
}
|
|
response = manager_client.post(
|
|
reverse('finance:bills'),
|
|
data=data,
|
|
status=status.HTTP_200_OK
|
|
)
|
|
results = json.loads(response.content)
|
|
assert models.Bill.objects.get(user=student).id == results['bill']['id']
|
|
assert models.Invoice.objects.get(
|
|
bill=results['bill']['id']).id == results['children'][0]['id']
|
|
assert api_client.post(
|
|
reverse('finance:bills'),
|
|
data=data,
|
|
status=status.HTTP_403_FORBIDDEN
|
|
)
|
|
assert student_client.post(
|
|
reverse('finance:bills'),
|
|
data=data,
|
|
status=status.HTTP_403_FORBIDDEN
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@mock.patch('django.core.mail.EmailMessage.send')
|
|
def test_confirm_pay_manager(mocked_email, manager_client, bill_cash, student):
|
|
assert Progress.objects.count() is 0
|
|
assert ProgressLesson.objects.count() is 0
|
|
data = {
|
|
'bill': {
|
|
'course_token': bill_cash.course_token.hex,
|
|
'opener': bill_cash.opener.email,
|
|
'user': bill_cash.user.email,
|
|
'comment': bill_cash.comment,
|
|
'description': bill_cash.description
|
|
},
|
|
'children': [{
|
|
'status': STATUS_PAID,
|
|
'method': METHOD_CASH,
|
|
'price': PRICE,
|
|
'real_price': PRICE,
|
|
}]
|
|
}
|
|
response = manager_client.post(
|
|
reverse('finance:bills'),
|
|
data=data,
|
|
status=status.HTTP_200_OK
|
|
)
|
|
results = json.loads(response.content)
|
|
assert models.Bill.objects.get(user=student).id == results['bill']['id']
|
|
assert Progress.objects.count() is 1
|
|
assert ProgressLesson.objects.count() is 1
|
|
progress = Progress.objects.filter(user=bill_cash.user).first()
|
|
assert Progress.objects.filter(user=bill_cash.user).exists() is True
|
|
assert ProgressLesson.objects.filter(progress=progress).exists() is True
|
|
assert mocked_email.call_count == 1
|
|
|
|
|
|
def fake_post_success(view, request):
|
|
pay = Payment.objects.get(order_number=request.data['orderNumber'])
|
|
pay.status = Payment.STATUS.SUCCESS
|
|
pay.save()
|
|
return Response('ok', status=status.HTTP_200_OK)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@mock.patch('django.core.mail.EmailMessage.send')
|
|
@mock.patch('finance.views.YandexCheckView.post', fake_post_success)
|
|
def test_confirm_pay_auto_success(mocked_email, api_client, bill_yandex, student):
|
|
assert Progress.objects.count() is 0
|
|
assert ProgressLesson.objects.count() is 0
|
|
invoice = bill_yandex.invoice_set.first()
|
|
payment = invoice.yandex_pay
|
|
data = {
|
|
'orderNumber': payment.order_number,
|
|
'shopSumAmount': str(payment.shop_amount)
|
|
}
|
|
api_client.post(
|
|
reverse('yandex_money_check'),
|
|
data=data,
|
|
status=status.HTTP_200_OK
|
|
)
|
|
assert Progress.objects.count() is 1
|
|
assert ProgressLesson.objects.count() is 1
|
|
progress = Progress.objects.filter(user=bill_yandex.user).first()
|
|
assert Progress.objects.filter(user=bill_yandex.user).exists() is True
|
|
assert ProgressLesson.objects.filter(progress=progress).exists() is True
|
|
assert mocked_email.call_count == 1
|
|
|
|
|
|
def fake_post_fail(view, request):
|
|
pay = Payment.objects.get(order_number=request.data['orderNumber'])
|
|
pay.status = Payment.STATUS.FAIL
|
|
pay.save()
|
|
return Response('fail', status=status.HTTP_200_OK)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@mock.patch('django.core.mail.EmailMessage.send')
|
|
@mock.patch('finance.views.YandexCheckView.post', fake_post_fail)
|
|
def test_confirm_pay_auto_fail(mocked_email, api_client, bill_yandex, student):
|
|
assert Progress.objects.count() is 0
|
|
assert ProgressLesson.objects.count() is 0
|
|
invoice = bill_yandex.invoice_set.first()
|
|
payment = invoice.yandex_pay
|
|
data = {
|
|
'orderNumber': payment.order_number,
|
|
'shopSumAmount': str(payment.shop_amount)
|
|
}
|
|
api_client.post(
|
|
reverse('yandex_money_check'),
|
|
data=data,
|
|
status=status.HTTP_200_OK
|
|
)
|
|
assert Progress.objects.count() is 0
|
|
assert ProgressLesson.objects.count() is 0
|
|
assert Progress.objects.filter(user=bill_yandex.user).exists() is False
|
|
assert mocked_email.call_count == 1
|
|
|