Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

63 linhas
2.2 KiB

  1. from django.core.urlresolvers import reverse
  2. from django.test import TestCase
  3. from django.test.utils import override_settings
  4. import mock
  5. from .auth.backends import CombinedAuthBackend
  6. from core.models import Image, Pin
  7. from .models import User
  8. def mock_requests_get(url, headers=None):
  9. response = mock.Mock(content=open('logo.png', 'rb').read())
  10. return response
  11. class CombinedAuthBackendTest(TestCase):
  12. def setUp(self):
  13. self.backend = CombinedAuthBackend()
  14. self.username = 'jdoe'
  15. self.email = 'jdoe@example.com'
  16. self.password = 'password'
  17. User.objects.create_user(username=self.username, email=self.email, password=self.password)
  18. def test_authenticate_username(self):
  19. self.assertTrue(self.backend.authenticate(username=self.username, password=self.password))
  20. def test_authenticate_email(self):
  21. self.assertTrue(self.backend.authenticate(username=self.email, password=self.password))
  22. def test_authenticate_wrong_password(self):
  23. self.assertIsNone(self.backend.authenticate(username=self.username, password='wrong-password'))
  24. def test_authenticate_unknown_user(self):
  25. self.assertIsNone(self.backend.authenticate(username='wrong-username', password='wrong-password'))
  26. class CreateUserTest(TestCase):
  27. def test_create_post(self):
  28. data = {
  29. 'username': 'jdoe',
  30. 'email': 'jdoe@example.com',
  31. 'password': 'password'
  32. }
  33. response = self.client.post(reverse('users:register'), data=data)
  34. self.assertRedirects(response, reverse('core:recent-pins'))
  35. self.assertIn('_auth_user_id', self.client.session)
  36. @override_settings(ALLOW_NEW_REGISTRATIONS=False)
  37. def test_create_post_not_allowed(self):
  38. response = self.client.get(reverse('users:register'))
  39. self.assertRedirects(response, reverse('core:recent-pins'))
  40. class LogoutViewTest(TestCase):
  41. def setUp(self):
  42. User.objects.create_user(username='jdoe', password='password')
  43. self.client.login(username='jdoe', password='password')
  44. def test_logout_view(self):
  45. response = self.client.get(reverse('users:logout'))
  46. self.assertRedirects(response, reverse('core:recent-pins'))