# ABOUTME: Tests for current user detection and avatar loading. # ABOUTME: Verifies user info retrieval from the system. import os from pathlib import Path from unittest.mock import patch from moonlock.users import get_current_user, get_avatar_path, User class TestGetCurrentUser: """Tests for current user detection.""" @patch("moonlock.users.os.getlogin", return_value="testuser") @patch("moonlock.users.pwd.getpwnam") def test_returns_user_with_correct_username(self, mock_pwd, mock_login): mock_pwd.return_value.pw_name = "testuser" mock_pwd.return_value.pw_gecos = "Test User" mock_pwd.return_value.pw_dir = "/home/testuser" mock_pwd.return_value.pw_uid = 1000 user = get_current_user() assert user.username == "testuser" assert user.display_name == "Test User" assert user.home == Path("/home/testuser") @patch("moonlock.users.os.getlogin", return_value="testuser") @patch("moonlock.users.pwd.getpwnam") def test_empty_gecos_falls_back_to_username(self, mock_pwd, mock_login): mock_pwd.return_value.pw_name = "testuser" mock_pwd.return_value.pw_gecos = "" mock_pwd.return_value.pw_dir = "/home/testuser" mock_pwd.return_value.pw_uid = 1000 user = get_current_user() assert user.display_name == "testuser" @patch("moonlock.users.os.getlogin", return_value="testuser") @patch("moonlock.users.pwd.getpwnam") def test_gecos_with_commas_uses_first_field(self, mock_pwd, mock_login): mock_pwd.return_value.pw_name = "testuser" mock_pwd.return_value.pw_gecos = "Test User,,,Room 42" mock_pwd.return_value.pw_dir = "/home/testuser" mock_pwd.return_value.pw_uid = 1000 user = get_current_user() assert user.display_name == "Test User" class TestGetAvatarPath: """Tests for avatar path resolution.""" def test_returns_face_file_if_exists(self, tmp_path: Path): face = tmp_path / ".face" face.write_text("fake image") path = get_avatar_path(tmp_path) assert path == face def test_returns_accountsservice_icon_if_exists(self, tmp_path: Path): username = "testuser" icons_dir = tmp_path / "icons" icons_dir.mkdir() icon = icons_dir / username icon.write_text("fake image") path = get_avatar_path( tmp_path, username=username, accountsservice_dir=icons_dir ) assert path == icon def test_face_file_takes_priority_over_accountsservice(self, tmp_path: Path): face = tmp_path / ".face" face.write_text("fake image") icons_dir = tmp_path / "icons" icons_dir.mkdir() icon = icons_dir / "testuser" icon.write_text("fake image") path = get_avatar_path( tmp_path, username="testuser", accountsservice_dir=icons_dir ) assert path == face def test_returns_none_when_no_avatar(self, tmp_path: Path): path = get_avatar_path(tmp_path) assert path is None