test_base.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. Radicale tests with simple requests.
  20. """
  21. import radicale
  22. import shutil
  23. import tempfile
  24. from dulwich.repo import Repo
  25. from sqlalchemy import create_engine
  26. from sqlalchemy.orm import sessionmaker
  27. from . import BaseTest
  28. from .helpers import get_file_content
  29. class BaseRequests(object):
  30. """Tests with simple requests."""
  31. storage_type = None
  32. def setup(self):
  33. radicale.config.set("storage", "type", self.storage_type)
  34. def test_root(self):
  35. """GET request at "/"."""
  36. status, headers, answer = self.request("GET", "/")
  37. assert status == 200
  38. assert "Radicale works!" in answer
  39. # Test the creation of the collection
  40. status, headers, answer = self.request("GET", "/calendar.ics/")
  41. assert "BEGIN:VCALENDAR" in answer
  42. #assert "VERSION:2.0" in answer
  43. assert "END:VCALENDAR" in answer
  44. #assert "PRODID:-//Radicale//NONSGML Radicale Server//EN" in answer
  45. def test_add_event(self):
  46. """Add an event."""
  47. self.request("GET", "/calendar.ics/")
  48. event = get_file_content("event.ics")
  49. path = "/calendar.ics/event.ics"
  50. status, headers, answer = self.request("PUT", path, event)
  51. assert status == 201
  52. status, headers, answer = self.request("GET", path)
  53. assert "ETag" in headers.keys()
  54. assert status == 200
  55. assert "VEVENT" in answer
  56. assert "Event" in answer
  57. assert "UID:event" in answer
  58. def test_add_todo(self):
  59. """Add a todo."""
  60. self.request("GET", "/calendar.ics/")
  61. todo = get_file_content("todo.ics")
  62. path = "/calendar.ics/todo.ics"
  63. status, headers, answer = self.request("PUT", path, todo)
  64. assert status == 201
  65. status, headers, answer = self.request("GET", path)
  66. assert "ETag" in headers.keys()
  67. assert "VTODO" in answer
  68. assert "Todo" in answer
  69. assert "UID:todo" in answer
  70. def test_delete(self):
  71. """Delete an event."""
  72. self.request("GET", "/calendar.ics/")
  73. event = get_file_content("event.ics")
  74. path = "/calendar.ics/event.ics"
  75. status, headers, answer = self.request("PUT", path, event)
  76. # Then we send a DELETE request
  77. status, headers, answer = self.request("DELETE", path)
  78. assert status == 200
  79. assert "href>%s</" % path in answer
  80. status, headers, answer = self.request("GET", "/calendar.ics/")
  81. assert "VEVENT" not in answer
  82. class TestFileSystem(BaseRequests, BaseTest):
  83. """Base class for filesystem tests."""
  84. storage_type = "filesystem"
  85. def setup(self):
  86. """Setup function for each test."""
  87. self.colpath = tempfile.mkdtemp()
  88. from radicale.storage import filesystem
  89. filesystem.FOLDER = self.colpath
  90. filesystem.GIT_REPOSITORY = None
  91. self.application = radicale.Application()
  92. def teardown(self):
  93. """Teardown function for each test."""
  94. shutil.rmtree(self.colpath)
  95. class TestMultiFileSystem(TestFileSystem):
  96. """Base class for multifilesystem tests."""
  97. storage_type = "multifilesystem"
  98. class TestDataBaseSystem(BaseRequests, BaseTest):
  99. """Base class for database tests"""
  100. storage_type = "database"
  101. def setup(self):
  102. super(TestDataBaseSystem, self).setup()
  103. radicale.config.set("storage", "database_url", "sqlite://")
  104. from radicale.storage import database
  105. database.Session = sessionmaker()
  106. database.Session.configure(bind=create_engine("sqlite://"))
  107. session = database.Session()
  108. for st in get_file_content("schema.sql").split(";"):
  109. session.execute(st)
  110. session.commit()
  111. self.application = radicale.Application()
  112. class TestGitFileSystem(TestFileSystem):
  113. """Base class for filesystem tests using Git"""
  114. def setup(self):
  115. super(TestGitFileSystem, self).setup()
  116. Repo.init(self.colpath)
  117. from radicale.storage import filesystem
  118. filesystem.GIT_REPOSITORY = Repo(self.colpath)
  119. class TestGitMultiFileSystem(TestGitFileSystem, TestMultiFileSystem):
  120. """Base class for multifilesystem tests using Git"""
  121. class TestCustomStorageSystem(BaseRequests, BaseTest):
  122. """Base class for custom backend tests."""
  123. storage_type = "custom"
  124. def setup(self):
  125. """Setup function for each test."""
  126. super(TestCustomStorageSystem, self).setup()
  127. self.colpath = tempfile.mkdtemp()
  128. radicale.config.set(
  129. "storage", "custom_handler", "tests.custom.storage")
  130. from tests.custom import storage
  131. storage.FOLDER = self.colpath
  132. storage.GIT_REPOSITORY = None
  133. self.application = radicale.Application()
  134. def teardown(self):
  135. """Teardown function for each test."""
  136. shutil.rmtree(self.colpath)