Skip to content

Commit c5c9eeb

Browse files
Harden metadata prefix fallback for file path messages
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent a96563d commit c5c9eeb

File tree

6 files changed

+90
-5
lines changed

6 files changed

+90
-5
lines changed

hyperbrowser/client/managers/extension_create_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ def normalize_extension_create_input(params: Any) -> Tuple[str, Dict[str, Any]]:
3232
missing_file_message=build_file_path_error_message(
3333
raw_file_path,
3434
prefix=EXTENSION_OPERATION_METADATA.missing_file_message_prefix,
35+
default_prefix="Extension file not found at path",
3536
),
3637
not_file_message=build_file_path_error_message(
3738
raw_file_path,
3839
prefix=EXTENSION_OPERATION_METADATA.not_file_message_prefix,
40+
default_prefix="Extension file path must point to a file",
3941
),
4042
)
4143
return file_path, payload

hyperbrowser/client/managers/session_upload_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ def normalize_upload_file_input(
3333
missing_file_message=build_file_path_error_message(
3434
raw_file_path,
3535
prefix=SESSION_OPERATION_METADATA.upload_missing_file_message_prefix,
36+
default_prefix="Upload file not found at path",
3637
),
3738
not_file_message=build_file_path_error_message(
3839
raw_file_path,
3940
prefix=SESSION_OPERATION_METADATA.upload_not_file_message_prefix,
41+
default_prefix="Upload file path must point to a file",
4042
),
4143
)
4244
return file_path, None

tests/test_extension_create_metadata_usage.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from pathlib import Path
23

34
import pytest
@@ -14,5 +15,17 @@ def test_extension_create_helper_uses_shared_operation_metadata_prefixes():
1415
assert "extension_operation_metadata import" in module_text
1516
assert "EXTENSION_OPERATION_METADATA.missing_file_message_prefix" in module_text
1617
assert "EXTENSION_OPERATION_METADATA.not_file_message_prefix" in module_text
17-
assert 'prefix="Extension file not found at path"' not in module_text
18-
assert 'prefix="Extension file path must point to a file"' not in module_text
18+
assert (
19+
re.search(
20+
r'(?<!default_)prefix="Extension file not found at path"',
21+
module_text,
22+
)
23+
is None
24+
)
25+
assert (
26+
re.search(
27+
r'(?<!default_)prefix="Extension file path must point to a file"',
28+
module_text,
29+
)
30+
is None
31+
)

tests/test_extension_create_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,29 @@ def test_normalize_extension_create_input_uses_metadata_missing_prefix(
142142
assert exc_info.value.original_error is None
143143

144144

145+
def test_normalize_extension_create_input_uses_default_missing_prefix_when_metadata_invalid(
146+
tmp_path, monkeypatch: pytest.MonkeyPatch
147+
):
148+
missing_path = tmp_path / "missing-extension.zip"
149+
params = CreateExtensionParams(name="missing-extension", file_path=missing_path)
150+
monkeypatch.setattr(
151+
extension_create_utils,
152+
"EXTENSION_OPERATION_METADATA",
153+
SimpleNamespace(
154+
missing_file_message_prefix=123,
155+
not_file_message_prefix="Custom extension not-file prefix",
156+
),
157+
)
158+
159+
with pytest.raises(
160+
HyperbrowserError,
161+
match="Extension file not found at path:",
162+
) as exc_info:
163+
normalize_extension_create_input(params)
164+
165+
assert exc_info.value.original_error is None
166+
167+
145168
def test_normalize_extension_create_input_uses_metadata_not_file_prefix(
146169
tmp_path, monkeypatch: pytest.MonkeyPatch
147170
):

tests/test_session_upload_metadata_usage.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from pathlib import Path
23

34
import pytest
@@ -15,6 +16,24 @@ def test_session_upload_helper_uses_shared_operation_metadata_prefixes():
1516
assert "SESSION_OPERATION_METADATA.upload_missing_file_message_prefix" in module_text
1617
assert "SESSION_OPERATION_METADATA.upload_not_file_message_prefix" in module_text
1718
assert "SESSION_OPERATION_METADATA.upload_open_file_error_prefix" in module_text
18-
assert 'prefix="Upload file not found at path"' not in module_text
19-
assert 'prefix="Upload file path must point to a file"' not in module_text
20-
assert 'prefix="Failed to open upload file at path"' not in module_text
19+
assert (
20+
re.search(
21+
r'(?<!default_)prefix="Upload file not found at path"',
22+
module_text,
23+
)
24+
is None
25+
)
26+
assert (
27+
re.search(
28+
r'(?<!default_)prefix="Upload file path must point to a file"',
29+
module_text,
30+
)
31+
is None
32+
)
33+
assert (
34+
re.search(
35+
r'(?<!default_)prefix="Failed to open upload file at path"',
36+
module_text,
37+
)
38+
is None
39+
)

tests/test_session_upload_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ def test_normalize_upload_file_input_uses_metadata_missing_prefix(
123123
assert exc_info.value.original_error is None
124124

125125

126+
def test_normalize_upload_file_input_uses_default_missing_prefix_when_metadata_invalid(
127+
monkeypatch: pytest.MonkeyPatch,
128+
):
129+
monkeypatch.setattr(
130+
session_upload_utils,
131+
"SESSION_OPERATION_METADATA",
132+
type(
133+
"_Metadata",
134+
(),
135+
{
136+
"upload_missing_file_message_prefix": 123,
137+
"upload_not_file_message_prefix": "Custom not-file prefix",
138+
"upload_open_file_error_prefix": "Custom open prefix",
139+
},
140+
)(),
141+
)
142+
143+
with pytest.raises(
144+
HyperbrowserError,
145+
match="Upload file not found at path:",
146+
) as exc_info:
147+
normalize_upload_file_input("/tmp/nonexistent-upload-default-prefix-test.txt")
148+
149+
assert exc_info.value.original_error is None
150+
151+
126152
def test_normalize_upload_file_input_returns_open_file_like_object():
127153
file_obj = io.BytesIO(b"content")
128154

0 commit comments

Comments
 (0)