2222* test_upload_file
2323"""
2424
25+ from contextlib import contextmanager
2526from pathlib import Path
2627
2728import yaml
2829
29- from osw .auth import CredentialManager
30-
3130# requires pytest_mock fixture --> pip install pytest-mock
3231
3332
@@ -45,6 +44,18 @@ def create_dummy_file(file_path: Path):
4544 file .write ("Hello, World!" )
4645
4746
47+ @contextmanager
48+ def preserve_entity_py_state ():
49+ path = Path (__file__ ).parents [2 ] / "src" / "osw" / "model" / "entity.py"
50+ with open (path , "r" ) as f :
51+ original_entity = f .read ()
52+ try :
53+ yield None
54+ finally :
55+ with open (path , "w" ) as f :
56+ f .write (original_entity )
57+
58+
4859def osw_express_and_credentials (osw_express , wiki_domain , wiki_username , wiki_password ):
4960 assert osw_express .domain == wiki_domain
5061 # An error would occur if the connection attempt to the wiki would have failed
@@ -65,23 +76,47 @@ def test_init_with_domain(wiki_domain, wiki_username, wiki_password, mocker):
6576 the installation of the dependencies should be triggered here."""
6677 # Here the initial connection to the wiki is mocked (passing domain, username and
6778 # password)
68-
6979 mocked_input = mocker .patch ("builtins.input" )
7080 mocked_getpass = mocker .patch ("getpass.getpass" )
7181 mocked_input .side_effect = [wiki_domain , wiki_username ]
7282 mocked_getpass .return_value = wiki_password
83+
84+ # Before making changes to osw.model.entity save the original state
85+ with preserve_entity_py_state ():
86+ # This import will trigger the install_dependencies method call on the first run
87+ import osw .express
88+
89+ # A second connection is then opened with domain already set, so mocking is
90+ # required for the username and password only
91+ mocked_input .side_effect = [wiki_username ]
92+ mocked_getpass .return_value = wiki_password
93+ osw_express = osw .express .OswExpress (domain = wiki_domain )
94+ osw_express_and_credentials (
95+ osw_express , wiki_domain , wiki_username , wiki_password
96+ )
97+ assert osw_express .cred_filepath == Path (
98+ osw .express .default_paths .cred_filepath
99+ )
100+ osw_express .shut_down ()
101+ osw_express .cred_filepath .unlink ()
102+
103+
104+ def test_init_with_defaults_set (wiki_domain , wiki_username , wiki_password ):
105+ from osw .defaults import params as default_params
106+ from osw .defaults import paths as default_paths
107+
108+ cred_filepath = Path .cwd () / "accounts.pwd.yaml"
109+ create_credentials_file (cred_filepath , wiki_domain , wiki_username , wiki_password )
110+ default_paths .cred_filepath = cred_filepath
111+ default_params .wiki_domain = wiki_domain
73112 # This import will trigger the install_dependencies method call on the first run
74113 import osw .express
75114
76- # A second connection is then opened with domain already set, so mocking is
77- # required for the username and password only
78- mocked_input .side_effect = [wiki_username ]
79- mocked_getpass .return_value = wiki_password
80115 osw_express = osw .express .OswExpress (domain = wiki_domain )
81116 osw_express_and_credentials (osw_express , wiki_domain , wiki_username , wiki_password )
82- assert osw_express .cred_filepath == Path (osw . express . default_paths . cred_filepath )
117+ assert osw_express .cred_filepath == Path (cred_filepath )
83118 osw_express .shut_down ()
84- osw_express . cred_filepath .unlink ()
119+ cred_filepath .unlink ()
85120
86121
87122def test_init_with_cred_filepath (wiki_domain , wiki_username , wiki_password ):
@@ -130,6 +165,8 @@ def test_init_with_cred_filepath_but_missing_credentials(
130165
131166def test_init_with_cred_mngr (wiki_domain , wiki_username , wiki_password ):
132167 """Test OswExpress initialization with defined domain and cred_mngr."""
168+ from osw .auth import CredentialManager
169+
133170 cred_filepath = Path .cwd () / "accounts.pwd.yaml"
134171 create_credentials_file (cred_filepath , wiki_domain , wiki_username , wiki_password )
135172 cred_mngr = CredentialManager (cred_filepath = cred_filepath )
0 commit comments