test_auth.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2013 Guillaume Ayoub
  3. # Copyright © 2012-2016 Jean-Marc Martins
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Radicale tests with simple requests and authentication.
  19. """
  20. import base64
  21. import hashlib
  22. import os
  23. import radicale
  24. import tempfile
  25. from radicale import config, auth
  26. from . import BaseTest
  27. class TestBaseAuthRequests(BaseTest):
  28. """Tests basic requests with auth.
  29. We should setup auth for each type before creating the Application object.
  30. """
  31. def setup(self):
  32. self.userpass = "dG1wOmJlcG8="
  33. def teardown(self):
  34. config.set("auth", "type", "None")
  35. radicale.auth.is_authenticated = lambda *_: True
  36. def test_root(self):
  37. """Htpasswd authentication."""
  38. self.colpath = tempfile.mkdtemp()
  39. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  40. with open(htpasswd_file_path, "wb") as fd:
  41. fd.write(b"tmp:{SHA}" + base64.b64encode(
  42. hashlib.sha1(b"bepo").digest()))
  43. config.set("auth", "type", "htpasswd")
  44. auth.FILENAME = htpasswd_file_path
  45. auth.ENCRYPTION = "sha1"
  46. self.application = radicale.Application()
  47. status, headers, answer = self.request(
  48. "GET", "/", HTTP_AUTHORIZATION=self.userpass)
  49. assert status == 200
  50. assert "Radicale works!" in answer
  51. def test_custom(self):
  52. """Custom authentication."""
  53. config.set("auth", "type", "tests.custom.auth")
  54. self.application = radicale.Application()
  55. status, headers, answer = self.request(
  56. "GET", "/", HTTP_AUTHORIZATION=self.userpass)
  57. assert status == 200
  58. assert "Radicale works!" in answer