SFM/tests/unit/test_opencloud_access.py
2026-09-17 13:04:35 +03:00

120 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
from app.opencloud_access import (
OpenCloudUploadError,
build_opencloud_url,
upload_local_file_to_opencloud,
)
def test_build_opencloud_url_encodes_cyrillic_and_spaces() -> None:
url = build_opencloud_url(
space_id="storage123$space456",
folder_segments=("Райков М.С.",),
file_name="report 2026.xlsx",
)
assert url == (
"https://docs.raisa.go.rshbank.ru/dav/spaces/storage123$space456/"
"%D0%A0%D0%B0%D0%B9%D0%BA%D0%BE%D0%B2%20%D0%9C.%D0%A1./"
"report%202026.xlsx"
)
def test_build_opencloud_url_without_folder() -> None:
url = build_opencloud_url(
space_id="abc", folder_segments=(), file_name="report.xlsx"
)
assert url == "https://docs.raisa.go.rshbank.ru/dav/spaces/abc/report.xlsx"
def test_build_opencloud_url_requires_space_id() -> None:
with pytest.raises(OpenCloudUploadError):
build_opencloud_url(space_id="", folder_segments=(), file_name="report.xlsx")
def test_upload_local_file_to_opencloud_success(tmp_path: Path) -> None:
local_file = tmp_path / "report.xlsx"
local_file.write_bytes(b"fake-xlsx-bytes")
fake_response = Mock(status_code=201, text="")
with patch("app.opencloud_access.requests.put", return_value=fake_response) as put:
url = upload_local_file_to_opencloud(
local_file,
space_id="abc$def",
folder_segments=("Райков М.С.",),
file_name="report.xlsx",
username="raykovmse",
token="coherent effects plausibly apple arming chef",
)
assert url.endswith("/report.xlsx")
_, kwargs = put.call_args
# Basic Auth (App Token), не Bearer — см. app/opencloud_access.py
assert "Authorization" not in kwargs["headers"]
auth = kwargs["auth"]
assert auth.username == "raykovmse"
assert auth.password == "coherent effects plausibly apple arming chef"
def test_upload_local_file_to_opencloud_missing_token(tmp_path: Path) -> None:
local_file = tmp_path / "report.xlsx"
local_file.write_bytes(b"data")
with pytest.raises(OpenCloudUploadError):
upload_local_file_to_opencloud(
local_file,
space_id="abc",
folder_segments=(),
file_name="report.xlsx",
username="raykovmse",
token="",
)
def test_upload_local_file_to_opencloud_missing_username(tmp_path: Path) -> None:
local_file = tmp_path / "report.xlsx"
local_file.write_bytes(b"data")
with pytest.raises(OpenCloudUploadError):
upload_local_file_to_opencloud(
local_file,
space_id="abc",
folder_segments=(),
file_name="report.xlsx",
username="",
token="token-123",
)
def test_upload_local_file_to_opencloud_missing_local_file(tmp_path: Path) -> None:
missing_file = tmp_path / "does-not-exist.xlsx"
with pytest.raises(OpenCloudUploadError):
upload_local_file_to_opencloud(
missing_file,
space_id="abc",
folder_segments=(),
file_name="report.xlsx",
username="raykovmse",
token="token-123",
)
def test_upload_local_file_to_opencloud_http_error(tmp_path: Path) -> None:
local_file = tmp_path / "report.xlsx"
local_file.write_bytes(b"data")
fake_response = Mock(status_code=401, text="Unauthorized")
with patch("app.opencloud_access.requests.put", return_value=fake_response):
with pytest.raises(OpenCloudUploadError):
upload_local_file_to_opencloud(
local_file,
space_id="abc",
folder_segments=(),
file_name="report.xlsx",
username="raykovmse",
token="token-123",
)