comparison bio/tests/test_views.py @ 766:22d158ef2217

More bio unit tests.
author Brian Neal <bgneal@gmail.com>
date Sun, 19 Jan 2014 21:26:42 -0600
parents f01bc9500aef
children a4ae99359286
comparison
equal deleted inserted replaced
765:f01bc9500aef 766:22d158ef2217
3 3
4 """ 4 """
5 from django.contrib.auth.models import User 5 from django.contrib.auth.models import User
6 from django.test import TestCase 6 from django.test import TestCase
7 from django.core.urlresolvers import reverse, NoReverseMatch 7 from django.core.urlresolvers import reverse, NoReverseMatch
8 from django.http import Http404
9 8
10 9
11 class MemberSearchTest(TestCase): 10 class MemberSearchTest(TestCase):
12 11
13 USERNAME = u'John' 12 USERNAME = u'John'
119 def test_invalid_page(self): 118 def test_invalid_page(self):
120 self._test_get('user', page=99, code=404) 119 self._test_get('user', page=99, code=404)
121 self._test_get('date', page=99, code=404) 120 self._test_get('date', page=99, code=404)
122 121
123 122
124 class MyProfileTest(TestCase): 123 class ProfileTest(TestCase):
125 124
126 def setUp(self): 125 def setUp(self):
127 u1 = User.objects.create_user('paul', '', 'pw') 126 u1 = User.objects.create_user('paul', '', 'pw')
128 u1.save() 127 u1.save()
129 u2 = User.objects.create_user('eddie', '', 'pw') 128 u2 = User.objects.create_user('eddie', '', 'pw')
130 u2.save() 129 u2.save()
131 130
132 def test_anon(self): 131 def test_anon_my_profile(self):
133 my_url = reverse('bio-me') 132 my_url = reverse('bio-me')
134 response = self.client.get(my_url) 133 response = self.client.get(my_url)
135 login_url = reverse('accounts-login') + '?next=' + my_url 134 login_url = reverse('accounts-login') + '?next=' + my_url
136 self.assertRedirects(response, login_url) 135 self.assertRedirects(response, login_url)
137 136
138 def test_authenticated(self): 137 def test_authenticated_my_profile(self):
139 self.client.login(username='paul', password='pw') 138 self.client.login(username='paul', password='pw')
140 response = self.client.get(reverse('bio-me')) 139 response = self.client.get(reverse('bio-me'))
141 self.assertEqual(response.status_code, 200) 140 self.assertEqual(response.status_code, 200)
141
142 def test_anon_other_profile(self):
143 url = reverse('bio-view_profile', kwargs={'username': 'eddie'})
144 response = self.client.get(url)
145 login_url = reverse('accounts-login') + '?next=' + url
146 self.assertRedirects(response, login_url)
147
148 def test_authenticated_other_profile(self):
149 self.client.login(username='paul', password='pw')
150 url = reverse('bio-view_profile', kwargs={'username': 'eddie'})
151 response = self.client.get(url)
152 self.assertEqual(response.status_code, 200)
153
154 def test_view_profile_as_me_redirects(self):
155 self.client.login(username='paul', password='pw')
156 url = reverse('bio-view_profile', kwargs={'username': 'paul'})
157 response = self.client.get(url)
158 self.assertRedirects(response, reverse('bio-me'))
159
160 def test_edit_anon(self):
161 url = reverse('bio-edit_profile')
162 response = self.client.get(url)
163 login_url = reverse('accounts-login') + '?next=' + url
164 self.assertRedirects(response, login_url)
165
166 def test_edit_profile(self):
167 self.client.login(username='paul', password='pw')
168 url = reverse('bio-edit_profile')
169 response = self.client.get(url)
170 self.assertEqual(response.status_code, 200)
171
172 # Save some new data. We don't exhaustively test every field.
173 response = self.client.post(url, {
174 'first_name': 'Paul',
175 'last_name': 'Johnson',
176 'email': 'pjmoto@example.com',
177 'occupation': 'Surf Guitar Pioneer',
178 'profile_text': 'Wrote Mr. Moto at age 15.',
179 'submit_button': 'Save',
180 })
181 self.assertRedirects(response, reverse('bio-me'))
182
183 u = User.objects.get(username='paul')
184 self.assertEqual(u.first_name, 'Paul')
185 self.assertEqual(u.last_name, 'Johnson')
186 self.assertEqual(u.email, 'pjmoto@example.com')
187 profile = u.get_profile()
188 self.assertEqual(profile.occupation, 'Surf Guitar Pioneer')
189 self.assertEqual(profile.profile_text, 'Wrote Mr. Moto at age 15.')