canaille-globuzma/tests/app/test_flaskutils.py

144 lines
3.8 KiB
Python
Raw Normal View History

2022-12-16 21:14:46 +00:00
import os
2022-12-16 21:33:09 +00:00
import pytest
import toml
from flask_webtest import TestApp
2022-12-16 21:14:46 +00:00
from canaille import create_app
2023-04-09 13:52:55 +00:00
from canaille.app.flask import set_parameter_in_url_query
def test_set_parameter_in_url_query():
assert (
set_parameter_in_url_query("https://auth.mydomain.tld", foo="bar")
== "https://auth.mydomain.tld?foo=bar"
)
assert (
set_parameter_in_url_query("https://auth.mydomain.tld?foo=baz", foo="bar")
== "https://auth.mydomain.tld?foo=bar"
)
assert (
set_parameter_in_url_query("https://auth.mydomain.tld?foo=baz", hello="world")
== "https://auth.mydomain.tld?foo=baz&hello=world"
)
2022-12-16 21:14:46 +00:00
def test_environment_configuration(configuration, tmp_path):
2022-12-16 21:33:09 +00:00
config_path = os.path.join(tmp_path, "config.toml")
with open(config_path, "w") as fd:
toml.dump(configuration, fd)
os.environ["CONFIG"] = config_path
app = create_app()
assert app.config["SMTP"]["FROM_ADDR"] == "admin@mydomain.tld"
2022-12-16 21:33:09 +00:00
del os.environ["CONFIG"]
os.remove(config_path)
def test_no_configuration():
with pytest.raises(Exception) as exc:
create_app()
2022-12-16 21:33:09 +00:00
assert "No configuration file found." in str(exc)
def test_file_log_config(configuration, backend, tmp_path, smtpd, admin):
assert len(smtpd.messages) == 0
log_path = os.path.join(tmp_path, "canaille-by-file.log")
file_content = LOGGING_CONF_FILE_CONTENT.format(log_path=log_path)
config_file_path = tmp_path / "logging.conf"
with open(config_file_path, "w") as fd:
fd.write(file_content)
logging_configuration = {**configuration, "LOGGING": config_file_path}
app = create_app(logging_configuration, backend=backend)
testclient = TestApp(app)
with testclient.session_transaction() as sess:
sess["user_id"] = [admin.id]
res = testclient.get("/admin/mail")
res.form["email"] = "test@test.com"
res = res.form.submit()
assert len(smtpd.messages) == 1
assert "Test email from" in smtpd.messages[0].get("Subject")
with open(log_path) as fd:
log_content = fd.read()
assert "Sending a mail to test@test.com: Test email from" in log_content
def test_dict_log_config(configuration, backend, tmp_path, smtpd, admin):
assert len(smtpd.messages) == 0
log_path = os.path.join(tmp_path, "canaille-by-dict.log")
2022-12-16 21:14:46 +00:00
logging_configuration = {
**configuration,
"LOGGING": {
"version": 1,
"formatters": {
"default": {
"format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
}
},
"handlers": {
"wsgi": {
"class": "logging.handlers.WatchedFileHandler",
"filename": log_path,
"formatter": "default",
}
},
"root": {"level": "DEBUG", "handlers": ["wsgi"]},
"loggers": {
"faker": {"level": "WARNING"},
},
"disable_existing_loggers": False,
},
2022-12-16 21:14:46 +00:00
}
app = create_app(logging_configuration, backend=backend)
2022-12-16 21:14:46 +00:00
2023-05-20 14:42:35 +00:00
testclient = TestApp(app)
with testclient.session_transaction() as sess:
sess["user_id"] = [admin.id]
res = testclient.get("/admin/mail")
res.form["email"] = "test@test.com"
res = res.form.submit()
2022-12-16 21:14:46 +00:00
assert len(smtpd.messages) == 1
2023-01-22 11:48:15 +00:00
assert "Test email from" in smtpd.messages[0].get("Subject")
2022-12-16 21:14:46 +00:00
with open(log_path) as fd:
log_content = fd.read()
2023-01-22 11:48:15 +00:00
assert "Sending a mail to test@test.com: Test email from" in log_content
LOGGING_CONF_FILE_CONTENT = """
[loggers]
keys=root
[handlers]
keys=wsgi
[formatters]
keys=default
[logger_root]
level=DEBUG
handlers=wsgi
[handler_wsgi]
class=logging.handlers.WatchedFileHandler
args=('{log_path}',)
formatter=default
[formatter_default]
format=[%(asctime)s] %(levelname)s in %(module)s: %(message)s
"""