-
Notifications
You must be signed in to change notification settings - Fork 968
Expand file tree
/
Copy pathtest_input_handler.py
More file actions
148 lines (117 loc) · 5.46 KB
/
Copy pathtest_input_handler.py
File metadata and controls
148 lines (117 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for skillspector input_handler (resolve directory, zip, single file)."""
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from skillspector.input_handler import ALLOWED_GIT_HOSTS, InputHandler
def test_resolve_directory(tmp_path: Path) -> None:
"""Resolving a local directory returns path and source_type directory."""
(tmp_path / "SKILL.md").write_text("# Skill", encoding="utf-8")
handler = InputHandler()
try:
resolved, source_type = handler.resolve(str(tmp_path))
assert resolved.is_dir()
assert (resolved / "SKILL.md").exists()
assert source_type == "directory"
finally:
handler.cleanup()
def test_resolve_single_md_file(tmp_path: Path) -> None:
"""Resolving a single .md file wraps it in a temp dir."""
f = tmp_path / "doc.md"
f.write_text("# Doc", encoding="utf-8")
handler = InputHandler()
try:
resolved, source_type = handler.resolve(str(f))
assert resolved.is_dir()
assert (resolved / "doc.md").exists()
assert source_type == "file"
finally:
handler.cleanup()
def test_resolve_zip_file(tmp_path: Path) -> None:
"""Resolving a .zip file extracts and returns the extract dir."""
import zipfile
(tmp_path / "SKILL.md").write_text("# Skill", encoding="utf-8")
zip_path = tmp_path / "skill.zip"
with zipfile.ZipFile(zip_path, "w") as zf:
zf.write(tmp_path / "SKILL.md", "SKILL.md")
handler = InputHandler()
try:
resolved, source_type = handler.resolve(str(zip_path))
assert resolved.is_dir()
assert source_type == "zip"
finally:
handler.cleanup()
def test_resolve_nonexistent_raises() -> None:
"""Resolving a nonexistent path raises FileNotFoundError or ValueError."""
handler = InputHandler()
with pytest.raises((FileNotFoundError, ValueError)):
handler.resolve("/nonexistent/path/xyz")
def test_resolve_single_non_md_file(tmp_path: Path) -> None:
"""Resolving a single non-.md file (e.g. .txt) wraps it in a temp dir."""
f = tmp_path / "readme.txt"
f.write_text("Read me", encoding="utf-8")
handler = InputHandler()
try:
resolved, source_type = handler.resolve(str(f))
assert resolved.is_dir()
assert (resolved / "readme.txt").exists()
assert source_type == "file"
finally:
handler.cleanup()
def test_cleanup_idempotent(tmp_path: Path) -> None:
"""cleanup() can be called after resolve and does not raise."""
(tmp_path / "a.md").write_text("x", encoding="utf-8")
handler = InputHandler()
handler.resolve(str(tmp_path / "a.md"))
handler.cleanup()
handler.cleanup()
def test_scp_url_is_git_url() -> None:
"""scp-style SSH URL is recognised as a Git URL."""
assert InputHandler()._is_git_url("git@github.com:org/repo.git") is True
def test_validate_url_host_scp_extracts_github() -> None:
"""_validate_url_host extracts 'github.com' from an scp-style URL."""
host = InputHandler()._validate_url_host("git@github.com:org/repo.git", ALLOWED_GIT_HOSTS)
assert host == "github.com"
def test_scp_valid_host_clones() -> None:
"""resolve() calls git clone with the scp URL when the host is allowed."""
handler = InputHandler()
try:
with patch(
"skillspector.input_handler.subprocess.run", return_value=MagicMock()
) as mock_run:
handler.resolve("git@github.com:org/repo.git")
assert mock_run.called
call_args = mock_run.call_args[0][0]
assert "git@github.com:org/repo.git" in call_args
finally:
handler.cleanup()
def test_scp_disallowed_host_raises() -> None:
"""_validate_url_host rejects an scp URL whose host is not in the allowlist."""
with pytest.raises(ValueError, match="not in the allowed hosts"):
InputHandler()._validate_url_host("git@malicious.internal:org/repo.git", ALLOWED_GIT_HOSTS)
def test_scp_private_ip_raises() -> None:
"""_validate_url_host rejects an scp URL whose extracted host is not in the allowlist."""
with pytest.raises(ValueError):
InputHandler()._validate_url_host("git@169.254.169.254:org/repo.git", ALLOWED_GIT_HOSTS)
def test_https_url_unchanged() -> None:
"""https URLs continue to extract the host via urlparse without hitting the scp fallback."""
host = InputHandler()._validate_url_host("https://github.com/org/repo.git", ALLOWED_GIT_HOSTS)
assert host == "github.com"
def test_scp_ssrf_gate_fires() -> None:
"""SSRF gate raises ValueError for an scp URL whose host resolves to a private IP."""
with patch("skillspector.input_handler._is_private_ip", return_value=True):
with pytest.raises(ValueError, match="private/internal IP"):
InputHandler()._validate_url_host("git@github.com:org/repo.git", ALLOWED_GIT_HOSTS)