Code

Ticket #10521: 10521-remote-user-tests-enh.diff

File 10521-remote-user-tests-enh.diff, 18.9 KB (added by ramiro, 5 years ago)

Patch that allows django.contrib.auth tests to run w/o errors from a project when using manage.py and the auth app is in INSTALLED_APPS

Line 
1diff -r f69520e5a824 django/contrib/auth/tests/__init__.py
2--- a/django/contrib/auth/tests/__init__.py     Tue Mar 17 07:40:04 2009 -0300
3+++ b/django/contrib/auth/tests/__init__.py     Tue Mar 17 16:12:25 2009 -0300
4@@ -1,5 +1,5 @@
5 from django.contrib.auth.tests.basic import BASIC_TESTS
6-from django.contrib.auth.tests.views \
7+from django.contrib.auth.tests.views_tests \
8         import PasswordResetTest, ChangePasswordTest
9 from django.contrib.auth.tests.forms import FORM_TESTS
10 from django.contrib.auth.tests.remote_user \
11diff -r f69520e5a824 django/contrib/auth/tests/remote_user.py
12--- a/django/contrib/auth/tests/remote_user.py  Tue Mar 17 07:40:04 2009 -0300
13+++ b/django/contrib/auth/tests/remote_user.py  Tue Mar 17 16:12:25 2009 -0300
14@@ -14,6 +14,8 @@
15     # Usernames to be passed in REMOTE_USER for the test_known_user test case.
16     known_user = 'knownuser'
17     known_user2 = 'knownuser2'
18+
19+    urls = 'django.contrib.auth.tests.test_urls'
20 
21     def setUp(self):
22         self.curr_middleware = settings.MIDDLEWARE_CLASSES
23@@ -28,15 +30,15 @@
24         """
25         num_users = User.objects.count()
26 
27-        response = self.client.get('/')
28+        response = self.client.get('/remote_user/')
29         self.assert_(isinstance(response.context['user'], AnonymousUser))
30         self.assertEqual(User.objects.count(), num_users)
31 
32-        response = self.client.get('/', REMOTE_USER=None)
33+        response = self.client.get('/remote_user/', REMOTE_USER=None)
34         self.assert_(isinstance(response.context['user'], AnonymousUser))
35         self.assertEqual(User.objects.count(), num_users)
36 
37-        response = self.client.get('/', REMOTE_USER='')
38+        response = self.client.get('/remote_user/', REMOTE_USER='')
39         self.assert_(isinstance(response.context['user'], AnonymousUser))
40         self.assertEqual(User.objects.count(), num_users)
41 
42@@ -46,13 +48,13 @@
43         as a User.
44         """
45         num_users = User.objects.count()
46-        response = self.client.get('/', REMOTE_USER='newuser')
47+        response = self.client.get('/remote_user/', REMOTE_USER='newuser')
48         self.assertEqual(response.context['user'].username, 'newuser')
49         self.assertEqual(User.objects.count(), num_users + 1)
50         User.objects.get(username='newuser')
51 
52         # Another request with same user should not create any new users.
53-        response = self.client.get('/', REMOTE_USER='newuser')
54+        response = self.client.get('/remote_user/', REMOTE_USER='newuser')
55         self.assertEqual(User.objects.count(), num_users + 1)
56 
57     def test_known_user(self):
58@@ -62,12 +64,12 @@
59         User.objects.create(username='knownuser')
60         User.objects.create(username='knownuser2')
61         num_users = User.objects.count()
62-        response = self.client.get('/', REMOTE_USER=self.known_user)
63+        response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
64         self.assertEqual(response.context['user'].username, 'knownuser')
65         self.assertEqual(User.objects.count(), num_users)
66         # Test that a different user passed in the headers causes the new user
67         # to be logged in.
68-        response = self.client.get('/', REMOTE_USER=self.known_user2)
69+        response = self.client.get('/remote_user/', REMOTE_USER=self.known_user2)
70         self.assertEqual(response.context['user'].username, 'knownuser2')
71         self.assertEqual(User.objects.count(), num_users)
72 
73@@ -82,13 +84,13 @@
74         user.last_login = default_login
75         user.save()
76 
77-        response = self.client.get('/', REMOTE_USER=self.known_user)
78+        response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
79         self.assertNotEqual(default_login, response.context['user'].last_login)
80 
81         user = User.objects.get(username='knownuser')
82         user.last_login = default_login
83         user.save()
84-        response = self.client.get('/', REMOTE_USER=self.known_user)
85+        response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
86         self.assertEqual(default_login, response.context['user'].last_login)
87 
88     def tearDown(self):
89@@ -113,7 +115,7 @@
90 
91     def test_unknown_user(self):
92         num_users = User.objects.count()
93-        response = self.client.get('/', REMOTE_USER='newuser')
94+        response = self.client.get('/remote_user/', REMOTE_USER='newuser')
95         self.assert_(isinstance(response.context['user'], AnonymousUser))
96         self.assertEqual(User.objects.count(), num_users)
97 
98diff -r f69520e5a824 django/contrib/auth/tests/test_urls.py
99--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
100+++ b/django/contrib/auth/tests/test_urls.py    Tue Mar 17 16:12:25 2009 -0300
101@@ -0,0 +1,12 @@
102+"""
103+
104+This is a urlconf to be loaded by remote_user.py. Add any urls needed
105+for tests only.
106+
107+"""
108+from django.conf.urls.defaults import *
109+from django.contrib.auth.tests.views import remote_user_auth_view
110+
111+urlpatterns = patterns('',
112+                       (r'^remote_user/', remote_user_auth_view),
113+                      )
114diff -r f69520e5a824 django/contrib/auth/tests/views.py
115--- a/django/contrib/auth/tests/views.py        Tue Mar 17 07:40:04 2009 -0300
116+++ b/django/contrib/auth/tests/views.py        Tue Mar 17 16:12:25 2009 -0300
117@@ -1,164 +1,8 @@
118+from django.http import HttpResponse
119+from django.shortcuts import render_to_response
120+from django.template import Template, RequestContext
121 
122-import os
123-import re
124-
125-from django.conf import settings
126-from django.contrib.auth.models import User
127-from django.test import TestCase
128-from django.core import mail
129-
130-class PasswordResetTest(TestCase):
131-    fixtures = ['authtestdata.json']
132-    urls = 'django.contrib.auth.urls'
133-
134-    def test_email_not_found(self):
135-        "Error is raised if the provided email address isn't currently registered"
136-        response = self.client.get('/password_reset/')
137-        self.assertEquals(response.status_code, 200)
138-        response = self.client.post('/password_reset/', {'email': 'not_a_real_email@email.com'})
139-        self.assertContains(response, "That e-mail address doesn't have an associated user account")
140-        self.assertEquals(len(mail.outbox), 0)
141-
142-    def test_email_found(self):
143-        "Email is sent if a valid email address is provided for password reset"
144-        response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})
145-        self.assertEquals(response.status_code, 302)
146-        self.assertEquals(len(mail.outbox), 1)
147-        self.assert_("http://" in mail.outbox[0].body)
148-
149-    def _test_confirm_start(self):
150-        # Start by creating the email
151-        response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})
152-        self.assertEquals(response.status_code, 302)
153-        self.assertEquals(len(mail.outbox), 1)
154-        return self._read_signup_email(mail.outbox[0])
155-
156-    def _read_signup_email(self, email):
157-        urlmatch = re.search(r"https?://[^/]*(/.*reset/\S*)", email.body)
158-        self.assert_(urlmatch is not None, "No URL found in sent email")
159-        return urlmatch.group(), urlmatch.groups()[0]
160-
161-    def test_confirm_valid(self):
162-        url, path = self._test_confirm_start()
163-        response = self.client.get(path)
164-        # redirect to a 'complete' page:
165-        self.assertEquals(response.status_code, 200)
166-        self.assert_("Please enter your new password" in response.content)
167-
168-    def test_confirm_invalid(self):
169-        url, path = self._test_confirm_start()
170-        # Lets munge the token in the path, but keep the same length,
171-        # in case the URL conf will reject a different length
172-        path = path[:-5] + ("0"*4) + path[-1]
173-
174-        response = self.client.get(path)
175-        self.assertEquals(response.status_code, 200)
176-        self.assert_("The password reset link was invalid" in response.content)
177-
178-    def test_confirm_invalid_post(self):
179-        # Same as test_confirm_invalid, but trying
180-        # to do a POST instead.
181-        url, path = self._test_confirm_start()
182-        path = path[:-5] + ("0"*4) + path[-1]
183-
184-        response = self.client.post(path, {'new_password1': 'anewpassword',
185-                                           'new_password2':' anewpassword'})
186-        # Check the password has not been changed
187-        u = User.objects.get(email='staffmember@example.com')
188-        self.assert_(not u.check_password("anewpassword"))
189-
190-    def test_confirm_complete(self):
191-        url, path = self._test_confirm_start()
192-        response = self.client.post(path, {'new_password1': 'anewpassword',
193-                                           'new_password2': 'anewpassword'})
194-        # It redirects us to a 'complete' page:
195-        self.assertEquals(response.status_code, 302)
196-        # Check the password has been changed
197-        u = User.objects.get(email='staffmember@example.com')
198-        self.assert_(u.check_password("anewpassword"))
199-
200-        # Check we can't use the link again
201-        response = self.client.get(path)
202-        self.assertEquals(response.status_code, 200)
203-        self.assert_("The password reset link was invalid" in response.content)
204-
205-    def test_confirm_different_passwords(self):
206-        url, path = self._test_confirm_start()
207-        response = self.client.post(path, {'new_password1': 'anewpassword',
208-                                           'new_password2':' x'})
209-        self.assertEquals(response.status_code, 200)
210-        self.assert_("The two password fields didn't match" in response.content)
211-
212-
213-class ChangePasswordTest(TestCase):
214-    fixtures = ['authtestdata.json']
215-    urls = 'django.contrib.auth.urls'
216-
217-    def setUp(self):
218-        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
219-        settings.TEMPLATE_DIRS = (
220-            os.path.join(
221-                os.path.dirname(__file__),
222-                'templates'
223-            )
224-        ,)
225-
226-    def tearDown(self):
227-        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
228-
229-    def login(self, password='password'):
230-        response = self.client.post('/login/', {
231-            'username': 'testclient',
232-            'password': password
233-            }
234-        )
235-        self.assertEquals(response.status_code, 302)
236-        self.assert_(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
237-
238-    def fail_login(self, password='password'):
239-        response = self.client.post('/login/', {
240-            'username': 'testclient',
241-            'password': password
242-            }
243-        )
244-        self.assertEquals(response.status_code, 200)
245-        self.assert_("Please enter a correct username and password. Note that both fields are case-sensitive." in response.content)
246-
247-    def logout(self):
248-        response = self.client.get('/logout/')
249-
250-    def test_password_change_fails_with_invalid_old_password(self):
251-        self.login()
252-        response = self.client.post('/password_change/', {
253-            'old_password': 'donuts',
254-            'new_password1': 'password1',
255-            'new_password2': 'password1',
256-            }
257-        )
258-        self.assertEquals(response.status_code, 200)
259-        self.assert_("Your old password was entered incorrectly. Please enter it again." in response.content)
260-
261-    def test_password_change_fails_with_mismatched_passwords(self):
262-        self.login()
263-        response = self.client.post('/password_change/', {
264-            'old_password': 'password',
265-            'new_password1': 'password1',
266-            'new_password2': 'donuts',
267-            }
268-        )
269-        self.assertEquals(response.status_code, 200)
270-        self.assert_("The two password fields didn't match." in response.content)
271-
272-    def test_password_change_succeeds(self):
273-        self.login()
274-        response = self.client.post('/password_change/', {
275-            'old_password': 'password',
276-            'new_password1': 'password1',
277-            'new_password2': 'password1',
278-            }
279-        )
280-        self.assertEquals(response.status_code, 302)
281-        self.assert_(response['Location'].endswith('/password_change/done/'))
282-        self.fail_login()
283-        self.login(password='password1')
284-
285+def remote_user_auth_view(request):
286+    t = Template("Username is {{ user }}.")
287+    c = RequestContext(request, {})
288+    return HttpResponse(t.render(c))
289diff -r f69520e5a824 django/contrib/auth/tests/views_tests.py
290--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
291+++ b/django/contrib/auth/tests/views_tests.py  Tue Mar 17 16:12:25 2009 -0300
292@@ -0,0 +1,164 @@
293+
294+import os
295+import re
296+
297+from django.conf import settings
298+from django.contrib.auth.models import User
299+from django.test import TestCase
300+from django.core import mail
301+
302+class PasswordResetTest(TestCase):
303+    fixtures = ['authtestdata.json']
304+    urls = 'django.contrib.auth.urls'
305+
306+    def test_email_not_found(self):
307+        "Error is raised if the provided email address isn't currently registered"
308+        response = self.client.get('/password_reset/')
309+        self.assertEquals(response.status_code, 200)
310+        response = self.client.post('/password_reset/', {'email': 'not_a_real_email@email.com'})
311+        self.assertContains(response, "That e-mail address doesn't have an associated user account")
312+        self.assertEquals(len(mail.outbox), 0)
313+
314+    def test_email_found(self):
315+        "Email is sent if a valid email address is provided for password reset"
316+        response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})
317+        self.assertEquals(response.status_code, 302)
318+        self.assertEquals(len(mail.outbox), 1)
319+        self.assert_("http://" in mail.outbox[0].body)
320+
321+    def _test_confirm_start(self):
322+        # Start by creating the email
323+        response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})
324+        self.assertEquals(response.status_code, 302)
325+        self.assertEquals(len(mail.outbox), 1)
326+        return self._read_signup_email(mail.outbox[0])
327+
328+    def _read_signup_email(self, email):
329+        urlmatch = re.search(r"https?://[^/]*(/.*reset/\S*)", email.body)
330+        self.assert_(urlmatch is not None, "No URL found in sent email")
331+        return urlmatch.group(), urlmatch.groups()[0]
332+
333+    def test_confirm_valid(self):
334+        url, path = self._test_confirm_start()
335+        response = self.client.get(path)
336+        # redirect to a 'complete' page:
337+        self.assertEquals(response.status_code, 200)
338+        self.assert_("Please enter your new password" in response.content)
339+
340+    def test_confirm_invalid(self):
341+        url, path = self._test_confirm_start()
342+        # Lets munge the token in the path, but keep the same length,
343+        # in case the URL conf will reject a different length
344+        path = path[:-5] + ("0"*4) + path[-1]
345+
346+        response = self.client.get(path)
347+        self.assertEquals(response.status_code, 200)
348+        self.assert_("The password reset link was invalid" in response.content)
349+
350+    def test_confirm_invalid_post(self):
351+        # Same as test_confirm_invalid, but trying
352+        # to do a POST instead.
353+        url, path = self._test_confirm_start()
354+        path = path[:-5] + ("0"*4) + path[-1]
355+
356+        response = self.client.post(path, {'new_password1': 'anewpassword',
357+                                           'new_password2':' anewpassword'})
358+        # Check the password has not been changed
359+        u = User.objects.get(email='staffmember@example.com')
360+        self.assert_(not u.check_password("anewpassword"))
361+
362+    def test_confirm_complete(self):
363+        url, path = self._test_confirm_start()
364+        response = self.client.post(path, {'new_password1': 'anewpassword',
365+                                           'new_password2': 'anewpassword'})
366+        # It redirects us to a 'complete' page:
367+        self.assertEquals(response.status_code, 302)
368+        # Check the password has been changed
369+        u = User.objects.get(email='staffmember@example.com')
370+        self.assert_(u.check_password("anewpassword"))
371+
372+        # Check we can't use the link again
373+        response = self.client.get(path)
374+        self.assertEquals(response.status_code, 200)
375+        self.assert_("The password reset link was invalid" in response.content)
376+
377+    def test_confirm_different_passwords(self):
378+        url, path = self._test_confirm_start()
379+        response = self.client.post(path, {'new_password1': 'anewpassword',
380+                                           'new_password2':' x'})
381+        self.assertEquals(response.status_code, 200)
382+        self.assert_("The two password fields didn't match" in response.content)
383+
384+
385+class ChangePasswordTest(TestCase):
386+    fixtures = ['authtestdata.json']
387+    urls = 'django.contrib.auth.urls'
388+
389+    def setUp(self):
390+        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
391+        settings.TEMPLATE_DIRS = (
392+            os.path.join(
393+                os.path.dirname(__file__),
394+                'templates'
395+            )
396+        ,)
397+
398+    def tearDown(self):
399+        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
400+
401+    def login(self, password='password'):
402+        response = self.client.post('/login/', {
403+            'username': 'testclient',
404+            'password': password
405+            }
406+        )
407+        self.assertEquals(response.status_code, 302)
408+        self.assert_(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
409+
410+    def fail_login(self, password='password'):
411+        response = self.client.post('/login/', {
412+            'username': 'testclient',
413+            'password': password
414+            }
415+        )
416+        self.assertEquals(response.status_code, 200)
417+        self.assert_("Please enter a correct username and password. Note that both fields are case-sensitive." in response.content)
418+
419+    def logout(self):
420+        response = self.client.get('/logout/')
421+
422+    def test_password_change_fails_with_invalid_old_password(self):
423+        self.login()
424+        response = self.client.post('/password_change/', {
425+            'old_password': 'donuts',
426+            'new_password1': 'password1',
427+            'new_password2': 'password1',
428+            }
429+        )
430+        self.assertEquals(response.status_code, 200)
431+        self.assert_("Your old password was entered incorrectly. Please enter it again." in response.content)
432+
433+    def test_password_change_fails_with_mismatched_passwords(self):
434+        self.login()
435+        response = self.client.post('/password_change/', {
436+            'old_password': 'password',
437+            'new_password1': 'password1',
438+            'new_password2': 'donuts',
439+            }
440+        )
441+        self.assertEquals(response.status_code, 200)
442+        self.assert_("The two password fields didn't match." in response.content)
443+
444+    def test_password_change_succeeds(self):
445+        self.login()
446+        response = self.client.post('/password_change/', {
447+            'old_password': 'password',
448+            'new_password1': 'password1',
449+            'new_password2': 'password1',
450+            }
451+        )
452+        self.assertEquals(response.status_code, 302)
453+        self.assert_(response['Location'].endswith('/password_change/done/'))
454+        self.fail_login()
455+        self.login(password='password1')
456+