test_base.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2016 Guillaume Ayoub
  3. #
  4. # This library is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Radicale tests with simple requests.
  18. """
  19. import logging
  20. import shutil
  21. import tempfile
  22. from radicale import Application, config
  23. from . import BaseTest
  24. from .helpers import get_file_content
  25. class BaseRequests:
  26. """Tests with simple requests."""
  27. storage_type = None
  28. def setup(self):
  29. self.configuration = config.load()
  30. self.configuration.set("storage", "type", self.storage_type)
  31. self.logger = logging.getLogger("radicale_test")
  32. def test_root(self):
  33. """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. self.request(
  39. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  40. status, headers, answer = self.request("GET", "/calendar.ics/")
  41. assert "BEGIN:VCALENDAR" in answer
  42. assert "END:VCALENDAR" in answer
  43. def test_add_event(self):
  44. """Add an event."""
  45. self.request(
  46. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  47. event = get_file_content("event.ics")
  48. path = "/calendar.ics/event.ics"
  49. status, headers, answer = self.request("PUT", path, event)
  50. assert status == 201
  51. status, headers, answer = self.request("GET", path)
  52. assert "ETag" in headers.keys()
  53. assert status == 200
  54. assert "VEVENT" in answer
  55. assert "Event" in answer
  56. assert "UID:event" in answer
  57. def test_add_todo(self):
  58. """Add a todo."""
  59. self.request(
  60. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  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(
  73. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  74. event = get_file_content("event.ics")
  75. path = "/calendar.ics/event.ics"
  76. status, headers, answer = self.request("PUT", path, event)
  77. # Then we send a DELETE request
  78. status, headers, answer = self.request("DELETE", path)
  79. assert status == 200
  80. assert "href>%s</" % path in answer
  81. status, headers, answer = self.request("GET", "/calendar.ics/")
  82. assert "VEVENT" not in answer
  83. class TestMultiFileSystem(BaseRequests, BaseTest):
  84. """Base class for filesystem tests."""
  85. storage_type = "multifilesystem"
  86. def setup(self):
  87. super().setup()
  88. self.colpath = tempfile.mkdtemp()
  89. self.configuration.set("storage", "filesystem_folder", self.colpath)
  90. self.application = Application(self.configuration, self.logger)
  91. def teardown(self):
  92. shutil.rmtree(self.colpath)
  93. class TestCustomStorageSystem(BaseRequests, BaseTest):
  94. """Base class for custom backend tests."""
  95. storage_type = "tests.custom.storage"
  96. def setup(self):
  97. super().setup()
  98. self.colpath = tempfile.mkdtemp()
  99. self.configuration.set("storage", "filesystem_folder", self.colpath)
  100. self.configuration.set("storage", "test_folder", self.colpath)
  101. self.application = Application(self.configuration, self.logger)
  102. def teardown(self):
  103. shutil.rmtree(self.colpath)