diff --git a/docs/tests.rst b/docs/tests.rst index 062c3f570..90f8ea18d 100644 --- a/docs/tests.rst +++ b/docs/tests.rst @@ -98,9 +98,6 @@ cover the new functionality. All apps have a ``tests`` module where tests should go. They will be discovered automatically by the test runner as long as the look like a test. -* Avoid naming test files ``test_utils.py``, since we use a library - with the same name. Use ``test__utils.py`` instead. - * If you're expecting ``reverse`` to return locales in the URL, use ``LocalizingClient`` instead of the default client for the ``TestCase`` class. diff --git a/kitsune/access/tests/__init__.py b/kitsune/access/tests/__init__.py index d32de8823..8581bacec 100644 --- a/kitsune/access/tests/__init__.py +++ b/kitsune/access/tests/__init__.py @@ -1,6 +1,6 @@ from django.contrib.contenttypes.models import ContentType +from django.test.client import RequestFactory -import test_utils from authority.models import Permission from nose.tools import eq_ @@ -21,7 +21,7 @@ class AccessTests(TestCase): """Test stuff in access/__init__.py""" def setUp(self): url = reverse('forums.threads', args=[u'test-forum']) - self.context = {'request': test_utils.RequestFactory().get(url)} + self.context = {'request': RequestFactory().get(url)} def test_admin_perm_thread(self): """Super user can do anything on any forum.""" diff --git a/kitsune/access/tests/test_decorators.py b/kitsune/access/tests/test_decorators.py index 321c6ac77..30c49ad8e 100644 --- a/kitsune/access/tests/test_decorators.py +++ b/kitsune/access/tests/test_decorators.py @@ -1,7 +1,7 @@ from django.contrib.auth.models import AnonymousUser from django.http import HttpResponse +from django.test.client import RequestFactory -import test_utils from nose.tools import eq_ from kitsune.access.decorators import ( @@ -16,21 +16,21 @@ def simple_view(request): class LogoutRequiredTestCase(TestCase): def test_logged_out_default(self): - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = AnonymousUser() view = logout_required(simple_view) response = view(request) eq_(200, response.status_code) def test_logged_in_default(self): - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = user(save=True) view = logout_required(simple_view) response = view(request) eq_(302, response.status_code) def test_logged_in_argument(self): - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = user(save=True) view = logout_required('/bar')(simple_view) response = view(request) @@ -39,7 +39,7 @@ class LogoutRequiredTestCase(TestCase): def test_no_redirect_ajax(self): """Ajax requests should not redirect.""" - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' request.user = user(save=True) view = logout_required(simple_view) @@ -49,7 +49,7 @@ class LogoutRequiredTestCase(TestCase): class LoginRequiredTestCase(TestCase): def test_logged_out_default(self): - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = AnonymousUser() view = login_required(simple_view) response = view(request) @@ -57,7 +57,7 @@ class LoginRequiredTestCase(TestCase): def test_logged_in_default(self): """Active user login.""" - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = user(save=True) view = login_required(simple_view) response = view(request) @@ -65,7 +65,7 @@ class LoginRequiredTestCase(TestCase): def test_logged_in_inactive(self): """Inactive user login not allowed by default.""" - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = user(is_active=False, save=True) view = login_required(simple_view) response = view(request) @@ -73,7 +73,7 @@ class LoginRequiredTestCase(TestCase): def test_logged_in_inactive_allow(self): """Inactive user login explicitly allowed.""" - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = user(is_active=False, save=True) view = login_required(simple_view, only_active=False) response = view(request) @@ -81,7 +81,7 @@ class LoginRequiredTestCase(TestCase): def test_no_redirect_ajax(self): """Ajax requests should not redirect.""" - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' request.user = AnonymousUser() view = login_required(simple_view) @@ -91,14 +91,14 @@ class LoginRequiredTestCase(TestCase): class PermissionRequiredTestCase(TestCase): def test_logged_out_default(self): - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = AnonymousUser() view = permission_required('perm')(simple_view) response = view(request) eq_(302, response.status_code) def test_logged_in_default(self): - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = user(save=True) view = permission_required('perm')(simple_view) response = view(request) @@ -106,14 +106,14 @@ class PermissionRequiredTestCase(TestCase): def test_logged_in_inactive(self): """Inactive user is denied access.""" - request = test_utils.RequestFactory().get('/foo') + request = 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_admin(self): - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.user = user(is_staff=True, is_superuser=True, save=True) view = permission_required('perm')(simple_view) response = view(request) @@ -121,7 +121,7 @@ class PermissionRequiredTestCase(TestCase): def test_no_redirect_ajax(self): """Ajax requests should not redirect.""" - request = test_utils.RequestFactory().get('/foo') + request = RequestFactory().get('/foo') request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' request.user = AnonymousUser() view = permission_required('perm')(simple_view) diff --git a/kitsune/community/tests/test__utils.py b/kitsune/community/tests/test_utils.py similarity index 100% rename from kitsune/community/tests/test__utils.py rename to kitsune/community/tests/test_utils.py diff --git a/kitsune/customercare/tests/test_views.py b/kitsune/customercare/tests/test_views.py index ef286b0c6..b65bba71a 100644 --- a/kitsune/customercare/tests/test_views.py +++ b/kitsune/customercare/tests/test_views.py @@ -2,10 +2,10 @@ from datetime import datetime, timedelta import json from django.conf import settings +from django.test.client import RequestFactory from mock import patch, Mock from nose.tools import eq_ -from test_utils import RequestFactory from kitsune.customercare.models import Tweet, Reply from kitsune.customercare.tests import tweet, twitter_account, reply diff --git a/kitsune/dashboards/tests/test__utils.py b/kitsune/dashboards/tests/test_utils.py similarity index 100% rename from kitsune/dashboards/tests/test__utils.py rename to kitsune/dashboards/tests/test_utils.py diff --git a/kitsune/forums/tests/test_notifications.py b/kitsune/forums/tests/test_notifications.py index c6f920c57..418ef2a18 100644 --- a/kitsune/forums/tests/test_notifications.py +++ b/kitsune/forums/tests/test_notifications.py @@ -3,10 +3,10 @@ from django.contrib.admin.options import ModelAdmin from django.contrib.auth.models import User from django.contrib.sites.models import Site from django.core import mail +from django.test.client import RequestFactory import mock from nose.tools import eq_ -import test_utils from kitsune.forums.events import NewPostEvent, NewThreadEvent from kitsune.forums.models import Thread, Post @@ -340,7 +340,7 @@ class NotificationsTests(ForumTestCase): self.client.login(username=admin_user.username, password='testpass') self._toggle_watch_thread_as(t, watcher, turn_on=True) url = reverse('admin:auth_user_delete', args=[u.id]) - request = test_utils.RequestFactory().get(url) + request = RequestFactory().get(url) request.user = admin_user request.session = self.client.session # The following blows up without our monkeypatch. diff --git a/kitsune/forums/tests/test_permissions.py b/kitsune/forums/tests/test_permissions.py index 9b6808b36..bef841ca2 100644 --- a/kitsune/forums/tests/test_permissions.py +++ b/kitsune/forums/tests/test_permissions.py @@ -1,6 +1,5 @@ from django.contrib.contenttypes.models import ContentType - -import test_utils +from django.test.client import RequestFactory from kitsune.access.helpers import has_perm, has_perm_or_owns from kitsune.access.tests import permission @@ -14,7 +13,7 @@ class ForumTestPermissions(ForumTestCase): def setUp(self): url = reverse('forums.threads', args=[u'test-forum']) - self.context = {'request': test_utils.RequestFactory().get(url)} + self.context = {'request': RequestFactory().get(url)} self.group = group(save=True) diff --git a/kitsune/gallery/tests/test__utils.py b/kitsune/gallery/tests/test_utils.py similarity index 100% rename from kitsune/gallery/tests/test__utils.py rename to kitsune/gallery/tests/test_utils.py diff --git a/kitsune/messages/tests/test_context_processors.py b/kitsune/messages/tests/test_context_processors.py index c06fd8542..70c83bcfc 100644 --- a/kitsune/messages/tests/test_context_processors.py +++ b/kitsune/messages/tests/test_context_processors.py @@ -1,8 +1,8 @@ from django.contrib.auth.models import AnonymousUser +from django.test.client import RequestFactory import mock from nose.tools import eq_ -from test_utils import RequestFactory from kitsune import messages from kitsune.messages.context_processors import unread_message_count diff --git a/kitsune/questions/tests/test__utils.py b/kitsune/questions/tests/test_utils.py similarity index 100% rename from kitsune/questions/tests/test__utils.py rename to kitsune/questions/tests/test_utils.py diff --git a/kitsune/search/tests/__init__.py b/kitsune/search/tests/__init__.py index fce4891f7..df34bb1c4 100644 --- a/kitsune/search/tests/__init__.py +++ b/kitsune/search/tests/__init__.py @@ -4,15 +4,13 @@ from django.test.utils import override_settings from elasticutils.contrib.django import get_es from nose import SkipTest -from test_utils import TestCase from kitsune.search import es_utils from kitsune.search.models import generate_tasks, Synonym -from kitsune.sumo.tests import with_save +from kitsune.sumo.tests import with_save, TestCase # Dummy request for passing to question_searcher() and brethren. -# There's no reason to use test_utils' RequestFactory. dummy_request = RequestFactory().get('/') diff --git a/kitsune/search/tests/test__utils.py b/kitsune/search/tests/test_utils.py similarity index 100% rename from kitsune/search/tests/test__utils.py rename to kitsune/search/tests/test_utils.py diff --git a/kitsune/settings.py b/kitsune/settings.py index 4e666543c..7d33c7e03 100644 --- a/kitsune/settings.py +++ b/kitsune/settings.py @@ -564,7 +564,6 @@ INSTALLED_APPS = ( # Extra apps for testing. 'django_nose', - 'test_utils', # Extra app for python migrations. 'django_extensions', diff --git a/kitsune/sumo/email_utils.py b/kitsune/sumo/email_utils.py index d0ba6f684..5ae0f6dd2 100644 --- a/kitsune/sumo/email_utils.py +++ b/kitsune/sumo/email_utils.py @@ -9,7 +9,7 @@ from django.utils import translation import jingo from premailer import transform -from test_utils import RequestFactory +from django.test.client import RequestFactory from kitsune.sumo.utils import uselocale diff --git a/kitsune/sumo/tests/__init__.py b/kitsune/sumo/tests/__init__.py index 2dcb174f7..aeb0a422b 100644 --- a/kitsune/sumo/tests/__init__.py +++ b/kitsune/sumo/tests/__init__.py @@ -5,10 +5,12 @@ from os.path import join, dirname from smtplib import SMTPRecipientsRefused from django.conf import settings +from django.core.cache import cache from django.core.management import call_command -from django.test import LiveServerTestCase +from django.test import LiveServerTestCase, TestCase as OriginalTestCase from django.test.client import Client from django.test.utils import override_settings +from django.utils.translation import trans_real import django_nose from nose import SkipTest @@ -16,7 +18,6 @@ from nose.tools import eq_ from selenium import webdriver from selenium.common.exceptions import WebDriverException from selenium.webdriver.firefox import firefox_binary -from test_utils import TestCase as OriginalTestCase from kitsune import sumo from kitsune.sumo.urlresolvers import reverse, split_path @@ -51,8 +52,13 @@ class TestSuiteRunner(django_nose.NoseTestSuiteRunner): @override_settings(ES_LIVE_INDEX=False) class TestCase(OriginalTestCase): - """A modification of ``test_utils.TestCase`` that skips live indexing.""" - pass + """TestCase that skips live indexing.""" + def _pre_setup(self): + cache.clear() + trans_real.deactivate() + trans_real._translations = {} # Django fails to clear this cache. + trans_real.activate(settings.LANGUAGE_CODE) + super(TestCase, self)._pre_setup() def attrs_eq(received, **expected): diff --git a/kitsune/sumo/tests/test_anonymous_middleware.py b/kitsune/sumo/tests/test_anonymous_middleware.py index 75aad0e3f..21d863a95 100644 --- a/kitsune/sumo/tests/test_anonymous_middleware.py +++ b/kitsune/sumo/tests/test_anonymous_middleware.py @@ -1,9 +1,8 @@ from nose.tools import eq_ -from django.http import HttpResponse from django.conf import settings - -import test_utils +from django.http import HttpResponse +from django.test.client import RequestFactory from kitsune.sumo.anonymous import AnonymousIdentityMiddleware from kitsune.sumo.tests import TestCase @@ -19,7 +18,7 @@ class TestAnonymousMiddleware(TestCase): def test_cookie_set(self): """The anonymous cookie is set when the anonymous id is created.""" # Create and process a request - request = test_utils.RequestFactory().request() + request = RequestFactory().request() self.middleware.process_request(request) # Make sure anonymous id isn't set then access it to generate it @@ -38,7 +37,7 @@ class TestAnonymousMiddleware(TestCase): 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() + request = RequestFactory().request() self.middleware.process_request(request) # Check if anonymous id is set (without creating one) @@ -55,7 +54,7 @@ class TestAnonymousMiddleware(TestCase): """Anonymous cookie is already set. Make sure the value is read correctly and a new one isn't set.""" # Create, add the anonymous cookie and process a request - request = test_utils.RequestFactory().request() + request = RequestFactory().request() anon_id = '63de20c227be94560e3c679330c678ee' request.COOKIES[settings.ANONYMOUS_COOKIE_NAME] = anon_id self.middleware.process_request(request) diff --git a/kitsune/sumo/tests/test_context_processors.py b/kitsune/sumo/tests/test_context_processors.py index c8e2208b0..a8105df5f 100644 --- a/kitsune/sumo/tests/test_context_processors.py +++ b/kitsune/sumo/tests/test_context_processors.py @@ -1,5 +1,6 @@ +from django.test.client import RequestFactory + from nose.tools import eq_ -from test_utils import RequestFactory from kitsune.sumo.tests import TestCase from kitsune.sumo.context_processors import geoip_cache_detector diff --git a/kitsune/sumo/tests/test_helpers.py b/kitsune/sumo/tests/test_helpers.py index 74dab8f94..d707f0b27 100644 --- a/kitsune/sumo/tests/test_helpers.py +++ b/kitsune/sumo/tests/test_helpers.py @@ -3,9 +3,9 @@ from collections import namedtuple from datetime import datetime from django.forms.fields import CharField +from django.test.client import RequestFactory import jingo -import test_utils from babel.dates import format_date, format_time, format_datetime from mock import Mock from nose.tools import eq_, assert_raises @@ -100,7 +100,7 @@ class TestDateTimeFormat(TestCase): 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': 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 diff --git a/kitsune/sumo/tests/test_middleware.py b/kitsune/sumo/tests/test_middleware.py index f6176e93d..c17bed8e0 100644 --- a/kitsune/sumo/tests/test_middleware.py +++ b/kitsune/sumo/tests/test_middleware.py @@ -1,8 +1,8 @@ from django.http import HttpResponsePermanentRedirect +from django.test.client import RequestFactory import mobility from nose.tools import eq_ -from test_utils import RequestFactory from kitsune.sumo.middleware import ( PlusToSpaceMiddleware, DetectMobileMiddleware) diff --git a/kitsune/sumo/tests/test_pagination.py b/kitsune/sumo/tests/test_pagination.py index 2d9390084..18e1750a8 100644 --- a/kitsune/sumo/tests/test_pagination.py +++ b/kitsune/sumo/tests/test_pagination.py @@ -1,4 +1,5 @@ -import test_utils +from django.test.client import RequestFactory + import pyquery from nose.tools import eq_, raises @@ -12,7 +13,7 @@ from kitsune.sumo.utils import paginate, simple_paginate 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) + request = RequestFactory().get(url) queryset = [{}, {}] paginated = paginate(request, queryset) eq_(paginated.url, @@ -21,7 +22,7 @@ def test_paginated_url(): def test_invalid_page_param(): url = '%s?%s' % (reverse('search'), 'page=a') - request = test_utils.RequestFactory().get(url) + request = RequestFactory().get(url) queryset = range(100) paginated = paginate(request, queryset) eq_(paginated.url, @@ -32,7 +33,7 @@ def test_paginator_filter(): # Correct number of