test_base.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 radicale
  20. import shutil
  21. import tempfile
  22. from . import BaseTest
  23. from .helpers import get_file_content
  24. class BaseRequests(object):
  25. """Tests with simple requests."""
  26. storage_type = None
  27. def setup(self):
  28. radicale.config.set("storage", "type", self.storage_type)
  29. def test_root(self):
  30. """GET request at "/"."""
  31. status, headers, answer = self.request("GET", "/")
  32. assert status == 200
  33. assert "Radicale works!" in answer
  34. # Test the creation of the collection
  35. self.request(
  36. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  37. status, headers, answer = self.request("GET", "/calendar.ics/")
  38. assert "BEGIN:VCALENDAR" in answer
  39. assert "END:VCALENDAR" in answer
  40. def test_add_event(self):
  41. """Add an event."""
  42. self.request(
  43. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  44. event = get_file_content("event.ics")
  45. path = "/calendar.ics/event.ics"
  46. status, headers, answer = self.request("PUT", path, event)
  47. assert status == 201
  48. status, headers, answer = self.request("GET", path)
  49. assert "ETag" in headers.keys()
  50. assert status == 200
  51. assert "VEVENT" in answer
  52. assert "Event" in answer
  53. assert "UID:event" in answer
  54. def test_add_todo(self):
  55. """Add a todo."""
  56. self.request(
  57. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  58. todo = get_file_content("todo.ics")
  59. path = "/calendar.ics/todo.ics"
  60. status, headers, answer = self.request("PUT", path, todo)
  61. assert status == 201
  62. status, headers, answer = self.request("GET", path)
  63. assert "ETag" in headers.keys()
  64. assert "VTODO" in answer
  65. assert "Todo" in answer
  66. assert "UID:todo" in answer
  67. def test_delete(self):
  68. """Delete an event."""
  69. self.request(
  70. "PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  71. event = get_file_content("event.ics")
  72. path = "/calendar.ics/event.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 TestMultiFileSystem(BaseRequests, BaseTest):
  81. """Base class for filesystem tests."""
  82. storage_type = "multifilesystem"
  83. def setup(self):
  84. """Setup function for each test."""
  85. self.colpath = tempfile.mkdtemp()
  86. from radicale import storage
  87. storage.FOLDER = self.colpath
  88. self.application = radicale.Application()
  89. def teardown(self):
  90. """Teardown function for each test."""
  91. shutil.rmtree(self.colpath)
  92. class TestCustomStorageSystem(BaseRequests, BaseTest):
  93. """Base class for custom backend tests."""
  94. storage_type = "custom"
  95. def setup(self):
  96. """Setup function for each test."""
  97. super().setup()
  98. self.colpath = tempfile.mkdtemp()
  99. radicale.config.set("storage", "type", "tests.custom.storage")
  100. from tests.custom import storage
  101. storage.FOLDER = self.colpath
  102. storage.GIT_REPOSITORY = None
  103. self.application = radicale.Application()
  104. def teardown(self):
  105. """Teardown function for each test."""
  106. shutil.rmtree(self.colpath)