def setUp(self): """ Overrides the default Django TestCase to clear out the threadlocal request variable during test setUp and tearDown. """ super(EddApiTestCaseMixin, self).setUp() set_thread_variable('request', None)
def tearDownClass(cls): """ Overrides the default Django TestCase to clear out the threadlocal request variable during class setUp and tearDown. """ super(EddApiTestCaseMixin, cls).tearDownClass() set_thread_variable('request', None)
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 setUpClass(cls): """ Overrides the default Django TestCase to clear out the threadlocal request variable during class setUp and tearDown. """ super().setUpClass() set_thread_variable("request", None)
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() site_configuration = SiteConfigurationFactory( partner__name='edX', site__id=settings.SITE_ID, site__domain=domain, segment_key='fake_segment_key', oauth_settings={ 'SOCIAL_AUTH_EDX_OIDC_KEY': 'key', 'SOCIAL_AUTH_EDX_OIDC_SECRET': 'secret' } ) self.partner = site_configuration.partner self.site = site_configuration.site self.request = RequestFactory().get('') self.request.session = None self.request.site = self.site set_thread_variable('request', self.request)
def tearDown(self): """ Overrides the default Django TestCase to clear out the threadlocal request variable during test setUp and tearDown. """ super().tearDown() set_thread_variable("request", None)
def setUpClass(cls): cls.user1 = factory.UserFactory() # fake a request so all calls to Update.load_update resolve to a singluar Update request = RequestFactory().get('/test-fixture') request.user = cls.user1 set_thread_variable('request', request) # call parent *after* fake request is set up super().setUpClass()
def _initialize_update(self): # set a fake request object with update info fake_request = HttpRequest() if "update_id" in self.params: update_id = self.params.get("update_id") fake_request.update_obj = models.Update.objects.get(pk=update_id) else: fake_request.update_obj = models.Update.load_update(user=self.user) set_thread_variable("request", fake_request)
def fake_request(cls, user=None, path=None): """ Context manager sets up a fake request, with a reference to an Update object as if created by Update.load_update(). """ try: fake_request = HttpRequest() fake_request.update_obj = cls.load_update(user=user, path=path) set_thread_variable("request", fake_request) yield finally: set_thread_variable("request", None)
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(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, segment_key='fake_segment_key' ) self.partner = site_configuration.partner self.site = site_configuration.site request = RequestFactory().get('') request.session = None request.site = self.site set_thread_variable('request', request)
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 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, segment_key='fake_segment_key', oauth_settings={ 'SOCIAL_AUTH_EDX_OIDC_KEY': 'key', 'SOCIAL_AUTH_EDX_OIDC_SECRET': 'secret' }) self.partner = site_configuration.partner self.site = site_configuration.site request = RequestFactory().get('') request.session = None request.site = self.site set_thread_variable('request', request)
def tearDown(self): set_thread_variable('request', None)
def __call__(self, request): domain_host, domain_port = split_domain_port(request.get_host()) set_thread_variable('requested_host', domain_host) response = self.get_response(request) return response
def tearDown(self): set_thread_variable('request', None) super(TestCase, self).tearDown()
def setUp(self): super(TestCase, self).setUp() set_thread_variable('request', None)
def test_get_current_request(self): self.assertEqual(get_current_request(), None) # tests default (None) request = RequestFactory().get(u'/') set_thread_variable('request', request) self.assertEqual(get_current_request(), request)
def setUpClass(cls): super().setUpClass() set_thread_variable("request", None)
def setUp(self): super(ImportTests, self).setUp() # fake a request so all calls to Update.load_update resolve to a singluar Update self.request = RequestFactory().get('/') self.request.user = self.user1 set_thread_variable('request', self.request)
def setUp(self): super().setUp() set_thread_variable("request", None)
def _cleanup_update(self): set_thread_variable("request", None)
def process_request(self, request): domain_host, domain_port = split_domain_port(request.get_host()) set_thread_variable('requested_host', domain_host)
def setUp(self): super(LineTests, self).setUp() # fake a request so all calls to Update.load_update resolve to a singluar Update request = RequestFactory().get("/") request.user = self.user1 set_thread_variable("request", request)
def test_get_set_thread_variable(self): set_thread_variable('test', {'test': 'test'}) gotten = get_thread_variable('test') self.assertEqual(gotten, {'test': 'test'})
def setUp(self): set_thread_variable('request', None)
def tearDownClass(cls): set_thread_variable("request", None) super().tearDownClass()
def tearDownClass(cls): super().tearDownClass() set_thread_variable('request', None)
def tearDownClass(cls): set_thread_variable('request', None) super(TestCase, cls).tearDownClass()
def handle(self, *args, **options): site_id = options.get('site_id') try: site = Site.objects.get(id=site_id) except Site.DoesNotExist: logger.exception('Site id %s does not exist', site_id) raise Exception try: site_configurations = SiteConfiguration.objects.get(site=site) configuration_helpers = site_configurations.edly_client_theme_branding_settings voucher_api_url = configuration_helpers.get( 'LUMSXPAY_VOUCHER_API_URL') if not voucher_api_url: logger.exception( 'Cron Job of Update Payment Statuses is canceled due to no ' 'LUMSXPAY_VOUCHER_API_URL in client theme branding') raise Exception except: logger.exception( 'Site Configurations with side id %s does not exist', site_id) raise Exception try: unpaid_challan_baskets = BasketChallanVoucher.objects.filter( is_paid=False) except: logger.exception( 'could not fetch the unpaid challan baskets from Database') raise Exception headers = { "Authorization": configuration_helpers.get('PAYMENT_AUTHORIZATION_KEY'), "Content-Type": "application/json" } paid_vouchers = [] unpaid_vouchers = unpaid_challan_baskets.values_list('voucher_number', flat=True) if unpaid_challan_baskets: for unpaid_vouchers_lst in list( self.equal_divided_chunks(list(unpaid_vouchers), self.VOUCHERS_PER_REQUEST)): unpaid_vouchers_str = ','.join(unpaid_vouchers_lst) url = '{}/{}'.format(voucher_api_url, unpaid_vouchers_str) response = requests.get(url, headers=headers) if response.status_code == 200: voucher_details = response.json() voucher_data = voucher_details['data'] if not isinstance(voucher_details['data'], list): voucher_data = [voucher_details['data']] for voucher in voucher_data: if voucher['paid_status'].lower() == 'paid': paid_vouchers.append(voucher['voucher_id']) else: logger.info('VOUCHER API doesnot return 200 OK') return else: logger.info('No unpaid voucher found for update payment status') return if not paid_vouchers: logger.info('No voucher paid so exiting the job') return unpaid_basket_ids = unpaid_challan_baskets.filter( voucher_number__in=paid_vouchers).values_list('basket_id', flat=True) paid_baskets = Basket.objects.filter(id__in=unpaid_basket_ids, status=Basket.OPEN) if not paid_baskets: logger.info( 'ERROR: Basket corresponding to voucher does not exist') raise Exception for basket in paid_baskets: shipping_method = NoShippingRequired() shipping_charge = shipping_method.calculate(basket) basket.strategy = DefaultStrategy() order_total = OrderTotalCalculator().calculate( basket, shipping_charge) user = basket.owner billing_address = None request = RequestFactory() request.site = site request.user = user request.site.siteconfiguration = site_configurations set_thread_variable('request', request) order = EdxOrderPlacementMixin().handle_order_placement( order_number=basket.order_number, user=user, basket=basket, shipping_address=None, shipping_method=shipping_method, shipping_charge=shipping_charge, billing_address=billing_address, order_total=order_total, request=request) EdxOrderPlacementMixin().handle_post_order(order) challan_voucher_basket = BasketChallanVoucher.objects.filter( basket_id=basket.id) if len(challan_voucher_basket) > 1: logger.info( 'more than one basket exist with same id in challan table.' ) elif challan_voucher_basket: challan_voucher_basket.update(is_paid=True) logger.info( 'Successfully finished the cron job for updating the order payment' ) return
def test_get_set_thread_variable(self): set_thread_variable('test', { 'test': 'test'}) gotten = get_thread_variable('test') self.assertEqual(gotten, { 'test': 'test'})
def setUpClass(cls): super(TestCase, cls).setUpClass() set_thread_variable('request', None)
def tearDown(self): set_thread_variable("request", None)