tests.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from django.contrib.auth.models import User
  2. from django.core.urlresolvers import reverse
  3. from django.test import TestCase
  4. from .models import Profile
  5. from .forms import UserForm, ProfileForm
  6. class ViewsTestCase(TestCase):
  7. def setUp(self):
  8. user = User.objects.create_user('user', first_name='first', last_name='last', email='user@example.net', password='user')
  9. def test_auth(self):
  10. self.assertEquals(self.client.get(reverse('login')).status_code, 200)
  11. self.assertEquals(self.client.get(reverse('password_reset')).status_code, 200)
  12. self.assertEquals(self.client.get(reverse('password_reset_done')).status_code, 200)
  13. def test_email_backend(self):
  14. self.assertFalse(self.client.login(username='user@example.net', password='wrong'))
  15. self.assertFalse(self.client.login(username='wrong@example.net', password='user'))
  16. self.assertTrue(self.client.login(username='user@example.net', password='user'))
  17. def test_login_logout(self):
  18. self.assertEquals(self.client.get(reverse('login')).status_code, 200)
  19. self.client.login(username='user', password='user')
  20. self.assertEquals(self.client.get(reverse('adhesion-detail-user')).status_code, 200)
  21. self.assertRedirects(self.client.get(reverse('logout')), reverse('adhesion-detail-user'), target_status_code=302) # user page is redirected to login
  22. self.assertRedirects(self.client.get(reverse('adhesion-detail-user')), reverse('login') + '?next=' + reverse('adhesion-detail-user'))
  23. def test_profile(self):
  24. response = self.client.get(reverse('profile'))
  25. self.assertRedirects(response, reverse('login') + '?next=' + reverse('profile'))
  26. self.client.login(username='user', password='user')
  27. response = self.client.get(reverse('profile'))
  28. self.assertEqual(response.status_code, 200)
  29. user = User.objects.get(username='user')
  30. user_form = UserForm(None, instance=user)
  31. data = {key: getattr(user_form.instance, key) for key in user_form.fields}
  32. profile_form = ProfileForm(instance=user.profile)
  33. data.update({key: getattr(profile_form.instance, key) for key in profile_form.fields})
  34. data['username'] = 'user2' # try to tamper username
  35. data['first_name'] = 'first2' # try to tamper username
  36. data['last_name'] = 'last2' # try to tamper username
  37. data['email'] = 'user@example.org'
  38. data['address'] = '221B Baker Street'
  39. response = self.client.post(reverse('profile'), data)
  40. self.assertRedirects(response, reverse('profile'))
  41. user = User.objects.get(pk=user.pk) # refresh user
  42. self.assertEquals(user.username, 'user') # should not be modified
  43. self.assertEquals(user.first_name, 'first') # should not be modified
  44. self.assertEquals(user.last_name, 'last') # should not be modified
  45. self.assertEquals(user.email, 'user@example.org')
  46. self.assertEquals(user.profile.address, '221B Baker Street')