@contextlib.contextmanager
def restrict_course(course_key, access_point='enrollment', disable_access_check=False):
 cache.clear()
 with mock.patch.object(pygeoip.GeoIP, 'country_code_by_addr') as mock_ip:
  CountryAccessRule.objects.all().delete()
  (country, __) = Country.objects.get_or_create(country='IR')
  (restricted_course, __) = RestrictedCourse.objects.get_or_create(course_key=course_key)
  restricted_course.enroll_msg_key = 'default'
  restricted_course.access_msg_key = 'default'
  restricted_course.disable_access_check = disable_access_check
  restricted_course.save()
  CountryAccessRule.objects.get_or_create(restricted_course=restricted_course, country=country, rule_type='blacklist')
  mock_ip.return_value = 'IR'
  redirect_url = reverse('embargo_blocked_message', kwargs={'access_point': access_point, 'message_key': 'default'})
  (yield redirect_url)

@ contextlib . contextmanager def restrict_course ( course_key , access_point = 'enrollment' , disable_access_check = False ) : cache . clear ( ) with mock . patch . object ( pygeoip . GeoIP , 'country_code_by_addr' ) as mock_ip : CountryAccessRule . objects . all ( ) . delete ( ) ( country , __ ) = Country . objects . get_or_create ( country = 'IR' ) ( restricted_course , __ ) = RestrictedCourse . objects . get_or_create ( course_key = course_key ) restricted_course . enroll_msg_key = 'default' restricted_course . access_msg_key = 'default' restricted_course . disable_access_check = disable_access_check restricted_course . save ( ) CountryAccessRule . objects . get_or_create ( restricted_course = restricted_course , country = country , rule_type = 'blacklist' ) mock_ip . return_value = 'IR' redirect_url = reverse ( 'embargo_blocked_message' , kwargs = { 'access_point' : access_point , 'message_key' : 'default' } ) ( yield redirect_url )
simulate that a course is restricted .