test_queuefeed.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. from pathlib import Path
  3. from typing import Optional
  4. import pytest
  5. from pytest import CaptureFixture
  6. from gemini_antenna.URLHelper import URLHelper
  7. from gemini_antenna.cgi import queuefeed
  8. from gemini_antenna.db import AntennaDB
  9. queuefeed_ask_input = "10 Feed URL:\r\n"
  10. queuefeed_success = """20 text/gemini\r
  11. Thank you for your submission!
  12. This feed will be processed in a few minutes.
  13. """
  14. queuefeed_invalid = """20 text/gemini\r
  15. Error adding feed.
  16. Feed URL is not valid.
  17. """
  18. queuefeed_blocked = """20 text/gemini\r
  19. Error adding feed.
  20. Feed URL is blocked by instance rules.
  21. """
  22. @pytest.mark.parametrize("query", [None, ""])
  23. def test_ask_input(tmp_path: Path, capfd: CaptureFixture[str],
  24. query: Optional[str]):
  25. if query is not None:
  26. os.environ['QUERY_STRING'] = query
  27. elif 'QUERY_STRING' in os.environ:
  28. del os.environ['QUERY_STRING']
  29. queuefeed.main()
  30. assert capfd.readouterr().out == queuefeed_ask_input
  31. @pytest.mark.blocklist_rules("gemini://example.com/~antenna", "http://")
  32. @pytest.mark.parametrize("query", [
  33. "https%3A%2F%2Fexample.com%2Ffeed.atom",
  34. "gemini.circumlunar.space/news/",
  35. "gemini://example.com/gemlog/about-http%3A%2F%2F-scheme",
  36. ])
  37. def test_success(tmp_path: Path, db: AntennaDB, capfd: CaptureFixture[str],
  38. query: str):
  39. os.environ['QUERY_STRING'] = query
  40. queuefeed.main()
  41. assert capfd.readouterr().out == queuefeed_success
  42. assert URLHelper.resolve(query) in db.getQueue()
  43. @pytest.mark.blocklist_rules("gemini://example.com/~antenna", "http://",)
  44. @pytest.mark.parametrize("query", [
  45. "/etc/passwd",
  46. "my cool feed",
  47. "42",
  48. ])
  49. def test_invalid(tmp_path: Path, db: AntennaDB, capfd: CaptureFixture[str],
  50. query: str):
  51. os.environ['QUERY_STRING'] = query
  52. queuefeed.main()
  53. assert capfd.readouterr().out == queuefeed_invalid
  54. assert not URLHelper.resolve(query) in db.getQueue()
  55. @pytest.mark.blocklist_rules("gemini://example.com/~antenna", "http")
  56. @pytest.mark.parametrize("query", [
  57. "http://example.org/feed",
  58. "https://example.org/feed",
  59. "example.com/~antenna",
  60. "gemini://example.com/./~antenna",
  61. "gemini://example.com/~user/../~antenna/feed.atom",
  62. ])
  63. def test_blocked(tmp_path: Path, db: AntennaDB, capfd: CaptureFixture[str],
  64. query: str):
  65. os.environ['QUERY_STRING'] = query
  66. queuefeed.main()
  67. assert capfd.readouterr().out == queuefeed_blocked
  68. assert not URLHelper.resolve(query) in db.getQueue()