def test_reg_code_with_multiple_courses_and_checkout_scenario(self): self.add_reg_code(self.course_key) # Two courses in user shopping cart self.login_user() PaidCourseRegistration.add_to_order(self.cart, self.course_key) PaidCourseRegistration.add_to_order(self.cart, self.testing_course.id) self.assertEquals(self.cart.orderitem_set.count(), 2) resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.reg_code}) self.assertEqual(resp.status_code, 200) resp = self.client.get(reverse('shoppingcart.views.show_cart', args=[])) self.assertIn('Check Out', resp.content) self.cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') resp = self.client.get(reverse('shoppingcart.views.show_receipt', args=[self.cart.id])) self.assertEqual(resp.status_code, 200) ((template, context), _) = render_mock.call_args # pylint: disable=W0621 self.assertEqual(template, 'shoppingcart/receipt.html') self.assertEqual(context['order'], self.cart) self.assertEqual(context['order'].total_cost, self.testing_cost) course_enrollment = CourseEnrollment.objects.filter(user=self.user) self.assertEqual(course_enrollment.count(), 2)
def test_clear_cart(self): self.login_user() PaidCourseRegistration.add_to_order(self.cart, self.course_key) CertificateItem.add_to_order(self.cart, self.verified_course_key, self.cost, 'honor') self.assertEquals(self.cart.orderitem_set.count(), 2) resp = self.client.post(reverse('shoppingcart.views.clear_cart', args=[])) self.assertEqual(resp.status_code, 200) self.assertEquals(self.cart.orderitem_set.count(), 0)
def add_to_cart(self): """ Adds content to self.user's cart """ course = CourseFactory.create(org='MITx', number='999', display_name='Robot Super Course') CourseModeFactory(course_id=course.id) cart = Order.get_cart_for_user(self.user) PaidCourseRegistration.add_to_order(cart, course.id)
def add_to_cart(self): """ Adds content to self.user's cart """ course = CourseFactory.create(org='MITx', number='999', display_name='Robot Super Course') CourseModeFactory.create(course_id=course.id) cart = Order.get_cart_for_user(self.user) PaidCourseRegistration.add_to_order(cart, course.id)
def test_user_cart_has_both_items(self): """ This test exists b/c having both CertificateItem and PaidCourseRegistration in an order used to break PaidCourseRegistration.contained_in_order """ cart = Order.get_cart_for_user(self.user) CertificateItem.add_to_order(cart, self.course_key, self.cost, 'honor') PaidCourseRegistration.add_to_order(self.cart, self.course_key) self.assertTrue(PaidCourseRegistration.contained_in_order(cart, self.course_key))
def test_report_csv(self): PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cart.purchase() self.login_user() self.add_to_download_group(self.user) response = self.client.post(reverse('payment_csv_report'), {'start_date': '1970-01-01', 'end_date': '2100-01-01'}) self.assertEqual(response['Content-Type'], 'text/csv') self.assertIn(",".join(OrderItem.csv_report_header_row()), response.content) self.assertIn(self.CORRECT_CSV_NO_DATE, response.content)
def test_add_course_to_cart_already_in_cart(self): PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.login_user() resp = self.client.post( reverse('shoppingcart.views.add_course_to_cart', args=[self.course_id])) self.assertEqual(resp.status_code, 400) self.assertIn( _('The course {0} is already in your cart.'.format( self.course_id)), resp.content)
def test_add_course_to_cart_already_in_cart(self): PaidCourseRegistration.add_to_order(self.cart, self.course_key) self.login_user() resp = self.client.post( reverse("shoppingcart.views.add_course_to_cart", args=[self.course_key.to_deprecated_string()]) ) self.assertEqual(resp.status_code, 400) self.assertIn( "The course {0} is already in your cart.".format(self.course_key.to_deprecated_string()), resp.content )
def change_enrollment(strategy, user=None, *args, **kwargs): """Enroll a user in a course. If a user entered the authentication flow when trying to enroll in a course, then attempt to enroll the user. We will try to do this if the pipeline was started with the querystring param `enroll_course_id`. In the following cases, we can't enroll the user: * The course does not have an honor mode. * The course has an honor mode with a minimum price. * The course is not yet open for enrollment. * The course does not exist. If we can't enroll the user now, then skip this step. For paid courses, users will be redirected to the payment flow upon completion of the authentication pipeline (configured using the ?next parameter to the third party auth login url). """ enroll_course_id = strategy.session_get('enroll_course_id') if enroll_course_id: course_id = CourseKey.from_string(enroll_course_id) modes = CourseMode.modes_for_course_dict(course_id) # If the email opt in parameter is found, set the preference. email_opt_in = strategy.session_get(AUTH_EMAIL_OPT_IN_KEY) if email_opt_in: opt_in = email_opt_in.lower() == 'true' profile.update_email_opt_in(user.username, course_id.org, opt_in) if CourseMode.can_auto_enroll(course_id, modes_dict=modes): try: CourseEnrollment.enroll(user, course_id, check_access=True) except CourseEnrollmentException: pass except Exception as ex: logger.exception(ex) # Handle white-label courses as a special case # If a course is white-label, we should add it to the shopping cart. elif CourseMode.is_white_label(course_id, modes_dict=modes): try: cart = Order.get_cart_for_user(user) PaidCourseRegistration.add_to_order(cart, course_id) except ( CourseDoesNotExistException, ItemAlreadyInCartException, AlreadyEnrolledInCourseException ): pass # It's more important to complete login than to # ensure that the course was added to the shopping cart. # Log errors, but don't stop the authentication pipeline. except Exception as ex: logger.exception(ex)
def test_report_csv(self): PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cart.purchase() self.login_user() self.add_to_download_group(self.user) response = self.client.post(reverse('payment_csv_report'), { 'start_date': '1970-01-01', 'end_date': '2100-01-01' }) self.assertEqual(response['Content-Type'], 'text/csv') self.assertIn(",".join(OrderItem.csv_report_header_row()), response.content) self.assertIn(self.CORRECT_CSV_NO_DATE, response.content)
def test_report_csv_too_long(self): PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cart.purchase() self.login_user() self.add_to_download_group(self.user) response = self.client.post(reverse('payment_csv_report'), {'start_date': '1970-01-01', 'end_date': '2100-01-01'}) ((template, context), unused_kwargs) = render_mock.call_args self.assertEqual(template, 'shoppingcart/download_report.html') self.assertTrue(context['total_count_error']) self.assertFalse(context['date_fmt_error']) self.assertIn(_("There are too many results in your report.") + " (>0)", response.content)
def change_enrollment(strategy, user=None, *args, **kwargs): """Enroll a user in a course. If a user entered the authentication flow when trying to enroll in a course, then attempt to enroll the user. We will try to do this if the pipeline was started with the querystring param `enroll_course_id`. In the following cases, we can't enroll the user: * The course does not have an honor mode. * The course has an honor mode with a minimum price. * The course is not yet open for enrollment. * The course does not exist. If we can't enroll the user now, then skip this step. For paid courses, users will be redirected to the payment flow upon completion of the authentication pipeline (configured using the ?next parameter to the third party auth login url). """ enroll_course_id = strategy.session_get('enroll_course_id') if enroll_course_id: course_id = CourseKey.from_string(enroll_course_id) modes = CourseMode.modes_for_course_dict(course_id) # If the email opt in parameter is found, set the preference. email_opt_in = strategy.session_get(AUTH_EMAIL_OPT_IN_KEY) if email_opt_in: opt_in = email_opt_in.lower() == 'true' profile.update_email_opt_in(user.username, course_id.org, opt_in) if CourseMode.can_auto_enroll(course_id, modes_dict=modes): try: CourseEnrollment.enroll(user, course_id, check_access=True) except CourseEnrollmentException: pass except Exception as ex: logger.exception(ex) # Handle white-label courses as a special case # If a course is white-label, we should add it to the shopping cart. elif CourseMode.is_white_label(course_id, modes_dict=modes): try: cart = Order.get_cart_for_user(user) PaidCourseRegistration.add_to_order(cart, course_id) except (CourseDoesNotExistException, ItemAlreadyInCartException, AlreadyEnrolledInCourseException): pass # It's more important to complete login than to # ensure that the course was added to the shopping cart. # Log errors, but don't stop the authentication pipeline. except Exception as ex: logger.exception(ex)
def test_already_in_cart(self): """ This makes sure if a user has this course in the cart, that the expected message appears """ self.setup_user() cart = Order.get_cart_for_user(self.user) PaidCourseRegistration.add_to_order(cart, self.course.id) url = reverse('about_course', args=[text_type(self.course.id)]) resp = self.client.get(url) self.assertEqual(resp.status_code, 200) self.assertIn("This course is in your", resp.content) self.assertNotIn("Add buyme to Cart <span>($10 USD)</span>", resp.content)
def test_already_in_cart(self): """ This makes sure if a user has this course in the cart, that the expected message appears """ self.setup_user() cart = Order.get_cart_for_user(self.user) PaidCourseRegistration.add_to_order(cart, self.course.id) url = reverse('about_course', args=[self.course.id.to_deprecated_string()]) resp = self.client.get(url) self.assertEqual(resp.status_code, 200) self.assertIn("This course is in your", resp.content) self.assertNotIn("Add buyme to Cart ($10)", resp.content)
def test_report_csv_itemized(self): report_type = 'itemized_purchase_report' start_date = '1970-01-01' end_date = '2100-01-01' PaidCourseRegistration.add_to_order(self.cart, self.course_key) self.cart.purchase() self.login_user() self.add_to_download_group(self.user) response = self.client.post(reverse('payment_csv_report'), {'start_date': start_date, 'end_date': end_date, 'requested_report': report_type}) self.assertEqual(response['Content-Type'], 'text/csv') report = initialize_report(report_type, start_date, end_date) self.assertIn(",".join(report.header()), response.content) self.assertIn(self.CORRECT_CSV_NO_DATE_ITEMIZED_PURCHASE, response.content)
def test_purchased_callback_exception(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key) reg1.course_id = CourseLocator(org="changed", course="forsome", run="reason") reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse( CourseEnrollment.is_enrolled(self.user, self.course_key)) reg1.course_id = CourseLocator(org="abc", course="efg", run="hij") reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse( CourseEnrollment.is_enrolled(self.user, self.course_key)) course_reg_code_item = CourseRegCodeItem.add_to_order( self.cart, self.course_key, 2) course_reg_code_item.course_id = CourseLocator(org="changed1", course="forsome1", run="reason1") course_reg_code_item.save() with self.assertRaises(PurchasedCallbackException): course_reg_code_item.purchased_callback()
def test_add_with_default_mode(self): """ Tests add_to_cart where the mode specified in the argument is NOT in the database and NOT the default "honor". In this case it just adds the user in the CourseMode.DEFAULT_MODE, 0 price """ reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key, mode_slug="DNE") self.assertEqual(reg1.unit_cost, 0) self.assertEqual(reg1.line_cost, 0) self.assertEqual(reg1.mode, "honor") self.assertEqual(reg1.user, self.user) self.assertEqual(reg1.status, "cart") self.assertEqual(self.cart.total_cost, 0) self.assertTrue( PaidCourseRegistration.contained_in_order(self.cart, self.course_key)) course_reg_code_item = CourseRegCodeItem.add_to_order(self.cart, self.course_key, 2, mode_slug="DNE") self.assertEqual(course_reg_code_item.unit_cost, 0) self.assertEqual(course_reg_code_item.line_cost, 0) self.assertEqual(course_reg_code_item.mode, "honor") self.assertEqual(course_reg_code_item.user, self.user) self.assertEqual(course_reg_code_item.status, "cart") self.assertEqual(self.cart.total_cost, 0) self.assertTrue( CourseRegCodeItem.contained_in_order(self.cart, self.course_key))
def test_student_paid_course_enrollment_report(self): """ test to check the paid user enrollment csv report status and enrollment source. """ student = UserFactory() student_cart = Order.get_cart_for_user(student) PaidCourseRegistration.add_to_order(student_cart, self.course.id) student_cart.purchase() task_input = {'features': []} with patch('instructor_task.tasks_helper._get_current_task'): result = upload_enrollment_report(None, None, self.course.id, task_input, 'generating_enrollment_report') self.assertDictContainsSubset({'attempted': 1, 'succeeded': 1, 'failed': 0}, result) self._verify_cell_data_in_csv(student.username, 'Enrollment Source', 'Credit Card - Individual') self._verify_cell_data_in_csv(student.username, 'Payment Status', 'purchased')
def test_student_used_enrollment_code_for_course_enrollment(self): """ test to check the user enrollment source and payment status in the enrollment detailed report """ student = UserFactory() self.client.login(username=student.username, password='******') student_cart = Order.get_cart_for_user(student) paid_course_reg_item = PaidCourseRegistration.add_to_order(student_cart, self.course.id) # update the quantity of the cart item paid_course_reg_item resp = self.client.post(reverse('shoppingcart.views.update_user_cart'), {'ItemId': paid_course_reg_item.id, 'qty': '4'}) self.assertEqual(resp.status_code, 200) student_cart.purchase() course_reg_codes = CourseRegistrationCode.objects.filter(order=student_cart) redeem_url = reverse('register_code_redemption', args=[course_reg_codes[0].code]) response = self.client.get(redeem_url) self.assertEquals(response.status_code, 200) # check button text self.assertTrue('Activate Course Enrollment' in response.content) response = self.client.post(redeem_url) self.assertEquals(response.status_code, 200) task_input = {'features': []} with patch('instructor_task.tasks_helper._get_current_task'): result = upload_enrollment_report(None, None, self.course.id, task_input, 'generating_enrollment_report') self.assertDictContainsSubset({'attempted': 1, 'succeeded': 1, 'failed': 0}, result) self._verify_cell_data_in_csv(student.username, 'Enrollment Source', 'Used Registration Code') self._verify_cell_data_in_csv(student.username, 'Payment Status', 'purchased')
def test_report_csv_itemized(self): report_type = "itemized_purchase_report" start_date = "1970-01-01" end_date = "2100-01-01" PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cart.purchase() self.login_user() self.add_to_download_group(self.user) response = self.client.post( reverse("payment_csv_report"), {"start_date": start_date, "end_date": end_date, "requested_report": report_type}, ) self.assertEqual(response["Content-Type"], "text/csv") report = initialize_report(report_type, start_date, end_date) self.assertIn(",".join(report.header()), response.content) self.assertIn(self.CORRECT_CSV_NO_DATE_ITEMIZED_PURCHASE, response.content)
def setUp(self): self.user = UserFactory.create() self.course_id = "MITx/999/Robot_Super_Course" self.cost = 40 self.course = CourseFactory.create(org='MITx', number='999', display_name=u'Robot Super Course') course_mode = CourseMode(course_id=self.course_id, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost) course_mode.save() course_mode2 = CourseMode(course_id=self.course_id, mode_slug="verified", mode_display_name="verified cert", min_price=self.cost) course_mode2.save() self.annotation = PaidCourseRegistrationAnnotation( course_id=self.course_id, annotation=self.TEST_ANNOTATION) self.annotation.save() self.cart = Order.get_cart_for_user(self.user) self.reg = PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cert_item = CertificateItem.add_to_order(self.cart, self.course_id, self.cost, 'verified') self.cart.purchase() self.now = datetime.datetime.now(pytz.UTC)
def test_show_receipt_404s(self): PaidCourseRegistration.add_to_order(self.cart, self.course_key) CertificateItem.add_to_order(self.cart, self.verified_course_key, self.cost, 'honor') self.cart.purchase() user2 = UserFactory.create() cart2 = Order.get_cart_for_user(user2) PaidCourseRegistration.add_to_order(cart2, self.course_key) cart2.purchase() self.login_user() resp = self.client.get(reverse('shoppingcart.views.show_receipt', args=[cart2.id])) self.assertEqual(resp.status_code, 404) resp2 = self.client.get(reverse('shoppingcart.views.show_receipt', args=[1000])) self.assertEqual(resp2.status_code, 404)
def add_course_to_user_cart(self, course_key): """ adding course to user cart """ self.login_user() reg_item = PaidCourseRegistration.add_to_order(self.cart, course_key) return reg_item
def add_course_to_user_cart(self): """ adding course to user cart """ self.login_user() reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_key) return reg_item
def test_remove_item(self, exception_log): self.login_user() reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_id) cert_item = CertificateItem.add_to_order(self.cart, self.verified_course_id, self.cost, 'honor') self.assertEquals(self.cart.orderitem_set.count(), 2) resp = self.client.post( reverse('shoppingcart.views.remove_item', args=[]), {'id': reg_item.id}) self.assertEqual(resp.status_code, 200) self.assertEquals(self.cart.orderitem_set.count(), 1) self.assertNotIn(reg_item, self.cart.orderitem_set.all().select_subclasses()) self.cart.purchase() resp2 = self.client.post( reverse('shoppingcart.views.remove_item', args=[]), {'id': cert_item.id}) self.assertEqual(resp2.status_code, 200) exception_log.assert_called_with( 'Cannot remove cart OrderItem id={0}. DoesNotExist or item is already purchased' .format(cert_item.id)) resp3 = self.client.post( reverse('shoppingcart.views.remove_item', args=[]), {'id': -1}) self.assertEqual(resp3.status_code, 200) exception_log.assert_called_with( 'Cannot remove cart OrderItem id={0}. DoesNotExist or item is already purchased' .format(-1))
def test_report_csv_too_long(self): PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cart.purchase() self.login_user() self.add_to_download_group(self.user) response = self.client.post(reverse('payment_csv_report'), { 'start_date': '1970-01-01', 'end_date': '2100-01-01' }) ((template, context), unused_kwargs) = render_mock.call_args self.assertEqual(template, 'shoppingcart/download_report.html') self.assertTrue(context['total_count_error']) self.assertFalse(context['date_fmt_error']) self.assertIn( _("There are too many results in your report.") + " (>0)", response.content)
def test_purchased_callback(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key) self.cart.purchase() self.assertTrue(CourseEnrollment.is_enrolled(self.user, self.course_key)) reg1 = PaidCourseRegistration.objects.get(id=reg1.id) # reload from DB to get side-effect self.assertEqual(reg1.status, "purchased") self.assertIsNotNone(reg1.course_enrollment) self.assertEqual(reg1.course_enrollment.id, CourseEnrollment.objects.get(user=self.user, course_id=self.course_key).id)
def test_purchased_callback(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cart.purchase() self.assertTrue(CourseEnrollment.is_enrolled(self.user, self.course_id)) reg1 = PaidCourseRegistration.objects.get( id=reg1.id) # reload from DB to get side-effect self.assertEqual(reg1.status, "purchased")
def test_generate_receipt_instructions(self): """ Add 2 courses to the order and make sure the instruction_set only contains 1 element (no dups) """ course2 = CourseFactory.create(org="MITx", number="998", display_name="Robot Duper Course") course_mode2 = CourseMode( course_id=course2.id, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost ) course_mode2.save() pr1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key) pr2 = PaidCourseRegistration.add_to_order(self.cart, course2.id) self.cart.purchase() inst_dict, inst_set = self.cart.generate_receipt_instructions() self.assertEqual(2, len(inst_dict)) self.assertEqual(1, len(inst_set)) self.assertIn("dashboard", inst_set.pop()) self.assertIn(pr1.pk_with_subclass, inst_dict) self.assertIn(pr2.pk_with_subclass, inst_dict)
def test_generate_receipt_instructions(self): """ Add 2 courses to the order and make sure the instruction_set only contains 1 element (no dups) """ course2 = CourseFactory.create() course_mode2 = CourseMode(course_id=course2.id, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost) course_mode2.save() pr1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key) pr2 = PaidCourseRegistration.add_to_order(self.cart, course2.id) self.cart.purchase() inst_dict, inst_set = self.cart.generate_receipt_instructions() self.assertEqual(2, len(inst_dict)) self.assertEqual(1, len(inst_set)) self.assertIn("dashboard", inst_set.pop()) self.assertIn(pr1.pk_with_subclass, inst_dict) self.assertIn(pr2.pk_with_subclass, inst_dict)
def test_total_amount_of_purchased_course(self): self.add_course_to_user_cart(self.course_key) self.assertEquals(self.cart.orderitem_set.count(), 1) self.add_coupon(self.course_key, True, self.coupon_code) resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.coupon_code}) self.assertEqual(resp.status_code, 200) self.cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') # Total amount of a particular course that is purchased by different users total_amount = PaidCourseRegistration.get_total_amount_of_purchased_item(self.course_key) self.assertEqual(total_amount, 36) self.client.login(username=self.instructor.username, password="******") cart = Order.get_cart_for_user(self.instructor) PaidCourseRegistration.add_to_order(cart, self.course_key) cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') total_amount = PaidCourseRegistration.get_total_amount_of_purchased_item(self.course_key) self.assertEqual(total_amount, 76)
def test_total_amount_of_purchased_course(self): self.add_course_to_user_cart() self.assertEquals(self.cart.orderitem_set.count(), 1) self.add_coupon(self.course_key, True) resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.coupon_code}) self.assertEqual(resp.status_code, 200) self.cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') # Total amount of a particular course that is purchased by different users total_amount = PaidCourseRegistration.get_total_amount_of_purchased_item(self.course_key) self.assertEqual(total_amount, 36) self.client.login(username=self.instructor.username, password="******") cart = Order.get_cart_for_user(self.instructor) PaidCourseRegistration.add_to_order(cart, self.course_key) cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') total_amount = PaidCourseRegistration.get_total_amount_of_purchased_item(self.course_key) self.assertEqual(total_amount, 76)
def test_add_to_order(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.assertEqual(reg1.unit_cost, self.cost) self.assertEqual(reg1.line_cost, self.cost) self.assertEqual(reg1.unit_cost, self.course_mode.min_price) self.assertEqual(reg1.mode, "honor") self.assertEqual(reg1.user, self.user) self.assertEqual(reg1.status, "cart") self.assertTrue(PaidCourseRegistration.contained_in_order(self.cart, self.course_id)) self.assertFalse(PaidCourseRegistration.contained_in_order(self.cart, self.course_id + "abcd")) self.assertEqual(self.cart.total_cost, self.cost)
def setUp(self): super(ItemizedPurchaseReportTest, self).setUp() self.user = UserFactory.create() self.cost = 40 self.course = CourseFactory.create(org='MITx', number='999', display_name=u'Robot Super Course') self.course_key = self.course.id course_mode = CourseMode(course_id=self.course_key, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost) course_mode.save() course_mode2 = CourseMode(course_id=self.course_key, mode_slug="verified", mode_display_name="verified cert", min_price=self.cost) course_mode2.save() self.annotation = PaidCourseRegistrationAnnotation( course_id=self.course_key, annotation=self.TEST_ANNOTATION) self.annotation.save() self.course_reg_code_annotation = CourseRegCodeItemAnnotation( course_id=self.course_key, annotation=self.TEST_ANNOTATION) self.course_reg_code_annotation.save() self.cart = Order.get_cart_for_user(self.user) self.reg = PaidCourseRegistration.add_to_order( self.cart, self.course_key, mode_slug=course_mode.mode_slug) self.cert_item = CertificateItem.add_to_order(self.cart, self.course_key, self.cost, 'verified') self.cart.purchase() self.now = datetime.datetime.now(pytz.UTC) paid_reg = PaidCourseRegistration.objects.get( course_id=self.course_key, user=self.user) paid_reg.fulfilled_time = self.now paid_reg.refund_requested_time = self.now paid_reg.save() cert = CertificateItem.objects.get(course_id=self.course_key, user=self.user) cert.fulfilled_time = self.now cert.refund_requested_time = self.now cert.save() self.CORRECT_CSV = dedent( (b""" Purchase Time,Order ID,Status,Quantity,Unit Cost,Total Cost,Currency,Description,Comments %s,1,purchased,1,40.00,40.00,usd,Registration for Course: Robot Super Course,Ba\xc3\xbc\xe5\x8c\x85 %s,1,purchased,1,40.00,40.00,usd,verified cert for course Robot Super Course, """ % (six.b(str(self.now)), six.b(str(self.now)))).decode('utf-8'))
def test_add_to_order(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.assertEqual(reg1.unit_cost, self.cost) self.assertEqual(reg1.line_cost, self.cost) self.assertEqual(reg1.unit_cost, self.course_mode.min_price) self.assertEqual(reg1.mode, "honor") self.assertEqual(reg1.user, self.user) self.assertEqual(reg1.status, "cart") self.assertTrue(PaidCourseRegistration.part_of_order(self.cart, self.course_id)) self.assertFalse(PaidCourseRegistration.part_of_order(self.cart, self.course_id + "abcd")) self.assertEqual(self.cart.total_cost, self.cost)
def test_add_to_order(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key) self.assertEqual(reg1.unit_cost, self.cost) self.assertEqual(reg1.line_cost, self.cost) self.assertEqual(reg1.unit_cost, self.course_mode.min_price) self.assertEqual(reg1.mode, "honor") self.assertEqual(reg1.user, self.user) self.assertEqual(reg1.status, "cart") self.assertTrue(PaidCourseRegistration.contained_in_order(self.cart, self.course_key)) self.assertFalse(PaidCourseRegistration.contained_in_order(self.cart, SlashSeparatedCourseKey("MITx", "999", "Robot_Super_Course_abcd"))) self.assertEqual(self.cart.total_cost, self.cost)
def test_purchased_callback_exception(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key) reg1.course_id = CourseLocator(org="changed", course="forsome", run="reason") reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse(CourseEnrollment.is_enrolled(self.user, self.course_key)) reg1.course_id = CourseLocator(org="abc", course="efg", run="hij") reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse(CourseEnrollment.is_enrolled(self.user, self.course_key))
def test_purchased_callback_exception(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_id) reg1.course_id = "changedforsomereason" reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse(CourseEnrollment.is_enrolled(self.user, self.course_id)) reg1.course_id = "abc/efg/hij" reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse(CourseEnrollment.is_enrolled(self.user, self.course_id))
def test_purchased_callback_exception(self): reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key) reg1.course_id = SlashSeparatedCourseKey("changed", "forsome", "reason") reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse(CourseEnrollment.is_enrolled(self.user, self.course_key)) reg1.course_id = SlashSeparatedCourseKey("abc", "efg", "hij") reg1.save() with self.assertRaises(PurchasedCallbackException): reg1.purchased_callback() self.assertFalse(CourseEnrollment.is_enrolled(self.user, self.course_key))
def test_show_receipt_success_with_upgrade(self): reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_id) cert_item = CertificateItem.add_to_order(self.cart, self.verified_course_id, self.cost, 'honor') self.cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.login_user() # When we come from the upgrade flow, we'll have a session variable showing that s = self.client.session s['attempting_upgrade'] = True s.save() self.mock_tracker.emit.reset_mock() # pylint: disable=maybe-no-member resp = self.client.get( reverse('shoppingcart.views.show_receipt', args=[self.cart.id])) # Once they've upgraded, they're no longer *attempting* to upgrade attempting_upgrade = self.client.session.get('attempting_upgrade', False) self.assertFalse(attempting_upgrade) self.assertEqual(resp.status_code, 200) self.assertIn('FirstNameTesting123', resp.content) self.assertIn('80.00', resp.content) ((template, context), _) = render_mock.call_args # When we come from the upgrade flow, we get these context variables self.assertEqual(template, 'shoppingcart/receipt.html') self.assertEqual(context['order'], self.cart) self.assertIn(reg_item, context['order_items']) self.assertIn(cert_item, context['order_items']) self.assertFalse(context['any_refunds']) course_enrollment = CourseEnrollment.get_or_create_enrollment( self.user, self.course_id) course_enrollment.emit_event('edx.course.enrollment.upgrade.succeeded') self.mock_tracker.emit.assert_any_call( # pylint: disable=maybe-no-member 'edx.course.enrollment.upgrade.succeeded', { 'user_id': course_enrollment.user.id, 'course_id': course_enrollment.course_id, 'mode': course_enrollment.mode } )
def test_add_with_default_mode(self): """ Tests add_to_cart where the mode specified in the argument is NOT in the database and NOT the default "honor". In this case it just adds the user in the CourseMode.DEFAULT_MODE, 0 price """ reg1 = PaidCourseRegistration.add_to_order(self.cart, self.course_id, mode_slug="DNE") self.assertEqual(reg1.unit_cost, 0) self.assertEqual(reg1.line_cost, 0) self.assertEqual(reg1.mode, "honor") self.assertEqual(reg1.user, self.user) self.assertEqual(reg1.status, "cart") self.assertEqual(self.cart.total_cost, 0) self.assertTrue(PaidCourseRegistration.part_of_order(self.cart, self.course_id))
def test_show_receipt_success_with_upgrade(self): reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_id) cert_item = CertificateItem.add_to_order(self.cart, self.verified_course_id, self.cost, 'honor') self.cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.login_user() # When we come from the upgrade flow, we'll have a session variable showing that s = self.client.session s['attempting_upgrade'] = True s.save() self.mock_server_track.reset_mock() resp = self.client.get(reverse('shoppingcart.views.show_receipt', args=[self.cart.id])) # Once they've upgraded, they're no longer *attempting* to upgrade attempting_upgrade = self.client.session.get('attempting_upgrade', False) self.assertFalse(attempting_upgrade) self.assertEqual(resp.status_code, 200) self.assertIn('FirstNameTesting123', resp.content) self.assertIn('80.00', resp.content) ((template, context), _) = render_mock.call_args # When we come from the upgrade flow, we get these context variables self.assertEqual(template, 'shoppingcart/receipt.html') self.assertEqual(context['order'], self.cart) self.assertIn(reg_item, context['order_items']) self.assertIn(cert_item, context['order_items']) self.assertFalse(context['any_refunds']) course_enrollment = CourseEnrollment.get_or_create_enrollment(self.user, self.course_id) course_enrollment.emit_event('edx.course.enrollment.upgrade.succeeded') self.mock_server_track.assert_any_call( None, 'edx.course.enrollment.upgrade.succeeded', { 'user_id': course_enrollment.user.id, 'course_id': course_enrollment.course_id, 'mode': course_enrollment.mode } )
def setUp(self): self.user = UserFactory.create() self.course_id = "MITx/999/Robot_Super_Course" self.cost = 40 self.course = CourseFactory.create(org='MITx', number='999', display_name=u'Robot Super Course') course_mode = CourseMode(course_id=self.course_id, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost) course_mode.save() course_mode2 = CourseMode(course_id=self.course_id, mode_slug="verified", mode_display_name="verified cert", min_price=self.cost) course_mode2.save() self.annotation = PaidCourseRegistrationAnnotation( course_id=self.course_id, annotation=self.TEST_ANNOTATION) self.annotation.save() self.cart = Order.get_cart_for_user(self.user) self.reg = PaidCourseRegistration.add_to_order(self.cart, self.course_id) self.cert_item = CertificateItem.add_to_order(self.cart, self.course_id, self.cost, 'verified') self.cart.purchase() self.now = datetime.datetime.now(pytz.UTC) paid_reg = PaidCourseRegistration.objects.get(course_id=self.course_id, user=self.user) paid_reg.fulfilled_time = self.now paid_reg.refund_requested_time = self.now paid_reg.save() cert = CertificateItem.objects.get(course_id=self.course_id, user=self.user) cert.fulfilled_time = self.now cert.refund_requested_time = self.now cert.save() self.CORRECT_CSV = dedent(""" Purchase Time,Order ID,Status,Quantity,Unit Cost,Total Cost,Currency,Description,Comments {time_str},1,purchased,1,40,40,usd,Registration for Course: Robot Super Course,Ba\xc3\xbc\xe5\x8c\x85 {time_str},1,purchased,1,40,40,usd,"Certificate of Achievement, verified cert for course Robot Super Course", """.format(time_str=str(self.now)))
def setUp(self): super(ItemizedPurchaseReportTest, self).setUp() self.user = UserFactory.create() self.cost = 40 self.course = CourseFactory.create(org="MITx", number="999", display_name=u"Robot Super Course") self.course_key = self.course.id course_mode = CourseMode( course_id=self.course_key, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost ) course_mode.save() course_mode2 = CourseMode( course_id=self.course_key, mode_slug="verified", mode_display_name="verified cert", min_price=self.cost ) course_mode2.save() self.annotation = PaidCourseRegistrationAnnotation(course_id=self.course_key, annotation=self.TEST_ANNOTATION) self.annotation.save() self.course_reg_code_annotation = CourseRegCodeItemAnnotation( course_id=self.course_key, annotation=self.TEST_ANNOTATION ) self.course_reg_code_annotation.save() self.cart = Order.get_cart_for_user(self.user) self.reg = PaidCourseRegistration.add_to_order(self.cart, self.course_key) self.cert_item = CertificateItem.add_to_order(self.cart, self.course_key, self.cost, "verified") self.cart.purchase() self.now = datetime.datetime.now(pytz.UTC) paid_reg = PaidCourseRegistration.objects.get(course_id=self.course_key, user=self.user) paid_reg.fulfilled_time = self.now paid_reg.refund_requested_time = self.now paid_reg.save() cert = CertificateItem.objects.get(course_id=self.course_key, user=self.user) cert.fulfilled_time = self.now cert.refund_requested_time = self.now cert.save() self.CORRECT_CSV = dedent( """ Purchase Time,Order ID,Status,Quantity,Unit Cost,Total Cost,Currency,Description,Comments {time_str},1,purchased,1,40,40,usd,Registration for Course: Robot Super Course,Ba\xc3\xbc\xe5\x8c\x85 {time_str},1,purchased,1,40,40,usd,verified cert for course Robot Super Course, """.format( time_str=str(self.now) ) )
def test_show_receipt_success(self): reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_key) cert_item = CertificateItem.add_to_order(self.cart, self.verified_course_key, self.cost, 'honor') self.cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.login_user() resp = self.client.get(reverse('shoppingcart.views.show_receipt', args=[self.cart.id])) self.assertEqual(resp.status_code, 200) self.assertIn('FirstNameTesting123', resp.content) self.assertIn('80.00', resp.content) ((template, context), _) = render_mock.call_args self.assertEqual(template, 'shoppingcart/receipt.html') self.assertEqual(context['order'], self.cart) self.assertIn(reg_item, context['order_items']) self.assertIn(cert_item, context['order_items']) self.assertFalse(context['any_refunds'])
def test_show_receipt_success(self): reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_id) cert_item = CertificateItem.add_to_order(self.cart, self.verified_course_id, self.cost, "honor") self.cart.purchase(first="FirstNameTesting123", street1="StreetTesting123") self.login_user() resp = self.client.get(reverse("shoppingcart.views.show_receipt", args=[self.cart.id])) self.assertEqual(resp.status_code, 200) self.assertIn("FirstNameTesting123", resp.content) self.assertIn("80.00", resp.content) ((template, context), _) = render_mock.call_args self.assertEqual(template, "shoppingcart/receipt.html") self.assertEqual(context["order"], self.cart) self.assertIn(reg_item, context["order_items"]) self.assertIn(cert_item, context["order_items"]) self.assertFalse(context["any_refunds"])
def test_show_receipt_success_refund(self): reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_id) cert_item = CertificateItem.add_to_order(self.cart, 'test/course1', self.cost, 'verified') self.cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') cert_item.status = "refunded" cert_item.save() self.assertEqual(self.cart.total_cost, 40) self.login_user() resp = self.client.get(reverse('shoppingcart.views.show_receipt', args=[self.cart.id])) self.assertEqual(resp.status_code, 200) self.assertIn('40.00', resp.content) ((template, context), _) = render_mock.call_args self.assertEqual(template, 'shoppingcart/receipt.html') self.assertEqual(context['order'], self.cart) self.assertIn(reg_item.orderitem_ptr, context['order_items']) self.assertIn(cert_item.orderitem_ptr, context['order_items']) self.assertTrue(context['any_refunds'])
def test_show_cart(self): self.login_user() reg_item = PaidCourseRegistration.add_to_order(self.cart, self.course_key) cert_item = CertificateItem.add_to_order(self.cart, self.verified_course_key, self.cost, 'honor') resp = self.client.get(reverse('shoppingcart.views.show_cart', args=[])) self.assertEqual(resp.status_code, 200) ((purchase_form_arg_cart,), _) = form_mock.call_args purchase_form_arg_cart_items = purchase_form_arg_cart.orderitem_set.all().select_subclasses() self.assertIn(reg_item, purchase_form_arg_cart_items) self.assertIn(cert_item, purchase_form_arg_cart_items) self.assertEqual(len(purchase_form_arg_cart_items), 2) ((template, context), _) = render_mock.call_args self.assertEqual(template, 'shoppingcart/list.html') self.assertEqual(len(context['shoppingcart_items']), 2) self.assertEqual(context['amount'], 80) self.assertIn("80.00", context['form_html'])
def setUp(self): super(TestOrderHistoryOnSiteDashboard, self).setUp() patcher = patch('student.models.tracker') self.mock_tracker = patcher.start() self.user = UserFactory.create() self.user.set_password('password') self.user.save() self.addCleanup(patcher.stop) # First Order with our (fakeX) site's course. course1 = CourseFactory.create(org='fakeX', number='999', display_name='fakeX Course') course1_key = course1.id course1_mode = CourseMode(course_id=course1_key, mode_slug="honor", mode_display_name="honor cert", min_price=20) course1_mode.save() cart = Order.get_cart_for_user(self.user) PaidCourseRegistration.add_to_order(cart, course1_key) cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.fakex_site_order_id = cart.id # Second Order with another(fooX) site's course course2 = CourseFactory.create(org='fooX', number='888', display_name='fooX Course') course2_key = course2.id course2_mode = CourseMode(course_id=course2.id, mode_slug="honor", mode_display_name="honor cert", min_price=20) course2_mode.save() cart = Order.get_cart_for_user(self.user) PaidCourseRegistration.add_to_order(cart, course2_key) cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.foox_site_order_id = cart.id # Third Order with course not attributed to any site. course3 = CourseFactory.create(org='fakeOtherX', number='777', display_name='fakeOtherX Course') course3_key = course3.id course3_mode = CourseMode(course_id=course3.id, mode_slug="honor", mode_display_name="honor cert", min_price=20) course3_mode.save() cart = Order.get_cart_for_user(self.user) PaidCourseRegistration.add_to_order(cart, course3_key) cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.order_id = cart.id # Fourth Order with course not attributed to any site but with a CertificateItem course4 = CourseFactory.create(org='fakeOtherX', number='888') course4_key = course4.id course4_mode = CourseMode(course_id=course4.id, mode_slug="verified", mode_display_name="verified cert", min_price=20) course4_mode.save() cart = Order.get_cart_for_user(self.user) CertificateItem.add_to_order(cart, course4_key, 20.0, 'verified') cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.certificate_order_id = cart.id # Fifth Order with course not attributed to any site but with a Donation course5 = CourseFactory.create(org='fakeOtherX', number='999') course5_key = course5.id cart = Order.get_cart_for_user(self.user) Donation.add_to_order(cart, 20.0, course5_key) cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.donation_order_id = cart.id # also add a donation not associated with a course to make sure the None case works OK Donation.add_to_order(cart, 10.0, None) cart.purchase(first='FirstNameTesting123', street1='StreetTesting123') self.courseless_donation_order_id = cart.id