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