Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Showing 4 changed files with 14 additions and 39 deletions.
6 changes: 3 additions & 3 deletions ecommerce/core/models.py
Original file line number Diff line number Diff line change
@@ -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,
1 change: 0 additions & 1 deletion ecommerce/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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
39 changes: 7 additions & 32 deletions ecommerce/extensions/basket/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -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)

7 changes: 4 additions & 3 deletions ecommerce/extensions/basket/views.py
Original file line number Diff line number Diff line change
@@ -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(

0 comments on commit 39d170e

Please sign in to comment.