-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_list.py
More file actions
286 lines (243 loc) · 11.8 KB
/
test_list.py
File metadata and controls
286 lines (243 loc) · 11.8 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Tests for list plugin"""
import re
import pytest
from cmem_plugin_base.testing import TestExecutionContext, TestPluginContext
from paramiko import AuthenticationException
from cmem_plugin_ssh.autocompletion import DirectoryParameterType
from cmem_plugin_ssh.list import ListFiles
from cmem_plugin_ssh.retrieval import SSHRetrieval
from tests.conftest import TestingEnvironment
def test_private_key_with_wrong_password(testing_environment: TestingEnvironment) -> None:
"""Test encrypted private key with wrong password failure"""
with pytest.raises(AuthenticationException, match="Authentication failed"):
ListFiles(
hostname=testing_environment.hostname,
port=testing_environment.port,
username=testing_environment.username,
private_key=testing_environment.private_key_with_password,
password="wrong_password", # noqa: S106
authentication_method=testing_environment.authentication_method,
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
).execute(inputs=[], context=TestExecutionContext())
def test_private_key_with_password_execution(testing_environment: TestingEnvironment) -> None:
"""Test autocompletion and base execution with a password encrypted private key"""
plugin = ListFiles(
hostname=testing_environment.hostname,
port=testing_environment.port,
username=testing_environment.username,
private_key=testing_environment.private_key_with_password,
password=testing_environment.password,
authentication_method=testing_environment.authentication_method,
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
)
autocompletion = DirectoryParameterType(url_expand="", display_name="")
depends_on = [
plugin.hostname,
plugin.port,
plugin.username,
plugin.private_key,
plugin.password,
plugin.authentication_method,
plugin.path,
]
autocompletion_result = autocompletion.autocomplete(
query_terms=["volume"],
depend_on_parameter_values=depends_on,
context=TestPluginContext(),
)
assert len(autocompletion_result) > 0
execution_result = plugin.execute(context=TestExecutionContext(), inputs=[])
assert len(list(execution_result.entities)) == testing_environment.no_of_files
def test_plugin_wrong_hostname(testing_environment: TestingEnvironment) -> None:
"""Test plugin execution with an incorrect port"""
with pytest.raises(TimeoutError, match="timed out"):
ListFiles(
hostname="123.45.6.78",
port=testing_environment.port,
username=testing_environment.username,
private_key=testing_environment.private_key,
password=testing_environment.password,
authentication_method=testing_environment.authentication_method,
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
).execute(inputs=[], context=TestExecutionContext())
def test_plugin_wrong_username(testing_environment: TestingEnvironment) -> None:
"""Test plugin execution with an incorrect port"""
with pytest.raises(AuthenticationException, match="Authentication failed"):
ListFiles(
hostname=testing_environment.hostname,
port=testing_environment.port,
username="wrong_username",
private_key=testing_environment.private_key,
password=testing_environment.password,
authentication_method=testing_environment.authentication_method,
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
).execute(inputs=[], context=TestExecutionContext())
def test_plugin_wrong_port(testing_environment: TestingEnvironment) -> None:
"""Test plugin execution with an incorrect port"""
with pytest.raises(OSError, match=re.escape(r"[Errno")):
ListFiles(
hostname=testing_environment.hostname,
port=1234,
username=testing_environment.username,
private_key=testing_environment.private_key,
password=testing_environment.password,
authentication_method=testing_environment.authentication_method,
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
).execute(inputs=[], context=TestExecutionContext())
def test_plugin_wrong_private_key(testing_environment: TestingEnvironment) -> None:
"""Test plugin execution with an incorrect private key"""
with pytest.raises(ValueError, match="Unsupported private key format"):
ListFiles(
hostname=testing_environment.hostname,
port=testing_environment.port,
username=testing_environment.username,
private_key="invalidkeymaterial",
password="",
authentication_method="key",
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
).execute(inputs=[], context=TestExecutionContext())
def test_plugin_wrong_password(testing_environment: TestingEnvironment) -> None:
"""Test plugin execution with an incorrect private key"""
with pytest.raises(AuthenticationException, match="Authentication failed"):
ListFiles(
hostname=testing_environment.hostname,
port=testing_environment.port,
username=testing_environment.username,
private_key=testing_environment.private_key,
password="1234", # noqa: S106
authentication_method="password",
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
).execute(inputs=[], context=TestExecutionContext())
def test_plugin_password_authentication_only(testing_environment: TestingEnvironment) -> None:
"""Test plugin execution using only password (no private key)"""
plugin = ListFiles(
hostname=testing_environment.hostname,
port=testing_environment.port,
username=testing_environment.username,
password=testing_environment.password,
private_key="",
authentication_method="password",
path=testing_environment.path,
regex=testing_environment.regex,
no_subfolder=testing_environment.no_subfolder,
error_handling=testing_environment.error_handling,
)
result_execution = plugin.execute(inputs=[], context=TestExecutionContext())
assert len(list(result_execution.entities)) == testing_environment.no_of_files
def test_plugin_private_key_base_execution(testing_environment: TestingEnvironment) -> None:
"""Test basic plugin execution"""
plugin = testing_environment.list_plugin
result_execution = plugin.execute(inputs=[], context=TestExecutionContext())
assert len(list(result_execution.entities)) == testing_environment.no_of_files
def test_plugin_execution_no_subfolder(testing_environment: TestingEnvironment) -> None:
"""Test plugin execution with no subfolder flag set"""
plugin = testing_environment.list_plugin
plugin.no_subfolder = True
result_execution = plugin.execute(inputs=[], context=TestExecutionContext())
assert len(list(result_execution.entities)) == 1
def test_plugin_no_matching_files(testing_environment: TestingEnvironment) -> None:
"""Test plugin when regex matches no files"""
plugin = testing_environment.list_plugin
plugin.regex = "nonmatchingpattern.*"
result_execution = plugin.execute(inputs=[], context=TestExecutionContext())
assert len(list(result_execution.entities)) == 0
def test_plugin_nonexistent_directory(testing_environment: TestingEnvironment) -> None:
"""Test plugin with a non-existing remote path"""
plugin = testing_environment.list_plugin
plugin.path = "non/existent/path"
with pytest.raises(ValueError, match=r"\[Errno 2\] No such file"):
plugin.execute(inputs=[], context=TestExecutionContext())
def test_preview_action(testing_environment: TestingEnvironment) -> None:
"""Test preview action"""
plugin = testing_environment.list_plugin
preview = plugin.preview_results()
assert "RootFile.txt" in preview
def test_execution_denied_permission(testing_environment: TestingEnvironment) -> None:
"""Test execution with a not permitted file and error_handling mode error"""
plugin = testing_environment.list_plugin
plugin.path = "/etc"
plugin.no_subfolder = True
with pytest.raises(ValueError, match=r"No access to '"):
plugin.execute(inputs=[], context=TestExecutionContext())
def test_execution_warning_error_handling(testing_environment: TestingEnvironment) -> None:
"""Test correct execution with warning as error_handling method."""
plugin = testing_environment.list_plugin
plugin.error_handling = "warning"
plugin.path = "/etc"
plugin._initialize_ssh_and_sftp_connections() # noqa: SLF001
retrieval = SSHRetrieval(
ssh_client=plugin.ssh_client,
no_subfolder=plugin.no_subfolder,
regex=plugin.regex,
)
all_files = retrieval.list_files_parallel(
files=[],
context=TestExecutionContext(),
path=plugin.path,
workers=plugin.max_workers,
error_handling=plugin.error_handling,
no_access_files=[],
)
correct_files = all_files[0]
faulty_files = all_files[1]
assert len(correct_files) > len(faulty_files)
faulty_filenames = [file.filename for file in faulty_files]
assert "/etc/sudoers" in faulty_filenames
execution_results = plugin.execute(inputs=[], context=TestExecutionContext())
assert len(list(execution_results.entities)) > 0
def test_execution_ignore_error_handling(testing_environment: TestingEnvironment) -> None:
"""Test correct listing when ignore is the error_handling method"""
plugin = testing_environment.list_plugin
plugin.error_handling = "ignore"
plugin.path = "/etc"
plugin._initialize_ssh_and_sftp_connections() # noqa: SLF001
retrieval = SSHRetrieval(
ssh_client=plugin.ssh_client,
no_subfolder=plugin.no_subfolder,
regex=plugin.regex,
)
all_files = retrieval.list_files_parallel(
files=[],
context=TestExecutionContext(),
path=plugin.path,
workers=plugin.max_workers,
error_handling=plugin.error_handling,
no_access_files=[],
)
correct_files = all_files[0]
faulty_files = all_files[1]
assert len(faulty_files) == 0
correct_filenames = [file.filename for file in correct_files]
assert "/etc/sudoers" in correct_filenames
execution_results = plugin.execute(inputs=[], context=TestExecutionContext())
assert len(list(execution_results.entities)) > 0
def test_preview_with_warning_error_handling(testing_environment: TestingEnvironment) -> None:
"""Test result preview with warnings also"""
plugin = testing_environment.list_plugin
plugin.error_handling = "warning"
plugin.regex = "^.txt$"
plugin.path = "/etc"
preview = plugin.preview_results()
assert "entities were found that the current user has no access to" in preview
assert "restricted.txt" in preview