provide *_ajax methods on the test client to remove ugliness

This commit is contained in:
Andy McKay 2011-08-25 11:00:30 -07:00
Родитель f1b7a0dfcb
Коммит 134c30aa60
4 изменённых файлов: 38 добавлений и 36 удалений

Просмотреть файл

@ -606,8 +606,8 @@ class TestDetailPage(amo.tests.TestCase):
def test_search_engine_works_with(self):
"""We don't display works-with info for search engines."""
addon = Addon.objects.filter(type=amo.ADDON_SEARCH)[0]
r = self.client.get(reverse('addons.detail_more', args=[addon.slug]),
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.get_ajax(reverse('addons.detail_more',
args=[addon.slug]))
headings = pq(r.content)('#detail-relnotes .item-info h5')
assert not any(th.text.strip().lower() == 'works with:'
for th in headings)
@ -837,9 +837,8 @@ class TestTagsBox(amo.tests.TestCase):
def test_tag_box(self):
"""Verify that we don't show duplicate tags."""
r = self.client.get(reverse('addons.detail_more', args=[8680]),
follow=True,
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.get_ajax(reverse('addons.detail_more', args=[8680]),
follow=True)
doc = pq(r.content)
eq_('SEO', doc('#tagbox ul').children().text())

Просмотреть файл

@ -1,4 +1,5 @@
from datetime import datetime, timedelta
from functools import partial
import os
import random
import shutil
@ -6,6 +7,7 @@ import time
from django import forms
from django.conf import settings
from django.test.client import Client
import elasticutils
import nose
@ -68,8 +70,23 @@ class RedisTest(object):
reset_redis(self._redis)
class TestClient(Client):
def __getattr__(self, name):
"""
Provides get_ajax, post_ajax, head_ajax methods etc in the
test_client so that you don't need to specify the headers.
"""
if name.endswith('_ajax'):
method = getattr(self, name.split('_')[0])
return partial(method, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
else:
raise AttributeError
class TestCase(RedisTest, test_utils.TestCase):
"""Base class for all amo tests."""
client_class = TestClient
def _pre_setup(self):
super(TestCase, self)._pre_setup()

Просмотреть файл

@ -255,8 +255,7 @@ class TestVotes(amo.tests.TestCase):
self.assertRedirects(r, self.c_url)
def test_ajax_response(self):
r = self.client.post(self.up, follow=True,
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.up, follow=True)
assert not r.redirect_chain
eq_(r.status_code, 200)
@ -615,8 +614,7 @@ class TestChangeAddon(amo.tests.TestCase):
def test_publisher(self):
CollectionUser.objects.create(user_id=4043307, collection=self.flig)
r = self.client.post(self.flig_add, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.flig_add, {'addon_id': self.addon.id})
self.check_redirect(r)
def test_no_addon(self):
@ -624,8 +622,7 @@ class TestChangeAddon(amo.tests.TestCase):
eq_(r.status_code, 400)
def test_add_success(self):
r = self.client.post(self.add, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.add, {'addon_id': self.addon.id})
self.check_redirect(r)
c = Collection.objects.get(author__username='jbalogh', slug='mobile')
self.assert_(self.addon in c.addons.all())
@ -635,17 +632,14 @@ class TestChangeAddon(amo.tests.TestCase):
"""
When we add to a private collection, make sure we don't log anything.
"""
self.client.post(self.add, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.client.post_ajax(self.add, {'addon_id': self.addon.id})
# There should be no log objects for this add-on
eq_(len(ActivityLog.objects.for_addons(self.addon)), 0)
def test_add_existing(self):
r = self.client.post(self.add, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.add, {'addon_id': self.addon.id})
self.check_redirect(r)
r = self.client.post(self.add, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.add, {'addon_id': self.addon.id})
self.check_redirect(r)
c = Collection.objects.get(author__username='jbalogh', slug='mobile')
self.assert_(self.addon in c.addons.all())
@ -656,28 +650,23 @@ class TestChangeAddon(amo.tests.TestCase):
When we remove from a private collection, make sure we don't log
anything.
"""
self.client.post(self.add, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.client.post(self.remove, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.client.post_ajax(self.add, {'addon_id': self.addon.id})
self.client.post_ajax(self.remove, {'addon_id': self.addon.id})
# There should be no log objects for this add-on
eq_(len(ActivityLog.objects.for_addons(self.addon)), 0)
def test_remove_success(self):
r = self.client.post(self.add, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.add, {'addon_id': self.addon.id})
self.check_redirect(r)
r = self.client.post(self.remove, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.remove, {'addon_id': self.addon.id})
self.check_redirect(r)
c = Collection.objects.get(author__username='jbalogh', slug='mobile')
eq_(c.addons.count(), 0)
def test_remove_nonexistent(self):
r = self.client.post(self.remove, {'addon_id': self.addon.id},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.remove, {'addon_id': self.addon.id})
self.check_redirect(r)
c = Collection.objects.get(author__username='jbalogh', slug='mobile')
eq_(c.addons.count(), 0)
@ -707,9 +696,8 @@ class AjaxTest(amo.tests.TestCase):
eq_(doc('li').attr('data-id'), '80')
def test_add_collection(self):
r = self.client.post(reverse('collections.ajax_add'),
{'addon_id': 3615, 'id': 80}, follow=True,
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(reverse('collections.ajax_add'),
{'addon_id': 3615, 'id': 80}, follow=True)
doc = pq(r.content)
eq_(doc('li.selected').attr('data-id'), '80')
@ -795,8 +783,7 @@ class TestWatching(amo.tests.TestCase):
eq_(r.context['amo_user'].watching, [57181])
def test_ajax_response(self):
r = self.client.post(self.url, follow=True,
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.post_ajax(self.url, follow=True)
eq_(r.status_code, 200)
eq_(json.loads(r.content), {'watching': True})
@ -898,7 +885,6 @@ class TestImpalaCollectionListing(amo.tests.TestCase):
eq_('followers' in doc('.items .item .followers').text(), True)
class TestCollectionDetailFeed(amo.tests.TestCase):
fixtures = ['base/collection_57181']

Просмотреть файл

@ -15,7 +15,7 @@ class TestManagement(amo.tests.TestCase):
def test_tags_details_view(self):
"""Test that there are some tags being shown on the details page."""
url = reverse('addons.detail_more', args=['a3615'])
r = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.get_ajax(url)
doc = pq(r.content)
eq_(len(doc('li.tag')), 4)
assert 'Tags' in [d.text for d in doc('h3')]
@ -37,7 +37,7 @@ class TestXSS(amo.tests.TestCase):
def test_tags_xss_detail(self):
"""Test xss tag detail."""
url = reverse('addons.detail_more', args=['a3615'])
r = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.get_ajax(url)
doc = pq(r.content)
eq_(doc('li.tag')[0].text_content().strip(), self.xss)
@ -65,7 +65,7 @@ class TestXSSURLFail(amo.tests.TestCase):
def test_tags_xss(self):
"""Test xss tag detail."""
url = reverse('addons.detail_more', args=['a3615'])
r = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
r = self.client.get_ajax(url)
doc = pq(r.content)
eq_(doc('li.tag')[0].text_content().strip(), self.xss)