def test_user_count(settings): """A basic test to execute the get_users_count Celery task.""" UserFactory.create_batch(3) settings.CELERY_TASK_ALWAYS_EAGER = True task_result = get_users_count.delay() assert isinstance(task_result, EagerResult) assert task_result.result == 3
def test_user_activation_view(self, mocker): # create test user # generate activation token # send uid + token using activation link test_user = UserFactory() test_token = default_token_generator.make_token(test_user) resp = self.client.get( f'/api/users/activation/{test_user.id}/{test_token}/') assert resp.status_code == 302 test_user.refresh_from_db() assert test_user.is_active
def test_purchased_items_search(self): test_user = UserFactory() test_vl = VideoLectureFactory(name='bar', description='bar') test_vl_oi = OrderItemVideoLectureFactory(customer=test_user, content_object=test_vl, is_paid=True) offline_lecture = OfflineLectureFactory(description="Footest") test_ticket = TicketFactory(offline_lecture=offline_lecture) test_ticket_oi = OrderItemTicketFactory(customer=test_user, content_object=test_ticket, is_paid=True) self.client.force_authenticate(user=test_user) resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/?filter[search]=foot' ) assert resp.status_code == 200 data = resp.json()['data'] assert len(data) == 1 assert data[0]['id'] == f'{test_ticket_oi.id}' resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/?filter[search]=toof' ) assert resp.status_code == 200 data = resp.json()['data'] assert len(data) == 0
def test_authenticated(self, user: User, rf: RequestFactory): request = rf.get("/fake-url/") request.user = UserFactory() response = user_detail_view(request, username=user.username) assert response.status_code == 200
def test_create_cart(self): client = APIClient() test_user = UserFactory() client.force_authenticate(user=test_user) test_order_item = OrderItemVideoLectureFactory() test_order_item1 = OrderItemVideoLectureFactory() test_data = { "data": { "type": "Cart", "attributes": { "order_items": [{ "type": "OrderItem", "id": f"{test_order_item.id}" }, { "type": "OrderItem", "id": f"{test_order_item1.id}" }], } } } response = client.post('/api/cart/', test_data) assert response.status_code == 201 assert response.json( )['data']['relationships']['order_items']['meta']['count'] == 2 assert response.json()['data']['relationships']['order_items']['data'][ 0]['id'] == f"{test_order_item.id}" assert response.json()['data']['relationships']['order_items']['data'][ 1]['id'] == f"{test_order_item1.id}"
def test_video_lectures_bullet_points_translated(): test_user = UserFactory(email='*****@*****.**', password='******') UserMembershipFactory(user=test_user) video_lecture = VideoLectureFactory() test_ru_text = "Какие то слова на русском" bullet_points = VideoLectureBulletPointFactory(video_lecture=video_lecture, text_ru=test_ru_text) # # Locale set to en-US client = APIClient() client.force_authenticate(user=test_user) response = client.get('/api/video-lectures/?include=bullet_points', None, **{"HTTP_ACCEPT_LANGUAGE": "en-US"}) assert response.status_code == 200 resp = response.json() assert resp['included'][0]['attributes']['text'] == bullet_points.text client.force_authenticate(user=None) # # # Locale set to ru-RU client = APIClient() client.force_authenticate(user=test_user) response = client.get('/api/video-lectures/?include=bullet_points', None, **{"HTTP_ACCEPT_LANGUAGE": "ru-RU"}) assert response.status_code == 200 resp = response.json() assert resp['included'][0]['attributes']['text'] == test_ru_text
def test_create_payment_for_cart(): payment_method = PaymentMethodFactory() test_user = UserFactory() offline_lecture = OfflineLectureFactory() test_cart = CartFactory() test_payment = link_payment_with_cart( payment_type=payment_method.payment_type, user=test_user, cart_id=test_cart.id) assert test_payment == Payment.objects.first() assert test_payment.cart == test_cart
def test_cart_total_vlecture_only(self): client = APIClient() test_user = UserFactory() test_cart = CartFactory() test_video_lecture = VideoLectureFactory() test_video_lecture_oi = OrderItemVideoLectureFactory( content_object=test_video_lecture) test_cart.order_items.add(test_video_lecture_oi) client.force_authenticate(user=test_user) response = client.get('/api/cart/') assert response.status_code == 200 cart_total = response.json()['data'][0]['attributes']['total'] assert cart_total == int(test_video_lecture.price.round(1).amount)
def test_create_subscription(self, mocker): payment_method = PaymentMethodFactory( payment_type=PaymentMethod.TYPE_YANDEX_CHECKOUT) test_user = UserFactory() test_url, external_id = ("https://foo.bar", "fff-ooo-bar") mocked_adaptor = mocker.patch.object(adaptor, 'charge', autospec=True, return_value=(test_url, external_id)) test_payment_type_value = payment_method.payment_type test_membership_type = Membership.TIER.PAID test_membership = MembershipFactory( membership_type=test_membership_type) test_amount = "100.00" test_currency = "RUB" client = APIClient() client.force_authenticate(user=test_user) test_data = { "data": { "type": "Subscription", "attributes": { "membership_type": test_membership_type, "payment_method": test_payment_type_value, } } } response = client.post('/api/payments/subscriptions/get-payment-link/', data=test_data) subscription = Subscription.objects.first() assert subscription.user_membership.membership == test_membership assert response.json() == { 'data': { 'type': 'Subscription', 'id': f'{subscription.id}', 'attributes': { 'membership_type': test_membership_type, 'payment_method': test_payment_type_value, 'payment_url': test_url } } } mocked_adaptor.assert_called_with( ANY, value=str(test_membership.price.round(2).amount), currency=test_currency, description=f"Subscription #{subscription.id}")
def test_offline_lecture_translated_fallback(): test_user = UserFactory(email='*****@*****.**', password='******') test_name_ru = '' test_name_en = 'Test Name' OfflineLectureFactory(name_ru=test_name_ru, name_en=test_name_en) # # Locale set to en-US client = APIClient() client.force_authenticate(user=test_user) # Russian name is not set, fallback to english. response = client.get('/api/offline-lectures/', None, **{"HTTP_ACCEPT_LANGUAGE": "ru-RU"}) assert response.data['results'][0]['name'] == test_name_en
def test_pay_for_cart(self, mocker): payment_method = PaymentMethodFactory( payment_type=PaymentMethod.TYPE_YANDEX_CHECKOUT) test_user = UserFactory() cart = cart_factories.CartFactory() test_order_item = OrderItemVideoLectureFactory() cart.order_items.add(test_order_item) test_url, external_id = ("https://foo.bar", "fff-ooo-bar") mocked_adaptor = mocker.patch.object(adaptor, 'charge', autospec=True, return_value=(test_url, external_id)) test_payment_type_value = payment_method.payment_type test_payment_for = "cart" test_amount = "100.00" test_currency = "RUB" factory = APIRequestFactory() view = PaymentCreateView.as_view() test_data = { "data": { "type": "PaymentCreateView", "attributes": { "payment_type_value": test_payment_type_value, "amount": test_amount, "currency": test_currency, "cart_id": cart.id, } } } request = factory.post('/api/payments/', data=test_data) force_authenticate(request, test_user) response = view(request) assert response.data == {"payment_url": test_url} payment = Payment.objects.first() payment_cart = payment.cart assert cart == payment_cart mocked_adaptor.assert_called_with(ANY, value=test_amount, currency=test_currency, description=f"Payment #{payment.id}")
def test_buy_one_ticket(self): client = APIClient() test_user = UserFactory() test_cart = CartFactory() test_ticket = TicketFactory() client.force_authenticate(user=test_user) test_data = { "data": { "type": "OrderItem", "attributes": { "customer_email": test_user.email, "item_type": 'ticket', "object_id": test_ticket.id, } } } response = client.post('/api/cart/buy-one/', data=test_data) assert response.status_code == 200 assert test_ticket == test_user.order_items.first().content_object
def test_edit_user_data(self): test_user = UserFactory() new_name = "Ivan Ivanov" new_birthdate = "2020-10-14" data = { "data": { "type": "User", "id": test_user.email, "attributes": { "name": new_name, "email": test_user.email, "birthdate": new_birthdate, } } } self.client.force_authenticate(user=test_user) resp = self.client.put(f'/api/users/{test_user.email}/', data=data) assert resp.status_code == 200 assert resp.data["birthdate"] == new_birthdate assert resp.data["name"] == new_name
def test_offline_lectures_translated(): test_user = UserFactory(email='*****@*****.**', password='******') test_name_ru = 'Тестовое Имя' test_name_en = 'Test Name' OfflineLectureFactory(name_ru=test_name_ru, name_en=test_name_en) # # Locale set to en-US client = APIClient() client.force_authenticate(user=test_user) response = client.get('/api/offline-lectures/', None, **{"HTTP_ACCEPT_LANGUAGE": "en-US"}) assert response.data['results'][0]['name'] == test_name_en client.force_authenticate(user=None) # # Locale set to ru-RU client = APIClient() client.force_authenticate(user=test_user) response = client.get('/api/offline-lectures/', **{"HTTP_ACCEPT_LANGUAGE": "ru-RU"}) assert response.data['results'][0]['name'] == test_name_ru
def test_raises_validation_error(self): factory = APIRequestFactory() view = PaymentCreateView.as_view() test_user = UserFactory() test_data = {"data": {"type": "PaymentCreateView", "attributes": {}}} request = factory.post('/api/payments/', data=test_data) force_authenticate(request, test_user) response = view(request) assert response.data == [{ 'detail': ErrorDetail(string='payment_type_value field is required', code='invalid'), 'status': '400', 'source': { 'pointer': '/data' }, 'code': 'invalid' }]
def test_course_lectures_translated(): test_user = UserFactory(email='*****@*****.**', password='******') UserMembershipFactory(user=test_user) test_name_ru = 'Тестовое Имя' test_name_en = 'Test Name' CourseLectureFactory(name_ru=test_name_ru, name_en=test_name_en) # # Locale set to en-US client = APIClient() client.force_authenticate(user=test_user) response = client.get('/api/videocourses/?include=lectures', None, **{"HTTP_ACCEPT_LANGUAGE": "en-US"}) assert response.json()['included'][0]['attributes']['name'] == test_name_en client.force_authenticate(user=None) # # Locale set to ru-RU client = APIClient() client.force_authenticate(user=test_user) response = client.get('/api/videocourses/?include=lectures', **{"HTTP_ACCEPT_LANGUAGE": "ru-RU"}) assert response.json()['included'][0]['attributes']['name'] == test_name_ru
def test_get_video_lecture_packages(): test_videolecture_package = VideoLecturePackageFactory() test_videolecture_1 = VideoLectureFactory() test_videolecture_2 = VideoLectureFactory() test_videolecture_package.videolectures.add(test_videolecture_1, test_videolecture_2) test_videolecture_package.save() client = APIClient() test_user = UserFactory() client.force_authenticate(user=test_user) response = client.get( f'/api/packages/videolectures/{test_videolecture_package.id}/') assert response.status_code == 200 assert response.json() == { 'data': { 'type': 'VideoLecturePackage', 'id': str(test_videolecture_package.id), 'attributes': { 'description': None, 'image': None, 'price': '1000.00', 'release_date': None }, 'relationships': { 'videolectures': { 'data': [{ 'type': 'VideoLecture', 'id': str(test_videolecture_1.id) }, { 'type': 'VideoLecture', 'id': str(test_videolecture_2.id) }] } } } }
def test_buy_videocourse_package(self): client = APIClient() test_user = UserFactory() test_cart = CartFactory() now = timezone.now() test_video_course = VideoCourseFactory(release_date=now) test_video_course1 = VideoCourseFactory(release_date=now) test_videocourse_package = VideoCoursePackageFactory() test_videocourse_package.videocourses.add(test_video_course, test_video_course1) test_videocourse_package.save() client.force_authenticate(user=test_user) test_data = { "data": { "type": "OrderItem", "attributes": { "customer_email": test_user.email, "item_type": 'videocoursepackage', "object_id": test_videocourse_package.id, } } } response = client.post('/api/cart/buy-one/', data=test_data) test_user_order_item = test_user.order_items.first() assert response.status_code == 200 assert test_videocourse_package == test_user_order_item.content_object test_user.order_items.update(is_paid=True) response = client.get(f'/api/users/{test_user.email}/purchased-items/') assert response.status_code == 200 assert response.json()['data'] == [{ 'type': 'OrderItem', 'id': str(test_user_order_item.id), 'attributes': { 'created_at': '2012-01-14T04:00:00+04:00', 'is_paid': True }, 'relationships': { 'content_object': { 'data': [{ 'allowed_memberships': [], 'category': { 'type': 'Category', 'id': str(test_video_course.category.id) }, 'course_type': None, 'course_parts': [], 'description': test_video_course.description, 'id': test_video_course.id, 'is_released': True, 'lecturer': { 'type': 'Lecturer', 'id': f'{test_video_course.lecturer.id}' }, 'lectures': [], 'name': test_video_course.name, 'parts': [], 'price': str(test_video_course.price.round(2).amount), 'price_currency': str(test_video_course.price.currency), 'release_date': '2012-01-14T04:00:00+04:00', 'type': 'VideoCourse' }, { 'allowed_memberships': [], 'category': { 'type': 'Category', 'id': str(test_video_course1.category.id) }, 'course_type': None, 'course_parts': [], 'description': test_video_course1.description, 'id': test_video_course1.id, 'is_released': True, 'lecturer': { 'type': 'Lecturer', 'id': f'{test_video_course1.lecturer.id}' }, 'lectures': [], 'name': test_video_course1.name, 'parts': [], 'price': str(test_video_course1.price.round(2).amount), 'price_currency': str(test_video_course1.price.currency), 'release_date': '2012-01-14T04:00:00+04:00', 'type': 'VideoCourse' }] } } }]
def test_user_purchased_items_endpoint(self): test_user = UserFactory() test_vl_oi = OrderItemVideoLectureFactory(customer=test_user, is_paid=True) test_ticket_oi = OrderItemTicketFactory(customer=test_user, is_paid=True) test_vl = test_vl_oi.content_object test_ticket = test_ticket_oi.content_object test_ticket.offline_lecture.lecture_date = timezone.now() test_ticket.offline_lecture.save() test_data = [{ 'type': 'OrderItem', 'id': str(test_vl_oi.id), 'attributes': { 'created_at': '2012-01-14T04:00:00+04:00', 'is_paid': True }, 'relationships': { 'content_object': { 'data': { 'id': test_vl.id, 'vimeo_video_id': test_vl.vimeo_video_id, 'certificate': { 'type': 'VideoLectureCertificate', 'id': str(test_vl.certificate.id) }, 'name': test_vl.name, 'picture': test_vl.picture.url, 'lecturer': { 'name': test_vl.lecturer.name, 'userpic': None, 'bio': test_vl.lecturer.bio, }, 'category': { 'type': 'Category', 'id': str(test_vl.category.id) }, 'description': '', 'price': str(test_vl.price.round(2).amount), 'price_currency': 'RUB', 'bullet_points': [], 'allowed_memberships': [], 'tests': [], 'type': 'VideoLecture', } } } }, { 'type': 'OrderItem', 'id': str(test_ticket_oi.id), 'attributes': { 'created_at': '2012-01-14T04:00:00+04:00', 'is_paid': True }, 'relationships': { 'content_object': { 'data': { 'id': str(test_ticket.id), 'customer_email': test_ticket.customer.email, 'activation_link': f'https://afi-backend.herokuapp.com/api/tickets/activate/{test_ticket.qrcode.code}', 'scanned': test_ticket.scanned, 'offline_lecture_id': str(test_ticket.offline_lecture.id), 'offline_lecture': { 'name': test_ticket.offline_lecture.name, 'address': test_ticket.offline_lecture.address, 'picture': test_ticket.offline_lecture.picture.url, 'lecture_date': '2012-01-14T04:00:00+04:00', 'lecture_date_ts': test_ticket.offline_lecture.lecture_date_ts(), 'lecturer': { 'type': 'Lecturer', 'id': f'{test_ticket.offline_lecture.lecturer.id}', }, 'category': { 'type': 'Category', 'id': f'{test_ticket.offline_lecture.category.id}' }, 'capacity': None, 'description': '', 'tickets_sold': 0, 'lecture_summary_file': None, 'price': '1000.00', 'price_currency': 'RUB' }, 'type': 'Ticket' } } } }] self.client.force_authenticate(user=test_user) resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/') assert resp.status_code == 200 assert test_data == resp.json()['data']
def test_case_sensitivity(self, rf: RequestFactory): request = rf.get("/fake-url/") request.user = UserFactory(username="******") with pytest.raises(Http404): user_detail_view(request, username="******")
def user() -> User: return UserFactory()
def test_progress_created_on_exam_creation(): tu = UserFactory() ta = MyTestAssignmentVideoLectureFactory() exam = Exam.objects.create(user=tu, test_assignment=ta) assert exam.progress == Progress.objects.last()
def test_purchased_items_filter(self): test_user = UserFactory() test_vl_oi = OrderItemVideoLectureFactory(customer=test_user, is_paid=True) test_ticket_oi = OrderItemTicketFactory(customer=test_user, is_paid=True) test_vl = test_vl_oi.content_object test_ticket = test_ticket_oi.content_object test_videocourse_oi = OrderItemVideoCourseFactory(customer=test_user, is_paid=True) test_videocourse = test_videocourse_oi.content_object self.client.force_authenticate(user=test_user) resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/?filter[item_type]=videolecture' ) assert resp.status_code == 200 data = resp.json()['data'] assert len(data) == 1 assert data[0]['relationships']['content_object']['data'][ 'type'] == 'VideoLecture' resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/?filter[item_type]=ticket' ) assert resp.status_code == 200 data = resp.json()['data'] assert len(data) == 1 assert data[0]['relationships']['content_object']['data'][ 'type'] == 'Ticket' resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/?filter[item_type]=videocourse' ) assert resp.status_code == 200 data = resp.json()['data'] assert len(data) == 1 assert data[0]['relationships']['content_object']['data'][ 'type'] == 'VideoCourse' test_vl_oi1 = OrderItemVideoLectureFactory(customer=test_user, is_paid=True) resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/?filter[item_type]=videolecture&filter[lecturer.id]={test_vl.lecturer.id}' ) assert resp.status_code == 200 data = resp.json()['data'] assert len(data) == 1 assert data[0]['relationships']['content_object']['data']['lecturer'][ 'name'] == test_vl.lecturer.name test_vc_oi1 = OrderItemVideoCourseFactory(customer=test_user, is_paid=True) resp = self.client.get( f'/api/users/{test_user.email}/purchased-items/?filter[item_type]=videocourse&filter[lecturer.id]={test_videocourse.lecturer.id}' ) assert resp.status_code == 200 data = resp.json()['data'] assert len(data) == 1 assert data[0]['relationships']['content_object']['data']['lecturer'][ 'id'] == f'{test_videocourse.lecturer.id}'
def test_cart_payment(self): # Add video lecture, ticket to Cart and check all logic is executed. factory = APIRequestFactory() view = YandexWebhook.as_view() test_external_id = '27068194-000f-5000-9000-1a8dcaefa7ee' test_user = UserFactory() test_order_item_video_lecture = OrderItemVideoLectureFactory( customer=test_user) test_order_item_ticket = cart_factories.OrderItemTicketFactory( customer=test_user) test_order_item_videocourse = cart_factories.OrderItemVideoCourseFactory( customer=test_user) cart = cart_factories.CartFactory.create(order_items=( test_order_item_video_lecture, test_order_item_ticket, test_order_item_videocourse, )) payment = PaymentFactory(external_id=test_external_id, cart=cart) test_data = { 'type': 'notification', 'event': 'payment.succeeded', 'object': { 'id': test_external_id, 'status': 'succeeded', 'paid': True, 'amount': { 'value': '8888.00', 'currency': 'RUB' }, 'authorization_details': { 'rrn': '629887981691', 'auth_code': '980113' }, 'captured_at': '2020-09-30T11:37:08.748Z', 'created_at': '2020-09-30T11:36:52.215Z', 'description': 'Payment #8404672d-356a-4532-8901-083e411f9a2c', 'metadata': { 'scid': '1056615' }, 'payment_method': { 'type': 'bank_card', 'id': '27068194-000f-5000-9000-1a8dcaefa7ee', 'saved': False, 'card': { 'first6': '555555', 'last4': '4444', 'expiry_month': '12', 'expiry_year': '2022', 'card_type': 'MasterCard', 'issuer_country': 'US' }, 'title': 'Bank card *4444' }, 'recipient': { 'account_id': '623083', 'gateway_id': '1603067' }, 'refundable': True, 'refunded_amount': { 'value': '0.00', 'currency': 'RUB' }, 'test': True } } assert payment.status == Payment.STATUS.PENDING assert not test_order_item_ticket.is_paid request = factory.post('/api/checkout-wh/', data=test_data, format='json') response = view(request) # assert Payment is paid, do afterpayment logic is called payment.refresh_from_db() test_order_item_ticket.refresh_from_db() test_order_item_video_lecture.refresh_from_db() test_order_item_videocourse.refresh_from_db() assert payment.status == Payment.STATUS.PAID assert test_order_item_ticket.is_paid assert test_order_item_video_lecture.is_paid assert test_order_item_videocourse.is_paid # ticket: assert code is generated, but not generated before Payment is confirmed # video lecture: assert video lecture is avalaible for user, but not available before. assert test_order_item_ticket.content_object.qrcode assert test_order_item_ticket in test_user.order_items.filter( is_paid=True) assert test_order_item_video_lecture in test_user.order_items.filter( is_paid=True) assert test_order_item_videocourse in test_user.order_items.filter( is_paid=True)