2013-10-13 17:17:56 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2014-08-16 09:25:18 +02:00
|
|
|
from unittest.mock import MagicMock, patch
|
2013-10-13 17:17:56 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
from openslides.utils import main
|
2019-10-18 14:18:49 +02:00
|
|
|
from tests.test_case import TestCase
|
2013-10-13 17:17:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestFunctions(TestCase):
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.utils.main.sys")
|
2015-01-16 14:18:34 +01:00
|
|
|
def test_detect_openslides_type_unix(self, mock_sys):
|
|
|
|
"""
|
|
|
|
Tests the return value on a unix system.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_sys.platform = "linux"
|
2015-01-16 14:18:34 +01:00
|
|
|
self.assertEqual(main.detect_openslides_type(), main.UNIX_VERSION)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.utils.main.os.path.basename")
|
|
|
|
@patch("openslides.utils.main.sys")
|
2015-01-16 14:18:34 +01:00
|
|
|
def test_detect_openslides_type_win_portable(self, mock_sys, mock_os):
|
|
|
|
"""
|
|
|
|
Tests the return value on a windows portable system.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_sys.platform = "win32"
|
|
|
|
mock_os.return_value = "openslides.exe"
|
2015-01-16 14:18:34 +01:00
|
|
|
self.assertEqual(main.detect_openslides_type(), main.WINDOWS_PORTABLE_VERSION)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.utils.main.os.path.basename")
|
|
|
|
@patch("openslides.utils.main.sys")
|
2015-01-16 14:18:34 +01:00
|
|
|
def test_detect_openslides_type_win(self, mock_sys, mock_os):
|
|
|
|
"""
|
|
|
|
Tests the return value on a windows system.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_sys.platform = "win32"
|
|
|
|
mock_os.return_value = "python"
|
2015-01-16 14:18:34 +01:00
|
|
|
self.assertEqual(main.detect_openslides_type(), main.WINDOWS_VERSION)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.utils.main.detect_openslides_type")
|
|
|
|
@patch("openslides.utils.main.os.path.expanduser")
|
2018-01-16 16:02:23 +01:00
|
|
|
def test_get_default_settings_dir_unix(self, mock_expanduser, mock_detect):
|
2021-03-18 13:52:51 +01:00
|
|
|
os.environ.pop("XDG_CONFIG_HOME", None)
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_expanduser.return_value = "/home/test/.config"
|
|
|
|
self.assertEqual(
|
|
|
|
main.get_default_settings_dir(main.UNIX_VERSION),
|
|
|
|
"/home/test/.config/openslides",
|
|
|
|
)
|
2015-01-16 14:18:34 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.utils.main.get_win32_app_data_dir")
|
2018-01-16 16:02:23 +01:00
|
|
|
def test_get_default_settings_dir_win(self, mock_win):
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_win.return_value = "win32"
|
|
|
|
self.assertEqual(
|
|
|
|
main.get_default_settings_dir(main.WINDOWS_VERSION), "win32/openslides"
|
|
|
|
)
|
2015-01-16 14:18:34 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.utils.main.get_win32_portable_dir")
|
2018-01-16 16:02:23 +01:00
|
|
|
def test_get_default_settings_dir_portable(self, mock_portable):
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_portable.return_value = "portable"
|
|
|
|
self.assertEqual(
|
|
|
|
main.get_default_settings_dir(main.WINDOWS_PORTABLE_VERSION),
|
|
|
|
"portable/openslides",
|
|
|
|
)
|
2015-01-16 14:18:34 +01:00
|
|
|
|
2018-01-16 16:02:23 +01:00
|
|
|
def test_get_local_settings_dir(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
|
|
|
main.get_local_settings_dir(), os.sep.join(("personal_data", "var"))
|
|
|
|
)
|
2013-10-13 17:17:56 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def test_setup_django_settings_module(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
main.setup_django_settings_module(
|
|
|
|
"test_dir_dhvnghfjdh456fzheg2f/test_path_bngjdhc756dzwncshdfnx.py"
|
|
|
|
)
|
2015-01-16 14:18:34 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
|
|
|
os.environ["DJANGO_SETTINGS_MODULE"], "test_path_bngjdhc756dzwncshdfnx"
|
|
|
|
)
|
|
|
|
self.assertEqual(sys.path[0], os.path.abspath("test_dir_dhvnghfjdh456fzheg2f"))
|
2013-10-13 17:17:56 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.utils.main.detect_openslides_type")
|
2014-01-11 15:47:15 +01:00
|
|
|
def test_get_default_settings_context_portable(self, detect_mock):
|
2015-01-16 14:18:34 +01:00
|
|
|
detect_mock.return_value = main.WINDOWS_PORTABLE_VERSION
|
|
|
|
context = main.get_default_settings_context()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
|
|
|
context["openslides_user_data_dir"], "get_win32_portable_user_data_dir()"
|
|
|
|
)
|
2013-10-13 17:17:56 +02:00
|
|
|
|
2018-01-16 16:02:23 +01:00
|
|
|
def test_get_default_user_data_dir(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertIn(
|
|
|
|
os.path.join(".local", "share"),
|
|
|
|
main.get_default_user_data_dir(main.UNIX_VERSION),
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch("openslides.utils.main.threading.Thread")
|
|
|
|
@patch("openslides.utils.main.time")
|
|
|
|
@patch("openslides.utils.main.webbrowser")
|
|
|
|
def test_start_browser(self, mock_webbrowser, mock_time, mock_Thread):
|
2013-10-30 17:48:09 +01:00
|
|
|
browser_mock = MagicMock()
|
|
|
|
mock_webbrowser.get.return_value = browser_mock
|
2015-01-16 14:18:34 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
main.start_browser("http://localhost:8234")
|
2015-01-16 14:18:34 +01:00
|
|
|
|
2013-11-04 08:41:51 +01:00
|
|
|
self.assertTrue(mock_Thread.called)
|
2019-01-06 16:22:33 +01:00
|
|
|
inner_function = mock_Thread.call_args[1]["target"]
|
2013-11-04 08:41:51 +01:00
|
|
|
inner_function()
|
2019-01-06 16:22:33 +01:00
|
|
|
browser_mock.open.assert_called_with("http://localhost:8234")
|