From d558a36127d3810d8d33b0069453ad0089f23407 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Fri, 17 Oct 2014 16:16:30 -0400 Subject: [PATCH] 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. --- .../third_party_auth/tests/testutil.py | 2 + common/djangoapps/util/testing.py | 39 ++++++++++++++----- .../student_account/test/test_views.py | 8 ++-- lms/djangoapps/student_account/urls.py | 4 +- .../student_profile/test/test_views.py | 2 +- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/common/djangoapps/third_party_auth/tests/testutil.py b/common/djangoapps/third_party_auth/tests/testutil.py index 6907cea26e..f4f3600df7 100644 --- a/common/djangoapps/third_party_auth/tests/testutil.py +++ b/common/djangoapps/third_party_auth/tests/testutil.py @@ -30,8 +30,10 @@ class TestCase(unittest.TestCase): def setUp(self): super(TestCase, self).setUp() + self._original_providers = provider.Registry._get_all() provider.Registry._reset() def tearDown(self): provider.Registry._reset() + provider.Registry.configure_once(self._original_providers) super(TestCase, self).tearDown() diff --git a/common/djangoapps/util/testing.py b/common/djangoapps/util/testing.py index fdd61a2a6a..311ed89244 100644 --- a/common/djangoapps/util/testing.py +++ b/common/djangoapps/util/testing.py @@ -19,19 +19,38 @@ class UrlResetMixin(object): that affect the contents of urls.py """ - def _reset_urls(self, urlconf=None): - if urlconf is None: - urlconf = settings.ROOT_URLCONF - - if urlconf in sys.modules: - reload(sys.modules[urlconf]) + def _reset_urls(self, urlconf_modules): + for urlconf in urlconf_modules: + if urlconf in sys.modules: + reload(sys.modules[urlconf]) clear_url_caches() # Resolve a URL so that the new urlconf gets loaded resolve('/') - def setUp(self, **kwargs): - """Reset django default urlconf before tests and after tests""" + def setUp(self, *args, **kwargs): + """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) - 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)) diff --git a/lms/djangoapps/student_account/test/test_views.py b/lms/djangoapps/student_account/test/test_views.py index 0f6900adad..198b438a1d 100644 --- a/lms/djangoapps/student_account/test/test_views.py +++ b/lms/djangoapps/student_account/test/test_views.py @@ -47,7 +47,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): @patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True}) def setUp(self): - super(StudentAccountViewTest, self).setUp() + super(StudentAccountViewTest, self).setUp("student_account.urls") # Create/activate a new account activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.OLD_EMAIL) @@ -62,8 +62,8 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): self.assertContains(response, "Student Account") @ddt.data( - ("login", "login"), - ("register", "register"), + ("account_login", "login"), + ("account_register", "register"), ) @ddt.unpack def test_login_and_registration_form(self, url_name, initial_mode): @@ -71,7 +71,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): expected_data = u"data-initial-mode=\"{mode}\"".format(mode=initial_mode) 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): response = self.client.get(reverse(url_name)) diff --git a/lms/djangoapps/student_account/urls.py b/lms/djangoapps/student_account/urls.py index d6e3b71233..0c8908ec5a 100644 --- a/lms/djangoapps/student_account/urls.py +++ b/lms/djangoapps/student_account/urls.py @@ -4,8 +4,8 @@ from django.conf import settings urlpatterns = patterns( 'student_account.views', - url(r'^login/$', 'login_and_registration_form', {'initial_mode': 'login'}, name='login'), - url(r'^register/$', 'login_and_registration_form', {'initial_mode': 'register'}, name='register'), + url(r'^login/$', 'login_and_registration_form', {'initial_mode': 'login'}, name='account_login'), + url(r'^register/$', 'login_and_registration_form', {'initial_mode': 'register'}, name='account_register'), ) if settings.FEATURES.get('ENABLE_NEW_DASHBOARD'): diff --git a/lms/djangoapps/student_profile/test/test_views.py b/lms/djangoapps/student_profile/test/test_views.py index db30146986..c24bd0ea0a 100644 --- a/lms/djangoapps/student_profile/test/test_views.py +++ b/lms/djangoapps/student_profile/test/test_views.py @@ -35,7 +35,7 @@ class StudentProfileViewTest(UrlResetMixin, TestCase): @patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True}) def setUp(self): - super(StudentProfileViewTest, self).setUp() + super(StudentProfileViewTest, self).setUp("student_profile.urls") # Create/activate a new account activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL)