Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

96 řádky
3.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 rest_framework.reverse import reverse as drf_reverse
  6. from .auth.backends import CombinedAuthBackend
  7. from .models import User
  8. def mock_requests_get(url, headers=None):
  9. response = mock.Mock(content=open('docs/src/imgs/logo-dark.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. 'password_repeat': 'password',
  33. }
  34. response = self.client.post(
  35. reverse('users:user-list'),
  36. data=data,
  37. )
  38. self.assertEqual(response.status_code, 201)
  39. @override_settings(ALLOW_NEW_REGISTRATIONS=False)
  40. def test_create_post_not_allowed(self):
  41. data = {
  42. 'username': 'jdoe',
  43. 'email': 'jdoe@example.com',
  44. 'password': 'password',
  45. 'password_repeat': 'password',
  46. }
  47. response = self.client.post(
  48. reverse('users:user-list'),
  49. data=data,
  50. )
  51. self.assertEqual(response.status_code, 401)
  52. class LogoutViewTest(TestCase):
  53. def setUp(self):
  54. User.objects.create_user(username='jdoe', password='password')
  55. self.client.login(username='jdoe', password='password')
  56. def test_logout_view(self):
  57. response = self.client.get(reverse('users:logout'))
  58. self.assertEqual(response.status_code, 302)
  59. class ProfileViewTest(TestCase):
  60. def setUp(self):
  61. self.first_user = User.objects.create_user(username='jdoe', password='password')
  62. self.second_user = User.objects.create_user(username='judy', password='password')
  63. self.client.login(username='jdoe', password='password')
  64. def test_should_have_access_to_token(self):
  65. from rest_framework.authtoken.models import Token
  66. url = drf_reverse('users:public-user-list')
  67. response = self.client.get(f"{url}?username={self.first_user.username}")
  68. self.assertEqual(response.status_code, 200)
  69. self.assertEqual(response.data[0]['token'], Token.objects.get(user=self.first_user).key)
  70. def test_should_have_no_access_to_token_of_other_user(self):
  71. url = drf_reverse('users:public-user-list')
  72. response = self.client.get(f"{url}?username={self.second_user.username}")
  73. self.assertEqual(response.status_code, 200)
  74. self.assertEqual(response.data[0]['token'], None)