def test_message_url_path_caching(self): self._restrict_course(self.course.id) # The first time we retrieve the message, we'll need # to hit the database. with self.assertNumQueries(2): embargo_api.message_url_path(self.course.id, "enrollment") # The second time, we should be using cached values with self.assertNumQueries(0): embargo_api.message_url_path(self.course.id, "enrollment")
def test_message_url_path_no_restrictions_for_course(self, access_point): # No restrictions for the course url_path = embargo_api.message_url_path(self.course.id, access_point) # Use a default path self.assertEqual(url_path, '/embargo/blocked-message/courseware/default/')
def test_message_url_stale_cache(self): # Retrieve the URL once, populating the cache with the list # of restricted courses. self._restrict_course(self.course.id) embargo_api.message_url_path(self.course.id, 'courseware') # Delete the restricted course entry RestrictedCourse.objects.get(course_key=self.course.id).delete() # Clear the message URL cache message_cache_key = ( 'embargo.message_url_path.courseware.{course_key}' ).format(course_key=self.course.id) cache.delete(message_cache_key) # Try again. Even though the cache results are stale, # we should still get a valid URL. url_path = embargo_api.message_url_path(self.course.id, 'courseware') self.assertEqual(url_path, '/embargo/blocked-message/courseware/default/')
def _third_party_auth_context(request): """Context for third party auth providers and the currently running pipeline. Arguments: request (HttpRequest): The request, used to determine if a pipeline is currently running. Returns: dict """ context = {"currentProvider": None, "providers": []} course_id = request.GET.get("course_id") email_opt_in = request.GET.get("email_opt_in") redirect_to = request.GET.get("next") # Check if the user is trying to enroll in a course # that they don't have access to based on country # access rules. # # If so, set the redirect URL to the blocked page. # We need to set it here, rather than redirecting # from within the pipeline, because a redirect # from the pipeline can prevent users # from completing the authentication process. # # Note that we can't check the user's country # profile at this point, since the user hasn't # authenticated. If the user ends up being blocked # by their country preference, we let them enroll; # they'll still be blocked when they try to access # the courseware. if course_id: try: course_key = CourseKey.from_string(course_id) redirect_url = embargo_api.redirect_if_blocked(course_key, ip_address=get_ip(request), url=request.path) if redirect_url: redirect_to = embargo_api.message_url_path(course_key, "enrollment") except InvalidKeyError: pass login_urls = auth_pipeline_urls( third_party_auth.pipeline.AUTH_ENTRY_LOGIN, course_id=course_id, email_opt_in=email_opt_in, redirect_url=redirect_to, ) register_urls = auth_pipeline_urls( third_party_auth.pipeline.AUTH_ENTRY_REGISTER, course_id=course_id, email_opt_in=email_opt_in, redirect_url=redirect_to, ) if third_party_auth.is_enabled(): context["providers"] = [ { "name": enabled.NAME, "iconClass": enabled.ICON_CLASS, "loginUrl": login_urls[enabled.NAME], "registerUrl": register_urls[enabled.NAME], } for enabled in third_party_auth.provider.Registry.enabled() ] running_pipeline = third_party_auth.pipeline.get(request) if running_pipeline is not None: current_provider = third_party_auth.provider.Registry.get_by_backend_name(running_pipeline.get("backend")) context["currentProvider"] = current_provider.NAME return context
def test_invalid_access_point(self): with self.assertRaises(InvalidAccessPoint): embargo_api.message_url_path(self.course.id, "invalid")
def test_message_url_path(self, access_point, expected_url_path): self._restrict_course(self.course.id) # Retrieve the URL to the blocked message page url_path = embargo_api.message_url_path(self.course.id, access_point) self.assertEqual(url_path, expected_url_path)
def _third_party_auth_context(request): """Context for third party auth providers and the currently running pipeline. Arguments: request (HttpRequest): The request, used to determine if a pipeline is currently running. Returns: dict """ context = { "currentProvider": None, "providers": [] } course_id = request.GET.get("course_id") email_opt_in = request.GET.get('email_opt_in') redirect_to = request.GET.get("next") # Check if the user is trying to enroll in a course # that they don't have access to based on country # access rules. # # If so, set the redirect URL to the blocked page. # We need to set it here, rather than redirecting # from within the pipeline, because a redirect # from the pipeline can prevent users # from completing the authentication process. # # Note that we can't check the user's country # profile at this point, since the user hasn't # authenticated. If the user ends up being blocked # by their country preference, we let them enroll; # they'll still be blocked when they try to access # the courseware. if course_id: try: course_key = CourseKey.from_string(course_id) redirect_url = embargo_api.redirect_if_blocked( course_key, ip_address=get_ip(request), url=request.path ) if redirect_url: redirect_to = embargo_api.message_url_path(course_key, "enrollment") except InvalidKeyError: pass login_urls = auth_pipeline_urls( third_party_auth.pipeline.AUTH_ENTRY_LOGIN, course_id=course_id, email_opt_in=email_opt_in, redirect_url=redirect_to ) register_urls = auth_pipeline_urls( third_party_auth.pipeline.AUTH_ENTRY_REGISTER, course_id=course_id, email_opt_in=email_opt_in, redirect_url=redirect_to ) if third_party_auth.is_enabled(): context["providers"] = [ { "name": enabled.NAME, "iconClass": enabled.ICON_CLASS, "loginUrl": login_urls[enabled.NAME], "registerUrl": register_urls[enabled.NAME] } for enabled in third_party_auth.provider.Registry.enabled() ] running_pipeline = third_party_auth.pipeline.get(request) if running_pipeline is not None: current_provider = third_party_auth.provider.Registry.get_by_backend_name( running_pipeline.get('backend') ) context["currentProvider"] = current_provider.NAME return context