-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathconftest.py
More file actions
213 lines (159 loc) · 6.61 KB
/
conftest.py
File metadata and controls
213 lines (159 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
"""
mslib.conftest
~~~~~~~~~~~~~~
common definitions for py.test
This file is part of MSS.
:copyright: Copyright 2016-2017 Reimar Bauer
:copyright: Copyright 2016-2026 by the MSS team, see AUTHORS.
:license: APACHE-2.0, see LICENSE for details.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import importlib.util
import os
import sys
# Disable pyc files
sys.dont_write_bytecode = True
import pytest
import shutil
import keyring
from mslib.mswms.seed import DataFiles
import tests.constants as constants
from mslib.utils.loggerdef import configure_mpl_logger
matplotlib_logger = configure_mpl_logger()
# This import must come after importing tests.constants due to MSUI_CONFIG_PATH being set there
from mslib.utils.config import read_config_file
class TestKeyring(keyring.backend.KeyringBackend):
"""A test keyring which always outputs the same password
from Runtime Configuration
https://pypi.org/project/keyring/#third-party-backends
"""
priority = 1
passwords = {}
def reset(self):
self.passwords = {}
def set_password(self, servicename, username, password):
self.passwords[servicename + username] = password
def get_password(self, servicename, username):
return self.passwords.get(servicename + username, "password from TestKeyring")
def delete_password(self, servicename, username):
if servicename + username in self.passwords:
del self.passwords[servicename + username]
# set the keyring for keyring lib
keyring.set_keyring(TestKeyring())
@pytest.fixture(autouse=True)
def keyring_reset():
keyring.get_keyring().reset()
def generate_initial_config():
"""Generate an initial state for the configuration directory in tests.constants.ROOT_FS
"""
# make a copy for mscolab test, so that we read different paths during parallel tests.
sample_path = os.path.join(os.path.dirname(__file__), "tests", "data")
shutil.copy(os.path.join(sample_path, "example.ftml"), constants.ROOT_DIR)
if not constants.MSWMS_SERVER_CONFIG_FILE_PATH.exists():
print('\n configure testdata')
# ToDo check pytest tmpdir_factory
print(constants.MSWMS_DATA_DIR)
examples = DataFiles(mswms_data_dir=constants.MSWMS_DATA_DIR,
mswms_server_config_dir=constants.MSWMS_SERVER_CONFIG_DIR)
examples.create_server_config(detailed_information=True)
examples.create_data()
if not constants.MSCOLAB_SERVER_CONFIG_FILE_PATH.exists():
config_string = f'''
# SQLALCHEMY_DATABASE_URI = 'mysql://user:pass@127.0.0.1/mscolab'
import os
import logging
import secrets
from pathlib import Path
from urllib.parse import urljoin
ROOT_DIR = "{constants.ROOT_DIR}"
# directory where mss output files are stored
DATA_DIR = "{constants.MSCOLAB_DATA_DIR}"
# this will be removed
OPERATIONS_DATA = Path(DATA_DIR)
BASE_DIR = ROOT_DIR
# mscolab data directory for operation git repositories
SSO_DIR = os.path.join(ROOT_DIR, 'datasso')
# In the unit days when Operations get archived because not used
ARCHIVE_THRESHOLD = 30
# To enable logging set to True or pass a logger object to use.
SOCKETIO_LOGGER = True
# To enable Engine.IO logging set to True or pass a logger object to use.
ENGINEIO_LOGGER = True
# used to generate and parse tokens
SECRET_KEY = secrets.token_urlsafe(16)
# used to generate the password token
SECURITY_PASSWORD_SALT = secrets.token_urlsafe(16)
# looks for a given category for an operation ending with GROUP_POSTFIX
# e.g. category = Tex will look for TexGroup
# all users in that Group are set to the operations of that category
# having the roles in the TexGroup
GROUP_POSTFIX = "Group"
# mail settings
MAIL_SERVER = 'localhost'
MAIL_PORT = 25
MAIL_USE_TLS = False
MAIL_USE_SSL = True
# mail authentication
MAIL_USERNAME = os.environ.get('APP_MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('APP_MAIL_PASSWORD')
# mail accounts
MAIL_DEFAULT_SENDER = 'MSS@localhost'
# enable verification by Mail
MAIL_ENABLED = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + urljoin(DATA_DIR, 'mscolab.db')
# enable SQLALCHEMY_ECHO
SQLALCHEMY_ECHO = False
# mscolab file upload settings
UPLOAD_FOLDER = os.path.join(DATA_DIR, 'uploads')
MAX_UPLOAD_SIZE = 2 * 1024 * 1024 # 2MB
enable_basic_http_authentication = False
# enable login by identity provider
USE_SAML2 = False
'''
MSCOLAB_CONFIG = constants.MSCOLAB_SERVER_CONFIG_FILE_PATH
MSCOLAB_CONFIG.write_text(config_string)
MSCOLAB_AUTH_FILE = constants.MSCOLAB_SERVER_CONFIG_DIR / constants.MSCOLAB_AUTH_FILE
if not MSCOLAB_AUTH_FILE.exists():
config_string = '''
import hashlib
class mscolab_auth:
password = "testvaluepassword"
allowed_users = [("user", hashlib.md5(password.encode('utf-8')).hexdigest())]
'''
MSCOLAB_AUTH_FILE.write_text(config_string)
def _load_module(module_name, path):
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
_load_module("mswms_settings", constants.MSWMS_SERVER_CONFIG_FILE_PATH)
_load_module("mscolab_settings", constants.MSCOLAB_SERVER_CONFIG_FILE_PATH)
generate_initial_config()
# This import must come after the call to generate_initial_config, otherwise SQLAlchemy will have a wrong database path
from tests.utils import create_msui_settings_file
@pytest.fixture(autouse=True)
def reset_config():
"""Reset the configuration directory used in the tests (tests.constants.ROOT_FS) after every test
"""
# Ideally this would just be shutil.rmtree(constants.MSCOLAB_SERVER_CONFIG_DIR),
# but SQLAlchemy complains if the SQLite file is deleted.
for item_name in constants.MSCOLAB_SERVER_CONFIG_DIR.iterdir():
if item_name.is_dir():
shutil.rmtree(item_name)
else:
if item_name.name != "mscolab.db":
item_name.unlink()
generate_initial_config()
create_msui_settings_file("{}")
read_config_file()
# Make fixtures available everywhere
from tests.fixtures import * # noqa: F401, F403