def test_frequent_by_date(self): """Test frequent terms by date.""" ts = Term.objects.frequent(date_end=self.split_date) eq_(ts[0].term, self.t1.term) ts = Term.objects.frequent(date_start=self.split_date) eq_(ts[0].term, self.t2.term)
def test_locale_fallback(self): """ Any specific flavor of a language (xx-YY) should return its generic site (xx) if it exists. """ patterns = ( ('fR,de;q=0.7', 'fr'), ('zh,fr;q=0.7', 'zh-CN'), ('eN,fr;q=0.7', 'en-US'), ('en,de;q=0.8', 'en-US'), ('en-us,en;q=0.7,de;q=0.8', 'en-US'), ('fr-FR,de-DE;q=0.5', 'fr'), ('zh, en-us;q=0.8, en;q=0.6', 'zh-CN'), ('xx-YY,es-ES;q=0.7,de-DE;q=0.5', 'es'), ('German', 'en-US'), # invalid # bug 582075 ('nb,no;q=0.8,nn;q=0.6,en-us;q=0.4,en;q=0.2', 'nb-NO'), # bug 629115 ('en-US,en;q=0.9,ro;q=0.8,ja;q=0.7,fr;q=0.6', 'en-US'), ) for pattern in patterns: r = self.client.get('/', HTTP_ACCEPT_LANGUAGE=pattern[0]) eq_(r.status_code, 301) did_it_work = r['Location'].rstrip('/').endswith(pattern[1]) self.assertTrue(did_it_work, "%s didn't match %s" % pattern)
def test_set_domains_no_args(self): Site.objects.create(pk=1, domain='hi', name='hi') Site.objects.create(pk=2, domain='there', name='there') cron.set_domains(None, None) # Assert NOOP eq_(Site.objects.get(id=1).domain, 'hi') eq_(Site.objects.get(id=2).domain, 'there')
def test_product_name(): """Show product name or ID if unknown.""" o = Opinion() o.product = FIREFOX.id eq_(o.product_name, FIREFOX.pretty) o.product = 17 # Unknown ID eq_(o.product_name, 17)
def test_platform_name(): """Show platform name or ID if unknown.""" o = Opinion() o.platform = 'win7' eq_(o.platform_name, WINDOWS_7.pretty) o.platform = 'win25' # Unknown ID eq_(o.platform_name, 'win25')
def test_trans_interpolation(): """Trans block with interpolation should be escaped.""" s = """ {% trans what="<a>" %} this is a <b>{{ what }}</b> {% endtrans %} """.strip() eq_(render(s), 'this is a <b><a></b>')
def test_cached_property(self): class A(object): _foo = 1 @decorators.cached_property def foo(self): return self._foo a = A() eq_(a.foo, 1) a._foo = 2 eq_(a.foo, 1)
def test_mobilesite_detection(self, mock): """Make sure we switch to the mobile site if the domain matches.""" def side_effect(*args, **kwargs): class FakeSite(object): id = settings.MOBILE_SITE_ID return FakeSite() mock.side_effect = side_effect # Get the front page. Since we mocked the Site model, the URL we # pass here does not matter. self.client.get('/', HTTP_HOST='m.example.com') eq_(settings.SITE_ID, settings.MOBILE_SITE_ID)
def test_channel_in_get(self): for ch in CHANNELS: request = Mock() request.path_info = '/' request.GET = dict(channel=ch) p = urlresolvers.Prefixer(request) eq_(p.get_channel(), ch) # Invalid channel request = Mock() request.path_info = '/' request.GET = dict(channel='bogus') p = urlresolvers.Prefixer(request) eq_(p.get_channel(), settings.DEFAULT_CHANNEL)
def test_forward_mobile(self, mock): fake_mobile_domain = 'mymobiledomain.example.com' def side_effect(*args, **kwargs): class FakeSite(object): id = settings.MOBILE_SITE_ID domain = fake_mobile_domain return FakeSite() mock.side_effect = side_effect r = self.mclient.get(reverse('dashboard') + '?foo=bar') eq_(r.status_code, 302) eq_(r['Location'], 'http://' + fake_mobile_domain + reverse('dashboard') + '?foo=bar')
def test_mobile_device_detection(self, mock): """ Requests to front page and submission pages should forward mobile users to mobile site. """ fake_mobile_domain = 'mymobiledomain.example.com' def side_effect(*args, **kwargs): class FakeSite(object): id = settings.MOBILE_SITE_ID domain = fake_mobile_domain return FakeSite() mock.side_effect = side_effect # URLs that should allow Mobile detection urls = ( reverse('dashboard', channel='beta'), reverse('feedback.happy'), reverse('feedback.sad'), reverse('feedback.idea'), reverse('feedback'), ) # User Agent Patterns: (UA, forward: true/false?) ua_patterns = ( # Fx ('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; ' 'rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13', False), # MSIE ('Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)', False), # Fennec ('Mozilla/5.0 (X11; U; Linux armv6l; fr; rv:1.9.1b1pre) Gecko/' '20081005220218 Gecko/2008052201 Fennec/0.9pre', True), # iPod touch ('Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 ' '(KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3', True), ) for test_url in urls: for ua, forward_this in ua_patterns: r = self.client.get(test_url, HTTP_USER_AGENT=ua) if forward_this: eq_(r.status_code, 301) assert r['Location'].find(fake_mobile_domain) >= 0 else: assert (r.status_code == 200 or # Page is served, or: r.status_code / 100 == 3 and # some redirect... # ... but not to the mobile domain. r['Location'].find(fake_mobile_domain) == -1)
def test_mobile_device_detection(self, mock): """ Requests to front page and submission pages should forward mobile users to mobile site. """ fake_mobile_domain = 'mymobiledomain.example.com' def side_effect(*args, **kwargs): class FakeSite(object): id = settings.MOBILE_SITE_ID domain = fake_mobile_domain return FakeSite() mock.side_effect = side_effect # URLs that should allow Mobile detection urls = ( reverse('dashboard'), reverse('feedback'), ) # User Agent Patterns: (UA, forward: true/false?) ua_patterns = ( # Fx ('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; ' 'rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13', False), # MSIE ('Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)', False ), # Fennec ('Mozilla/5.0 (X11; U; Linux armv6l; fr; rv:1.9.1b1pre) Gecko/' '20081005220218 Gecko/2008052201 Fennec/0.9pre', True), # iPod touch ('Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 ' '(KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3', True ), ) for test_url in urls: for ua, forward_this in ua_patterns: r = self.client.get(test_url, HTTP_USER_AGENT=ua) if forward_this: eq_(r.status_code, 302, test_url) assert r['Location'].find(fake_mobile_domain) >= 0 else: assert (r.status_code == 200 or # Page is served, or: r.status_code / 100 == 3 and # some redirect... # ... but not to the mobile domain. r['Location'].find(fake_mobile_domain) == -1)
def test_split_queryset(): """Split a queryset into pieces.""" # Mock queryset object: List with qs-style count function. class MockQs(list): def count(self): return len(self) bucket_size = api.cron.BUCKET_SIZE try: api.cron.BUCKET_SIZE = 10 my_qs = MockQs(range(100)) splits = list(_split_queryset(my_qs)) eq_(len(splits), 10) finally: api.cron.BUCKET_SIZE = bucket_size
def test_forward_mobile(self, mock): fake_mobile_domain = 'mymobiledomain.example.com' populate(1) id = Opinion.objects.values_list('id', flat=True)[0] def side_effect(*args, **kwargs): class FakeSite(object): id = settings.MOBILE_SITE_ID domain = fake_mobile_domain return FakeSite() mock.side_effect = side_effect r = self.mclient.get(reverse('opinion.detail', args=[id]) + '?foo=bar') eq_(r.status_code, 302) eq_(r['Location'], 'http://' + fake_mobile_domain + reverse('opinion.detail', args=[id]) + '?foo=bar')
def test_forward_mobile(self, mock): fake_mobile_domain = 'mymobiledomain.example.com' populate(1) id = Opinion.objects.values_list('id', flat=True)[0] def side_effect(*args, **kwargs): class FakeSite(object): id = settings.MOBILE_SITE_ID domain = fake_mobile_domain return FakeSite() mock.side_effect = side_effect r = self.mclient.get(reverse('opinion.detail', args=[id]) + '?foo=bar') eq_(r.status_code, 302) eq_( r['Location'], 'http://' + fake_mobile_domain + reverse('opinion.detail', args=[id]) + '?foo=bar')
def test_set_domains(self): Site.objects.create(pk=1, domain='hi', name='hi') Site.objects.create(pk=2, domain='there', name='there') cron.set_domains('f', 'u') eq_(Site.objects.get(id=1).domain, 'f') eq_(Site.objects.get(id=2).domain, 'u')
def test_absolute_url(self): """Build an absolute URL from a relative one.""" request = HttpRequest() request.META = {'HTTP_HOST': 'example.com'} r = render('{{ absolute_url("/somewhere") }}', {'request': request}) eq_(r, 'http://example.com/somewhere')
def test_frequent_stats(self): """Test frequent terms weights.""" ts = Term.objects.frequent() freq = frequent_terms(qs=ts) eq_(len(freq), 2)
def test_term_extraction(self): """Make sure we create our terms.""" settings.DISABLE_TERMS = False op = Opinion.objects.create(product=1, description='This is a test') terms = [term.term for term in op.terms.all()] eq_(terms, ['test'])
def test_locale_name(): eq_(locale_name('de'), 'German') eq_(locale_name('de', native=True), 'Deutsch') eq_(locale_name('omg'), 'Unknown')
def test_locale_in_get(self): request = Mock() request.path_info = '/' request.GET = dict(lang='en-US') p = urlresolvers.Prefixer(request) eq_(p.get_language(), 'en-US')
def test_redirect_with_locale(self): r = self.client.get(reverse('search') + '?lang=fr') eq_(r['Location'], 'http://testserver/fr/')
def test_no_prefixer(self): urlresolvers.clean_url_prefixes() eq_(urlresolvers.reverse('feedback'), '/feedback')
def test_get_format(self, get_language): """Ensure unknown locale falls back to default locale in get_format.""" get_language.return_value = 'fuuuuu' eq_(str(helpers._get_format().locale), 'en_US')
def test_redirect_with_querystring(self): r = self.client.get('/?foo=bar') eq_(r['Location'], 'http://testserver/en-US/?foo=bar')
def test_mobilesite_nohost(self): """Make sure we serve the desktop site if there's no HTTP_HOST set.""" # This won't contain HTTP_HOST. Must not fail. self.client.get('/') eq_(settings.SITE_ID, settings.DESKTOP_SITE_ID)
def test_isotime_fake_time(self): eq_(helpers.isotime(None), None)
def ts(delta, expected): """Test timesince string for the given delta.""" test_time = datetime.now() - timedelta(**delta) eq_(timesince(test_time), expected)
def test_smiley(): assert smiley('happy').find('span') >= 0 assert smiley('sad').find('span') >= 0 eq_(smiley('cheesecake'), '')
def test_almost_locale(self): request = Mock() request.path_info = '/en/' p = urlresolvers.Prefixer(request) eq_(p.locale, 'en-US')
def test_redirect_with_locale(self): r = self.client.get(reverse('dashboard', channel='beta') + '?lang=fr') eq_(r['Location'], 'http://testserver/fr/beta/')
def test_x_frame_options(self): """Ensure X-Frame-Options middleware works as expected.""" r = self.client.get('/') eq_(r['x-frame-options'], 'DENY')
def test_no_locale(self): request = Mock() request.path_info = '/beta' p = urlresolvers.Prefixer(request) eq_(p.locale, '')
def test_fake_locale(self): r = self.factory.get('/zf/beta') p = urlresolvers.Prefixer(r) eq_(p.locale, '')
def test_unicode(self): """Term's unicode representation.""" t = Term(term='Hello') eq_(unicode(t), u'Hello')
def test_mobilesite_nohost(self): """Make sure we serve the desktop site if there's no HTTP_HOST set.""" # This won't contain HTTP_HOST. Must not fail. self.client.get('/', follow=True) eq_(settings.SITE_ID, settings.DESKTOP_SITE_ID)
def test_almost_locale_with_channel(self): request = Mock() request.path_info = '/en/release' p = urlresolvers.Prefixer(request) eq_(p.locale, 'en-US') eq_(p.channel, 'release')