annotate bio/tests/test_views.py @ 765:f01bc9500aef

Added some more tests for the bio application.
author Brian Neal <bgneal@gmail.com>
date Sun, 19 Jan 2014 17:23:07 -0600
parents 8789299c75b1
children 22d158ef2217
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@765 8 from django.http import Http404
bgneal@463 9
bgneal@463 10
bgneal@463 11 class MemberSearchTest(TestCase):
bgneal@463 12
bgneal@463 13 USERNAME = u'John'
bgneal@463 14
bgneal@463 15 def setUp(self):
bgneal@463 16 user = User.objects.create_user(self.USERNAME, '', 'password')
bgneal@463 17 user.save()
bgneal@463 18
bgneal@463 19 self.username = 'test_user'
bgneal@463 20 self.pw = 'password'
bgneal@463 21 self.user = User.objects.create_user(self.username, '', self.pw)
bgneal@463 22 self.user.save()
bgneal@463 23 self.assertTrue(self.client.login(username=self.username,
bgneal@463 24 password=self.pw))
bgneal@463 25
bgneal@463 26 def tearDown(self):
bgneal@463 27 self.client.logout()
bgneal@463 28
bgneal@463 29 def testValidName(self):
bgneal@463 30 """
bgneal@463 31 Test a valid username.
bgneal@463 32 """
bgneal@463 33
bgneal@463 34 response = self.client.post(reverse('bio-member_search'),
bgneal@463 35 {'username': self.USERNAME},
bgneal@463 36 follow=True)
bgneal@463 37
bgneal@463 38 self.assertEqual(len(response.redirect_chain), 1)
bgneal@463 39 if response.redirect_chain:
bgneal@463 40 self.assertEqual(response.redirect_chain[0][0],
bgneal@463 41 'http://testserver' + reverse('bio-view_profile',
bgneal@463 42 kwargs={'username': self.USERNAME}))
bgneal@463 43 self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@463 44
bgneal@463 45 self.assertEqual(response.status_code, 200)
bgneal@463 46
bgneal@463 47 def testInvalidName(self):
bgneal@463 48 """
bgneal@463 49 Test a invalid username.
bgneal@463 50 """
bgneal@463 51
bgneal@463 52 response = self.client.post(reverse('bio-member_search'),
bgneal@463 53 {'username': self.USERNAME + '!'})
bgneal@463 54
bgneal@463 55 self.assertEqual(response.status_code, 200)
bgneal@463 56 self.assertContains(response, "That username does not exist.")
bgneal@463 57
bgneal@463 58 def testTrailingSpace(self):
bgneal@463 59 """
bgneal@463 60 Test a username with a trailing space.
bgneal@463 61 """
bgneal@463 62
bgneal@463 63 try:
bgneal@463 64 response = self.client.post(reverse('bio-member_search'),
bgneal@463 65 {'username': self.USERNAME + ' '},
bgneal@463 66 follow=True)
bgneal@463 67 except NoReverseMatch:
bgneal@463 68 self.fail('bit by a MySQL bug?')
bgneal@463 69
bgneal@463 70 self.assertEqual(len(response.redirect_chain), 1)
bgneal@463 71 if response.redirect_chain:
bgneal@463 72 self.assertEqual(response.redirect_chain[0][0],
bgneal@463 73 'http://testserver' + reverse('bio-view_profile',
bgneal@463 74 kwargs={'username': self.USERNAME}))
bgneal@463 75 self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@463 76
bgneal@463 77 self.assertEqual(response.status_code, 200)
bgneal@765 78
bgneal@765 79
bgneal@765 80 class AnonMemberListTest(TestCase):
bgneal@765 81
bgneal@765 82 def _test_login_required(self, list_by):
bgneal@765 83
bgneal@765 84 member_list_url = reverse('bio-member_list', kwargs={'type': list_by})
bgneal@765 85 response = self.client.get(member_list_url, follow=True)
bgneal@765 86 login_url = reverse('accounts-login') + '?next=' + member_list_url
bgneal@765 87 self.assertRedirects(response, login_url)
bgneal@765 88
bgneal@765 89 def test_login_required(self):
bgneal@765 90 self._test_login_required('user')
bgneal@765 91 self._test_login_required('date')
bgneal@765 92
bgneal@765 93
bgneal@765 94 class MemberListTest(TestCase):
bgneal@765 95
bgneal@765 96 def setUp(self):
bgneal@765 97 u1 = User.objects.create_user('paul', '', 'pw')
bgneal@765 98 u1.save()
bgneal@765 99 u2 = User.objects.create_user('eddie', '', 'pw')
bgneal@765 100 u2.save()
bgneal@765 101
bgneal@765 102 self.user = u1
bgneal@765 103 self.client.login(username=u1.username, password='pw')
bgneal@765 104
bgneal@765 105 def _test_get(self, list_by, page=None, code=200):
bgneal@765 106
bgneal@765 107 member_list_url = reverse('bio-member_list', kwargs={'type': list_by})
bgneal@765 108 if page is not None:
bgneal@765 109 member_list_url += '?page=%d' % page
bgneal@765 110 response = self.client.get(member_list_url)
bgneal@765 111 self.assertEqual(response.status_code, code)
bgneal@765 112
bgneal@765 113 def test_get(self):
bgneal@765 114 self._test_get('user')
bgneal@765 115 self._test_get('date')
bgneal@765 116 self._test_get('user', page=1)
bgneal@765 117 self._test_get('date', page=1)
bgneal@765 118
bgneal@765 119 def test_invalid_page(self):
bgneal@765 120 self._test_get('user', page=99, code=404)
bgneal@765 121 self._test_get('date', page=99, code=404)
bgneal@765 122
bgneal@765 123
bgneal@765 124 class MyProfileTest(TestCase):
bgneal@765 125
bgneal@765 126 def setUp(self):
bgneal@765 127 u1 = User.objects.create_user('paul', '', 'pw')
bgneal@765 128 u1.save()
bgneal@765 129 u2 = User.objects.create_user('eddie', '', 'pw')
bgneal@765 130 u2.save()
bgneal@765 131
bgneal@765 132 def test_anon(self):
bgneal@765 133 my_url = reverse('bio-me')
bgneal@765 134 response = self.client.get(my_url)
bgneal@765 135 login_url = reverse('accounts-login') + '?next=' + my_url
bgneal@765 136 self.assertRedirects(response, login_url)
bgneal@765 137
bgneal@765 138 def test_authenticated(self):
bgneal@765 139 self.client.login(username='paul', password='pw')
bgneal@765 140 response = self.client.get(reverse('bio-me'))
bgneal@765 141 self.assertEqual(response.status_code, 200)