Skip to content

Commit b71dc13

Browse files
authored
added ruff SIM check (#76)
Signed-off-by: Alexander Piskun <[email protected]>
1 parent 3eab4ec commit b71dc13

File tree

11 files changed

+46
-83
lines changed

11 files changed

+46
-83
lines changed

nc_py_api/_session.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ def _ocs(self, method: str, path_params: str, headers: dict, data: Optional[byte
204204
response_data = loads(response.text)
205205
ocs_meta = response_data["ocs"]["meta"]
206206
if ocs_meta["status"] != "ok":
207-
if not nested_req:
208-
if ocs_meta["statuscode"] == 403:
209-
if str(ocs_meta["message"]).lower().find("password confirmation is required") != -1:
210-
self.adapter.close()
211-
self.init_adapter(restart=True)
212-
return self._ocs(method, path_params, headers, data, **kwargs, nested_req=True)
207+
if (
208+
not nested_req
209+
and ocs_meta["statuscode"] == 403
210+
and str(ocs_meta["message"]).lower().find("password confirmation is required") != -1
211+
):
212+
self.adapter.close()
213+
self.init_adapter(restart=True)
214+
return self._ocs(method, path_params, headers, data, **kwargs, nested_req=True)
213215
if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND):
214216
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info)
215217
raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ preview = true
104104
[tool.ruff]
105105
line-length = 120
106106
target-version = "py39"
107-
select = ["A", "B", "C", "D", "E", "F", "UP", "Q", "W"]
107+
select = ["A", "B", "C", "D", "E", "F", "G", "I", "UP", "SIM", "Q", "W"]
108108
extend-ignore = ["D107", "D105", "D203", "D213", "D401"]
109109

110110
[tool.ruff.per-file-ignores]

tests/_install_wait.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ def check_heartbeat(url: str, regexp: str, n_tries: int, wait_interval: float) -
99
for _ in range(n_tries):
1010
try:
1111
result = get(url)
12-
if result.text:
13-
if re.search(regexp, result.text, re.IGNORECASE) is not None:
14-
return 0
12+
if result.text and re.search(regexp, result.text, re.IGNORECASE) is not None:
13+
return 0
1514
except Exception as _:
1615
_ = _
1716
sleep(wait_interval)

tests/files_sharing_test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import datetime
23

34
import pytest
@@ -235,10 +236,8 @@ def test_get_inherited(nc):
235236
@pytest.mark.parametrize("nc", NC_TO_TEST)
236237
@pytest.mark.skipif(NC is None, reason="Usual Nextcloud mode required for the test")
237238
def test_share_with(nc):
238-
try:
239+
with contextlib.suppress(NextcloudException):
239240
NC.users.create(TEST_USER_NAME, password=TEST_USER_PASSWORD)
240-
except NextcloudException:
241-
pass
242241
nc_second_user = Nextcloud(nc_auth_user=TEST_USER_NAME, nc_auth_pass=TEST_USER_PASSWORD)
243242
assert not nc_second_user.files.sharing.get_list()
244243
nc.files.makedirs("test_folder1/test_subfolder", exist_ok=True)
@@ -260,7 +259,7 @@ def test_share_with(nc):
260259
finally:
261260
nc.files.delete("share_test.txt", not_fail=True)
262261
nc.files.delete("test_folder1", not_fail=True)
263-
# NC.users.delete(TEST_USER_NAME)
262+
NC.users.delete(TEST_USER_NAME)
264263

265264

266265
@pytest.mark.parametrize("nc", NC_TO_TEST)

tests/files_test.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ def test_file_download(nc):
113113
@pytest.mark.parametrize("chunk_size", (15, 32, 64, None))
114114
def test_file_download2stream(nc, data_type, chunk_size):
115115
srv_admin_manual_buf = MyBytesIO()
116-
if data_type == "str":
117-
content = "".join(choice(ascii_lowercase) for _ in range(64))
118-
else:
119-
content = randbytes(64)
116+
content = "".join(choice(ascii_lowercase) for _ in range(64)) if data_type == "str" else randbytes(64)
120117
nc.files.upload("test_file.txt", content=content)
121118
if chunk_size is not None:
122119
nc.files.download2stream("/test_file.txt", srv_admin_manual_buf, chunk_size=chunk_size)
@@ -384,10 +381,7 @@ def test_move_copy_dir(nc, op_type):
384381
files = ("file1.txt", "file2.txt", "file3.txt")
385382
for n in files:
386383
nc.files.upload(f"{dir_name}/{n}", content=n)
387-
if op_type == "move":
388-
result = nc.files.move(dir_name, dest_dir_name)
389-
else:
390-
result = nc.files.copy(dir_name, dest_dir_name)
384+
result = nc.files.move(dir_name, dest_dir_name) if op_type == "move" else nc.files.copy(dir_name, dest_dir_name)
391385
assert result.file_id
392386
assert result.is_dir
393387
assert nc.files.by_path(result).is_dir
@@ -509,10 +503,7 @@ def test_fs_node_fields(nc):
509503

510504
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
511505
def test_makedirs(nc):
512-
try:
513-
nc.files.delete("abc")
514-
except NextcloudException:
515-
pass
506+
nc.files.delete("abc", not_fail=True)
516507
result = nc.files.makedirs("abc/def")
517508
assert result.is_dir
518509
with pytest.raises(NextcloudException) as exc_info:

tests/gfixture.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
environ["APP_VERSION"] = "1.0.0"
1111
environ["APP_SECRET"] = "12345"
1212

13-
if environ.get("SKIP_NC_CLIENT_TESTS", False):
14-
NC = None
15-
else:
16-
NC = Nextcloud()
13+
14+
NC = None if environ.get("SKIP_NC_CLIENT_TESTS", False) else Nextcloud()
1715

1816
if environ.get("SKIP_AE_TESTS", False):
1917
NC_APP = None

tests/logs_test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def test_log_without_app_ecosystem_v2():
5757
log_lvl = srv_capabilities["app_ecosystem_v2"].pop("loglevel")
5858
srv_capabilities.pop("app_ecosystem_v2")
5959
patched_capabilities = {"capabilities": srv_capabilities, "version": srv_version}
60-
with mock.patch.dict("gfixture.NC_APP._session._capabilities", patched_capabilities, clear=True):
61-
with mock.patch("gfixture.NC_APP._session._ocs") as _ocs:
62-
NC_APP.log(log_lvl, "will not be sent")
63-
_ocs.assert_not_called()
60+
with (
61+
mock.patch.dict("gfixture.NC_APP._session._capabilities", patched_capabilities, clear=True),
62+
mock.patch("gfixture.NC_APP._session._ocs") as _ocs,
63+
):
64+
NC_APP.log(log_lvl, "will not be sent")
65+
_ocs.assert_not_called()

tests/options_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,5 @@ def test_timeouts():
4343
@pytest.mark.skipif(not NC_TO_TEST, reason="Need Nextcloud or NextcloudApp.")
4444
def test_xdebug_session():
4545
nc_py_api.options.XDEBUG_SESSION = "12345"
46-
if isinstance(NC_TO_TEST, nc_py_api.Nextcloud):
47-
new_nc = nc_py_api.Nextcloud()
48-
else:
49-
new_nc = nc_py_api.NextcloudApp()
46+
new_nc = nc_py_api.Nextcloud() if isinstance(NC_TO_TEST[0], nc_py_api.Nextcloud) else nc_py_api.NextcloudApp()
5047
assert new_nc._session.adapter.cookies["XDEBUG_SESSION"] == "12345"

tests/users_groups_test.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import contextlib
2+
13
import pytest
24
from gfixture import NC_APP, NC_TO_TEST
35

@@ -13,10 +15,8 @@
1315
@pytest.mark.parametrize("params", ((TEST_GROUP_NAME,), (TEST_GROUP_NAME, "display name")))
1416
def test_create_delete_group(nc, params):
1517
test_group_name = params[0]
16-
try:
18+
with contextlib.suppress(NextcloudException):
1719
nc.users.groups.delete(test_group_name)
18-
except NextcloudException:
19-
pass
2020
nc.users.groups.create(*params)
2121
with pytest.raises(NextcloudException):
2222
nc.users.groups.create(*params)
@@ -29,10 +29,8 @@ def test_create_delete_group(nc, params):
2929
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
3030
def test_group_get_list(nc):
3131
for i in (TEST_GROUP_NAME, TEST_GROUP_NAME2):
32-
try:
32+
with contextlib.suppress(NextcloudException):
3333
nc.users.groups.create(i)
34-
except NextcloudException:
35-
pass
3634
groups = nc.users.groups.get_list()
3735
assert isinstance(groups, list)
3836
assert len(groups) >= 2
@@ -58,14 +56,10 @@ def test_get_non_existing_group(nc):
5856
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
5957
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
6058
def test_group_get_details(nc):
61-
try:
59+
with contextlib.suppress(NextcloudException):
6260
nc.users.groups.delete(TEST_GROUP_NAME)
63-
except NextcloudException:
64-
pass
65-
try:
61+
with contextlib.suppress(NextcloudException):
6662
nc.users.groups.create(TEST_GROUP_NAME)
67-
except NextcloudException:
68-
pass
6963
groups = nc.users.groups.get_details(mask=TEST_GROUP_NAME)
7064
assert len(groups) == 1
7165
group = groups[0]
@@ -81,10 +75,8 @@ def test_group_get_details(nc):
8175
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
8276
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
8377
def test_group_edit(nc):
84-
try:
78+
with contextlib.suppress(NextcloudException):
8579
nc.users.groups.create(TEST_GROUP_NAME)
86-
except NextcloudException:
87-
pass
8880
nc.users.groups.edit(TEST_GROUP_NAME, display_name="earth people")
8981
assert nc.users.groups.get_details(mask=TEST_GROUP_NAME)[0].display_name == "earth people"
9082
nc.users.groups.delete(TEST_GROUP_NAME)
@@ -100,21 +92,17 @@ def test_group_edit(nc):
10092
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
10193
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
10294
def test_group_members_promote_demote(nc):
103-
try:
95+
with contextlib.suppress(NextcloudException):
10496
nc.users.groups.create(TEST_GROUP_NAME)
105-
except NextcloudException:
106-
pass
10797
group_members = nc.users.groups.get_members(TEST_GROUP_NAME)
10898
assert not group_members
10999
assert isinstance(group_members, list)
110100
group_subadmins = nc.users.groups.get_subadmins(TEST_GROUP_NAME)
111101
assert isinstance(group_subadmins, list)
112102
assert not group_subadmins
113103
try:
114-
try:
104+
with contextlib.suppress(NextcloudException):
115105
nc.users.create("test_group_user", password="test_group_user")
116-
except NextcloudException:
117-
pass
118106
nc.users.add_to_group("test_group_user", TEST_GROUP_NAME)
119107
group_members = nc.users.groups.get_members(TEST_GROUP_NAME)
120108
assert group_members
@@ -133,10 +121,8 @@ def test_group_members_promote_demote(nc):
133121
assert not group_members
134122
finally:
135123
nc.users.groups.delete(TEST_GROUP_NAME)
136-
try:
124+
with contextlib.suppress(NextcloudException):
137125
nc.users.delete("test_group_user")
138-
except NextcloudException:
139-
pass
140126

141127

142128
@pytest.mark.skipif(NC_APP is None, reason="Test only for NextcloudApp.")

tests/users_test.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import contextlib
2+
13
import pytest
24
from gfixture import NC_TO_TEST
35

@@ -38,10 +40,8 @@ def test_get_user_404(nc):
3840
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
3941
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
4042
def test_create_user(nc):
41-
try:
43+
with contextlib.suppress(NextcloudException):
4244
nc.users.delete(TEST_USER_NAME)
43-
except NextcloudException:
44-
pass
4545
nc.users.create(TEST_USER_NAME, password=TEST_USER_PASSWORD)
4646
with pytest.raises(NextcloudException):
4747
nc.users.create(TEST_USER_NAME, password=TEST_USER_PASSWORD)
@@ -51,10 +51,8 @@ def test_create_user(nc):
5151
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
5252
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
5353
def test_create_user_with_groups(nc):
54-
try:
54+
with contextlib.suppress(NextcloudException):
5555
nc.users.delete(TEST_USER_NAME)
56-
except NextcloudException:
57-
pass
5856
nc.users.create(TEST_USER_NAME, password=TEST_USER_PASSWORD, groups=["admin"])
5957
admin_group = nc.users.groups.get_members("admin")
6058
assert TEST_USER_NAME in admin_group
@@ -64,10 +62,8 @@ def test_create_user_with_groups(nc):
6462
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
6563
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
6664
def test_create_user_no_name_mail(nc):
67-
try:
65+
with contextlib.suppress(NextcloudException):
6866
nc.users.delete(TEST_USER_NAME)
69-
except NextcloudException:
70-
pass
7167
with pytest.raises(ValueError):
7268
nc.users.create(TEST_USER_NAME)
7369
with pytest.raises(ValueError):
@@ -79,10 +75,8 @@ def test_create_user_no_name_mail(nc):
7975
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
8076
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
8177
def test_delete_user(nc):
82-
try:
78+
with contextlib.suppress(NextcloudException):
8379
nc.users.create(TEST_USER_NAME, password=TEST_USER_PASSWORD)
84-
except NextcloudException:
85-
pass
8680
nc.users.delete(TEST_USER_NAME)
8781
with pytest.raises(NextcloudExceptionNotFound):
8882
nc.users.delete(TEST_USER_NAME)
@@ -91,10 +85,8 @@ def test_delete_user(nc):
9185
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
9286
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
9387
def test_users_get_list(nc):
94-
try:
88+
with contextlib.suppress(NextcloudException):
9589
nc.users.create(TEST_USER_NAME, password=TEST_USER_PASSWORD)
96-
except NextcloudException:
97-
pass
9890
users = nc.users.get_list()
9991
assert isinstance(users, list)
10092
assert "admin" in users
@@ -109,10 +101,8 @@ def test_users_get_list(nc):
109101
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
110102
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
111103
def test_enable_disable_user(nc):
112-
try:
104+
with contextlib.suppress(NextcloudException):
113105
nc.users.create(TEST_USER_NAME, password=TEST_USER_PASSWORD)
114-
except NextcloudException:
115-
pass
116106
nc.users.disable(TEST_USER_NAME)
117107
user = nc.users.get_details(TEST_USER_NAME)
118108
assert not user["enabled"]

0 commit comments

Comments
 (0)