def test_get_from_email(self): """ Validate SiteConfiguration.get_from_email() along with whether, or not, the base from email address is actually changed when a site-specific value is specified. """ site_config = SiteConfigurationFactory(from_email='') self.assertEqual(site_config.get_from_email(), settings.OSCAR_FROM_EMAIL) expected_from_email = '*****@*****.**' site_config = SiteConfigurationFactory(from_email=expected_from_email) self.assertEqual(site_config.get_from_email(), expected_from_email)
def test_with_orders(self): """ The view should return a list of the user's orders, sorted reverse chronologically, filtered by current site's partner. """ order = create_order(site=self.site, user=self.user) site = SiteConfigurationFactory().site create_order(site=site, user=self.user) response = self.client.get(self.path, HTTP_AUTHORIZATION=self.token) self.assertEqual(response.status_code, 200) content = json.loads(response.content.decode('utf-8')) self.assertEqual(Order.objects.count(), 2) self.assertEqual(content['count'], 1) self.assertEqual(content['results'][0]['number'], six.text_type(order.number)) # Test ordering order_2 = create_order(site=self.site, user=self.user) response = self.client.get(self.path, HTTP_AUTHORIZATION=self.token) self.assertEqual(response.status_code, 200) content = json.loads(response.content.decode('utf-8')) self.assertEqual(content['count'], 2) self.assertEqual(content['results'][0]['number'], six.text_type(order_2.number)) self.assertEqual(content['results'][1]['number'], six.text_type(order.number))
def test_settings_override_middleware_overrides_settings_correctly_if_list_tuple( self): """ Tests "SettingsOverrideMiddleware" correctly overrides tuple/list django settings. Tests if a value being overriden through the middleware is a tuple/list value, the value is being extended not replaced. """ django_override_settings = { 'ALLOWED_HOSTS': ( 'localhost', '.edx.devstack.lms', ), 'EDLY_WORDPRESS_URL': 'http://red.wordpress.edx.devstack.lms', 'CSRF_TRUSTED_ORIGINS': [], 'OSCAR_FROM_EMAIL': '*****@*****.**', 'PLATFORM_NAME': 'Test Platform', } SiteConfiguration.objects.all().delete() SiteConfigurationFactory(site=self.request.site, edly_client_theme_branding_settings={ 'DJANGO_SETTINGS_OVERRIDE': django_override_settings }) self._assert_settings_values(self.default_settings) self.client.get('/', follow=True) default_settings = self.default_settings.copy() default_settings.get('ALLOWED_HOSTS', []).extend( django_override_settings.get('ALLOWED_HOSTS', [])) self._assert_settings_values(default_settings)
def test_IDVerification_workflow_url_configured(self, course_id): account_url = 'https://account.edx.org' site_config = SiteConfigurationFactory( account_microfrontend_url=account_url ) self.assertEqual(site_config.account_microfrontend_url, account_url) self._validate_IDVerification_workflow_url(site_config, account_url, course_id)
def test_flush_with_product(self): """ Verify the method fires 'Product Removed' Segment event with the correct information when basket is not empty """ self.site1.siteconfiguration = SiteConfigurationFactory() self.site1.siteconfiguration.segment_key = 'fake_key' basket = create_basket(empty=True) basket.owner = factories.UserFactory() basket.site = self.site1 basket.save() course = CourseFactory() seat = course.create_or_update_seat('verified', True, 100, self.partner) basket.add_product(seat) properties = translate_basket_line_for_segment(basket.lines.first()) user_tracking_id, lms_client_id, lms_ip = parse_tracking_context(basket.owner) context = { 'ip': lms_ip, 'Google Analytics': { 'clientId': lms_client_id } } with mock.patch.object(Client, 'track') as mock_track: basket.flush() mock_track.assert_called_once_with(user_tracking_id, 'Product Removed', properties, context=context)
def test_create(self): """Test the create method.""" site_configuration = SiteConfigurationFactory(partner__name='TestX') site = SiteFactory() site.siteconfiguration = site_configuration data = { 'title': 'Test coupon', 'client_username': '******', 'stock_record_ids': [1], 'start_date': '2015-1-1', 'end_date': '2020-1-1', 'code': '', 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': 100, 'voucher_type': Voucher.SINGLE_USE, 'quantity': 1, 'price': 100 } request = RequestFactory() request.data = data request.site = site response = CouponViewSet().create(request) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.data, {'payment_data': {'payment_processor_name': 'Invoice'}, 'id': 1, 'order': 1, 'coupon_id': 3} )
def setUp(self): super(BasketUtilsTests, self).setUp() self.request = RequestFactory() self.request.COOKIES = {} self.request.user = self.create_user() site_configuration = SiteConfigurationFactory(partner__name='Tester') self.request.site = site_configuration.site
def test_delete_coupon(self): """Test the coupon deletion.""" coupon = self.create_coupon(partner=self.partner) self.assertEqual( Product.objects.filter( product_class=self.coupon_product_class).count(), 1) self.assertEqual(StockRecord.objects.filter(product=coupon).count(), 1) coupon_voucher_qs = CouponVouchers.objects.filter(coupon=coupon) self.assertEqual(coupon_voucher_qs.count(), 1) self.assertEqual(coupon_voucher_qs.first().vouchers.count(), 5) request = RequestFactory() request.site = SiteConfigurationFactory().site response = CouponViewSet().destroy(request, coupon.id) self.assertEqual( Product.objects.filter( product_class=self.coupon_product_class).count(), 0) self.assertEqual(StockRecord.objects.filter(product=coupon).count(), 0) coupon_voucher_qs = CouponVouchers.objects.filter(coupon=coupon) self.assertEqual(coupon_voucher_qs.count(), 0) self.assertEqual(Voucher.objects.count(), 0) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) response = CouponViewSet().destroy(request, 100) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
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_is_satisfied_site_mismatch(self): """ Ensure the condition returns False if the offer site does not match the basket site. """ offer = factories.EnterpriseOfferFactory( site=SiteConfigurationFactory().site, condition=self.condition) basket = factories.BasketFactory(site=self.site, owner=self.user) basket.add_product(self.test_product) self.assertFalse(self.condition.is_satisfied(offer, basket))
def test_check_condition_applicability_site_mismatch(self): """ Validate check_condition_applicability decorator returns False if the offer site and basket site do not match. """ basket = create_basket(self.user, SiteConfigurationFactory().site) self.assertFalse(check_condition_applicability()( self.condition.is_satisfied)(self.condition, self.offer, basket))
def test_is_satisfied_site_mismatch(self): """ Ensure the condition returns False if the offer partner does not match the basket site partner. """ offer = factories.ProgramOfferFactory( partner=SiteConfigurationFactory().partner, condition=self.condition) basket = BasketFactory(site=self.site, owner=UserFactory()) basket.add_product(self.test_product) self.assertFalse(self.condition.is_satisfied(offer, basket))
def test_voucher_not_valid_for_other_site(self): """ Verify correct error message is returned when coupon is applied against on the wrong site. """ other_site = SiteConfigurationFactory().site self.mock_access_token_response() self.mock_account_api(self.request, self.user.username, data={'is_active': True}) voucher, product = prepare_voucher(code=COUPON_CODE, site=other_site) self.basket.add_product(product) self.assert_form_valid_message("Coupon code '{code}' is not valid for this basket.".format(code=voucher.code))
def setUp(self): super(SignalTests, self).setUp() self.request = RequestFactory() self.user = self.create_user() self.request.user = self.user self.site_configuration = SiteConfigurationFactory( partner__name='Tester', from_email='*****@*****.**') self.request.site = self.site_configuration.site
def test_oauth_api_client(self): """ Verify the property retrieves, and caches, an access token from the OAuth 2.0 provider. """ token = self.mock_access_token_response() site_config = SiteConfigurationFactory() client = site_config.oauth_api_client self.assertEqual(type(client), OAuthAPIClient) self.assertEqual(client.get_jwt_access_token(), token) self.assertEqual(len(responses.calls), 1)
def test_order_number_for_basket_without_site(self): """ Verify the order number is linked to the default site, if the basket has no associated site. """ site_configuration = SiteConfigurationFactory(site__domain='acme.fake', partner__name='ACME') site = site_configuration.site partner = site_configuration.partner basket = BasketFactory(site=None) with override_settings(SITE_ID=site.id): self.assert_order_number_matches_basket(basket, partner)
def test_migrate_partner(self): """ Test that command successfully add partner to orders.""" initial_count = 4 SiteConfigurationFactory() OrderFactory.create_batch(initial_count, partner=None) self.assertEqual( Order.objects.filter(partner__isnull=True).count(), initial_count) call_command('migrate_partner_to_orders', batch_size=2, sleep_time=1) self.assertEqual(Order.objects.filter(partner__isnull=True).count(), 0)
def setUp(self): super(SiteConfigurationViewSetTests, self).setUp() self.site_configuration = SiteConfigurationFactory( partner__name='TestX', site__domain='test.api.endpoint', segment_key='test_segment_key', enable_enrollment_codes=True) self.path = reverse('api:v2:siteconfiguration-list') self.user = self.create_user(is_staff=True) self.client.login(username=self.user.username, password=self.password)
def test_configuration(self): """ Verifies configuration is read from settings. """ other_site = SiteConfigurationFactory(partner__short_code='other').site for site in (self.site, other_site): processor = self.processor_class(site) # pylint: disable=not-callable short_code = site.siteconfiguration.partner.short_code.lower() self.assertDictEqual( processor.configuration, settings.PAYMENT_PROCESSOR_CONFIG[short_code][ processor.NAME.lower()])
def setUp(self): super(CustomerUtilsTests, self).setUp() self.dispatcher = Dispatcher() self.request = RequestFactory() self.user = self.create_user() self.user.email = '*****@*****.**' self.request.user = self.user self.site_configuration = SiteConfigurationFactory(partner__name='Tester', from_email='*****@*****.**') self.request.site = self.site_configuration.site self.order = create_order() self.order.user = self.user
def test_create_order_model_basket_no_referral(self): """ Verify the create_order_model method logs error if no referral.""" # Create a site config to clean up log messages site_configuration = SiteConfigurationFactory(site__domain='star.fake', partner__name='star') site = site_configuration.site # Create the basket WITHOUT an associated referral basket = self.create_basket(site) with LogCapture(LOGGER_NAME, level=logging.DEBUG) as l: order = self.create_order_model(basket) message = 'Order [{order_id}] has no referral associated with its basket.'.format(order_id=order.id) l.check((LOGGER_NAME, 'DEBUG', message))
def test_create_fake_orders(self): partner = PartnerFactory() SiteConfigurationFactory(partner=partner) product = create_product() stockrecord = create_stockrecord(product=product, partner_name=partner.name) self.assertEqual(Order.objects.all().count(), 0) self.assertEqual(Basket.objects.all().count(), 0) call_command('create_fake_orders', '--count=5', '--sku={}'.format(stockrecord.partner_sku)) self.assertEqual(Order.objects.all().count(), 5) self.assertEqual(Basket.objects.all().count(), 5)
def test_flush_without_product(self): """ Verify the method does not fireSegment event when basket is empty """ self.site1.siteconfiguration = SiteConfigurationFactory() self.site1.siteconfiguration.segment_key = 'fake_key' basket = create_basket(empty=True) basket.owner = factories.UserFactory() basket.site = self.site1 with mock.patch.object(Client, 'track') as mock_track: basket.flush() self.assertEqual(mock_track.call_count, 0)
def create_basket(owner=None, site=None, empty=False): # pylint:disable=function-redefined if site is None: site = SiteConfigurationFactory().site if owner is None: owner = UserFactory() basket = Basket.objects.create(site=site, owner=owner) basket.strategy = Default() if not empty: product = create_product() create_stockrecord(product, num_in_stock=2, price_excl_tax=D('10.00')) basket.add_product(product) return basket
def test_create_order_model_basket_site(self): """ Verify the create_order_model method associates the order with the basket's site. """ # Create a non-default site site_configuration = SiteConfigurationFactory(site__domain='star.fake', partner__name='star') site = site_configuration.site # Associate the basket with the non-default site basket = self.create_basket(site) # Ensure the order has the non-default site order = self.create_order_model(basket) self.assertEqual(order.site, site)
def test_order_number_for_basket_without_site(self): """ Verify the order number is linked to the default site, if the basket has no associated site. """ site_configuration = SiteConfigurationFactory(site__domain='acme.fake', partner__name='ACME') site = site_configuration.site partner = site_configuration.partner basket = BasketFactory(site=None) request = RequestFactory().get('') request.session = None request.site = site with mock.patch('ecommerce.extensions.order.utils.get_current_request', mock.Mock(return_value=request)): self.assert_order_number_matches_basket(basket, partner)
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.get_current().delete() site_configuration = SiteConfigurationFactory( partner__name='edX', site__id=settings.SITE_ID, site__domain=domain) self.partner = site_configuration.partner self.site = site_configuration.site
def test_create_order_model_basket_referral(self): """ Verify the create_order_model method associates the order with the basket's site. """ site_configuration = SiteConfigurationFactory(site__domain='star.fake', partner__name='star') site = site_configuration.site affiliate_id = 'test affiliate' # Create the basket and associated referral basket = self.create_basket(site) self.create_referral(basket, affiliate_id) # Ensure the referral is now associated with the order and has the correct affiliate id order = self.create_order_model(basket) referral = Referral.objects.get(order_id=order.id) self.assertEqual(referral.affiliate_id, affiliate_id)
def test_partner_not_supported(self, mock_log_error, mock_update_course_enrollment): """ Verify Sailthru is not contacted if the Partner does not support Sailthru. """ site_configuration = SiteConfigurationFactory(partner__name='TestX') site_configuration.partner.enable_sailthru = False self.request.site.siteconfiguration = site_configuration process_basket_addition(None, request=self.request) self.assertFalse(mock_update_course_enrollment.called) self.assertFalse(mock_log_error.called) __, order = self._create_order(99) order.site.siteconfiguration = site_configuration process_checkout_complete(None, order=order) self.assertFalse(mock_update_course_enrollment.called) self.assertFalse(mock_log_error.called)
def test_create_order_model_basket_referral_error(self): """ Verify the create_order_model method logs error for referral errors. """ # Create a site config to clean up log messages site_configuration = SiteConfigurationFactory(site__domain='star.fake', partner__name='star') site = site_configuration.site # Create the basket WITHOUT an associated referral basket = self.create_basket(site) with LogCapture(LOGGER_NAME, level=logging.ERROR) as l: Referral.objects.get = mock.Mock() Referral.objects.get.side_effect = Exception order = self.create_order_model(basket) message = 'Referral for Order [{order_id}] failed to save.'.format(order_id=order.id) l.check((LOGGER_NAME, 'ERROR', message))