__init__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012-2013 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Tests for Radicale.
  20. """
  21. import base64
  22. import hashlib
  23. import os
  24. import shutil
  25. import sys
  26. import tempfile
  27. from dulwich.repo import Repo
  28. from io import BytesIO
  29. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  30. import radicale
  31. os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname(
  32. os.path.dirname(__file__)), "config")
  33. from radicale import config
  34. from radicale.auth import htpasswd
  35. from .helpers import get_file_content
  36. from sqlalchemy.orm import sessionmaker
  37. from sqlalchemy import create_engine
  38. class BaseTest(object):
  39. """Base class for tests."""
  40. def request(self, method, path, data=None, **args):
  41. """Send a request."""
  42. self.application._status = None
  43. self.application._headers = None
  44. self.application._answer = None
  45. for key in args:
  46. args[key.upper()] = args[key]
  47. args["REQUEST_METHOD"] = method.upper()
  48. args["PATH_INFO"] = path
  49. if data:
  50. if sys.version_info[0] >= 3:
  51. data = data.encode("utf-8")
  52. args["wsgi.input"] = BytesIO(data)
  53. args["CONTENT_LENGTH"] = str(len(data))
  54. self.application._answer = self.application(args, self.start_response)
  55. return (
  56. int(self.application._status.split()[0]),
  57. dict(self.application._headers),
  58. self.application._answer[0].decode("utf-8")
  59. if self.application._answer else None)
  60. def start_response(self, status, headers):
  61. """Put the response values into the current application."""
  62. self.application._status = status
  63. self.application._headers = headers
  64. class FileSystem(BaseTest):
  65. """Base class for filesystem tests."""
  66. storage_type = "filesystem"
  67. def setup(self):
  68. """Setup function for each test."""
  69. self.colpath = tempfile.mkdtemp()
  70. config.set("storage", "type", self.storage_type)
  71. from radicale.storage import filesystem
  72. filesystem.FOLDER = self.colpath
  73. filesystem.GIT_REPOSITORY = None
  74. self.application = radicale.Application()
  75. def teardown(self):
  76. """Teardown function for each test."""
  77. shutil.rmtree(self.colpath)
  78. class MultiFileSystem(FileSystem):
  79. """Base class for multifilesystem tests."""
  80. storage_type = "multifilesystem"
  81. class DataBaseSystem(BaseTest):
  82. """Base class for database tests"""
  83. def setup(self):
  84. config.set("storage", "type", "database")
  85. config.set("storage", "database_url", "sqlite://")
  86. from radicale.storage import database
  87. database.Session = sessionmaker()
  88. database.Session.configure(bind=create_engine("sqlite://"))
  89. session = database.Session()
  90. # session.execute(get_file_content("schema.sql"))
  91. for st in get_file_content("schema.sql").split(";"):
  92. session.execute(st)
  93. session.commit()
  94. self.application = radicale.Application()
  95. class GitFileSystem(FileSystem):
  96. """Base class for filesystem tests using Git"""
  97. def setup(self):
  98. super(GitFileSystem, self).setup()
  99. Repo.init(self.colpath)
  100. from radicale.storage import filesystem
  101. filesystem.GIT_REPOSITORY = Repo(self.colpath)
  102. class GitMultiFileSystem(GitFileSystem, MultiFileSystem):
  103. """Base class for multifilesystem tests using Git"""
  104. class HtpasswdAuthSystem(BaseTest):
  105. """Base class to test Radicale with Htpasswd authentication"""
  106. def setup(self):
  107. self.colpath = tempfile.mkdtemp()
  108. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  109. with open(htpasswd_file_path, "wb") as fd:
  110. fd.write(b"tmp:{SHA}" + base64.b64encode(
  111. hashlib.sha1(b"bepo").digest()))
  112. config.set("auth", "type", "htpasswd")
  113. self.userpass = base64.b64encode(b"tmp:bepo")
  114. self.application = radicale.Application()
  115. htpasswd.FILENAME = htpasswd_file_path
  116. htpasswd.ENCRYPTION = "sha1"
  117. def teardown(self):
  118. config.set("auth", "type", "None")
  119. radicale.auth.is_authenticated = lambda *_: True