""" Tests for commerce views. """ import json import ddt import mock from django.urls import reverse from course_modes.models import CourseMode from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme from student.models import CourseEnrollment from student.tests.factories import UserFactory from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory class UserMixin(object): """ Mixin for tests involving users. """ def setUp(self): super(UserMixin, self).setUp() self.user = UserFactory() def _login(self): """ Log into LMS. """ self.client.login(username=self.user.username, password='test') @ddt.ddt class ReceiptViewTests(UserMixin, ModuleStoreTestCase): """ Tests for the receipt view. """ def setUp(self): """ Add a user and a course """ super(ReceiptViewTests, self).setUp() self.user = UserFactory() self.client.login(username=self.user.username, password='test') self.course = CourseFactory.create( org='edX', course='900', run='test_run' ) def test_login_required(self): """ The view should redirect to the login page if the user is not logged in. """ self.client.logout() response = self.client.post(reverse('commerce:checkout_receipt')) self.assertEqual(response.status_code, 302) def post_to_receipt_page(self, post_data): """ DRY helper """ response = self.client.post(reverse('commerce:checkout_receipt'), params={'basket_id': 1}, data=post_data) self.assertEqual(response.status_code, 200) return response def test_user_verification_status_success(self): """ Test user verification status. If the user enrollment for the course belongs to verified modes e.g. Verified, Professional then verification is required. """ # Enroll as verified in the course with the current user. CourseEnrollment.enroll(self.user, self.course.id, mode=CourseMode.VERIFIED) response = self.client.get( reverse('commerce:user_verification_status'), data={'course_id': str(self.course.id)} ) json_data = json.loads(response.content.decode('utf-8')) self.assertEqual(json_data['is_verification_required'], True) # Enroll as honor in the course with the current user. CourseEnrollment.enroll(self.user, self.course.id, mode=CourseMode.HONOR) response = self.client.get( reverse('commerce:user_verification_status'), data={'course_id': str(self.course.id)} ) json_data = json.loads(response.content.decode('utf-8')) self.assertEqual(json_data['is_verification_required'], False) def test_user_verification_status_failure(self): """ Test user verification status failure. View should required HttpResponseBadRequest 400 if course id is missing. """ response = self.client.get(reverse('commerce:user_verification_status')) self.assertEqual(response.status_code, 400) @ddt.data('decision', 'reason_code', 'signed_field_names', None) def test_is_cybersource(self, post_key): """ Ensure the view uses three specific POST keys to detect a request initiated by Cybersource. """ self._login() post_data = {'decision': 'REJECT', 'reason_code': '200', 'signed_field_names': 'dummy'} if post_key is not None: # a key will be missing; we will not expect the receipt page to handle a cybersource decision del post_data[post_key] expected_pattern = r"