test_authentication.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import re
  2. from django.core import mail
  3. from rest_framework import status
  4. from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED
  5. from desecapi.models import Token, User
  6. from desecapi.tests.base import DynDomainOwnerTestCase, DesecTestCase
  7. class DynUpdateAuthenticationTestCase(DynDomainOwnerTestCase):
  8. NUM_OWNED_DOMAINS = 1
  9. def _get_dyndns12(self):
  10. with self.assertPdnsNoRequestsBut(self.requests_desec_rr_sets_update()):
  11. return self.client.get(self.reverse('v1:dyndns12update'))
  12. def assertDynDNS12Status(self, code=HTTP_200_OK, authorization=None):
  13. if authorization:
  14. self.client.set_credentials_basic_auth(authorization)
  15. self.assertStatus(self._get_dyndns12(), code)
  16. def test_username_password(self):
  17. # noinspection PyPep8Naming
  18. def assertDynDNS12AuthenticationStatus(username, token, code):
  19. self.client.set_credentials_basic_auth(username, token)
  20. self.assertDynDNS12Status(code)
  21. assertDynDNS12AuthenticationStatus('', self.token.key, HTTP_200_OK)
  22. assertDynDNS12AuthenticationStatus(self.owner.get_username(), self.token.key, HTTP_200_OK)
  23. assertDynDNS12AuthenticationStatus(self.my_domain.name, self.token.key, HTTP_200_OK)
  24. assertDynDNS12AuthenticationStatus(' ' + self.my_domain.name, self.token.key, HTTP_401_UNAUTHORIZED)
  25. assertDynDNS12AuthenticationStatus('wrong', self.token.key, HTTP_401_UNAUTHORIZED)
  26. assertDynDNS12AuthenticationStatus('', 'wrong', HTTP_401_UNAUTHORIZED)
  27. assertDynDNS12AuthenticationStatus(self.user.get_username(), 'wrong', HTTP_401_UNAUTHORIZED)
  28. def test_malformed_basic_auth(self):
  29. for authorization in [
  30. 'asdf:asdf:sadf',
  31. 'asdf',
  32. 'bull[%]shit',
  33. '你好',
  34. '💩💩💩💩',
  35. '💩💩:💩💩',
  36. ]:
  37. self.assertDynDNS12Status(authorization=authorization, code=HTTP_401_UNAUTHORIZED)
  38. class SignUpLoginTestCase(DesecTestCase):
  39. EMAIL = None
  40. PASSWORD = None
  41. REGISTRATION_ENDPOINT = None
  42. LOGIN_ENDPOINT = None
  43. REGISTRATION_STATUS = status.HTTP_202_ACCEPTED
  44. LOGIN_STATUS = status.HTTP_200_OK
  45. def __init__(self, *args, **kwargs):
  46. super().__init__(*args, **kwargs)
  47. self.EMAIL = self.random_username()
  48. self.PASSWORD = self.random_password()
  49. if not self.REGISTRATION_ENDPOINT:
  50. self.REGISTRATION_ENDPOINT = self.reverse('v1:register')
  51. if not self.LOGIN_ENDPOINT:
  52. self.LOGIN_ENDPOINT = self.reverse('v1:login')
  53. def sign_up(self):
  54. self.assertStatus(
  55. self.client.post(self.REGISTRATION_ENDPOINT, {
  56. 'email': self.EMAIL,
  57. 'password': self.PASSWORD,
  58. }),
  59. self.REGISTRATION_STATUS
  60. )
  61. def activate(self):
  62. total = 1
  63. self.assertEqual(len(mail.outbox), total, "Expected %i message in the outbox, but found %i." %
  64. (total, len(mail.outbox)))
  65. email = mail.outbox[-1]
  66. self.assertTrue('Welcome' in email.subject)
  67. confirmation_link = re.search(r'following link:\s+([^\s]*)', email.body).group(1)
  68. self.client.get(confirmation_link)
  69. def log_in(self):
  70. response = self.client.post(self.LOGIN_ENDPOINT, {
  71. 'email': self.EMAIL,
  72. 'password': self.PASSWORD,
  73. })
  74. self.assertContains(response, "auth_token", status_code=self.LOGIN_STATUS)
  75. def test_sign_up(self):
  76. self.sign_up()
  77. self.assertFalse(User.objects.get(email=self.EMAIL).is_active)
  78. def test_activate(self):
  79. self.sign_up()
  80. self.activate()
  81. self.assertTrue(User.objects.get(email=self.EMAIL).is_active)
  82. def test_log_in(self):
  83. self.sign_up()
  84. self.activate()
  85. self.log_in()
  86. def test_log_in_twice(self):
  87. self.sign_up()
  88. self.activate()
  89. self.log_in()
  90. self.log_in()
  91. def test_log_in_two_tokens(self):
  92. self.sign_up()
  93. self.activate()
  94. for _ in range(2):
  95. Token.objects.create(user=User.objects.get(email=self.EMAIL))
  96. self.log_in()
  97. class TokenAuthenticationTestCase(DynDomainOwnerTestCase):
  98. def _get_domains(self):
  99. with self.assertPdnsNoRequestsBut(self.request_pdns_zone_retrieve_crypto_keys()):
  100. return self.client.get(self.reverse('v1:domain-list'))
  101. def assertAuthenticationStatus(self, code=HTTP_200_OK, token=''):
  102. self.client.set_credentials_token_auth(token)
  103. self.assertStatus(self._get_domains(), code)
  104. def test_token_case_sensitive(self):
  105. self.assertAuthenticationStatus(HTTP_200_OK, self.token.key)
  106. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.key.upper())
  107. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.key.lower())