From 00152e800ed3abef7e4c4c3e827365d5ce038616 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 12 May 2024 19:32:50 +0100 Subject: [PATCH] Potential for account directories to be outside the application directory --- epicyon.py | 2 ++ person.py | 2 ++ tests.py | 22 ++++++++++++---------- utils.py | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/epicyon.py b/epicyon.py index 252774860..7d3a28876 100644 --- a/epicyon.py +++ b/epicyon.py @@ -73,6 +73,7 @@ from tests import run_all_tests from auth import store_basic_credentials from auth import create_password from utils import data_dir +from utils import data_dir_testing from utils import string_ends_with from utils import remove_html from utils import remove_eol @@ -826,6 +827,7 @@ def _command_options() -> None: if argb.testsnetwork: print('Network Tests') base_dir = os.getcwd() + data_dir_testing(base_dir) test_shared_items_federation(base_dir) test_group_follow(base_dir) test_post_message_between_servers(base_dir) diff --git a/person.py b/person.py index 0aed2c3b4..dea255332 100644 --- a/person.py +++ b/person.py @@ -565,6 +565,8 @@ def _create_person_base(base_dir: str, nickname: str, domain: str, port: int, if save_to_file: # save person to file + if not os.path.isdir(base_dir): + os.mkdir(base_dir) people_subdir = data_dir(base_dir) if not os.path.isdir(people_subdir): os.mkdir(people_subdir) diff --git a/tests.py b/tests.py index 9819a5b62..2488d00b3 100644 --- a/tests.py +++ b/tests.py @@ -57,6 +57,7 @@ from follow import send_follow_request_via_server from follow import send_unfollow_request_via_server from siteactive import site_is_active from utils import data_dir +from utils import data_dir_testing from utils import remove_link_tracking from utils import uninvert_text from utils import get_url_from_post @@ -8358,10 +8359,10 @@ def _test_book_link(base_dir: str): max_recent_books = 1000 max_cached_readers = 10 - base_dir += '/.testbookevents' - if os.path.isdir(base_dir): - shutil.rmtree(base_dir, ignore_errors=False, onerror=None) - os.mkdir(base_dir) + base_dir2 = base_dir + '/.testbookevents' + if os.path.isdir(base_dir2): + shutil.rmtree(base_dir2, ignore_errors=False, onerror=None) + os.mkdir(base_dir2) content = 'Not a link' result = get_book_link_from_content(content) @@ -8440,7 +8441,7 @@ def _test_book_link(base_dir: str): assert result['name'] == title assert result['id'] == id_str - assert store_book_events(base_dir, + assert store_book_events(base_dir2, post_json_object, system_language, languages_understood, @@ -8557,7 +8558,7 @@ def _test_book_link(base_dir: str): assert result['name'] == title assert result['id'] == id_str - assert store_book_events(base_dir, + assert store_book_events(base_dir2, post_json_object, system_language, languages_understood, @@ -8609,7 +8610,7 @@ def _test_book_link(base_dir: str): assert result['name'] == title assert result['id'] == id_str - assert store_book_events(base_dir, + assert store_book_events(base_dir2, post_json_object, system_language, languages_understood, @@ -8658,7 +8659,7 @@ def _test_book_link(base_dir: str): assert result['rating'] == rating assert result['id'] == id_str - assert store_book_events(base_dir, + assert store_book_events(base_dir2, post_json_object, system_language, languages_understood, @@ -8678,8 +8679,8 @@ def _test_book_link(base_dir: str): assert books_cache['reader_list'][expected_readers - 1] == actor assert books_cache['readers'].get(actor) - if os.path.isdir(base_dir): - shutil.rmtree(base_dir, ignore_errors=False, onerror=None) + if os.path.isdir(base_dir2): + shutil.rmtree(base_dir2, ignore_errors=False, onerror=None) def _test_uninvert2(): @@ -8840,6 +8841,7 @@ def _test_link_tracking() -> None: def run_all_tests(): base_dir = os.getcwd() + data_dir_testing(base_dir) print('Running tests...') update_default_themes_list(os.getcwd()) _test_source_contains_no_tabs() diff --git a/utils.py b/utils.py index 1fbe126aa..2bbf5c9ec 100644 --- a/utils.py +++ b/utils.py @@ -6,6 +6,8 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@libreserver.org" __status__ = "Production" __module_group__ = "Core" +__accounts_data_path__ = None +__accounts_data_path_tests__ = False import os import re @@ -570,10 +572,44 @@ def get_base_content_from_post(post_json_object: {}, return this_post_json['content'] -def data_dir(base_dir) -> str: +def data_dir_testing(base_dir: str) -> None: + """During unit tests __accounts_data_path__ should not be retained + """ + global __accounts_data_path__ + global __accounts_data_path_tests__ + __accounts_data_path_tests__ = True + __accounts_data_path__ = base_dir + '/accounts' + print('Data directory is in testing mode') + + +def data_dir(base_dir: str) -> str: """Returns the directory where account data is stored """ - return base_dir + '/accounts' + global __accounts_data_path__ + global __accounts_data_path_tests__ + if __accounts_data_path_tests__: + __accounts_data_path__ = base_dir + '/accounts' + return __accounts_data_path__ + + if not __accounts_data_path__: + # the default path for accounts data + __accounts_data_path__ = base_dir + '/accounts' + + # is an alternative path set? + accounts_data_path_filename = base_dir + '/data_path.txt' + if os.path.isfile(accounts_data_path_filename): + path = None + try: + with open(accounts_data_path_filename, 'r', + encoding='utf-8') as file: + path = file.read() + except OSError: + print('EX: unable to read ' + accounts_data_path_filename) + if path: + __accounts_data_path__ = path.strip() + print('Accounts data path set to ' + __accounts_data_path__) + + return __accounts_data_path__ def acct_dir(base_dir: str, nickname: str, domain: str) -> str: