You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 lines
2.4 KiB

  1. from django.test import TestCase
  2. from django.test.utils import override_settings
  3. import mock
  4. from django.urls import reverse
  5. from .auth.backends import CombinedAuthBackend
  6. from .models import User
  7. def mock_requests_get(url, headers=None):
  8. response = mock.Mock(content=open('docs/src/imgs/logo-dark.png', 'rb').read())
  9. return response
  10. class CombinedAuthBackendTest(TestCase):
  11. def setUp(self):
  12. self.backend = CombinedAuthBackend()
  13. self.username = 'jdoe'
  14. self.email = 'jdoe@example.com'
  15. self.password = 'password'
  16. User.objects.create_user(username=self.username, email=self.email, password=self.password)
  17. def test_authenticate_username(self):
  18. self.assertTrue(self.backend.authenticate(username=self.username, password=self.password))
  19. def test_authenticate_email(self):
  20. self.assertTrue(self.backend.authenticate(username=self.email, password=self.password))
  21. def test_authenticate_wrong_password(self):
  22. self.assertIsNone(self.backend.authenticate(username=self.username, password='wrong-password'))
  23. def test_authenticate_unknown_user(self):
  24. self.assertIsNone(self.backend.authenticate(username='wrong-username', password='wrong-password'))
  25. class CreateUserTest(TestCase):
  26. def test_create_post(self):
  27. data = {
  28. 'username': 'jdoe',
  29. 'email': 'jdoe@example.com',
  30. 'password': 'password',
  31. 'password_repeat': 'password',
  32. }
  33. response = self.client.post(
  34. reverse('users:user-list'),
  35. data=data,
  36. )
  37. self.assertEqual(response.status_code, 201)
  38. @override_settings(ALLOW_NEW_REGISTRATIONS=False)
  39. def test_create_post_not_allowed(self):
  40. data = {
  41. 'username': 'jdoe',
  42. 'email': 'jdoe@example.com',
  43. 'password': 'password',
  44. 'password_repeat': 'password',
  45. }
  46. response = self.client.post(
  47. reverse('users:user-list'),
  48. data=data,
  49. )
  50. self.assertEqual(response.status_code, 403)
  51. class LogoutViewTest(TestCase):
  52. def setUp(self):
  53. User.objects.create_user(username='jdoe', password='password')
  54. self.client.login(username='jdoe', password='password')
  55. def test_logout_view(self):
  56. response = self.client.get(reverse('users:logout'))
  57. self.assertEqual(response.status_code, 302)