Commit 6e543c14 by Jason Bau

pep8 + pylint

parent 4c664240
......@@ -8,6 +8,7 @@ navigation. We want to do this in the middleware to
from django.conf import settings
import shoppingcart
class UserHasCartMiddleware(object):
"""
Detects whether request.user has a cart and sets it as part of the request
......@@ -23,9 +24,11 @@ class UserHasCartMiddleware(object):
be displayed. Anonymous users don't.
"""
request.display_shopping_cart = False
if (request.user.is_authenticated() and # user exists
settings.MITX_FEATURES.get('ENABLE_PAID_COURSE_REGISTRATION') and # settings are set
settings.MITX_FEATURES.get('ENABLE_SHOPPING_CART') and
shoppingcart.models.Order.user_cart_has_items(request.user)): # user's cart is non-empty
if (
request.user.is_authenticated() and # user exists
settings.MITX_FEATURES.get('ENABLE_PAID_COURSE_REGISTRATION') and # settings is set
settings.MITX_FEATURES.get('ENABLE_SHOPPING_CART') and # setting is set
shoppingcart.models.Order.user_cart_has_items(request.user) # user's cart is non-empty
):
request.display_shopping_cart = True
return None
......@@ -14,14 +14,21 @@ from course_modes.models import CourseMode
from shoppingcart.models import Order, PaidCourseRegistration
from shoppingcart.middleware import UserHasCartMiddleware
@override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE)
class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
"""
Unit test for shoppingcart middleware UserHasCartMiddleware
"""
def setUp(self):
self.user = UserFactory.create()
self.request = Mock()
self.mw = UserHasCartMiddleware()
self.middleware = UserHasCartMiddleware()
def add_to_cart(self):
"""
Adds content to self.user's cart
"""
course = CourseFactory.create(org='MITx', number='999', display_name='Robot Super Course')
course_mode = CourseMode(course_id=course.id,
mode_slug="honor",
......@@ -38,7 +45,7 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
"""
self.add_to_cart()
self.request.user = self.user
self.mw.process_request(self.request)
self.middleware.process_request(self.request)
self.assertFalse(self.request.display_shopping_cart)
@patch.dict(settings.MITX_FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': False})
......@@ -48,7 +55,7 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
"""
self.add_to_cart()
self.request.user = self.user
self.mw.process_request(self.request)
self.middleware.process_request(self.request)
self.assertFalse(self.request.display_shopping_cart)
@patch.dict(settings.MITX_FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': True})
......@@ -57,7 +64,7 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
Tests when request.user is anonymous
"""
self.request.user = AnonymousUser()
self.mw.process_request(self.request)
self.middleware.process_request(self.request)
self.assertFalse(self.request.display_shopping_cart)
@patch.dict(settings.MITX_FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': True})
......@@ -66,7 +73,7 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
Tests when request.user doesn't have a cart with items
"""
self.request.user = self.user
self.mw.process_request(self.request)
self.middleware.process_request(self.request)
self.assertFalse(self.request.display_shopping_cart)
@patch.dict(settings.MITX_FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': True})
......@@ -76,5 +83,5 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
"""
self.add_to_cart()
self.request.user = self.user
self.mw.process_request(self.request)
self.middleware.process_request(self.request)
self.assertTrue(self.request.display_shopping_cart)
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