def test_create_unknown_method(self) -> None:
     parser = configparser.ConfigParser()
     parser.read_string(
         """
         [section]
         method = asdfasdf
         """
     )
     with pytest.raises(ConfigurationError):
         XIdleTime.create("name", parser["section"])
 def test_create_no_int(self) -> None:
     parser = configparser.ConfigParser()
     parser.read_string(
         """
         [section]
         timeout = string
         """
     )
     with pytest.raises(ConfigurationError):
         XIdleTime.create("name", parser["section"])
 def test_create_broken_users_re(self) -> None:
     parser = configparser.ConfigParser()
     parser.read_string(
         """
         [section]
         ignore_users = [[a-9]
         """
     )
     with pytest.raises(ConfigurationError):
         XIdleTime.create("name", parser["section"])
 def test_create_default(self) -> None:
     parser = configparser.ConfigParser()
     parser.read_string("""[section]""")
     check = XIdleTime.create("name", parser["section"])
     assert check._timeout == 600
     assert check._ignore_process_re == re.compile(r"a^")
     assert check._ignore_users_re == re.compile(r"a^")
     assert check._provide_sessions == check._list_sessions_sockets
 def test_create_default(self):
     parser = configparser.ConfigParser()
     parser.read_string('''[section]''')
     check = XIdleTime.create('name', parser['section'])
     assert check._timeout == 600
     assert check._ignore_process_re == re.compile(r'a^')
     assert check._ignore_users_re == re.compile(r'a^')
     assert check._provide_sessions == check._list_sessions_sockets
    def test_list_sessions_logind_dbus_error(self, mocker) -> None:
        mock = mocker.patch("autosuspend.checks.activity.list_logind_sessions")
        mock.side_effect = LogindDBusException()

        parser = configparser.ConfigParser()
        parser.read_string("""[section]""")
        check = XIdleTime.create("name", parser["section"])
        with pytest.raises(TemporaryCheckError):
            check._list_sessions_logind()
    def test_list_sessions_logind(self, mocker):
        mock = mocker.patch('autosuspend.checks.activity.list_logind_sessions')
        mock.return_value = [('c1', {'Name': 'foo'}),
                             ('c2', {'Display': 'asdfasf'}),
                             ('c3', {'Name': 'hello', 'Display': 'nonumber'}),
                             ('c4', {'Name': 'hello', 'Display': '3'})]

        parser = configparser.ConfigParser()
        parser.read_string('''[section]''')
        check = XIdleTime.create('name', parser['section'])
        assert check._list_sessions_logind() == [(3, 'hello')]
 def test_create(self):
     parser = configparser.ConfigParser()
     parser.read_string('''[section]
                           timeout = 42
                           ignore_if_process = .*test
                           ignore_users = test.*test
                           method = logind''')
     check = XIdleTime.create('name', parser['section'])
     assert check._timeout == 42
     assert check._ignore_process_re == re.compile(r'.*test')
     assert check._ignore_users_re == re.compile(r'test.*test')
     assert check._provide_sessions == check._list_sessions_logind
    def test_list_sessions_logind(self, mocker) -> None:
        mock = mocker.patch("autosuspend.checks.activity.list_logind_sessions")
        mock.return_value = [
            ("c1", {"Name": "foo"}),
            ("c2", {"Display": "asdfasf"}),
            ("c3", {"Name": "hello", "Display": "nonumber"}),
            ("c4", {"Name": "hello", "Display": "3"}),
        ]

        parser = configparser.ConfigParser()
        parser.read_string("""[section]""")
        check = XIdleTime.create("name", parser["section"])
        assert check._list_sessions_logind() == [(3, "hello")]
 def test_create(self) -> None:
     parser = configparser.ConfigParser()
     parser.read_string(
         """
         [section]
         timeout = 42
         ignore_if_process = .*test
         ignore_users = test.*test
         method = logind
         """
     )
     check = XIdleTime.create("name", parser["section"])
     assert check._timeout == 42
     assert check._ignore_process_re == re.compile(r".*test")
     assert check._ignore_users_re == re.compile(r"test.*test")
     assert check._provide_sessions == check._list_sessions_logind
    def test_list_sessions_socket(self, mocker):
        mock_glob = mocker.patch('glob.glob')
        mock_glob.return_value = ['/tmp/.X11-unix/X0',
                                  '/tmp/.X11-unix/X42',
                                  '/tmp/.X11-unix/Xnum']

        stat_return = os.stat(os.path.realpath(__file__))
        this_user = pwd.getpwuid(stat_return.st_uid)
        mock_stat = mocker.patch('os.stat')
        mock_stat.return_value = stat_return

        mock_pwd = mocker.patch('pwd.getpwuid')
        mock_pwd.return_value = this_user

        parser = configparser.ConfigParser()
        parser.read_string('''[section]''')
        check = XIdleTime.create('name', parser['section'])
        assert check._list_sessions_sockets() == [(0, this_user.pw_name),
                                                  (42, this_user.pw_name)]
    def test_list_sessions_socket(self, mocker) -> None:
        mock_glob = mocker.patch("glob.glob")
        mock_glob.return_value = [
            "/tmp/.X11-unix/X0",
            "/tmp/.X11-unix/X42",
            "/tmp/.X11-unix/Xnum",
        ]

        stat_return = os.stat(os.path.realpath(__file__))
        this_user = pwd.getpwuid(stat_return.st_uid)
        mock_stat = mocker.patch("os.stat")
        mock_stat.return_value = stat_return

        mock_pwd = mocker.patch("pwd.getpwuid")
        mock_pwd.return_value = this_user

        parser = configparser.ConfigParser()
        parser.read_string("""[section]""")
        check = XIdleTime.create("name", parser["section"])
        assert check._list_sessions_sockets() == [
            (0, this_user.pw_name),
            (42, this_user.pw_name),
        ]
 def test_create_unknown_method(self):
     parser = configparser.ConfigParser()
     parser.read_string('''[section]
                           method = asdfasdf''')
     with pytest.raises(ConfigurationError):
         XIdleTime.create('name', parser['section'])
 def test_create_broken_users_re(self):
     parser = configparser.ConfigParser()
     parser.read_string('''[section]
                           ignore_users = [[a-9]''')
     with pytest.raises(ConfigurationError):
         XIdleTime.create('name', parser['section'])
 def test_create_no_int(self):
     parser = configparser.ConfigParser()
     parser.read_string('''[section]
                           timeout = string''')
     with pytest.raises(ConfigurationError):
         XIdleTime.create('name', parser['section'])