bgneal@463
|
1 """
|
bgneal@463
|
2 View tests for the bio application.
|
bgneal@463
|
3
|
bgneal@463
|
4 """
|
bgneal@767
|
5 from io import BytesIO
|
bgneal@767
|
6 import os
|
bgneal@767
|
7
|
bgneal@463
|
8 from django.contrib.auth.models import User
|
bgneal@463
|
9 from django.test import TestCase
|
bgneal@463
|
10 from django.core.urlresolvers import reverse, NoReverseMatch
|
bgneal@768
|
11 from django.core import mail
|
bgneal@463
|
12
|
bgneal@767
|
13 from PIL import Image
|
bgneal@767
|
14
|
bgneal@768
|
15 from bio.models import UserProfile, UserProfileFlag
|
bgneal@767
|
16
|
bgneal@463
|
17
|
bgneal@463
|
18 class MemberSearchTest(TestCase):
|
bgneal@463
|
19
|
bgneal@463
|
20 USERNAME = u'John'
|
bgneal@463
|
21
|
bgneal@463
|
22 def setUp(self):
|
bgneal@463
|
23 user = User.objects.create_user(self.USERNAME, '', 'password')
|
bgneal@463
|
24 user.save()
|
bgneal@463
|
25
|
bgneal@463
|
26 self.username = 'test_user'
|
bgneal@463
|
27 self.pw = 'password'
|
bgneal@463
|
28 self.user = User.objects.create_user(self.username, '', self.pw)
|
bgneal@463
|
29 self.user.save()
|
bgneal@463
|
30 self.assertTrue(self.client.login(username=self.username,
|
bgneal@463
|
31 password=self.pw))
|
bgneal@463
|
32
|
bgneal@463
|
33 def tearDown(self):
|
bgneal@463
|
34 self.client.logout()
|
bgneal@463
|
35
|
bgneal@463
|
36 def testValidName(self):
|
bgneal@463
|
37 """
|
bgneal@463
|
38 Test a valid username.
|
bgneal@463
|
39 """
|
bgneal@463
|
40
|
bgneal@769
|
41 response = self.client.get(reverse('bio-member_search'),
|
bgneal@463
|
42 {'username': self.USERNAME},
|
bgneal@463
|
43 follow=True)
|
bgneal@463
|
44
|
bgneal@463
|
45 self.assertEqual(len(response.redirect_chain), 1)
|
bgneal@463
|
46 if response.redirect_chain:
|
bgneal@1206
|
47 self.assertEqual(
|
bgneal@1206
|
48 response.redirect_chain[0][0],
|
bgneal@1206
|
49 reverse(
|
bgneal@1206
|
50 'bio-view_profile',
|
bgneal@463
|
51 kwargs={'username': self.USERNAME}))
|
bgneal@463
|
52 self.assertEqual(response.redirect_chain[0][1], 302)
|
bgneal@463
|
53
|
bgneal@463
|
54 self.assertEqual(response.status_code, 200)
|
bgneal@463
|
55
|
bgneal@463
|
56 def testInvalidName(self):
|
bgneal@463
|
57 """
|
bgneal@463
|
58 Test a invalid username.
|
bgneal@463
|
59 """
|
bgneal@463
|
60
|
bgneal@769
|
61 response = self.client.get(reverse('bio-member_search'),
|
bgneal@463
|
62 {'username': self.USERNAME + '!'})
|
bgneal@463
|
63
|
bgneal@463
|
64 self.assertEqual(response.status_code, 200)
|
bgneal@463
|
65 self.assertContains(response, "That username does not exist.")
|
bgneal@463
|
66
|
bgneal@463
|
67 def testTrailingSpace(self):
|
bgneal@463
|
68 """
|
bgneal@463
|
69 Test a username with a trailing space.
|
bgneal@463
|
70 """
|
bgneal@463
|
71
|
bgneal@463
|
72 try:
|
bgneal@769
|
73 response = self.client.get(reverse('bio-member_search'),
|
bgneal@463
|
74 {'username': self.USERNAME + ' '},
|
bgneal@463
|
75 follow=True)
|
bgneal@463
|
76 except NoReverseMatch:
|
bgneal@463
|
77 self.fail('bit by a MySQL bug?')
|
bgneal@463
|
78
|
bgneal@463
|
79 self.assertEqual(len(response.redirect_chain), 1)
|
bgneal@463
|
80 if response.redirect_chain:
|
bgneal@1206
|
81 self.assertEqual(
|
bgneal@1206
|
82 response.redirect_chain[0][0],
|
bgneal@1206
|
83 reverse(
|
bgneal@1206
|
84 'bio-view_profile',
|
bgneal@463
|
85 kwargs={'username': self.USERNAME}))
|
bgneal@463
|
86 self.assertEqual(response.redirect_chain[0][1], 302)
|
bgneal@463
|
87
|
bgneal@463
|
88 self.assertEqual(response.status_code, 200)
|
bgneal@765
|
89
|
bgneal@765
|
90
|
bgneal@765
|
91 class AnonMemberListTest(TestCase):
|
bgneal@765
|
92
|
bgneal@765
|
93 def _test_login_required(self, list_by):
|
bgneal@765
|
94
|
bgneal@765
|
95 member_list_url = reverse('bio-member_list', kwargs={'type': list_by})
|
bgneal@765
|
96 response = self.client.get(member_list_url, follow=True)
|
bgneal@765
|
97 login_url = reverse('accounts-login') + '?next=' + member_list_url
|
bgneal@765
|
98 self.assertRedirects(response, login_url)
|
bgneal@765
|
99
|
bgneal@765
|
100 def test_login_required(self):
|
bgneal@765
|
101 self._test_login_required('user')
|
bgneal@765
|
102 self._test_login_required('date')
|
bgneal@765
|
103
|
bgneal@765
|
104
|
bgneal@765
|
105 class MemberListTest(TestCase):
|
bgneal@765
|
106
|
bgneal@765
|
107 def setUp(self):
|
bgneal@765
|
108 u1 = User.objects.create_user('paul', '', 'pw')
|
bgneal@765
|
109 u1.save()
|
bgneal@765
|
110 u2 = User.objects.create_user('eddie', '', 'pw')
|
bgneal@765
|
111 u2.save()
|
bgneal@765
|
112
|
bgneal@765
|
113 self.user = u1
|
bgneal@765
|
114 self.client.login(username=u1.username, password='pw')
|
bgneal@765
|
115
|
bgneal@765
|
116 def _test_get(self, list_by, page=None, code=200):
|
bgneal@765
|
117
|
bgneal@765
|
118 member_list_url = reverse('bio-member_list', kwargs={'type': list_by})
|
bgneal@765
|
119 if page is not None:
|
bgneal@765
|
120 member_list_url += '?page=%d' % page
|
bgneal@765
|
121 response = self.client.get(member_list_url)
|
bgneal@765
|
122 self.assertEqual(response.status_code, code)
|
bgneal@765
|
123
|
bgneal@765
|
124 def test_get(self):
|
bgneal@765
|
125 self._test_get('user')
|
bgneal@765
|
126 self._test_get('date')
|
bgneal@765
|
127 self._test_get('user', page=1)
|
bgneal@765
|
128 self._test_get('date', page=1)
|
bgneal@765
|
129
|
bgneal@765
|
130 def test_invalid_page(self):
|
bgneal@765
|
131 self._test_get('user', page=99, code=404)
|
bgneal@765
|
132 self._test_get('date', page=99, code=404)
|
bgneal@765
|
133
|
bgneal@765
|
134
|
bgneal@766
|
135 class ProfileTest(TestCase):
|
bgneal@765
|
136
|
bgneal@765
|
137 def setUp(self):
|
bgneal@765
|
138 u1 = User.objects.create_user('paul', '', 'pw')
|
bgneal@765
|
139 u1.save()
|
bgneal@765
|
140 u2 = User.objects.create_user('eddie', '', 'pw')
|
bgneal@765
|
141 u2.save()
|
bgneal@765
|
142
|
bgneal@767
|
143 def tearDown(self):
|
bgneal@767
|
144 profile = UserProfile.objects.get(user__username='paul')
|
bgneal@767
|
145 if profile.avatar:
|
bgneal@767
|
146 os.remove(profile.avatar.path)
|
bgneal@767
|
147
|
bgneal@766
|
148 def test_anon_my_profile(self):
|
bgneal@765
|
149 my_url = reverse('bio-me')
|
bgneal@765
|
150 response = self.client.get(my_url)
|
bgneal@765
|
151 login_url = reverse('accounts-login') + '?next=' + my_url
|
bgneal@765
|
152 self.assertRedirects(response, login_url)
|
bgneal@765
|
153
|
bgneal@766
|
154 def test_authenticated_my_profile(self):
|
bgneal@765
|
155 self.client.login(username='paul', password='pw')
|
bgneal@765
|
156 response = self.client.get(reverse('bio-me'))
|
bgneal@765
|
157 self.assertEqual(response.status_code, 200)
|
bgneal@766
|
158
|
bgneal@766
|
159 def test_anon_other_profile(self):
|
bgneal@766
|
160 url = reverse('bio-view_profile', kwargs={'username': 'eddie'})
|
bgneal@766
|
161 response = self.client.get(url)
|
bgneal@766
|
162 login_url = reverse('accounts-login') + '?next=' + url
|
bgneal@766
|
163 self.assertRedirects(response, login_url)
|
bgneal@766
|
164
|
bgneal@766
|
165 def test_authenticated_other_profile(self):
|
bgneal@766
|
166 self.client.login(username='paul', password='pw')
|
bgneal@766
|
167 url = reverse('bio-view_profile', kwargs={'username': 'eddie'})
|
bgneal@766
|
168 response = self.client.get(url)
|
bgneal@766
|
169 self.assertEqual(response.status_code, 200)
|
bgneal@766
|
170
|
bgneal@766
|
171 def test_view_profile_as_me_redirects(self):
|
bgneal@766
|
172 self.client.login(username='paul', password='pw')
|
bgneal@766
|
173 url = reverse('bio-view_profile', kwargs={'username': 'paul'})
|
bgneal@766
|
174 response = self.client.get(url)
|
bgneal@766
|
175 self.assertRedirects(response, reverse('bio-me'))
|
bgneal@766
|
176
|
bgneal@766
|
177 def test_edit_anon(self):
|
bgneal@766
|
178 url = reverse('bio-edit_profile')
|
bgneal@766
|
179 response = self.client.get(url)
|
bgneal@766
|
180 login_url = reverse('accounts-login') + '?next=' + url
|
bgneal@766
|
181 self.assertRedirects(response, login_url)
|
bgneal@766
|
182
|
bgneal@766
|
183 def test_edit_profile(self):
|
bgneal@766
|
184 self.client.login(username='paul', password='pw')
|
bgneal@766
|
185 url = reverse('bio-edit_profile')
|
bgneal@766
|
186 response = self.client.get(url)
|
bgneal@766
|
187 self.assertEqual(response.status_code, 200)
|
bgneal@766
|
188
|
bgneal@766
|
189 # Save some new data. We don't exhaustively test every field.
|
bgneal@766
|
190 response = self.client.post(url, {
|
bgneal@766
|
191 'first_name': 'Paul',
|
bgneal@766
|
192 'last_name': 'Johnson',
|
bgneal@766
|
193 'email': 'pjmoto@example.com',
|
bgneal@766
|
194 'occupation': 'Surf Guitar Pioneer',
|
bgneal@766
|
195 'profile_text': 'Wrote Mr. Moto at age 15.',
|
bgneal@766
|
196 'submit_button': 'Save',
|
bgneal@766
|
197 })
|
bgneal@766
|
198 self.assertRedirects(response, reverse('bio-me'))
|
bgneal@766
|
199
|
bgneal@766
|
200 u = User.objects.get(username='paul')
|
bgneal@766
|
201 self.assertEqual(u.first_name, 'Paul')
|
bgneal@766
|
202 self.assertEqual(u.last_name, 'Johnson')
|
bgneal@766
|
203 self.assertEqual(u.email, 'pjmoto@example.com')
|
bgneal@789
|
204 profile = u.profile
|
bgneal@766
|
205 self.assertEqual(profile.occupation, 'Surf Guitar Pioneer')
|
bgneal@766
|
206 self.assertEqual(profile.profile_text, 'Wrote Mr. Moto at age 15.')
|
bgneal@767
|
207
|
bgneal@767
|
208 def test_avatar_change(self):
|
bgneal@767
|
209 self.client.login(username='paul', password='pw')
|
bgneal@767
|
210 url = reverse('bio-change_avatar')
|
bgneal@767
|
211 response = self.client.get(url)
|
bgneal@767
|
212 self.assertEqual(response.status_code, 200)
|
bgneal@767
|
213
|
bgneal@767
|
214 # Create an in-memory image for a dummy avatar
|
bgneal@767
|
215 file_obj = BytesIO()
|
bgneal@767
|
216 image = Image.new('RGBA', size=(100, 100))
|
bgneal@767
|
217 image.save(file_obj, 'png')
|
bgneal@767
|
218 file_obj.name = 'avatar.png'
|
bgneal@767
|
219 file_obj.seek(0)
|
bgneal@767
|
220
|
bgneal@767
|
221 response = self.client.post(url, {'avatar_file': file_obj}, follow=True)
|
bgneal@767
|
222 self.assertRedirects(response, reverse('bio-me'))
|
bgneal@767
|
223
|
bgneal@768
|
224 profile = UserProfile.objects.get(user__username='paul')
|
bgneal@768
|
225 self.assertTrue(profile.avatar.path)
|
bgneal@768
|
226 self.assertTrue(profile.avatar.name)
|
bgneal@768
|
227 self.assertTrue(os.path.exists(profile.avatar.path))
|
bgneal@768
|
228
|
bgneal@768
|
229 def test_flag_profile_get(self):
|
bgneal@768
|
230 paul = User.objects.get(username='paul')
|
bgneal@768
|
231 eddie = User.objects.get(username='eddie')
|
bgneal@768
|
232 url = reverse('bio-flag_profile', args=[eddie.pk])
|
bgneal@768
|
233
|
bgneal@768
|
234 # not logged in, wrong request method:
|
bgneal@768
|
235 response = self.client.get(url)
|
bgneal@768
|
236 self.assertEqual(response.status_code, 405)
|
bgneal@768
|
237
|
bgneal@768
|
238 # logged in, wrong request method
|
bgneal@768
|
239 self.client.login(username='paul', password='pw')
|
bgneal@768
|
240 response = self.client.get(url)
|
bgneal@768
|
241 self.assertEqual(response.status_code, 405)
|
bgneal@768
|
242
|
bgneal@768
|
243 # logged out, right request method
|
bgneal@768
|
244 self.client.logout()
|
bgneal@768
|
245 response = self.client.post(url)
|
bgneal@768
|
246 self.assertEqual(response.status_code, 200)
|
bgneal@768
|
247 self.assertEqual(UserProfileFlag.objects.all().count(), 0)
|
bgneal@768
|
248
|
bgneal@768
|
249 # logged in, right request method
|
bgneal@768
|
250 self.client.login(username='paul', password='pw')
|
bgneal@768
|
251 response = self.client.post(url)
|
bgneal@768
|
252 self.assertEqual(response.status_code, 200)
|
bgneal@768
|
253 self.assertEqual(UserProfileFlag.objects.all().count(), 1)
|
bgneal@768
|
254
|
bgneal@768
|
255 flag = UserProfileFlag.objects.first()
|
bgneal@768
|
256 self.assertEqual(flag.user, paul)
|
bgneal@789
|
257 self.assertEqual(flag.profile, eddie.profile)
|
bgneal@768
|
258
|
bgneal@768
|
259 self.assertEqual(len(mail.outbox), 1)
|
bgneal@768
|
260 self.assertTrue('A Profile Has Been Flagged' in mail.outbox[0].subject)
|