Commit 39d170e4 by Simon Chen

Revert to correct the Learners getting errors on SingleBasketItemView ECOM-7349

parent 0e6e7624
......@@ -17,7 +17,7 @@ from django.utils.translation import ugettext_lazy as _
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 @@ class User(AbstractUser):
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,
......
......@@ -78,7 +78,6 @@ class UserTests(CourseCatalogTestMixin, LmsApiMockMixin, TestCase):
""" 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
......
......@@ -37,7 +37,7 @@ from ecommerce.extensions.payment.forms import PaymentForm
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 @@ COUPON_CODE = 'COUPONTEST'
@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 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, CourseCatal
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 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, CourseCatal
(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 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, CourseCatal
(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)
......@@ -206,29 +203,6 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, CourseCatal
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 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, CourseCatal
"""
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)
......
......@@ -62,9 +62,10 @@ class BasketSingleItemView(View):
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(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment