Commit d558a361 by Will Daly

Fix unit test failures due to duplicate url names

Reset the correct URLs module to fix test failures.

Fix third party auth to reconfigure the registry on test cleanup.
parent 6250c95d
...@@ -30,8 +30,10 @@ class TestCase(unittest.TestCase): ...@@ -30,8 +30,10 @@ class TestCase(unittest.TestCase):
def setUp(self): def setUp(self):
super(TestCase, self).setUp() super(TestCase, self).setUp()
self._original_providers = provider.Registry._get_all()
provider.Registry._reset() provider.Registry._reset()
def tearDown(self): def tearDown(self):
provider.Registry._reset() provider.Registry._reset()
provider.Registry.configure_once(self._original_providers)
super(TestCase, self).tearDown() super(TestCase, self).tearDown()
...@@ -19,19 +19,38 @@ class UrlResetMixin(object): ...@@ -19,19 +19,38 @@ class UrlResetMixin(object):
that affect the contents of urls.py that affect the contents of urls.py
""" """
def _reset_urls(self, urlconf=None): def _reset_urls(self, urlconf_modules):
if urlconf is None: for urlconf in urlconf_modules:
urlconf = settings.ROOT_URLCONF if urlconf in sys.modules:
reload(sys.modules[urlconf])
if urlconf in sys.modules:
reload(sys.modules[urlconf])
clear_url_caches() clear_url_caches()
# Resolve a URL so that the new urlconf gets loaded # Resolve a URL so that the new urlconf gets loaded
resolve('/') resolve('/')
def setUp(self, **kwargs): def setUp(self, *args, **kwargs):
"""Reset django default urlconf before tests and after tests""" """Reset Django urls before tests and after tests
If you need to reset `urls.py` from a particular Django app (or apps),
specify these modules in *args.
Examples:
# Reload only the root urls.py
super(MyTestCase, self).setUp()
# Reload urls from my_app
super(MyTestCase, self).setUp("my_app.urls")
# Reload urls from my_app and another_app
super(MyTestCase, self).setUp("my_app.urls", "another_app.urls")
"""
super(UrlResetMixin, self).setUp(**kwargs) super(UrlResetMixin, self).setUp(**kwargs)
self._reset_urls()
self.addCleanup(self._reset_urls) urlconf_modules = [settings.ROOT_URLCONF]
if args:
urlconf_modules.extend(args)
self._reset_urls(urlconf_modules)
self.addCleanup(lambda: self._reset_urls(urlconf_modules))
...@@ -47,7 +47,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): ...@@ -47,7 +47,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase):
@patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True}) @patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True})
def setUp(self): def setUp(self):
super(StudentAccountViewTest, self).setUp() super(StudentAccountViewTest, self).setUp("student_account.urls")
# Create/activate a new account # Create/activate a new account
activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.OLD_EMAIL) activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.OLD_EMAIL)
...@@ -62,8 +62,8 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): ...@@ -62,8 +62,8 @@ class StudentAccountViewTest(UrlResetMixin, TestCase):
self.assertContains(response, "Student Account") self.assertContains(response, "Student Account")
@ddt.data( @ddt.data(
("login", "login"), ("account_login", "login"),
("register", "register"), ("account_register", "register"),
) )
@ddt.unpack @ddt.unpack
def test_login_and_registration_form(self, url_name, initial_mode): def test_login_and_registration_form(self, url_name, initial_mode):
...@@ -71,7 +71,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): ...@@ -71,7 +71,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase):
expected_data = u"data-initial-mode=\"{mode}\"".format(mode=initial_mode) expected_data = u"data-initial-mode=\"{mode}\"".format(mode=initial_mode)
self.assertContains(response, expected_data) self.assertContains(response, expected_data)
@ddt.data("login", "register") @ddt.data("account_login", "account_register")
def test_login_and_registration_third_party_auth_urls(self, url_name): def test_login_and_registration_third_party_auth_urls(self, url_name):
response = self.client.get(reverse(url_name)) response = self.client.get(reverse(url_name))
......
...@@ -4,8 +4,8 @@ from django.conf import settings ...@@ -4,8 +4,8 @@ from django.conf import settings
urlpatterns = patterns( urlpatterns = patterns(
'student_account.views', 'student_account.views',
url(r'^login/$', 'login_and_registration_form', {'initial_mode': 'login'}, name='login'), url(r'^login/$', 'login_and_registration_form', {'initial_mode': 'login'}, name='account_login'),
url(r'^register/$', 'login_and_registration_form', {'initial_mode': 'register'}, name='register'), url(r'^register/$', 'login_and_registration_form', {'initial_mode': 'register'}, name='account_register'),
) )
if settings.FEATURES.get('ENABLE_NEW_DASHBOARD'): if settings.FEATURES.get('ENABLE_NEW_DASHBOARD'):
......
...@@ -35,7 +35,7 @@ class StudentProfileViewTest(UrlResetMixin, TestCase): ...@@ -35,7 +35,7 @@ class StudentProfileViewTest(UrlResetMixin, TestCase):
@patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True}) @patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True})
def setUp(self): def setUp(self):
super(StudentProfileViewTest, self).setUp() super(StudentProfileViewTest, self).setUp("student_profile.urls")
# Create/activate a new account # Create/activate a new account
activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment