test_authentication.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND
  2. from desecapi.tests.base import DynDomainOwnerTestCase
  3. class DynUpdateAuthenticationTestCase(DynDomainOwnerTestCase):
  4. NUM_OWNED_DOMAINS = 1
  5. def _get_dyndns12(self):
  6. with self.assertPdnsNoRequestsBut(self.requests_desec_rr_sets_update()):
  7. return self.client.get(self.reverse('v1:dyndns12update'))
  8. def assertDynDNS12Status(self, status=HTTP_200_OK, authorization=None):
  9. if authorization:
  10. self.client.set_credentials_basic_auth(authorization)
  11. self.assertStatus(self._get_dyndns12(), status)
  12. def test_username_password(self):
  13. def _test_DynDNS12AuthenticationStatus(username, token, status):
  14. self.client.set_credentials_basic_auth(username, token)
  15. self.assertDynDNS12Status(status)
  16. _test_DynDNS12AuthenticationStatus('', self.token.key, HTTP_200_OK)
  17. _test_DynDNS12AuthenticationStatus(self.owner.get_username(), self.token.key, HTTP_200_OK)
  18. _test_DynDNS12AuthenticationStatus(self.my_domain.name, self.token.key, HTTP_200_OK)
  19. _test_DynDNS12AuthenticationStatus(' ' + self.my_domain.name, self.token.key, HTTP_401_UNAUTHORIZED)
  20. _test_DynDNS12AuthenticationStatus('wrong', self.token.key, HTTP_401_UNAUTHORIZED)
  21. _test_DynDNS12AuthenticationStatus('', 'wrong', HTTP_401_UNAUTHORIZED)
  22. _test_DynDNS12AuthenticationStatus(self.user.get_username(), 'wrong', HTTP_401_UNAUTHORIZED)
  23. def test_malformed_basic_auth(self):
  24. for authorization in [
  25. 'asdf:asdf:sadf',
  26. 'asdf',
  27. 'bull[%]shit',
  28. '你好',
  29. '💩💩💩💩',
  30. '💩💩:💩💩',
  31. ]:
  32. self.assertDynDNS12Status(authorization=authorization, status=HTTP_401_UNAUTHORIZED)
  33. class TokenAuthenticationTestCase(DynDomainOwnerTestCase):
  34. def _get_domains(self):
  35. with self.assertPdnsNoRequestsBut(self.request_pdns_zone_retrieve_crypto_keys()):
  36. return self.client.get(self.reverse('v1:domain-list'))
  37. def assertAuthenticationStatus(self, status=HTTP_200_OK, token=''):
  38. self.client.set_credentials_token_auth(token)
  39. self.assertStatus(self._get_domains(), status)
  40. def test_token_case_sensitive(self):
  41. self.assertAuthenticationStatus(HTTP_200_OK, self.token.key)
  42. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.key.upper())
  43. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.key.lower())