No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

78 líneas
3.0 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. @mock.patch('requests.get', mock_requests_get)
  27. def test_has_perm_on_pin(self):
  28. image = Image.objects.create_for_url('http://testserver/mocked/screenshot.png')
  29. user = User.objects.get(username=self.username)
  30. pin = Pin.objects.create(submitter=user, image=image)
  31. self.assertTrue(self.backend.has_perm(user, 'add_pin', pin))
  32. @mock.patch('requests.get', mock_requests_get)
  33. def test_has_perm_on_pin_unauthorized(self):
  34. image = Image.objects.create_for_url('http://testserver/mocked/screenshot.png')
  35. user = User.objects.get(username=self.username)
  36. other_user = User.objects.create_user('test', 'test@example.com', 'test')
  37. pin = Pin.objects.create(submitter=user, image=image)
  38. self.assertFalse(self.backend.has_perm(other_user, 'add_pin', pin))
  39. class CreateUserTest(TestCase):
  40. def test_create_post(self):
  41. data = {
  42. 'username': 'jdoe',
  43. 'email': 'jdoe@example.com',
  44. 'password': 'password'
  45. }
  46. response = self.client.post(reverse('users:register'), data=data)
  47. self.assertRedirects(response, reverse('core:recent-pins'))
  48. self.assertIn('_auth_user_id', self.client.session)
  49. @override_settings(ALLOW_NEW_REGISTRATIONS=False)
  50. def test_create_post_not_allowed(self):
  51. response = self.client.get(reverse('users:register'))
  52. self.assertRedirects(response, reverse('core:recent-pins'))
  53. class LogoutViewTest(TestCase):
  54. def setUp(self):
  55. User.objects.create_user(username='jdoe', password='password')
  56. self.client.login(username='jdoe', password='password')
  57. def test_logout_view(self):
  58. response = self.client.get(reverse('users:logout'))
  59. self.assertRedirects(response, reverse('core:recent-pins'))