From 39d170e41d6e5476a0f4d1a0b8337f462ce2e4fd Mon Sep 17 00:00:00 2001 From: Simon Chen Date: Thu, 2 Mar 2017 16:25:07 -0500 Subject: [PATCH] Revert to correct the Learners getting errors on SingleBasketItemView ECOM-7349 --- ecommerce/core/models.py | 6 +-- ecommerce/core/tests/test_models.py | 1 - .../extensions/basket/tests/test_views.py | 39 ++++--------------- ecommerce/extensions/basket/views.py | 7 ++-- 4 files changed, 14 insertions(+), 39 deletions(-) diff --git a/ecommerce/core/models.py b/ecommerce/core/models.py index 88b23f2e4eb..ad162dfd121 100644 --- a/ecommerce/core/models.py +++ b/ecommerce/core/models.py @@ -17,7 +17,7 @@ from edx_rest_api_client.client import EdxRestApiClient from jsonfield.fields import JSONField from requests.exceptions import ConnectionError, Timeout -from slumber.exceptions import HttpNotFoundError, SlumberBaseException, HttpClientError +from slumber.exceptions import HttpNotFoundError, SlumberBaseException from ecommerce.core.exceptions import VerificationStatusError from ecommerce.core.url_utils import get_lms_url @@ -434,11 +434,11 @@ def is_user_already_enrolled(self, request, seat): try: api = EdxRestApiClient( request.site.siteconfiguration.build_lms_url('/api/enrollment/v1'), - jwt=request.site.siteconfiguration.access_token, + oauth_access_token=self.access_token, append_slash=False ) status = api.enrollment(','.join([self.username, course_key])).get() - except (ConnectionError, SlumberBaseException, Timeout, HttpClientError): + except (ConnectionError, SlumberBaseException, Timeout): log.exception( 'Failed to retrieve enrollment details for [%s] in course [%s]', self.username, diff --git a/ecommerce/core/tests/test_models.py b/ecommerce/core/tests/test_models.py index 37915de1ae0..6888861b7c7 100644 --- a/ecommerce/core/tests/test_models.py +++ b/ecommerce/core/tests/test_models.py @@ -78,7 +78,6 @@ def test_is_user_enrolled(self, mode, id_verification): """ Verify check for user enrollment in a course. """ user = self.create_user() self.request.user = user - self.mock_access_token_response() course_id1 = 'course-v1:test+test+test' __, enrolled_seat = self.create_course_and_seat( course_id=course_id1, seat_type=mode, id_verification=id_verification diff --git a/ecommerce/extensions/basket/tests/test_views.py b/ecommerce/extensions/basket/tests/test_views.py index c6e2b6506ca..a1ec98bdaf5 100644 --- a/ecommerce/extensions/basket/tests/test_views.py +++ b/ecommerce/extensions/basket/tests/test_views.py @@ -37,7 +37,7 @@ from ecommerce.extensions.payment.tests.processors import DummyProcessor from ecommerce.extensions.test.factories import prepare_voucher from ecommerce.tests.factories import StockRecordFactory -from ecommerce.tests.mixins import ApiMockMixin, LmsApiMockMixin, SiteMixin +from ecommerce.tests.mixins import ApiMockMixin, LmsApiMockMixin from ecommerce.tests.testcases import TestCase Applicator = get_class('offer.utils', 'Applicator') @@ -57,8 +57,7 @@ @ddt.ddt -class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, LmsApiMockMixin, TestCase, - SiteMixin): +class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, LmsApiMockMixin, TestCase): """ BasketSingleItemView view tests. """ path = reverse('basket:single-item') @@ -144,7 +143,6 @@ def test_redirect_to_basket_summary(self): Verify the view redirects to the basket summary page, and that the user's basket is prepared for checkout. """ self.mock_enrollment_api_success_enrolled(self.course.id) - self.mock_access_token_response() self.create_coupon(catalog=self.catalog, code=COUPON_CODE, benefit_value=5) self.mock_dynamic_catalog_course_runs_api(course_run=self.course) @@ -169,14 +167,14 @@ def test_enrolled_verified_student(self, mode, id_verification): (The Enrollment API call being used returns an active enrollment record in this case) """ course = CourseFactory() - self.mock_access_token_response() self.mock_enrollment_api_success_enrolled(course.id, mode=mode) - product = course.create_or_update_seat(mode, id_verification, 0, self.partner, create_enrollment_code=False) + product = course.create_or_update_seat(mode, id_verification, 0, self.partner) stock_record = StockRecordFactory(product=product, partner=self.partner) catalog = Catalog.objects.create(partner=self.partner) catalog.stock_records.add(stock_record) + self.create_coupon(catalog=catalog, code=COUPON_CODE, benefit_value=5) - url = '{path}?sku={sku}'.format(path=self.path, sku=stock_record.partner_sku) + url = '{path}?sku={sku}&code={code}'.format(path=self.path, sku=stock_record.partner_sku, code=COUPON_CODE) expected_content = 'You are already enrolled in {product}.'.format(product=product.course.name) response = self.client.get(url) self.assertEqual(response.status_code, 400) @@ -191,7 +189,6 @@ def test_unenrolled_verified_student(self, mode, id_verification): (The Enrollment API call being used returns an inactive enrollment record in this case) """ course = CourseFactory() - self.mock_access_token_response() self.mock_enrollment_api_success_unenrolled(course.id, mode=mode) product = course.create_or_update_seat(mode, id_verification, 0, self.partner) stock_record = StockRecordFactory(product=product, partner=self.partner) @@ -205,29 +202,6 @@ def test_unenrolled_verified_student(self, mode, id_verification): self.assertEqual(response.wsgi_request.path_info, '/basket/single-item/') self.assertEqual(response.wsgi_request.GET['sku'], sku) - @httpretty.activate - @ddt.data(('verified', False), ('professional', True), ('no-id-professional', False)) - @ddt.unpack - def test_enrolled_verified_student_for_enrollment_code(self, mode, id_verification): - """ - Verify the view return HTTP 303 if the student is enrolled as verified and purchasing enrollment code - (The Enrollment API call being used returns an inactive enrollment record in this case) - """ - course = CourseFactory() - self.mock_enrollment_api_success_enrolled(course.id, mode=mode) - toggle_switch(ENROLLMENT_CODE_SWITCH, True) - course.create_or_update_seat(mode, id_verification, 10, self.partner, create_enrollment_code=True) - product = Product.objects.get(product_class__name=ENROLLMENT_CODE_PRODUCT_CLASS_NAME) - stock_record = StockRecordFactory(product=product, partner=self.partner) - catalog = Catalog.objects.create(partner=self.partner) - catalog.stock_records.add(stock_record) - - url = '{path}?sku={sku}'.format(path=self.path, sku=stock_record.partner_sku) - response = self.client.get(url) - self.assertEqual(response.status_code, 303) - self.assertEqual(response.wsgi_request.path_info, '/basket/single-item/') - self.assertEqual(response.wsgi_request.GET['sku'], stock_record.partner_sku) - @httpretty.activate @ddt.data(ConnectionError, SlumberBaseException, Timeout) def test_enrollment_api_failure(self, error): @@ -236,7 +210,8 @@ def test_enrollment_api_failure(self, error): """ self.request.user = self.user self.mock_enrollment_api_error(self.request, self.user, self.course.id, error) - url = '{path}?sku={sku}'.format(path=self.path, sku=self.stock_record.partner_sku) + self.create_coupon(catalog=self.catalog, code=COUPON_CODE, benefit_value=5) + url = '{path}?sku={sku}&code={code}'.format(path=self.path, sku=self.stock_record.partner_sku, code=COUPON_CODE) response = self.client.get(url) self.assertEqual(response.status_code, 400) diff --git a/ecommerce/extensions/basket/views.py b/ecommerce/extensions/basket/views.py index 3f6306b7743..4d8d43355f4 100644 --- a/ecommerce/extensions/basket/views.py +++ b/ecommerce/extensions/basket/views.py @@ -62,9 +62,10 @@ def get(self, request): msg = _('Product [{product}] not available to buy.').format(product=product.title) return HttpResponseBadRequest(msg) - # If the product is not an Enrollment Code, we check to see if the user is already - # enrolled to prevent double-enrollment and/or accidental coupon usage - if not product.is_enrollment_code_product: + # If the product is not an Enrollment Code and this is a Coupon Redemption request, + # we check to see if the user is already enrolled + # to prevent double-enrollment and/or accidental coupon usage. + if not product.is_enrollment_code_product and code: try: if request.user.is_user_already_enrolled(request, product): logger.warning(