test_auth.py 2.3 KB

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