annotate bio/tests/test_views.py @ 908:2181da65c98b

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