test_auth.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 logging
  23. import os
  24. import shutil
  25. import tempfile
  26. from radicale import Application, config
  27. from . import BaseTest
  28. class TestBaseAuthRequests(BaseTest):
  29. """Tests basic requests with auth.
  30. We should setup auth for each type before creating the Application object.
  31. """
  32. def setup(self):
  33. self.colpath = tempfile.mkdtemp()
  34. def teardown(self):
  35. shutil.rmtree(self.colpath)
  36. def test_root(self):
  37. """Htpasswd authentication."""
  38. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  39. with open(htpasswd_file_path, "wb") as fd:
  40. fd.write(b"tmp:{SHA}" + base64.b64encode(
  41. hashlib.sha1(b"bepo").digest()))
  42. configuration = config.load()
  43. configuration.set("auth", "type", "htpasswd")
  44. configuration.set("auth", "htpasswd_filename", htpasswd_file_path)
  45. configuration.set("auth", "htpasswd_encryption", "sha1")
  46. self.application = Application(
  47. configuration, logging.getLogger("radicale_test"))
  48. status, headers, answer = self.request(
  49. "GET", "/", HTTP_AUTHORIZATION="dG1wOmJlcG8=")
  50. assert status == 200
  51. assert "Radicale works!" in answer
  52. def test_custom(self):
  53. """Custom authentication."""
  54. configuration = config.load()
  55. configuration.set("auth", "type", "tests.custom.auth")
  56. self.application = Application(
  57. configuration, logging.getLogger("radicale_test"))
  58. status, headers, answer = self.request(
  59. "GET", "/", HTTP_AUTHORIZATION="dG1wOmJlcG8=")
  60. assert status == 200
  61. assert "Radicale works!" in answer