2022-12-16 21:14:46 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import ldap
|
2022-12-16 21:33:09 +00:00
|
|
|
import pytest
|
|
|
|
import toml
|
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
|
2022-12-16 21:14:46 +00:00
|
|
|
from flask import g
|
|
|
|
from flask_webtest import TestApp
|
2022-12-11 21:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2023-04-10 21:08:09 +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()
|
2023-04-10 21:08:09 +00:00
|
|
|
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:
|
2022-12-28 23:41:32 +00:00
|
|
|
create_app()
|
2022-12-16 21:33:09 +00:00
|
|
|
|
|
|
|
assert "No configuration file found." in str(exc)
|
|
|
|
|
|
|
|
|
2022-12-16 21:14:46 +00:00
|
|
|
def test_logging_to_file(configuration, tmp_path, smtpd, admin, slapd_server):
|
2022-12-21 20:52:01 +00:00
|
|
|
assert len(smtpd.messages) == 0
|
2022-12-16 21:14:46 +00:00
|
|
|
log_path = os.path.join(tmp_path, "canaille.log")
|
|
|
|
logging_configuration = {
|
|
|
|
**configuration,
|
|
|
|
"LOGGING": {"LEVEL": "DEBUG", "PATH": log_path},
|
|
|
|
}
|
|
|
|
app = create_app(logging_configuration)
|
|
|
|
app.config["TESTING"] = True
|
|
|
|
|
|
|
|
with app.app_context():
|
2023-03-07 13:37:07 +00:00
|
|
|
g.ldap_connection = ldap.ldapobject.SimpleLDAPObject(slapd_server.ldap_uri)
|
|
|
|
g.ldap_connection.protocol_version = 3
|
|
|
|
g.ldap_connection.simple_bind_s(slapd_server.root_dn, slapd_server.root_pw)
|
2022-12-16 21:14:46 +00:00
|
|
|
|
|
|
|
testclient = TestApp(app)
|
|
|
|
with testclient.session_transaction() as sess:
|
2023-02-05 18:08:25 +00:00
|
|
|
sess["user_id"] = [admin.id]
|
2022-12-16 21:14:46 +00:00
|
|
|
|
|
|
|
res = testclient.get("/admin/mail")
|
2023-02-05 17:57:18 +00:00
|
|
|
res.form["email"] = "test@test.com"
|
2022-12-16 21:14:46 +00:00
|
|
|
res = res.form.submit()
|
|
|
|
|
2023-03-07 13:37:07 +00:00
|
|
|
g.ldap_connection.unbind_s()
|
2022-12-16 21:14:46 +00:00
|
|
|
|
2022-12-21 20:52:01 +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-21 20:52:01 +00:00
|
|
|
|
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
|