Files
life-echo/api/tests/test_cos_url_keys.py
2026-03-20 15:15:35 +08:00

33 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""cos_url_keys仅当 host 匹配配置时才解析 key。"""
import unittest
from unittest.mock import patch
from app.core.cos_url_keys import extract_cos_object_key_if_owned
class TestExtractCosObjectKeyIfOwned(unittest.TestCase):
def test_non_http_returns_none(self):
self.assertIsNone(extract_cos_object_key_if_owned("audio-segment:x:0"))
self.assertIsNone(extract_cos_object_key_if_owned(None))
@patch("app.core.cos_url_keys.settings")
def test_matching_host_returns_key(self, mock_settings):
mock_settings.tencent_cos_bucket = "mybucket"
mock_settings.tencent_cos_region = "ap-shanghai"
mock_settings.tencent_cos_base_url = ""
url = "https://mybucket.cos.ap-shanghai.myqcloud.com/chapters/u1/c1/a.png"
self.assertEqual(extract_cos_object_key_if_owned(url), "chapters/u1/c1/a.png")
@patch("app.core.cos_url_keys.settings")
def test_foreign_host_returns_none(self, mock_settings):
mock_settings.tencent_cos_bucket = "mybucket"
mock_settings.tencent_cos_region = "ap-shanghai"
mock_settings.tencent_cos_base_url = ""
url = "https://evil.com/chapters/u1/c1/a.png"
self.assertIsNone(extract_cos_object_key_if_owned(url))
if __name__ == "__main__":
unittest.main()