Fix issues with RequestFactory used as Request.

* Centralize creation of quick request objects.
* Isolate caches to individual tests to prevent test ordering
  dependencies.

TNL-5811
This commit is contained in:
J. Cliff Dyer
2016-11-02 14:38:11 -04:00
parent 4319a8288a
commit 9366c43a83
17 changed files with 112 additions and 92 deletions

View File

@@ -359,13 +359,15 @@ class TestMasqueradedGroup(StaffMasqueradeTestCase):
}
request = self._create_mock_json_request(
self.test_user,
body=json.dumps(request_json),
data=request_json,
session=self.session
)
handle_ajax(request, unicode(self.course.id))
response = handle_ajax(request, unicode(self.course.id))
# pylint has issues analyzing this class (maybe due to circular imports?)
self.assertEquals(response.status_code, 200) # pylint: disable=no-member
# Now setup the masquerade for the test user
setup_masquerade(request, self.test_user, True)
setup_masquerade(request, self.course.id, True)
scheme = self.user_partition.scheme
self.assertEqual(
scheme.get_group_for_user(self.course.id, self.test_user, self.user_partition),

View File

@@ -14,23 +14,14 @@ from django.test.utils import override_settings
from mock import patch
from nose.plugins.attrib import attr
from openedx.core.djangolib.testing.utils import get_mock_request
from student.tests.factories import UserFactory
from ..middleware import SafeSessionMiddleware, SafeCookieData
from .test_utils import TestSafeSessionsLogMixin
def create_mock_request():
"""
Creates and returns a mock request object for testing.
"""
request = RequestFactory()
request.COOKIES = {}
request.META = {}
request.path = '/'
return request
@attr(shard=2)
class TestSafeSessionProcessRequest(TestSafeSessionsLogMixin, TestCase):
"""
@@ -39,7 +30,7 @@ class TestSafeSessionProcessRequest(TestSafeSessionsLogMixin, TestCase):
def setUp(self):
super(TestSafeSessionProcessRequest, self).setUp()
self.user = UserFactory.create()
self.request = create_mock_request()
self.request = get_mock_request()
def assert_response(self, safe_cookie_data=None, success=True):
"""
@@ -141,7 +132,7 @@ class TestSafeSessionProcessResponse(TestSafeSessionsLogMixin, TestCase):
def setUp(self):
super(TestSafeSessionProcessResponse, self).setUp()
self.user = UserFactory.create()
self.request = create_mock_request()
self.request = get_mock_request()
self.request.session = {}
self.client.response = HttpResponse()
self.client.response.cookies = SimpleCookie()
@@ -245,7 +236,7 @@ class TestSafeSessionMiddleware(TestSafeSessionsLogMixin, TestCase):
def setUp(self):
super(TestSafeSessionMiddleware, self).setUp()
self.user = UserFactory.create()
self.request = create_mock_request()
self.request = get_mock_request()
self.client.response = HttpResponse()
self.client.response.cookies = SimpleCookie()

View File

@@ -11,10 +11,10 @@ from pytz import common_timezones, utc
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.utils import override_settings
from dateutil.parser import parse as parse_datetime
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase
from openedx.core.lib.time_zone_utils import get_display_time_zone
from student.tests.factories import UserFactory
@@ -43,7 +43,7 @@ from ...preferences.api import (
@attr(shard=2)
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Account APIs are only supported in LMS')
class TestPreferenceAPI(TestCase):
class TestPreferenceAPI(CacheIsolationTestCase):
"""
These tests specifically cover the parts of the API methods that are not covered by test_views.py.
This includes the specific types of error raised, and default behavior when optional arguments
@@ -441,7 +441,7 @@ class UpdateEmailOptInTests(ModuleStoreTestCase):
@ddt.ddt
class CountryTimeZoneTest(TestCase):
class CountryTimeZoneTest(CacheIsolationTestCase):
"""
Test cases to validate country code api functionality
"""

View File

@@ -0,0 +1,29 @@
"""
Test that testing utils do what they say.
"""
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.http.request import HttpRequest
from django.test import TestCase
from ..utils import get_mock_request
USER_MODEL = get_user_model()
class TestGetMockRequest(TestCase):
"""
Validate the behavior of get_mock_request
"""
def test_mock_request_is_request(self):
request = get_mock_request(USER_MODEL())
self.assertIsInstance(request, HttpRequest)
def test_user_is_attached_to_mock_request(self):
user = USER_MODEL()
request = get_mock_request(user)
self.assertIs(request.user, user)
def test_mock_request_without_user(self):
request = get_mock_request()
self.assertIsInstance(request.user, AnonymousUser)

View File

@@ -10,9 +10,11 @@ Utility classes for testing django applications.
import copy
import crum
from django import db
from django.contrib.auth.models import AnonymousUser
from django.core.cache import caches
from django.test import TestCase, override_settings
from django.test import RequestFactory, TestCase, override_settings
from django.conf import settings
from django.contrib import sites
@@ -158,3 +160,18 @@ class NoseDatabaseIsolation(Plugin):
"""
for db_ in db.connections.all():
db_.close()
def get_mock_request(user=None):
"""
Create a request object for the user, if specified.
"""
request = RequestFactory().get('/')
if user is not None:
request.user = user
else:
request.user = AnonymousUser()
request.is_secure = lambda: True
request.get_host = lambda: "edx.org"
crum.set_current_request(request)
return request