33import os
44import shutil
55from pathlib import Path
6+ from urllib .error import HTTPError
7+ from unittest .mock import patch
68from ruamel .yaml import YAML
79
810import src .python .add_update_member as mod
11+ import src .python as core
912
1013
1114class TestAddUpdateMember (unittest .TestCase ):
@@ -73,5 +76,58 @@ def test_update_member(self):
7376
7477 self .assertEqual (output , expected , error_message )
7578
79+ def test_find_urls_handles_markdown_html_and_plain_text (self ):
80+ text = (
81+ ' '
82+ '<img src="https://bucket.example.com/photos/profile.jpg?download=1"> '
83+ 'https://plain.example.org/pic.webp'
84+ )
85+
86+ urls = core .find_urls (text )
87+
88+ self .assertEqual (
89+ urls ,
90+ [
91+ "https://example.com/image.png" ,
92+ "https://bucket.example.com/photos/profile.jpg?download=1" ,
93+ "https://plain.example.org/pic.webp" ,
94+ ],
95+ )
96+
97+ @patch ("src.python.Image.open" )
98+ @patch ("src.python.urlretrieve" )
99+ def test_save_url_image_skips_failed_downloads (self , mock_urlretrieve , mock_image_open ):
100+ class DummyImage :
101+ def thumbnail (self , * _ ):
102+ return None
103+
104+ def save (self , * _args , ** _kwargs ):
105+ return None
106+
107+ mock_image_open .return_value = DummyImage ()
108+ mock_urlretrieve .side_effect = [
109+ HTTPError ("https://example.com/missing.jpg" , 404 , "Not Found" , None , None ),
110+ None ,
111+ ]
112+
113+ profile = {
114+ "avatar" : (
115+ " "
116+ ""
117+ )
118+ }
119+
120+ saved_path = core .save_url_image (
121+ fname = "john-doe" ,
122+ profile = profile ,
123+ key = "avatar" ,
124+ image_dir = "tests/scratch/assets/images/bio/" ,
125+ crop_center = False ,
126+ size = (300 , 300 ),
127+ )
128+
129+ self .assertEqual (saved_path , "/tests/scratch/assets/images/bio/john-doe.png" )
130+ self .assertEqual (mock_urlretrieve .call_count , 2 )
131+
76132
77133
0 commit comments