您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_model.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import importlib
  2. import types
  3. import pytest
  4. from models.model import Message
  5. @pytest.fixture(autouse=True)
  6. def patch_file_helpers(monkeypatch: pytest.MonkeyPatch):
  7. """
  8. Patch file_helpers.get_signed_file_url to a deterministic stub.
  9. """
  10. model_module = importlib.import_module("models.model")
  11. dummy = types.SimpleNamespace(get_signed_file_url=lambda fid: f"https://signed.example/{fid}")
  12. # Inject/override file_helpers on models.model
  13. monkeypatch.setattr(model_module, "file_helpers", dummy, raising=False)
  14. def _wrap_md(url: str) -> str:
  15. """
  16. Wrap a raw URL into the markdown that re_sign_file_url_answer expects:
  17. [link](<url>)
  18. """
  19. return f"please click [file]({url}) to download."
  20. def test_file_preview_valid_replaced():
  21. """
  22. Valid file-preview URL must be re-signed:
  23. - Extract upload_file_id correctly
  24. - Replace the original URL with the signed URL
  25. """
  26. upload_id = "abc-123"
  27. url = f"/files/{upload_id}/file-preview?timestamp=111&nonce=222&sign=333"
  28. msg = Message(answer=_wrap_md(url))
  29. out = msg.re_sign_file_url_answer
  30. assert f"https://signed.example/{upload_id}" in out
  31. assert url not in out
  32. def test_file_preview_misspelled_not_replaced():
  33. """
  34. Misspelled endpoint 'file-previe?timestamp=' should NOT be rewritten.
  35. """
  36. upload_id = "zzz-001"
  37. # path deliberately misspelled: file-previe? (missing 'w')
  38. # and we append &note=file-preview to trick the old `"file-preview" in url` check.
  39. url = f"/files/{upload_id}/file-previe?timestamp=111&nonce=222&sign=333&note=file-preview"
  40. original = _wrap_md(url)
  41. msg = Message(answer=original)
  42. out = msg.re_sign_file_url_answer
  43. # Expect NO replacement, should not rewrite misspelled file-previe URL
  44. assert out == original
  45. def test_image_preview_valid_replaced():
  46. """
  47. Valid image-preview URL must be re-signed.
  48. """
  49. upload_id = "img-789"
  50. url = f"/files/{upload_id}/image-preview?timestamp=123&nonce=456&sign=789"
  51. msg = Message(answer=_wrap_md(url))
  52. out = msg.re_sign_file_url_answer
  53. assert f"https://signed.example/{upload_id}" in out
  54. assert url not in out
  55. def test_image_preview_misspelled_not_replaced():
  56. """
  57. Misspelled endpoint 'image-previe?timestamp=' should NOT be rewritten.
  58. """
  59. upload_id = "img-err-42"
  60. url = f"/files/{upload_id}/image-previe?timestamp=1&nonce=2&sign=3&note=image-preview"
  61. original = _wrap_md(url)
  62. msg = Message(answer=original)
  63. out = msg.re_sign_file_url_answer
  64. # Expect NO replacement, should not rewrite misspelled image-previe URL
  65. assert out == original