input-remapper/tests/unit/test_user.py

65 lines
2.1 KiB
Python
Raw Normal View History

2021-08-17 21:06:14 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
2022-01-01 12:00:49 +00:00
# input-remapper - GUI for device specific keyboard mappings
2022-01-01 12:52:33 +00:00
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
2021-08-17 21:06:14 +00:00
#
2022-01-01 12:00:49 +00:00
# This file is part of input-remapper.
2021-08-17 21:06:14 +00:00
#
2022-01-01 12:00:49 +00:00
# input-remapper is free software: you can redistribute it and/or modify
2021-08-17 21:06:14 +00:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2022-01-01 12:00:49 +00:00
# input-remapper is distributed in the hope that it will be useful,
2021-08-17 21:06:14 +00:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2022-01-01 12:00:49 +00:00
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
2021-08-17 21:06:14 +00:00
import os
import unittest
from unittest import mock
2022-01-01 12:00:49 +00:00
from inputremapper.user import get_user, get_home
2021-08-17 21:06:14 +00:00
from tests.test import quick_cleanup
def _raise(error):
raise error
2021-08-17 21:11:26 +00:00
class TestUser(unittest.TestCase):
2021-08-17 21:06:14 +00:00
def tearDown(self):
quick_cleanup()
def test_get_user(self):
2021-09-26 10:44:56 +00:00
with mock.patch("os.getlogin", lambda: "foo"):
self.assertEqual(get_user(), "foo")
2021-08-17 21:06:14 +00:00
2021-09-26 10:44:56 +00:00
with mock.patch("os.getlogin", lambda: "root"):
self.assertEqual(get_user(), "root")
2021-08-17 21:06:14 +00:00
property_mock = mock.Mock()
2021-09-26 10:44:56 +00:00
property_mock.configure_mock(pw_name="quix")
with mock.patch("os.getlogin", lambda: _raise(OSError())), mock.patch(
"pwd.getpwuid", return_value=property_mock
):
os.environ["USER"] = "root"
os.environ["SUDO_USER"] = "qux"
self.assertEqual(get_user(), "qux")
2021-08-17 21:06:14 +00:00
2021-09-26 10:44:56 +00:00
os.environ["USER"] = "root"
del os.environ["SUDO_USER"]
os.environ["PKEXEC_UID"] = "1000"
self.assertNotEqual(get_user(), "root")
def test_get_home(self):
property_mock = mock.Mock()
2021-09-26 10:44:56 +00:00
property_mock.configure_mock(pw_dir="/custom/home/foo")
with mock.patch("pwd.getpwnam", return_value=property_mock):
self.assertEqual(get_home("foo"), "/custom/home/foo")