33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
|
|
"""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()
|