def test_valid_voucher(self): """ Verify voucher_is_valid() assess that the voucher is valid. """ voucher, product = prepare_voucher(code=COUPON_CODE) request = RequestFactory().request() valid, msg = voucher_is_valid(voucher=voucher, products=[product], request=request) self.assertTrue(valid) self.assertEquals(msg, '')
def setUp(self): super(SiteMixin, self).setUp() # Set the domain used for all test requests domain = 'testserver.fake' self.client = self.client_class(SERVER_NAME=domain) Site.objects.all().delete() self.site_configuration = SiteConfigurationFactory( from_email='*****@*****.**', oauth_settings={ 'SOCIAL_AUTH_EDX_OIDC_KEY': 'key', 'SOCIAL_AUTH_EDX_OIDC_SECRET': 'secret' }, partner__name='edX', partner__short_code='edx', segment_key='fake_segment_key', site__domain=domain, site__id=settings.SITE_ID, base_cookie_domain=domain, ) self.partner = self.site_configuration.partner self.site = self.site_configuration.site self.request = RequestFactory(SERVER_NAME=domain).get('') self.request.session = None self.request.site = self.site set_thread_variable('request', self.request)
def test_once_per_customer_voucher(self): """ Verify the coupon is valid for anonymous users. """ voucher, product = prepare_voucher(usage=Voucher.ONCE_PER_CUSTOMER) request = RequestFactory().request() valid, msg = voucher_is_valid(voucher=voucher, products=[product], request=request) self.assertTrue(valid) self.assertEqual(msg, '')
def test_voucher_unavailable_to_buy(self): """ Verify that False is returned for unavialable products. """ request = RequestFactory().request() voucher, product = prepare_voucher(code=COUPON_CODE) product.expires = pytz.utc.localize(datetime.datetime.min) valid, __ = voucher_is_valid(voucher=voucher, products=[product], request=request) self.assertFalse(valid)
def test_logged_in_user_no_basket(self): self.init_checkout() req = RequestFactory().get('/') req.user = self.user resp = views.ShippingMethodView.as_view()(req) assert resp.status_code == 302 assert resp.url == reverse( 'basket:summary'), 'Should be redirected to the basket page'
def assert_error_messages(self, voucher, product, user, error_msg): """ Assert the proper error message is returned. """ voucher.offers.first().record_usage(discount={'freq': 1, 'discount': 1}) request = RequestFactory().request() request.user = user valid, msg = voucher_is_valid(voucher=voucher, products=[product], request=request) self.assertFalse(valid) self.assertEqual(msg, error_msg)
def test_omitting_unavailable_voucher(self): """ Verify if there are more than one product, that availability check is omitted. """ request = RequestFactory().request() voucher, product = prepare_voucher(code=COUPON_CODE) product.expires = pytz.utc.localize(datetime.datetime.min) __, seat = self.create_course_and_seat() valid, __ = voucher_is_valid(voucher=voucher, products=[product, seat], request=request) self.assertTrue(valid)
def test_user_needs_to_order_min_quantity(self): self.init_checkout() req = RequestFactory().get('/') req.user = self.user req.basket.add_product(self.product) resp = views.ShippingAddressView.as_view()(req) assert resp.status_code == 302 assert '/basket/' in resp.url
def test_used_voucher(self): """ Verify voucher_is_valid() assess that the voucher is unavailable. """ voucher, product = self.prepare_voucher() order = OrderFactory() user = self.create_user() VoucherApplication.objects.create(voucher=voucher, user=user, order=order) request = RequestFactory().request() valid, msg = voucher_is_valid(voucher=voucher, product=product, request=request) self.assertFalse(valid) self.assertEqual(msg, _('This coupon has already been used'))
def test_searching_child_product_by_upc_returns_parent_product(self): self.parent_product = create_product(structure=Product.PARENT, title='Parent', upc='PARENT_UPC') create_product(structure=Product.CHILD, title='Child', parent=self.parent_product, upc='CHILD_UPC') view = ProductListView(request=RequestFactory().get('/?upc=CHILD_UPC')) assert list(view.get_queryset()) == [self.parent_product]
def test_user_can_go_to_shipping_address_page(self): self.init_checkout() req = RequestFactory().get('/') req.user = self.user req.basket.add_product(self.product) req.basket.add_product(self.product) req.basket.add_product(self.product) req.basket.add_product(self.product) req.basket.add_product(self.product) resp = views.ShippingAddressView.as_view()(req) assert resp.status_code == 200
def test_anon_user_login_redirect(self): self.init_checkout() req = RequestFactory().get('/') req.basket.add_product(self.product) req.basket.add_product(self.product) req.basket.add_product(self.product) req.basket.add_product(self.product) req.basket.add_product(self.product) req.basket.add_product(self.product) resp = IndexView.as_view()(req) assert resp.status_code == 200, 'Should be logged in to checkout'
def test_post_with_missing_voucher(self): """ If the voucher is missing, verify the view queues a message and redirects. """ pk = '12345' view = VoucherRemoveView.as_view() request = RequestFactory().post('/') request.basket.save() response = view(request, pk=pk) self.assertEqual(response.status_code, 302) actual = list(get_messages(request))[-1].message expected = "No voucher found with id '{}'".format(pk) self.assertEqual(actual, expected)
def test_searching_child_product_by_title_returns_1_parent_product_if_title_is_partially_shared( self): self.parent_product = create_product(structure=Product.PARENT, title='Shared', upc='PARENT_UPC') create_product(structure=Product.CHILD, title='Shared', parent=self.parent_product, upc='CHILD_UPC') create_product(structure=Product.CHILD, title='Shared1', parent=self.parent_product, upc='CHILD_UPC1') view = ProductListView(request=RequestFactory().get('/?title=Shared')) assert list(view.get_queryset()) == [self.parent_product]
def _install_current_request(self, site): """Install a thread-local fake request, setting its site. This is necessary since publishing to the LMS requires inspecting the 'current request' and using its attached site to construct LMS urls. See ecommerce.core.url_utils for the implementation details. Arguments: site (Site): The site to set. Returns: None """ request = RequestFactory() request.site = site set_thread_variable('request', request)
def setUp(self): super(VoucherAddViewTests, self).setUp() self.user = self.create_user() self.client.login(username=self.user.username, password=self.password) self.basket = factories.BasketFactory(owner=self.user, site=self.site) # Fallback storage is needed in tests with messages self.request = RequestFactory().post('/') self.request.user = self.user self.request.basket = self.basket self.view = VoucherAddView() self.view.request = self.request self.form = BasketVoucherForm() self.form.cleaned_data = {'code': COUPON_CODE}
def setUp(self): super(SiteMixin, self).setUp() # Set the domain used for all test requests domain = 'testserver.fake' self.client = self.client_class(SERVER_NAME=domain) Course.objects.all().delete() Partner.objects.all().delete() Site.objects.all().delete() lms_url_root = "http://lms.testserver.fake" self.site_configuration = SiteConfigurationFactory( lms_url_root=lms_url_root, from_email='*****@*****.**', oauth_settings={ 'SOCIAL_AUTH_EDX_OAUTH2_KEY': 'key', 'SOCIAL_AUTH_EDX_OAUTH2_SECRET': 'secret', 'BACKEND_SERVICE_EDX_OAUTH2_KEY': 'key', 'BACKEND_SERVICE_EDX_OAUTH2_SECRET': 'secret', 'SOCIAL_AUTH_EDX_OAUTH2_LOGOUT_URL': lms_url_root + '/logout', }, partner__name='edX', partner__short_code='edx', segment_key='fake_segment_key', site__domain=domain, site__id=settings.SITE_ID, base_cookie_domain=domain, ) self.partner = self.site_configuration.partner self.partner.default_site = self.site = self.site_configuration.site self.partner.save() self.request = RequestFactory(SERVER_NAME=domain).get('') self.request.session = None self.request.site = self.site set_thread_variable('request', self.request) set_current_request(self.request) self.addCleanup(set_current_request)
def test_anonymous_user_no_basket(self): req = RequestFactory().get('/') resp = views.ShippingMethodView.as_view()(req) assert resp.status_code == 302 assert resp.url == reverse( 'basket:summary'), 'Should be redirected to the basket page'
def setUp(self): self.request = RequestFactory().get('/') self.product = factories.create_product(num_in_stock=10) self.stock_record = self.product.stockrecords.first()