test_authentication.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from rest_framework import status
  2. from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED
  3. from desecapi.models import Token, User
  4. from desecapi.tests.base import DynDomainOwnerTestCase, DesecTestCase
  5. class DynUpdateAuthenticationTestCase(DynDomainOwnerTestCase):
  6. NUM_OWNED_DOMAINS = 1
  7. def _get_dyndns12(self):
  8. with self.assertPdnsNoRequestsBut(self.requests_desec_rr_sets_update()):
  9. return self.client.get(self.reverse('v1:dyndns12update'))
  10. def assertDynDNS12Status(self, status=HTTP_200_OK, authorization=None):
  11. if authorization:
  12. self.client.set_credentials_basic_auth(authorization)
  13. self.assertStatus(self._get_dyndns12(), status)
  14. def test_username_password(self):
  15. def _test_DynDNS12AuthenticationStatus(username, token, status):
  16. self.client.set_credentials_basic_auth(username, token)
  17. self.assertDynDNS12Status(status)
  18. _test_DynDNS12AuthenticationStatus('', self.token.key, HTTP_200_OK)
  19. _test_DynDNS12AuthenticationStatus(self.owner.get_username(), self.token.key, HTTP_200_OK)
  20. _test_DynDNS12AuthenticationStatus(self.my_domain.name, self.token.key, HTTP_200_OK)
  21. _test_DynDNS12AuthenticationStatus(' ' + self.my_domain.name, self.token.key, HTTP_401_UNAUTHORIZED)
  22. _test_DynDNS12AuthenticationStatus('wrong', self.token.key, HTTP_401_UNAUTHORIZED)
  23. _test_DynDNS12AuthenticationStatus('', 'wrong', HTTP_401_UNAUTHORIZED)
  24. _test_DynDNS12AuthenticationStatus(self.user.get_username(), 'wrong', HTTP_401_UNAUTHORIZED)
  25. def test_malformed_basic_auth(self):
  26. for authorization in [
  27. 'asdf:asdf:sadf',
  28. 'asdf',
  29. 'bull[%]shit',
  30. '你好',
  31. '💩💩💩💩',
  32. '💩💩:💩💩',
  33. ]:
  34. self.assertDynDNS12Status(authorization=authorization, status=HTTP_401_UNAUTHORIZED)
  35. class SignUpLoginTestCase(DesecTestCase):
  36. EMAIL = None
  37. PASSWORD = None
  38. REGISTRATION_ENDPOINT = None
  39. LOGIN_ENDPOINT = None
  40. REGISTRATION_STATUS = status.HTTP_201_CREATED
  41. LOGIN_STATUS = status.HTTP_200_OK
  42. def __init__(self, *args, **kwargs):
  43. super().__init__(*args, **kwargs)
  44. self.EMAIL = self.random_username()
  45. self.PASSWORD = self.random_password()
  46. if not self.REGISTRATION_ENDPOINT:
  47. self.REGISTRATION_ENDPOINT = self.reverse('v1:register')
  48. if not self.LOGIN_ENDPOINT:
  49. self.LOGIN_ENDPOINT = self.reverse('v1:login')
  50. def sign_up(self):
  51. self.assertStatus(
  52. self.client.post(self.REGISTRATION_ENDPOINT, {
  53. 'email': self.EMAIL,
  54. 'password': self.PASSWORD,
  55. }),
  56. self.REGISTRATION_STATUS
  57. )
  58. def log_in(self):
  59. response = self.client.post(self.LOGIN_ENDPOINT, {
  60. 'email': self.EMAIL,
  61. 'password': self.PASSWORD,
  62. })
  63. self.assertContains(response, "auth_token", status_code=self.LOGIN_STATUS)
  64. def test_sign_up(self):
  65. self.sign_up()
  66. def test_log_in(self):
  67. self.sign_up()
  68. self.log_in()
  69. def test_log_in_twice(self):
  70. self.sign_up()
  71. self.log_in()
  72. self.log_in()
  73. def test_log_in_two_tokens(self):
  74. self.sign_up() # this may create a token
  75. for _ in range(2):
  76. Token.objects.create(user=User.objects.get(email=self.EMAIL))
  77. self.log_in()
  78. class URLSignUpLoginTestCase(SignUpLoginTestCase):
  79. REGISTRATION_ENDPOINT = '/api/v1/auth/users/'
  80. LOGIN_ENDPOINT = '/api/v1/auth/token/login/'
  81. LOGIN_STATUS = status.HTTP_201_CREATED
  82. class LegacyURLSignUpLoginTestCase(SignUpLoginTestCase):
  83. REGISTRATION_ENDPOINT = '/api/v1/auth/users/create/'
  84. LOGIN_ENDPOINT = '/api/v1/auth/token/create/'
  85. LOGIN_STATUS = status.HTTP_201_CREATED
  86. class LegacyURLSignUpLoginTestCase2(SignUpLoginTestCase):
  87. REGISTRATION_ENDPOINT = '/api/v1/auth/users/create/'
  88. LOGIN_ENDPOINT = '/api/v1/auth/token/create'
  89. LOGIN_STATUS = status.HTTP_200_OK
  90. class TokenAuthenticationTestCase(DynDomainOwnerTestCase):
  91. def _get_domains(self):
  92. with self.assertPdnsNoRequestsBut(self.request_pdns_zone_retrieve_crypto_keys()):
  93. return self.client.get(self.reverse('v1:domain-list'))
  94. def assertAuthenticationStatus(self, status=HTTP_200_OK, token=''):
  95. self.client.set_credentials_token_auth(token)
  96. self.assertStatus(self._get_domains(), status)
  97. def test_token_case_sensitive(self):
  98. self.assertAuthenticationStatus(HTTP_200_OK, self.token.key)
  99. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.key.upper())
  100. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.key.lower())