bgneal@463: """
bgneal@463: View tests for the bio application.
bgneal@463: 
bgneal@463: """
bgneal@463: import datetime
bgneal@463: 
bgneal@463: from django.contrib.auth.models import User
bgneal@463: from django.test import TestCase
bgneal@463: from django.core.urlresolvers import reverse, NoReverseMatch
bgneal@463: 
bgneal@463: 
bgneal@463: class MemberSearchTest(TestCase):
bgneal@463: 
bgneal@463:     USERNAME = u'John'
bgneal@463: 
bgneal@463:     def setUp(self):
bgneal@463:         user = User.objects.create_user(self.USERNAME, '', 'password')
bgneal@463:         user.save()
bgneal@463: 
bgneal@463:         self.username = 'test_user'
bgneal@463:         self.pw = 'password'
bgneal@463:         self.user = User.objects.create_user(self.username, '', self.pw)
bgneal@463:         self.user.save()
bgneal@463:         self.assertTrue(self.client.login(username=self.username,
bgneal@463:             password=self.pw))
bgneal@463: 
bgneal@463:     def tearDown(self):
bgneal@463:         self.client.logout()
bgneal@463: 
bgneal@463:     def testValidName(self):
bgneal@463:         """
bgneal@463:         Test a valid username.
bgneal@463:         """
bgneal@463: 
bgneal@463:         response = self.client.post(reverse('bio-member_search'),
bgneal@463:                             {'username': self.USERNAME},
bgneal@463:                             follow=True)
bgneal@463: 
bgneal@463:         self.assertEqual(len(response.redirect_chain), 1)
bgneal@463:         if response.redirect_chain:
bgneal@463:             self.assertEqual(response.redirect_chain[0][0],
bgneal@463:                     'http://testserver' + reverse('bio-view_profile',
bgneal@463:                         kwargs={'username': self.USERNAME}))
bgneal@463:             self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@463: 
bgneal@463:         self.assertEqual(response.status_code, 200)
bgneal@463: 
bgneal@463:     def testInvalidName(self):
bgneal@463:         """
bgneal@463:         Test a invalid username.
bgneal@463:         """
bgneal@463: 
bgneal@463:         response = self.client.post(reverse('bio-member_search'),
bgneal@463:                             {'username': self.USERNAME + '!'})
bgneal@463: 
bgneal@463:         self.assertEqual(response.status_code, 200)
bgneal@463:         self.assertContains(response, "That username does not exist.")
bgneal@463: 
bgneal@463:     def testTrailingSpace(self):
bgneal@463:         """
bgneal@463:         Test a username with a trailing space.
bgneal@463:         """
bgneal@463: 
bgneal@463:         try:
bgneal@463:             response = self.client.post(reverse('bio-member_search'),
bgneal@463:                                 {'username': self.USERNAME + ' '},
bgneal@463:                                 follow=True)
bgneal@463:         except NoReverseMatch:
bgneal@463:             self.fail('bit by a MySQL bug?')
bgneal@463: 
bgneal@463:         self.assertEqual(len(response.redirect_chain), 1)
bgneal@463:         if response.redirect_chain:
bgneal@463:             self.assertEqual(response.redirect_chain[0][0],
bgneal@463:                     'http://testserver' + reverse('bio-view_profile',
bgneal@463:                         kwargs={'username': self.USERNAME}))
bgneal@463:             self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@463: 
bgneal@463:         self.assertEqual(response.status_code, 200)