Skip to content

Commit f557b10

Browse files
authored
modify test config handling (#647)
1 parent bcfd3e1 commit f557b10

22 files changed

+74
-69
lines changed

gramps_webapi/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def deprecated_config_from_env(app):
7676
return app
7777

7878

79-
def create_app(config: Optional[Dict[str, Any]] = None):
79+
def create_app(config: Optional[Dict[str, Any]] = None, config_from_env: bool = True):
8080
"""Flask application factory."""
8181
app = Flask(__name__)
8282

@@ -93,7 +93,8 @@ def create_app(config: Optional[Dict[str, Any]] = None):
9393
deprecated_config_from_env(app)
9494

9595
# use prefixed environment variables if exist
96-
app.config.from_prefixed_env(prefix="GRAMPSWEB")
96+
if config_from_env:
97+
app.config.from_prefixed_env(prefix="GRAMPSWEB")
9798

9899
# update config from dictionary if present
99100
if config:

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
from gramps_webapi.__main__ import cli
3232
from gramps_webapi.app import create_app
33+
from gramps_webapi.auth import get_user_details
3334
from gramps_webapi.const import ENV_CONFIG_FILE
3435
from gramps_webapi.dbmanager import WebDbManager
35-
from gramps_webapi.auth import get_user_details
3636

3737

3838
class TestCLI(unittest.TestCase):
@@ -50,7 +50,7 @@ def setUpClass(cls):
5050
with open(cls.config_file.name, "w") as f:
5151
f.write(config)
5252
with patch.dict("os.environ", {ENV_CONFIG_FILE: cls.config_file.name}):
53-
cls.app = create_app()
53+
cls.app = create_app(config_from_env=False)
5454
cls.app.config["TESTING"] = True
5555
cls.client = cls.app.test_client()
5656
cls.runner = CliRunner()

tests/test_endpoints/test_config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from gramps.gen.dbstate import DbState
2929

3030
from gramps_webapi.app import create_app
31-
from gramps_webapi.auth import user_db, add_user
31+
from gramps_webapi.auth import add_user, user_db
3232
from gramps_webapi.auth.const import (
3333
ROLE_ADMIN,
3434
ROLE_MEMBER,
@@ -47,7 +47,10 @@ def setUp(self):
4747
dirpath, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite")
4848
tree = os.path.basename(dirpath)
4949
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
50-
self.app = create_app(config={"TESTING": True, "RATELIMIT_ENABLED": False})
50+
self.app = create_app(
51+
config={"TESTING": True, "RATELIMIT_ENABLED": False},
52+
config_from_env=False,
53+
)
5154
self.client = self.app.test_client()
5255
with self.app.app_context():
5356
user_db.create_all()

tests/test_endpoints/test_delete.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def setUpClass(cls):
6060
dirpath, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
6161
tree = os.path.basename(dirpath)
6262
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
63-
cls.app = create_app()
63+
cls.app = create_app(config_from_env=False)
6464
cls.app.config["TESTING"] = True
6565
cls.client = cls.app.test_client()
6666
with cls.app.app_context():
@@ -235,7 +235,7 @@ def setUpClass(cls):
235235
dirpath, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
236236
tree = os.path.basename(dirpath)
237237
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
238-
cls.app = create_app()
238+
cls.app = create_app(config_from_env=False)
239239
cls.app.config["TESTING"] = True
240240
cls.client = cls.app.test_client()
241241
with cls.app.app_context():

tests/test_endpoints/test_dna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def setUpClass(cls):
6969
dbpath, _ = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
7070
tree = os.path.basename(dbpath)
7171
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
72-
cls.app = create_app()
72+
cls.app = create_app(config_from_env=False)
7373
cls.app.config["TESTING"] = True
7474
cls.client = cls.app.test_client()
7575
with cls.app.app_context():

tests/test_endpoints/test_history.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,8 @@ def setUp(self):
5252
self.dbman = CLIDbManager(DbState())
5353
dirpath, _ = self.dbman.create_new_db_cli(self.name, dbid="sqlite")
5454
tree = os.path.basename(dirpath)
55-
with patch.dict(
56-
"os.environ",
57-
{
58-
ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG,
59-
"GRAMPSWEB_TREE": self.name,
60-
},
61-
):
62-
self.app = create_app()
55+
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG}):
56+
self.app = create_app(config_from_env=False, config={"TREE": self.name})
6357
self.app.config["TESTING"] = True
6458
self.client = self.app.test_client()
6559
with self.app.app_context():

tests/test_endpoints/test_import_media.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@ def setUpClass(cls):
6060
cls.dbman = CLIDbManager(DbState())
6161
cls.dbpath, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
6262
cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
63-
with patch.dict(
64-
"os.environ",
65-
{
66-
ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG,
67-
"GRAMPSWEB_TREE": cls.name,
68-
},
69-
):
63+
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG}):
7064
cls.test_app = create_app(
71-
{"EXPORT_DIR": cls.export_dir, "MEDIA_BASE_DIR": cls.media_dir}
65+
{
66+
"EXPORT_DIR": cls.export_dir,
67+
"MEDIA_BASE_DIR": cls.media_dir,
68+
"TREE": cls.name,
69+
},
7270
)
7371
cls.test_app.config["TESTING"] = True
7472
cls.client = cls.test_app.test_client()

tests/test_endpoints/test_importers.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,8 @@ def setUpClass(cls):
103103
cls.dbman = CLIDbManager(DbState())
104104
cls.dbpath, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
105105
cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
106-
with patch.dict(
107-
"os.environ",
108-
{
109-
ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG,
110-
"TREE": cls.name,
111-
},
112-
):
113-
cls.test_app = create_app()
106+
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG}):
107+
cls.test_app = create_app(config_from_env=False, config={"TREE": cls.name})
114108
cls.test_app.config["TESTING"] = True
115109
cls.client = cls.test_app.test_client()
116110
cls.tree = os.path.basename(cls.dbpath)

tests/test_endpoints/test_ocr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def setUpClass(cls):
5050
dirpath, _ = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
5151
tree = os.path.basename(dirpath)
5252
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
53-
cls.app = create_app()
53+
cls.app = create_app(config_from_env=False)
5454
cls.app.config["TESTING"] = True
5555
cls.media_base_dir = tempfile.mkdtemp()
5656
cls.app.config["MEDIA_BASE_DIR"] = cls.media_base_dir

tests/test_endpoints/test_post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def setUpClass(cls):
6666
dbpath, _ = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
6767
tree = os.path.basename(dbpath)
6868
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
69-
cls.app = create_app()
69+
cls.app = create_app(config_from_env=False)
7070
cls.app.config["TESTING"] = True
7171
cls.client = cls.app.test_client()
7272
with cls.app.app_context():

0 commit comments

Comments
 (0)