def test_invalid_page_param(): url = '%s?%s' % (reverse('search'), 'page=a') request = test_utils.RequestFactory().get(url) queryset = range(100) paginated = paginate(request, queryset) eq_(paginated.url, request.build_absolute_uri(request.path) + '?')
def setUp(self): super(TestFlaggedUsersPremiumApps, self).setUp() self.req = test_utils.RequestFactory().get('/') self.req.user = self.user = UserProfile.objects.get(pk=2519).user self.filter_kwargs = { 'mobile': False, 'gaia': False, 'tablet': False, } self.query_string = { 'dev': 'android', 'device': 'mobile', } self.prem_exclude = { 'not': { 'filter': { 'in': { 'premium_type': (1, 2) } } } }
def test_logged_in_inactive(self): """Inactive user is denied access.""" request = test_utils.RequestFactory().get('/foo') request.user = user(is_active=False, save=True) view = permission_required('perm')(simple_view) response = view(request) eq_(403, response.status_code)
def test_logged_in_default(self): """Active user login.""" request = test_utils.RequestFactory().get('/foo') request.user = User.objects.get(username='******') view = login_required(simple_view) response = view(request) eq_(200, response.status_code)
def test_logged_in_argument(self): request = test_utils.RequestFactory().get('/foo') request.user = user(save=True) view = logout_required('/bar')(simple_view) response = view(request) eq_(302, response.status_code) eq_('/bar', response['location'])
def setUp(self): url = reverse('forums.threads', args=[u'test-forum']) self.context = {'request': test_utils.RequestFactory().get(url)} self.group = group(save=True) # Set up forum_1 f = self.forum_1 = forum(save=True) ct = ContentType.objects.get_for_model(self.forum_1) permission(codename='forums_forum.thread_edit_forum', content_type=ct, object_id=f.id, group=self.group, save=True) permission(codename='forums_forum.post_edit_forum', content_type=ct, object_id=f.id, group=self.group, save=True) permission(codename='forums_forum.post_delete_forum', content_type=ct, object_id=f.id, group=self.group, save=True) permission(codename='forums_forum.thread_delete_forum', content_type=ct, object_id=f.id, group=self.group, save=True) permission(codename='forums_forum.thread_sticky_forum', content_type=ct, object_id=f.id, group=self.group, save=True) permission(codename='forums_forum.thread_move_forum', content_type=ct, object_id=f.id, group=self.group, save=True) # Set up forum_2 f = self.forum_2 = forum(save=True) permission(codename='forums_forum.thread_move_forum', content_type=ct, object_id=f.id, group=self.group, save=True)
def test_logged_in_default(self): """Active user login.""" request = test_utils.RequestFactory().get('/foo') request.user = user(save=True) view = login_required(simple_view) response = view(request) eq_(200, response.status_code)
def test_logged_in_argument(self): request = test_utils.RequestFactory().get('/foo') request.user = User.objects.get(username='******') view = logout_required('/bar')(simple_view) response = view(request) eq_(302, response.status_code) eq_('/bar', response['location'])
def test_logged_in_inactive_allow(self): """Inactive user login explicitly allowed.""" request = test_utils.RequestFactory().get('/foo') request.user = user(is_active=False, save=True) view = login_required(simple_view, only_active=False) response = view(request) eq_(200, response.status_code)
def test_logged_in_inactive(self): """Inactive user login not allowed by default.""" request = test_utils.RequestFactory().get('/foo') request.user = user(is_active=False, save=True) view = login_required(simple_view) response = view(request) eq_(302, response.status_code)
def setUp(self): super(TestSearchFilters, self).setUp() self.req = test_utils.RequestFactory().get('/') self.req.user = AnonymousUser() self.category = Category.objects.create(name='games', type=amo.ADDON_WEBAPP)
def handle(self, *args, **options): # Not ideal, but we need to temporarily remove inline elemnents as a void/ignored element # TO DO: Can this clone code be shortened? new_void_set = set() for item in html5lib_constants.voidElements: new_void_set.add(item) new_void_set.remove('link') new_void_set.remove('img') html5lib_constants.voidElements = frozenset(new_void_set) # Create a mock request for the sake of rendering the template request = test_utils.RequestFactory().get('/') request.locale = settings.LANGUAGE_CODE # Load the page with sphinx template content = render(request, 'wiki/sphinx.html', {'is_sphinx': True, 'gettext': _}).content # Use a filter to make links absolute tool = (wiki.content.parse(content, is_full_document=True)) content = tool.absolutizeAddresses(base_url=settings.PRODUCTION_URL, tag_attributes={ 'a': 'href', 'img': 'src', 'form': 'action', 'link': 'href', 'script': 'src' }).serialize() # Output the response print content.encode('utf8')
def test_locale_url(): rf = test_utils.RequestFactory() request = rf.get('/de', SCRIPT_NAME='/z') prefixer = urlresolvers.Prefixer(request) urlresolvers.set_url_prefix(prefixer) s = render('{{ locale_url("mobile") }}') eq_(s, '/z/de/mobile')
def test_no_redirect_ajax(self): """Ajax requests should not redirect.""" request = test_utils.RequestFactory().get('/foo') request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' request.user = User.objects.get(username='******') view = logout_required(simple_view) response = view(request) eq_(403, response.status_code)
def test_no_redirect_ajax(self): """Ajax requests should not redirect.""" request = test_utils.RequestFactory().get('/foo') request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' request.user = AnonymousUser() view = login_required(simple_view) response = view(request) eq_(403, response.status_code)
def test_paginator_filter(): # Correct number of <li>s on page 1. url = reverse('search') request = test_utils.RequestFactory().get(url) pager = paginate(request, range(100), per_page=9) html = paginator(pager) doc = pyquery.PyQuery(html) eq_(11, len(doc('li'))) # Correct number of <li>s in the middle. url = '%s?%s' % (reverse('search'), 'page=10') request = test_utils.RequestFactory().get(url) pager = paginate(request, range(200), per_page=10) html = paginator(pager) doc = pyquery.PyQuery(html) eq_(13, len(doc('li')))
def test_paginated_url(): """Avoid duplicating page param in pagination.""" url = '%s?%s' % (reverse('search'), 'q=bookmarks&page=2') request = test_utils.RequestFactory().get(url) queryset = [{}, {}] paginated = paginate(request, queryset) eq_(paginated.url, request.build_absolute_uri(request.path) + '?q=bookmarks')
def setUp(self): super(TestSearchFilters, self).setUp() self.req = test_utils.RequestFactory().get('/') self.req.user = AnonymousUser() # Pick a region that has relatively few filters. set_region(regions.UK.slug) self.form_class = ApiSearchForm
def activate(self, locale): old_prefix = get_url_prefix() old_locale = translation.get_language() rf = test_utils.RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale, )))) translation.activate(locale) yield set_url_prefix(old_prefix) translation.activate(old_locale)
def setUp(self): super(TestSearchFilters, self).setUp() self.req = test_utils.RequestFactory().get('/') self.req.user = AnonymousUser() self.category = Category.objects.create(name='games', type=amo.ADDON_WEBAPP) # Pick a region that has relatively few filters. set_region(regions.UK.slug)
def activate(self, locale): """Context manager that temporarily activates a locale.""" old_prefix = get_url_prefix() old_locale = get_language() rf = test_utils.RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale, )))) activate(locale) yield set_url_prefix(old_prefix) activate(old_locale)
def setUp(self): self.timezone = timezone('US/Pacific') self.locale = 'en_US' url_ = reverse('forums.threads', args=['testslug']) self.context = {'request': test_utils.RequestFactory().get(url_)} self.context['request'].LANGUAGE_CODE = self.locale user_profile = profile(timezone=self.timezone, locale=self.locale) self.context['request'].user = user_profile self.context['request'].user.is_authenticated = Mock(return_value=True) self.context['request'].session = {'timezone': self.timezone}
def test_logged_in_inactive(self): """Inactive user is denied access.""" request = test_utils.RequestFactory().get('/foo') user = User.objects.get(username='******') user.is_active = False user.save() request.user = user view = permission_required('perm')(simple_view) response = view(request) eq_(403, response.status_code)
def test_logged_in_inactive(self): """Inactive user login not allowed by default.""" request = test_utils.RequestFactory().get('/foo') user = User.objects.get(username='******') user.is_active = False user.save() request.user = user view = login_required(simple_view) response = view(request) eq_(302, response.status_code)
def test_logged_in_inactive_allow(self): """Inactive user login explicitly allowed.""" request = test_utils.RequestFactory().get('/foo') user = User.objects.get(username='******') user.is_active = False user.save() request.user = user view = login_required(simple_view, only_active=False) response = view(request) eq_(200, response.status_code)
def test_script_name(self): rf = test_utils.RequestFactory() request = rf.get('/foo', SCRIPT_NAME='/oremj') prefixer = urlresolvers.Prefixer(request) eq_(prefixer.fix(prefixer.shortened_path), '/oremj/en-US/firefox/foo') # Now check reverse. urlresolvers.set_url_prefix(prefixer) set_script_prefix('/oremj') eq_(urlresolvers.reverse('home'), '/oremj/en-US/firefox/')
def test_prefixer_get_language(self): # Skipping this test for now, because it hits unreliable prod resources raise SkipTest() # language precedence is GET param > cookie > Accept-Language req = test_utils.RequestFactory().get('/', {'lang': 'es'}) prefixer = urlresolvers.Prefixer(req) eq_(prefixer.get_language(), 'es') req = test_utils.RequestFactory().get('/') req.COOKIES['lang'] = 'de' prefixer = urlresolvers.Prefixer(req) eq_(prefixer.get_language(), 'de') req = test_utils.RequestFactory().get('/') req.META['HTTP_ACCEPT_LANGUAGE'] = 'fr' prefixer = urlresolvers.Prefixer(req) eq_(prefixer.get_language(), 'fr')
def test_devmo_url_mindtouch_disabled(self): _old = settings.DEKIWIKI_ENDPOINT settings.DEKIWIKI_ENDPOINT = False localized_page = 'article-title' req = test_utils.RequestFactory().get('/') context = {'request': req} req.locale = 'fr' eq_(devmo_url(context, localized_page), '/fr/docs/le-title') settings.DEKIWIKI_ENDPOINT = _old
class SimplePaginatorTestCase(TestCase): rf = test_utils.RequestFactory() def test_no_explicit_page(self): """No 'page' query param implies page 1.""" request = self.rf.get('/questions') queryset = [{}, {}] page = simple_paginate(request, queryset, per_page=2) eq_(1, page.number) def test_page_1_without_next(self): """Test page=1, doesn't have next page.""" request = self.rf.get('/questions?page=1') queryset = [{}, {}] page = simple_paginate(request, queryset, per_page=2) eq_(1, page.number) assert not page.has_previous() assert not page.has_next() def test_page_1_with_next(self): """Test page=1, has next page.""" request = self.rf.get('/questions?page=1') queryset = [{}, {}, {}] page = simple_paginate(request, queryset, per_page=2) eq_(1, page.number) assert not page.has_previous() assert page.has_next() def test_page_2_without_next(self): """Test page=2, doesn't have next page.""" request = self.rf.get('/questions?page=2') queryset = [{}, {}, {}] page = simple_paginate(request, queryset, per_page=2) eq_(2, page.number) assert page.has_previous() assert not page.has_next() @raises(EmptyPage) def test_page_2_empty(self): """Test page=1, has next page.""" request = self.rf.get('/questions?page=2') queryset = [{}, {}] simple_paginate(request, queryset, per_page=2) @raises(PageNotAnInteger) def test_page_isnt_an_int(self): """Test page=1, has next page.""" request = self.rf.get('/questions?page=foo') queryset = [{}, {}] simple_paginate(request, queryset, per_page=2)
def test_cookie_not_set(self): """The anonymous cookie isn't set if it isn't created.""" # Create and process a request request = test_utils.RequestFactory().request() self.middleware.process_request(request) # Check if anonymous id is set (without creating one) request.anonymous.has_id # Create and process the response response = HttpResponse() response = self.middleware.process_response(request, response) # Make sure cookie was't set eq_(False, settings.ANONYMOUS_COOKIE_NAME in response.cookies)