mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 07:26:18 -05:00
Samba is installed and can be configured in the Controller.
This commit is contained in:
parent
0ceac364ec
commit
ce0cdd3a2e
15 changed files with 837 additions and 5 deletions
|
|
@ -57,7 +57,7 @@ class TestManagedServicesConfig:
|
|||
def test_managed_services_count(self):
|
||||
"""Should have expected number of managed services."""
|
||||
services = DEFAULT_CONFIG["managed_services"]
|
||||
assert len(services) == 12
|
||||
assert len(services) == 14
|
||||
|
||||
@pytest.mark.parametrize("name,expected", [
|
||||
("wrolpi-api",
|
||||
|
|
|
|||
204
controller/test/test_samba.py
Normal file
204
controller/test/test_samba.py
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
"""
|
||||
Unit tests for controller.lib.samba module.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
from unittest import mock
|
||||
|
||||
from controller.lib.samba import (
|
||||
SambaStatus,
|
||||
_generate_smb_conf,
|
||||
add_share,
|
||||
apply_samba_from_config,
|
||||
get_samba_status,
|
||||
get_samba_status_dict,
|
||||
remove_share,
|
||||
)
|
||||
|
||||
|
||||
class TestGetSambaStatus:
|
||||
"""Tests for get_samba_status function."""
|
||||
|
||||
def test_returns_unavailable_in_docker_mode(self):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=True):
|
||||
assert get_samba_status() == SambaStatus.unavailable
|
||||
|
||||
def test_returns_running_when_active(self):
|
||||
mock_result = mock.Mock()
|
||||
mock_result.stdout = "active\n"
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("subprocess.run", return_value=mock_result):
|
||||
assert get_samba_status() == SambaStatus.running
|
||||
|
||||
def test_returns_stopped_when_inactive(self):
|
||||
mock_result = mock.Mock()
|
||||
mock_result.stdout = "inactive\n"
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("subprocess.run", return_value=mock_result):
|
||||
assert get_samba_status() == SambaStatus.stopped
|
||||
|
||||
def test_returns_unavailable_when_systemctl_not_found(self):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("subprocess.run", side_effect=FileNotFoundError()):
|
||||
assert get_samba_status() == SambaStatus.unavailable
|
||||
|
||||
def test_returns_unknown_on_timeout(self):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("subprocess.run", side_effect=subprocess.TimeoutExpired("cmd", 5)):
|
||||
assert get_samba_status() == SambaStatus.unknown
|
||||
|
||||
|
||||
class TestGetSambaStatusDict:
|
||||
"""Tests for get_samba_status_dict function."""
|
||||
|
||||
def test_returns_expected_fields(self):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=True):
|
||||
result = get_samba_status_dict()
|
||||
assert "enabled" in result
|
||||
assert "available" in result
|
||||
assert "shares" in result
|
||||
|
||||
def test_unavailable_in_docker(self):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=True):
|
||||
result = get_samba_status_dict()
|
||||
assert result["available"] is False
|
||||
assert result["reason"] is not None
|
||||
|
||||
|
||||
class TestGenerateSmbConf:
|
||||
"""Tests for _generate_smb_conf function."""
|
||||
|
||||
def test_generates_global_section(self, reset_runtime_config):
|
||||
conf = _generate_smb_conf()
|
||||
assert "[global]" in conf
|
||||
assert "workgroup = WORKGROUP" in conf
|
||||
assert "map to guest = Bad User" in conf
|
||||
|
||||
def test_generates_share_sections(self, reset_runtime_config):
|
||||
from controller.lib.config import update_config
|
||||
update_config("samba.shares", [
|
||||
{"name": "TestShare", "path": "/media/wrolpi/test", "read_only": True, "comment": "Test"},
|
||||
])
|
||||
conf = _generate_smb_conf()
|
||||
assert "[TestShare]" in conf
|
||||
assert "path = /media/wrolpi/test" in conf
|
||||
assert "read only = yes" in conf
|
||||
assert "guest ok = yes" in conf
|
||||
|
||||
def test_read_write_share(self, reset_runtime_config):
|
||||
from controller.lib.config import update_config
|
||||
update_config("samba.shares", [
|
||||
{"name": "Writable", "path": "/media/wrolpi", "read_only": False, "comment": ""},
|
||||
])
|
||||
conf = _generate_smb_conf()
|
||||
assert "read only = no" in conf
|
||||
|
||||
def test_empty_shares_produces_global_only(self, reset_runtime_config):
|
||||
conf = _generate_smb_conf()
|
||||
assert "[global]" in conf
|
||||
# No share sections.
|
||||
lines = conf.split("\n")
|
||||
sections = [l for l in lines if l.startswith("[") and l != "[global]"]
|
||||
assert len(sections) == 0
|
||||
|
||||
|
||||
class TestAddShare:
|
||||
"""Tests for add_share function."""
|
||||
|
||||
def test_fails_in_docker_mode(self):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=True):
|
||||
result = add_share("test", "/media/wrolpi")
|
||||
assert result["success"] is False
|
||||
|
||||
def test_rejects_invalid_name(self, reset_runtime_config):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
result = add_share("test/bad", "/media/wrolpi")
|
||||
assert result["success"] is False
|
||||
assert "alphanumeric" in result["error"]
|
||||
|
||||
def test_rejects_path_outside_media_dir(self, reset_runtime_config, test_directory):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("controller.lib.samba.get_media_directory", return_value=test_directory):
|
||||
result = add_share("test", "/tmp")
|
||||
assert result["success"] is False
|
||||
assert "must be under" in result["error"]
|
||||
|
||||
def test_rejects_nonexistent_path(self, reset_runtime_config, test_directory):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("controller.lib.samba.get_media_directory", return_value=test_directory):
|
||||
result = add_share("test", str(test_directory / "nonexistent"))
|
||||
assert result["success"] is False
|
||||
assert "does not exist" in result["error"]
|
||||
|
||||
def test_rejects_duplicate_name(self, reset_runtime_config, test_directory):
|
||||
from controller.lib.config import update_config
|
||||
update_config("samba.shares", [
|
||||
{"name": "existing", "path": str(test_directory), "read_only": True, "comment": ""},
|
||||
])
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("controller.lib.samba.get_media_directory", return_value=test_directory):
|
||||
with mock.patch("controller.lib.samba.save_config"):
|
||||
with mock.patch("controller.lib.samba._write_smb_conf", return_value={"success": True}):
|
||||
result = add_share("Existing", str(test_directory))
|
||||
assert result["success"] is False
|
||||
assert "already exists" in result["error"]
|
||||
|
||||
def test_adds_share_successfully(self, reset_runtime_config, test_directory):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("controller.lib.samba.get_media_directory", return_value=test_directory):
|
||||
with mock.patch("controller.lib.samba.save_config"):
|
||||
with mock.patch("controller.lib.samba._write_smb_conf", return_value={"success": True}):
|
||||
result = add_share("MyShare", str(test_directory), read_only=False, comment="My files")
|
||||
assert result["success"] is True
|
||||
assert result["share"]["name"] == "MyShare"
|
||||
assert result["share"]["read_only"] is False
|
||||
|
||||
|
||||
class TestRemoveShare:
|
||||
"""Tests for remove_share function."""
|
||||
|
||||
def test_removes_share(self, reset_runtime_config):
|
||||
from controller.lib.config import update_config
|
||||
update_config("samba.shares", [
|
||||
{"name": "test", "path": "/media/wrolpi", "read_only": True, "comment": ""},
|
||||
])
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("controller.lib.samba.save_config"):
|
||||
with mock.patch("controller.lib.samba._write_smb_conf", return_value={"success": True}):
|
||||
result = remove_share("test")
|
||||
assert result["success"] is True
|
||||
|
||||
from controller.lib.config import get_config_value
|
||||
assert len(get_config_value("samba.shares", [])) == 0
|
||||
|
||||
def test_returns_error_for_missing_share(self, reset_runtime_config):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
result = remove_share("nonexistent")
|
||||
assert result["success"] is False
|
||||
assert "not found" in result["error"]
|
||||
|
||||
|
||||
class TestApplySambaFromConfig:
|
||||
"""Tests for apply_samba_from_config function."""
|
||||
|
||||
def test_skips_in_docker_mode(self):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=True):
|
||||
with mock.patch("controller.lib.samba._write_smb_conf") as mock_write:
|
||||
apply_samba_from_config()
|
||||
mock_write.assert_not_called()
|
||||
|
||||
def test_writes_config_when_shares_exist(self, reset_runtime_config):
|
||||
from controller.lib.config import update_config
|
||||
update_config("samba.shares", [
|
||||
{"name": "test", "path": "/media/wrolpi", "read_only": True, "comment": ""},
|
||||
])
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("controller.lib.samba._write_smb_conf", return_value={"success": True}) as mock_write:
|
||||
apply_samba_from_config()
|
||||
mock_write.assert_called_once()
|
||||
|
||||
def test_skips_when_no_shares(self, reset_runtime_config):
|
||||
with mock.patch("controller.lib.samba.is_docker_mode", return_value=False):
|
||||
with mock.patch("controller.lib.samba._write_smb_conf") as mock_write:
|
||||
apply_samba_from_config()
|
||||
mock_write.assert_not_called()
|
||||
55
controller/test/test_samba_api.py
Normal file
55
controller/test/test_samba_api.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"""
|
||||
Integration tests for Controller Samba API endpoints.
|
||||
"""
|
||||
|
||||
|
||||
class TestSambaStatusEndpoint:
|
||||
"""Tests for /api/samba/status endpoint."""
|
||||
|
||||
def test_returns_200(self, test_client):
|
||||
response = test_client.get("/api/samba/status")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_returns_expected_fields(self, test_client):
|
||||
response = test_client.get("/api/samba/status")
|
||||
data = response.json()
|
||||
assert "enabled" in data
|
||||
assert "available" in data
|
||||
assert "shares" in data
|
||||
|
||||
def test_shows_unavailable_in_docker(self, test_client_docker_mode):
|
||||
response = test_client_docker_mode.get("/api/samba/status")
|
||||
data = response.json()
|
||||
assert data["available"] is False
|
||||
|
||||
|
||||
class TestSambaShareAddEndpoint:
|
||||
"""Tests for POST /api/samba/shares endpoint."""
|
||||
|
||||
def test_rejects_invalid_name(self, test_client):
|
||||
response = test_client.post("/api/samba/shares", json={
|
||||
"name": "bad/name",
|
||||
"path": "/media/wrolpi",
|
||||
})
|
||||
assert response.status_code == 400
|
||||
|
||||
def test_fails_in_docker(self, test_client_docker_mode):
|
||||
response = test_client_docker_mode.post("/api/samba/shares", json={
|
||||
"name": "test",
|
||||
"path": "/media/wrolpi",
|
||||
})
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
class TestSambaShareRemoveEndpoint:
|
||||
"""Tests for DELETE /api/samba/shares/{name} endpoint."""
|
||||
|
||||
def test_returns_404_for_missing_share(self, test_client):
|
||||
response = test_client.delete("/api/samba/shares/nonexistent")
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_fails_in_docker(self, test_client_docker_mode):
|
||||
response = test_client_docker_mode.delete("/api/samba/shares/test")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue