Compare commits
8 Commits
master
...
feature/te
| Author | SHA1 | Date |
|---|---|---|
|
|
da4c7fc3c1 | 8 years ago |
|
|
41de0175e9 | 8 years ago |
|
|
73d02e862a | 8 years ago |
|
|
c9892b454c | 8 years ago |
|
|
01ba718ad4 | 8 years ago |
|
|
62d749222e | 8 years ago |
|
|
1b70063ea6 | 8 years ago |
|
|
1ae3ac332d | 8 years ago |
25 changed files with 823 additions and 224 deletions
@ -0,0 +1,25 @@ |
||||
ADMIN = 1 |
||||
STUDENTS = 2 |
||||
TEACHERS = 3 |
||||
MANAGERS = 4 |
||||
FINANCE_MANAGERS = 5 |
||||
LEAD_MANAGERS = 6 |
||||
CURATORS = 7 |
||||
PARTNERS = 8 |
||||
SUPPORTS = 9 |
||||
FINANCE = 10 |
||||
PROJECT_MANAGERS = 11 |
||||
|
||||
STATUS_CHOICES = ( |
||||
(ADMIN, 'admin'), |
||||
(STUDENTS, 'students'), |
||||
(TEACHERS, 'teachers'), |
||||
(MANAGERS, 'managers'), |
||||
(FINANCE_MANAGERS, 'finance_managers'), |
||||
(LEAD_MANAGERS, 'lead_managers'), |
||||
(CURATORS, 'curators'), |
||||
(PARTNERS, 'partners'), |
||||
(SUPPORTS, 'supports'), |
||||
(FINANCE, 'finance'), |
||||
(PROJECT_MANAGERS, 'project_managers'), |
||||
) |
||||
@ -0,0 +1,56 @@ |
||||
import os |
||||
import shutil |
||||
|
||||
import pytest |
||||
|
||||
from tests.client import BetterAPIClient |
||||
|
||||
pytest_plugins = [ |
||||
'access.tests.fixtures', |
||||
] |
||||
|
||||
|
||||
def pytest_sessionfinish(session, exitstatus): |
||||
""" whole test run finishes. """ |
||||
print('pytest finish: cleanup') |
||||
if os.path.exists('media_qa'): |
||||
shutil.rmtree('media_qa') |
||||
|
||||
|
||||
@pytest.fixture |
||||
def api_client(): |
||||
"""Anonymous client for REST API.""" |
||||
client = BetterAPIClient() |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def admin_client(admin): |
||||
"""Authorized as admin(superuser) client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=admin) |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def student_client(student): |
||||
"""Authorized as student client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=student) |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def manager_client(manager): |
||||
"""Authorized as manager client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=manager) |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def lead_manager_client(lead_manager): |
||||
"""Authorized as lead manager client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=lead_manager) |
||||
return client |
||||
@ -0,0 +1,122 @@ |
||||
import pytest |
||||
|
||||
from access.factories import UserFactory, AccountFactory |
||||
|
||||
from access import groups |
||||
|
||||
|
||||
@pytest.fixture |
||||
def admin(): |
||||
""" |
||||
Create user as staff with data: |
||||
email = 'admin@example.com' |
||||
password = 'test' |
||||
is_staff = True |
||||
is_active = True |
||||
""" |
||||
user = UserFactory( |
||||
email='admin@example.com', |
||||
is_staff=True, |
||||
is_active=True, |
||||
groups__name=(groups.ADMIN,) |
||||
) |
||||
AccountFactory(owner=user) |
||||
return user |
||||
|
||||
|
||||
@pytest.fixture |
||||
def student(): |
||||
""" |
||||
Create user as student with data: |
||||
email = 'student@example.com' |
||||
password = 'test' |
||||
is_active = True |
||||
in groups 'students' |
||||
""" |
||||
user = UserFactory.create( |
||||
email='student@example.com', |
||||
is_staff=False, |
||||
is_active=True, |
||||
groups__name=(groups.STUDENTS,) |
||||
) |
||||
|
||||
AccountFactory(owner=user) |
||||
return user |
||||
|
||||
|
||||
@pytest.fixture |
||||
def student_not_active(): |
||||
""" |
||||
Create user as student with data: |
||||
email = 'notactivestudent@example.com' |
||||
password = 'test' |
||||
is_active = False |
||||
in groups 'students' |
||||
""" |
||||
user = UserFactory.create( |
||||
email='notactivestudent@example.com', |
||||
is_staff=False, |
||||
is_active=False, |
||||
groups__name=(groups.STUDENTS,) |
||||
) |
||||
return user |
||||
|
||||
|
||||
@pytest.fixture |
||||
def manager(): |
||||
""" |
||||
Create user as manager with data: |
||||
email = 'manager@example.com' |
||||
password = 'test' |
||||
is_staff = True |
||||
is_active = True |
||||
in groups 'managers' |
||||
""" |
||||
user = UserFactory.create( |
||||
email='manager@example.com', |
||||
is_staff=True, |
||||
is_active=True, |
||||
groups=(groups.MANAGERS,) |
||||
) |
||||
AccountFactory(owner=user) |
||||
return user |
||||
|
||||
|
||||
@pytest.fixture |
||||
def lead_manager(): |
||||
""" |
||||
Create user as lead manager with data: |
||||
email = 'lead_manager@example.com' |
||||
password = 'test' |
||||
is_staff = True |
||||
is_active = True |
||||
in groups 'lead_managers' |
||||
""" |
||||
user = UserFactory.create( |
||||
email='lead_manager@example.com', |
||||
is_staff=True, |
||||
is_active=True, |
||||
groups=(groups.LEAD_MANAGERS,) |
||||
) |
||||
AccountFactory(owner=user) |
||||
return user |
||||
|
||||
|
||||
@pytest.fixture |
||||
def teacher(): |
||||
""" |
||||
Create user as teacher with data: |
||||
email = 'teacher@example.com' |
||||
password = 'test' |
||||
is_staff = True |
||||
is_active = True |
||||
in groups 'teachers' |
||||
""" |
||||
user = UserFactory.create( |
||||
email='teacher@example.com', |
||||
is_staff=True, |
||||
is_active=True, |
||||
groups=(groups.TEACHERS,) |
||||
) |
||||
AccountFactory(owner=user) |
||||
return user |
||||
@ -0,0 +1,120 @@ |
||||
import mock |
||||
import pytest |
||||
|
||||
from django.contrib.auth import get_user_model |
||||
from django.urls import reverse |
||||
|
||||
from rest_framework import status |
||||
from rest_framework.generics import get_object_or_404 |
||||
|
||||
from access.factories import USER_PASSWORD |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
@mock.patch('django.core.mail.EmailMessage.send') |
||||
def test_generate_password_by_manager(mocked_send_mail, manager_client, |
||||
student_client, student): |
||||
""" |
||||
Test generate new password from admin area by manager |
||||
""" |
||||
assert manager_client.get( |
||||
reverse('users:management-password'), |
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED |
||||
) |
||||
assert student_client.get( |
||||
reverse('users:management-password'), |
||||
status=status.HTTP_403_FORBIDDEN |
||||
) |
||||
data = { |
||||
'email': student.email, |
||||
} |
||||
assert manager_client.post( |
||||
reverse('users:management-password'), |
||||
data=data, |
||||
status=status.HTTP_201_CREATED |
||||
) |
||||
test_user = get_object_or_404(get_user_model(), email=student.email) |
||||
assert not test_user.check_password('test') |
||||
assert mocked_send_mail.call_count == 1 |
||||
assert student_client.post( |
||||
reverse('users:management-password'), |
||||
data=data, |
||||
status=status.HTTP_403_FORBIDDEN |
||||
) |
||||
wrong_data = { |
||||
'email': 'no_user@example.com', |
||||
} |
||||
assert manager_client.post( |
||||
reverse('users:management-password'), |
||||
data=wrong_data, |
||||
status=status.HTTP_404_NOT_FOUND |
||||
) |
||||
wrong_email = { |
||||
'email': 'no_user@example', |
||||
} |
||||
assert manager_client.post( |
||||
reverse('users:management-password'), |
||||
data=wrong_email, |
||||
status=status.HTTP_404_NOT_FOUND |
||||
) |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
def test_generate_password_by_manager_for_not_active_student(manager_client, student_not_active): |
||||
""" |
||||
Test generate new password from admin area by manager for not active student |
||||
""" |
||||
|
||||
data = { |
||||
'email': student_not_active.email, |
||||
} |
||||
assert manager_client.post( |
||||
reverse('users:management-password'), |
||||
data=data, |
||||
status=status.HTTP_201_CREATED |
||||
) |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
class TestLogin: |
||||
def test_login_user(self, api_client, student): |
||||
""" |
||||
Test login user |
||||
""" |
||||
data = { |
||||
'email': student.email, |
||||
'password': USER_PASSWORD |
||||
} |
||||
assert api_client.post( |
||||
reverse('users:login'), |
||||
data=data, |
||||
status=status.HTTP_200_OK |
||||
) |
||||
|
||||
def test_login_user_wrong_password(self, api_client, student): |
||||
""" |
||||
Test login user with wrong password |
||||
""" |
||||
data = { |
||||
'email': student.email, |
||||
'password': USER_PASSWORD + '1' |
||||
} |
||||
assert api_client.post( |
||||
reverse('users:login'), |
||||
data=data, |
||||
status=status.HTTP_403_FORBIDDEN |
||||
) |
||||
|
||||
def test_login_user_wrong_user(self, api_client, student): |
||||
""" |
||||
Test login user with wrong password |
||||
""" |
||||
data = { |
||||
'email': student.email + '1', |
||||
'password': USER_PASSWORD |
||||
} |
||||
assert api_client.post( |
||||
reverse('users:login'), |
||||
data=data, |
||||
status=status.HTTP_404_NOT_FOUND |
||||
) |
||||
@ -0,0 +1,62 @@ |
||||
import uuid |
||||
|
||||
import factory |
||||
import factory.fuzzy |
||||
|
||||
from functools import partial |
||||
|
||||
from access.factories import UserFactory |
||||
|
||||
from access import groups |
||||
|
||||
IMAGE_URL = 'https://dummyimage.com/240x240/000/fff.png' |
||||
BIG_IMAGE_URL = 'https://dummyimage.com/1200x800/000/fff.png' |
||||
BIG_MOBILE_IMAGE_URL = 'https://dummyimage.com/600x400/000/fff.png' |
||||
|
||||
Faker = partial(factory.Faker, locale='ru_RU') |
||||
|
||||
|
||||
class CourseFactory(factory.django.DjangoModelFactory): |
||||
class Meta: |
||||
model = 'courses.Course' |
||||
|
||||
token = factory.LazyFunction(uuid.uuid4) |
||||
slug = factory.LazyAttribute(lambda x: factory.Faker('sentence'). |
||||
generate({'nb_words': 3}).replace(' ', '-').replace('.', '')) |
||||
title = factory.LazyAttribute(lambda x: Faker('sentence').generate({'nb_words': 4})) |
||||
description = factory.LazyAttribute( |
||||
lambda x: '\n'.join(Faker('paragraphs').generate({'nb': 15})) |
||||
) |
||||
direction = factory.fuzzy.FuzzyChoice(range(1, 5)) |
||||
public = True |
||||
image = IMAGE_URL |
||||
big_image = BIG_IMAGE_URL |
||||
big_mobile_image = BIG_MOBILE_IMAGE_URL |
||||
|
||||
@factory.lazy_attribute |
||||
def teacher_tokens(self): |
||||
teacher = UserFactory(groups=(groups.TEACHERS,)) |
||||
return [teacher.out_key] |
||||
|
||||
|
||||
class TopicFactory(factory.django.DjangoModelFactory): |
||||
class Meta: |
||||
model = 'courses.Topic' |
||||
|
||||
course = factory.SubFactory(CourseFactory) |
||||
title = factory.LazyAttribute(lambda x: Faker('sentence').generate({'nb_words': 4})) |
||||
description = factory.LazyAttribute( |
||||
lambda x: '\n'.join(Faker('paragraphs').generate({'nb': 15})) |
||||
) |
||||
sort = 0 |
||||
|
||||
|
||||
class LessonFactory(factory.django.DjangoModelFactory): |
||||
class Meta: |
||||
model = 'courses.Lesson' |
||||
|
||||
token = factory.LazyFunction(uuid.uuid4) |
||||
key = factory.LazyFunction(uuid.uuid4) |
||||
topic = factory.SubFactory(TopicFactory) |
||||
title = factory.LazyAttribute(lambda x: Faker('sentence').generate({'nb_words': 4})) |
||||
sort = 0 |
||||
@ -0,0 +1,18 @@ |
||||
import pytest |
||||
|
||||
from courses.factories import CourseFactory, LessonFactory, TopicFactory |
||||
|
||||
|
||||
@pytest.fixture |
||||
def course(): |
||||
""" |
||||
Create course |
||||
""" |
||||
_course = CourseFactory() |
||||
topic = TopicFactory( |
||||
course=_course |
||||
) |
||||
LessonFactory( |
||||
topic=topic |
||||
) |
||||
return _course |
||||
@ -0,0 +1,72 @@ |
||||
import uuid |
||||
|
||||
import factory |
||||
import factory.fuzzy |
||||
|
||||
from functools import partial |
||||
|
||||
from django.utils import timezone |
||||
from yandex_money.models import Payment |
||||
|
||||
from courses.factories import CourseFactory, TopicFactory, LessonFactory |
||||
from access.factories import UserFactory |
||||
from finance.models import Invoice |
||||
|
||||
Faker = partial(factory.Faker, locale='ru_RU') |
||||
|
||||
|
||||
class BillFactory(factory.django.DjangoModelFactory): |
||||
class Meta: |
||||
model = 'finance.Bill' |
||||
|
||||
user = factory.SubFactory(UserFactory) |
||||
opener = factory.SubFactory(UserFactory) |
||||
description = Faker('text') |
||||
|
||||
@factory.lazy_attribute |
||||
def course_token(self): |
||||
course = CourseFactory() |
||||
topic = TopicFactory( |
||||
course=course |
||||
) |
||||
LessonFactory( |
||||
topic=topic |
||||
) |
||||
return course.token |
||||
|
||||
|
||||
class InvoiceFactory(factory.django.DjangoModelFactory): |
||||
class Meta: |
||||
model = 'finance.Invoice' |
||||
|
||||
status = factory.fuzzy.FuzzyChoice( |
||||
(choice[0] for choice in Invoice.BILL_STATUSES) |
||||
) |
||||
price = factory.fuzzy.FuzzyDecimal(1000) |
||||
real_price = factory.fuzzy.FuzzyDecimal(1000) |
||||
method = factory.fuzzy.FuzzyChoice( |
||||
(choice[0] for choice in Invoice.BILL_METHOD) |
||||
) |
||||
key = factory.Faker('pyint') |
||||
yandex_pay = factory.SubFactory('factories.finance.PaymentFactory') |
||||
comment = Faker('word') |
||||
bill = factory.SubFactory(BillFactory) |
||||
|
||||
|
||||
class PaymentFactory(factory.django.DjangoModelFactory): |
||||
class Meta: |
||||
model = 'yandex_money.Payment' |
||||
|
||||
user = factory.SubFactory(UserFactory) |
||||
pub_date = timezone.now() |
||||
order_amount = factory.fuzzy.FuzzyDecimal(1000) |
||||
payment_type = factory.fuzzy.FuzzyChoice( |
||||
(choice[0] for choice in Payment.PAYMENT_TYPE.CHOICES) |
||||
) |
||||
order_number = factory.LazyFunction(uuid.uuid4) |
||||
status = factory.fuzzy.FuzzyChoice( |
||||
(choice[0] for choice in Payment.STATUS.CHOICES) |
||||
) |
||||
invoice_id = factory.Faker('pyint') |
||||
shop_amount = factory.fuzzy.FuzzyDecimal(1000) |
||||
performed_datetime = timezone.now() |
||||
@ -0,0 +1,58 @@ |
||||
import os |
||||
import shutil |
||||
|
||||
import pytest |
||||
|
||||
from tests.client import BetterAPIClient |
||||
|
||||
pytest_plugins = [ |
||||
'access.tests.fixtures', |
||||
'finance.tests.fixtures', |
||||
'courses.tests.fixtures' |
||||
] |
||||
|
||||
|
||||
def pytest_sessionfinish(session, exitstatus): |
||||
""" whole test run finishes. """ |
||||
print('pytest finish: cleanup') |
||||
if os.path.exists('media_qa'): |
||||
shutil.rmtree('media_qa') |
||||
|
||||
|
||||
@pytest.fixture |
||||
def api_client(): |
||||
"""Anonymous client for REST API.""" |
||||
client = BetterAPIClient() |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def admin_client(admin): |
||||
"""Authorized as admin(superuser) client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=admin) |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def student_client(student): |
||||
"""Authorized as student client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=student) |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def manager_client(manager): |
||||
"""Authorized as manager client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=manager) |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def lead_manager_client(lead_manager): |
||||
"""Authorized as lead manager client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=lead_manager) |
||||
return client |
||||
@ -0,0 +1,53 @@ |
||||
import pytest |
||||
from yandex_money.models import Payment |
||||
|
||||
from finance.factories import BillFactory, InvoiceFactory, PaymentFactory |
||||
|
||||
PRICE = 1000.00 |
||||
METHOD_CASH = 'C' |
||||
METHOD_YANDEX = 'Y' |
||||
STATUS_WAIT = 'W' |
||||
STATUS_ON_PAYMENT = 'P' |
||||
STATUS_PAID = 'F' |
||||
|
||||
|
||||
@pytest.fixture |
||||
def bill_cash(student, manager): |
||||
""" |
||||
Create bill for the student |
||||
""" |
||||
_bill = BillFactory( |
||||
user=student, |
||||
opener=manager, |
||||
) |
||||
InvoiceFactory( |
||||
status=STATUS_ON_PAYMENT, |
||||
method=METHOD_CASH, |
||||
yandex_pay=None, |
||||
bill=_bill |
||||
) |
||||
return _bill |
||||
|
||||
|
||||
@pytest.fixture |
||||
def bill_yandex(student, manager): |
||||
""" |
||||
Create bill for the student |
||||
""" |
||||
_bill = BillFactory( |
||||
user=student, |
||||
opener=manager, |
||||
) |
||||
# credit cart |
||||
payment = PaymentFactory( |
||||
user=student, |
||||
payment_type=Payment.PAYMENT_TYPE.AC, |
||||
status=Payment.STATUS.PROCESSED |
||||
) |
||||
InvoiceFactory( |
||||
status=STATUS_ON_PAYMENT, |
||||
method=METHOD_YANDEX, |
||||
yandex_pay=payment, |
||||
bill=_bill |
||||
) |
||||
return _bill |
||||
@ -0,0 +1,198 @@ |
||||
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 finance.tests.fixtures 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 == 2 |
||||
|
||||
|
||||
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 |
||||
@ -0,0 +1,21 @@ |
||||
import logging |
||||
from .settings import * # noqa |
||||
|
||||
DEBUG = True |
||||
EMAIL_DEBUG = True |
||||
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG # noqa |
||||
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_qa') # noqa |
||||
|
||||
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' |
||||
PASSWORD_HASHERS = ('django.contrib.auth.hashers.MD5PasswordHasher', ) |
||||
|
||||
CELERY_TASK_ALWAYS_EAGER = True |
||||
|
||||
# Disable cache during testing |
||||
CACHE_DISABLED = True |
||||
|
||||
LOGGING = {} |
||||
LOCAL_APPS_LOGGERS = {} |
||||
# Disable all logging calls with levels less severe than or equal to CRITICAL |
||||
logging.disable(logging.CRITICAL) |
||||
@ -1,28 +0,0 @@ |
||||
import pytest |
||||
from tests.client import BetterAPIClient |
||||
|
||||
pytest_plugins = [ |
||||
'tests.fixtures.users', |
||||
] |
||||
|
||||
@pytest.fixture |
||||
def api_client(): |
||||
"""Anonymous client for REST API.""" |
||||
client = BetterAPIClient() |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def staff_client(user_staff): |
||||
"""Authorized as staff client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=user_staff) |
||||
return client |
||||
|
||||
|
||||
@pytest.fixture |
||||
def student_client(user_student): |
||||
"""Authorized as staff client for REST API.""" |
||||
client = BetterAPIClient() |
||||
client.force_authenticate(user=user_student) |
||||
return client |
||||
@ -1,63 +0,0 @@ |
||||
import pytest |
||||
|
||||
from factories.users import UserFactory, AccountFactory |
||||
|
||||
|
||||
@pytest.fixture |
||||
def user_staff(): |
||||
""" |
||||
Create user as staff with data: |
||||
email = 'admin@example.com' |
||||
password = 'test' |
||||
is_staff=True |
||||
is_active = True |
||||
is_superuser = True |
||||
""" |
||||
admin = UserFactory( |
||||
last_name='Иванов', |
||||
first_name='Иван', |
||||
email='admin@example.com', |
||||
is_staff=True, |
||||
is_active=True, |
||||
is_superuser=True |
||||
) |
||||
AccountFactory(owner=admin) |
||||
return admin |
||||
|
||||
|
||||
@pytest.fixture |
||||
def user_student(): |
||||
""" |
||||
Create user as student with data: |
||||
email = 'student@example.com' |
||||
password = 'test' |
||||
is_active = True |
||||
""" |
||||
student = UserFactory( |
||||
last_name='Иванов', |
||||
first_name='Иван', |
||||
email='student@example.com', |
||||
is_staff=False, |
||||
is_active=True, |
||||
) |
||||
|
||||
AccountFactory(owner=student) |
||||
return student |
||||
|
||||
|
||||
@pytest.fixture |
||||
def user_not_active_student(): |
||||
""" |
||||
Create user as student with data: |
||||
email = 'notactivestudent@example.com' |
||||
password = 'test' |
||||
is_active = False |
||||
""" |
||||
student = UserFactory( |
||||
last_name='Иванов', |
||||
first_name='Иван', |
||||
email='notactivestudent@example.com', |
||||
is_staff=False, |
||||
is_active=False, |
||||
) |
||||
return student |
||||
@ -1,124 +0,0 @@ |
||||
import mock |
||||
import pytest |
||||
|
||||
from django.contrib.auth import get_user_model |
||||
from django.urls import reverse |
||||
|
||||
from rest_framework import status |
||||
from rest_framework.generics import get_object_or_404 |
||||
|
||||
from factories.users import USER_PASSWORD |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
@mock.patch('django.core.mail.EmailMessage.send') |
||||
def test_generate_password_by_manager(mocked_send_mail, staff_client, |
||||
student_client, user_student): |
||||
""" |
||||
Test generate new password from admin area by manager |
||||
""" |
||||
assert staff_client.get( |
||||
reverse('users:management-password'), |
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED |
||||
) |
||||
assert student_client.get( |
||||
reverse('users:management-password'), |
||||
status=status.HTTP_403_FORBIDDEN |
||||
) |
||||
data = { |
||||
'email': user_student.email, |
||||
} |
||||
assert staff_client.post( |
||||
reverse('users:management-password'), |
||||
data=data, |
||||
status=status.HTTP_201_CREATED |
||||
) |
||||
test_user = get_object_or_404(get_user_model(), email=user_student.email) |
||||
assert not test_user.check_password('test') |
||||
assert mocked_send_mail.call_count == 1 |
||||
assert student_client.post( |
||||
reverse('users:management-password'), |
||||
data=data, |
||||
status=status.HTTP_403_FORBIDDEN |
||||
) |
||||
wrong_data = { |
||||
'email': 'no_user@example.com', |
||||
} |
||||
assert staff_client.post( |
||||
reverse('users:management-password'), |
||||
data=wrong_data, |
||||
status=status.HTTP_404_NOT_FOUND |
||||
) |
||||
wrong_email = { |
||||
'email': 'no_user@example', |
||||
} |
||||
assert staff_client.post( |
||||
reverse('users:management-password'), |
||||
data=wrong_email, |
||||
status=status.HTTP_404_NOT_FOUND |
||||
) |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
def test_generate_password_by_manager_for_not_active_student(staff_client, |
||||
user_not_active_student): |
||||
""" |
||||
Test generate new password from admin area by manager for not active student |
||||
""" |
||||
|
||||
data = { |
||||
'email': user_not_active_student.email, |
||||
} |
||||
assert staff_client.post( |
||||
reverse('users:management-password'), |
||||
data=data, |
||||
status=status.HTTP_201_CREATED |
||||
) |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
def test_login_user(api_client, user_student): |
||||
""" |
||||
Test login user |
||||
""" |
||||
data = { |
||||
'email': user_student.email, |
||||
'password': USER_PASSWORD |
||||
} |
||||
assert api_client.post( |
||||
reverse('users:login'), |
||||
data=data, |
||||
status=status.HTTP_200_OK |
||||
) |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
def test_login_user_wrong_password(api_client, user_student): |
||||
""" |
||||
Test login user with wrong password |
||||
""" |
||||
data = { |
||||
'email': user_student.email, |
||||
'password': USER_PASSWORD + '1' |
||||
} |
||||
assert api_client.post( |
||||
reverse('users:login'), |
||||
data=data, |
||||
status=status.HTTP_403_FORBIDDEN |
||||
) |
||||
|
||||
|
||||
@pytest.mark.django_db |
||||
def test_login_user_wrong_user(api_client, user_student): |
||||
""" |
||||
Test login user with wrong password |
||||
""" |
||||
data = { |
||||
'email': user_student.email + '1', |
||||
'password': USER_PASSWORD |
||||
} |
||||
assert api_client.post( |
||||
reverse('users:login'), |
||||
data=data, |
||||
status=status.HTTP_404_NOT_FOUND |
||||
) |
||||
Loading…
Reference in new issue