test_base.py 5.7 KB

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