tests: replace make_config with example_config, add default config params

This commit is contained in:
missytake
2023-12-12 15:14:52 +01:00
parent 74f9e7536b
commit 98fd4b61c9
3 changed files with 54 additions and 49 deletions

View File

@@ -1,27 +1,31 @@
from chatmaild.config import read_config from chatmaild.config import read_config
def test_read_config_basic(make_config): def test_read_config_basic(example_config):
config = make_config("chat.example.org") assert example_config.mail_domain == "chat.example.org"
assert config.mail_domain == "chat.example.org" assert not example_config.privacy_supervisor and not example_config.privacy_mail
assert not config.privacy_supervisor and not config.privacy_mail assert not example_config.privacy_pdo and not example_config.privacy_postal
assert not config.privacy_pdo and not config.privacy_postal
inipath = config._inipath inipath = example_config._inipath
inipath.write_text(inipath.read_text().replace("60", "37")) inipath.write_text(inipath.read_text().replace("60", "37"))
config = read_config(inipath) example_config = read_config(inipath)
assert config.max_user_send_per_minute == 37 assert example_config.max_user_send_per_minute == 37
assert config.mail_domain == "chat.example.org" assert example_config.mail_domain == "chat.example.org"
def test_read_config_testrun(make_config): def test_read_config_testrun(example_config):
config = make_config("something.testrun.org") assert example_config.mail_domain == "something.testrun.org"
assert config.mail_domain == "something.testrun.org" assert len(example_config.privacy_postal.split("\n")) > 1
assert len(config.privacy_postal.split("\n")) > 1 assert len(example_config.privacy_supervisor.split("\n")) > 1
assert len(config.privacy_supervisor.split("\n")) > 1 assert len(example_config.privacy_pdo.split("\n")) > 1
assert len(config.privacy_pdo.split("\n")) > 1 assert example_config.privacy_mail == "privacy@testrun.org"
assert config.privacy_mail == "privacy@testrun.org" assert example_config.filtermail_smtp_port == 10080
assert config.filtermail_smtp_port == 10080 assert example_config.postfix_reinject_port == 10025
assert config.postfix_reinject_port == 10025 assert example_config.max_user_send_per_minute == 60
assert config.max_user_send_per_minute == 60 assert example_config.max_mailbox_size == "100M"
assert config.passthrough_recipients assert example_config.delete_mails_after == "40d"
assert example_config.username_min_length == 6
assert example_config.username_max_length == 20
assert example_config.password_min_length == 9
assert example_config.passthrough_senders
assert example_config.passthrough_recipients

View File

@@ -9,32 +9,37 @@ from chatmaild.doveauth import get_user_data, lookup_passdb, handle_dovecot_requ
from chatmaild.database import DBError from chatmaild.database import DBError
def test_basic(db, make_config): def test_basic(db, example_config):
config = make_config("c1.testrun.org") lookup_passdb(
lookup_passdb(db, config, "link2xt@c1.testrun.org", "Pieg9aeToe3eghuthe5u") db, example_config, "link2xt@chat.example.org", "Pieg9aeToe3eghuthe5u"
data = get_user_data(db, "link2xt@c1.testrun.org") )
data = get_user_data(db, "link2xt@chat.example.org")
assert data assert data
data2 = lookup_passdb(db, config, "link2xt@c1.testrun.org", "Pieg9aeToe3eghuthe5u") data2 = lookup_passdb(
db, example_config, "link2xt@chat.example.org", "Pieg9aeToe3eghuthe5u"
)
assert data == data2 assert data == data2
def test_dont_overwrite_password_on_wrong_login(db, make_config): def test_dont_overwrite_password_on_wrong_login(db, example_config):
"""Test that logging in with a different password doesn't create a new user""" """Test that logging in with a different password doesn't create a new user"""
config = make_config("something.org") res = lookup_passdb(
res = lookup_passdb(db, config, "newuser1@something.org", "kajdlkajsldk12l3kj1983") db, example_config, "newuser1@chat.example.org", "kajdlkajsldk12l3kj1983"
)
assert res["password"] assert res["password"]
res2 = lookup_passdb(db, config, "newuser1@something.org", "kajdlqweqwe") res2 = lookup_passdb(db, example_config, "newuser1@chat.example.org", "kajdlqweqwe")
# this function always returns a password hash, which is actually compared by dovecot. # this function always returns a password hash, which is actually compared by dovecot.
assert res["password"] == res2["password"] assert res["password"] == res2["password"]
def test_nocreate_file(db, monkeypatch, tmpdir, make_config): def test_nocreate_file(db, monkeypatch, tmpdir, example_config):
config = make_config("something.org")
p = tmpdir.join("nocreate") p = tmpdir.join("nocreate")
p.write("") p.write("")
monkeypatch.setattr(chatmaild.doveauth, "NOCREATE_FILE", str(p)) monkeypatch.setattr(chatmaild.doveauth, "NOCREATE_FILE", str(p))
lookup_passdb(db, config, "newuser1@something.org", "zequ0Aimuchoodaechik") lookup_passdb(
assert not get_user_data(db, "newuser1@something.org") db, example_config, "newuser1@chat.example.org", "zequ0Aimuchoodaechik"
)
assert not get_user_data(db, "newuser1@chat.example.org")
def test_db_version(db): def test_db_version(db):
@@ -48,34 +53,32 @@ def test_too_high_db_version(db):
db.ensure_tables() db.ensure_tables()
def test_handle_dovecot_request(db, make_config): def test_handle_dovecot_request(db, example_config):
config = make_config("c3.testrun.org")
msg = ( msg = (
"Lshared/passdb/laksjdlaksjdlaksjdlk12j3l1k2j3123/" "Lshared/passdb/laksjdlaksjdlaksjdlk12j3l1k2j3123/"
"some42@c3.testrun.org\tsome42@c3.testrun.org" "some42@chat.example.org\tsome42@chat.example.org"
) )
res = handle_dovecot_request(msg, db, config) res = handle_dovecot_request(msg, db, example_config)
assert res assert res
assert res[0] == "O" and res.endswith("\n") assert res[0] == "O" and res.endswith("\n")
userdata = json.loads(res[1:].strip()) userdata = json.loads(res[1:].strip())
assert userdata["home"] == "/home/vmail/some42@c3.testrun.org" assert userdata["home"] == "/home/vmail/some42@chat.example.org"
assert userdata["uid"] == userdata["gid"] == "vmail" assert userdata["uid"] == userdata["gid"] == "vmail"
assert userdata["password"].startswith("{SHA512-CRYPT}") assert userdata["password"].startswith("{SHA512-CRYPT}")
def test_50_concurrent_lookups_different_accounts( def test_50_concurrent_lookups_different_accounts(
db, gencreds, make_config, maildomain db, gencreds, example_config, maildomain
): ):
num_threads = 50 num_threads = 50
req_per_thread = 5 req_per_thread = 5
results = queue.Queue() results = queue.Queue()
config = make_config(maildomain)
def lookup(db): def lookup(db):
for i in range(req_per_thread): for i in range(req_per_thread):
addr, password = gencreds() addr, password = gencreds()
try: try:
lookup_passdb(db, config, addr, password) lookup_passdb(db, example_config, addr, password)
except Exception: except Exception:
results.put(traceback.format_exc()) results.put(traceback.format_exc())
else: else:

View File

@@ -4,26 +4,24 @@ import chatmaild
from chatmaild.newemail import create_newemail_dict, print_new_account from chatmaild.newemail import create_newemail_dict, print_new_account
def test_create_newemail_dict(make_config): def test_create_newemail_dict(example_config):
config = make_config("example.org") ac1 = create_newemail_dict(example_config)
ac1 = create_newemail_dict(config)
assert "@" in ac1["email"] assert "@" in ac1["email"]
assert len(ac1["password"]) >= 10 assert len(ac1["password"]) >= 10
ac2 = create_newemail_dict(config) ac2 = create_newemail_dict(example_config)
assert ac1["email"] != ac2["email"] assert ac1["email"] != ac2["email"]
assert ac1["password"] != ac2["password"] assert ac1["password"] != ac2["password"]
def test_print_new_account(capsys, monkeypatch, maildomain, tmpdir, make_config): def test_print_new_account(capsys, monkeypatch, maildomain, tmpdir, example_config):
config = make_config(maildomain) monkeypatch.setattr(chatmaild.newemail, "CONFIG_PATH", str(example_config._inipath))
monkeypatch.setattr(chatmaild.newemail, "CONFIG_PATH", str(config._inipath))
print_new_account() print_new_account()
out, err = capsys.readouterr() out, err = capsys.readouterr()
lines = out.split("\n") lines = out.split("\n")
assert lines[0] == "Content-Type: application/json" assert lines[0] == "Content-Type: application/json"
assert not lines[1] assert not lines[1]
dic = json.loads(lines[2]) dic = json.loads(lines[2])
assert dic["email"].endswith(f"@{config.mail_domain}") assert dic["email"].endswith(f"@{example_config.mail_domain}")
assert len(dic["password"]) >= 10 assert len(dic["password"]) >= 10