comparison gpp/bio/tests/view_tests.py @ 463:452835f4429f

Fixing #225; for some reason MySQL finds the user 'John' when searching for 'John ' (note trailing space). This doesn't happen on SQLite. This causes a NoReverseMatch when searching for 'John ' in the member search. The solution is to call strip() on the form field contents in the clean_username() method of the search form.
author Brian Neal <bgneal@gmail.com>
date Sat, 09 Jul 2011 02:00:48 +0000
parents
children
comparison
equal deleted inserted replaced
462:53fdaf0da539 463:452835f4429f
1 """
2 View tests for the bio application.
3
4 """
5 import datetime
6
7 from django.contrib.auth.models import User
8 from django.test import TestCase
9 from django.core.urlresolvers import reverse, NoReverseMatch
10
11
12 class MemberSearchTest(TestCase):
13
14 USERNAME = u'John'
15
16 def setUp(self):
17 user = User.objects.create_user(self.USERNAME, '', 'password')
18 user.save()
19
20 self.username = 'test_user'
21 self.pw = 'password'
22 self.user = User.objects.create_user(self.username, '', self.pw)
23 self.user.save()
24 self.assertTrue(self.client.login(username=self.username,
25 password=self.pw))
26
27 def tearDown(self):
28 self.client.logout()
29
30 def testValidName(self):
31 """
32 Test a valid username.
33 """
34
35 response = self.client.post(reverse('bio-member_search'),
36 {'username': self.USERNAME},
37 follow=True)
38
39 self.assertEqual(len(response.redirect_chain), 1)
40 if response.redirect_chain:
41 self.assertEqual(response.redirect_chain[0][0],
42 'http://testserver' + reverse('bio-view_profile',
43 kwargs={'username': self.USERNAME}))
44 self.assertEqual(response.redirect_chain[0][1], 302)
45
46 self.assertEqual(response.status_code, 200)
47
48 def testInvalidName(self):
49 """
50 Test a invalid username.
51 """
52
53 response = self.client.post(reverse('bio-member_search'),
54 {'username': self.USERNAME + '!'})
55
56 self.assertEqual(response.status_code, 200)
57 self.assertContains(response, "That username does not exist.")
58
59 def testTrailingSpace(self):
60 """
61 Test a username with a trailing space.
62 """
63
64 try:
65 response = self.client.post(reverse('bio-member_search'),
66 {'username': self.USERNAME + ' '},
67 follow=True)
68 except NoReverseMatch:
69 self.fail('bit by a MySQL bug?')
70
71 self.assertEqual(len(response.redirect_chain), 1)
72 if response.redirect_chain:
73 self.assertEqual(response.redirect_chain[0][0],
74 'http://testserver' + reverse('bio-view_profile',
75 kwargs={'username': self.USERNAME}))
76 self.assertEqual(response.redirect_chain[0][1], 302)
77
78 self.assertEqual(response.status_code, 200)