conftest.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import dataclasses
  2. import os
  3. import socket
  4. import subprocess
  5. import sys
  6. import time
  7. import httpx
  8. import pytest
  9. def pytest_collection_modifyitems(config, items):
  10. """Skip tests marked 'live' unless ROOM_CAL_LIVE_TESTS=1.
  11. Live tests hit real service APIs and need credentials that dev systems
  12. only sometimes have (especially for 2FA-gated backends).
  13. """
  14. if os.environ.get('ROOM_CAL_LIVE_TESTS'):
  15. return
  16. skip_live = pytest.mark.skip(reason='live service-api test; set ROOM_CAL_LIVE_TESTS=1 to run')
  17. for item in items:
  18. if 'live' in item.keywords:
  19. item.add_marker(skip_live)
  20. def _free_port() -> int:
  21. with socket.socket() as s:
  22. s.bind(('127.0.0.1', 0))
  23. return s.getsockname()[1]
  24. @pytest.fixture(scope='session')
  25. def radicale_server(tmp_path_factory):
  26. """Start a local Radicale CalDAV server for the test session.
  27. auth.type = none accepts any credentials; the username in the HTTP auth
  28. still determines the path prefix (http://host/{username}/), so we always
  29. use username='test'. The 'House' calendar is created on first use; any
  30. test that writes to it cleans up in its own finally block.
  31. """
  32. port = _free_port()
  33. data_dir = tmp_path_factory.mktemp('radicale')
  34. config_ini = data_dir / 'config.ini'
  35. config_ini.write_text('\n'.join([
  36. '[server]',
  37. f'hosts = 127.0.0.1:{port}',
  38. '[auth]',
  39. 'type = none',
  40. '[storage]',
  41. f'filesystem_folder = {data_dir / "collections"}',
  42. ]))
  43. proc = subprocess.Popen(
  44. [sys.executable, '-m', 'radicale', '--config', str(config_ini)],
  45. stdout=subprocess.DEVNULL,
  46. stderr=subprocess.DEVNULL,
  47. )
  48. url = f'http://127.0.0.1:{port}/'
  49. for _ in range(50):
  50. try:
  51. httpx.get(url, timeout=1.0)
  52. break
  53. except Exception:
  54. time.sleep(0.1)
  55. else:
  56. proc.kill()
  57. pytest.fail('Radicale did not start within 5 seconds')
  58. import caldav
  59. client = caldav.DAVClient(url=url, username='test', password='test')
  60. principal = client.principal()
  61. principal.make_calendar(name='House', cal_id='house')
  62. yield {'url': url, 'username': 'test', 'password': 'test', 'calendar_name': 'House'}
  63. proc.terminate()
  64. try:
  65. proc.wait(timeout=5)
  66. except subprocess.TimeoutExpired:
  67. proc.kill()
  68. @pytest.fixture
  69. def require_user_cal(radicale_server, monkeypatch):
  70. """Redirect user_cal to the local Radicale server instead of skipping.
  71. Patches get_config in the two modules that call it on the user_cal path:
  72. - room_cal.user_cal (UserCal.__init__)
  73. - room_cal.actions.fetch_user_cal (calendar_name lookup)
  74. """
  75. from room_cal._utils import get_config
  76. cfg = get_config()
  77. test_user_cal_cfg = dataclasses.replace(
  78. cfg.user_cal,
  79. url=radicale_server['url'],
  80. username=radicale_server['username'],
  81. password=radicale_server['password'],
  82. calendar_name=radicale_server['calendar_name'],
  83. )
  84. test_cfg = dataclasses.replace(cfg, user_cal=test_user_cal_cfg)
  85. mock = lambda: test_cfg
  86. monkeypatch.setattr('room_cal.user_cal.get_config', mock)
  87. monkeypatch.setattr('room_cal.actions.fetch_user_cal.get_config', mock)
  88. # reset the memoized user_cal Calendar so it's rebuilt from the patched
  89. # config (and restored after the test)
  90. monkeypatch.setattr('room_cal.user_cal._user_calendar', None)