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