|
| 1 | +"""Functional tests for tls_external_cert_and_key option.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import chatmaild.newemail |
| 6 | +import pytest |
| 7 | +from chatmaild.config import read_config, write_initial_config |
| 8 | + |
| 9 | + |
| 10 | +def test_external_tls_config_reads_paths(tmp_path): |
| 11 | + """Config correctly reads explicit cert/key paths.""" |
| 12 | + inipath = tmp_path / "chatmail.ini" |
| 13 | + write_initial_config( |
| 14 | + inipath, |
| 15 | + "chat.example.org", |
| 16 | + overrides={ |
| 17 | + "tls_external_cert_and_key": ( |
| 18 | + "/etc/letsencrypt/live/chat.example.org/fullchain.pem" |
| 19 | + " /etc/letsencrypt/live/chat.example.org/privkey.pem" |
| 20 | + ), |
| 21 | + }, |
| 22 | + ) |
| 23 | + config = read_config(inipath) |
| 24 | + assert config.tls_cert_mode == "external" |
| 25 | + assert ( |
| 26 | + config.tls_cert_path == "/etc/letsencrypt/live/chat.example.org/fullchain.pem" |
| 27 | + ) |
| 28 | + assert config.tls_key_path == "/etc/letsencrypt/live/chat.example.org/privkey.pem" |
| 29 | + |
| 30 | + |
| 31 | +def test_external_tls_missing_option_uses_acme(tmp_path): |
| 32 | + """Without tls_external_cert_and_key, normal domain uses acme.""" |
| 33 | + inipath = tmp_path / "chatmail.ini" |
| 34 | + write_initial_config(inipath, "chat.example.org", overrides={}) |
| 35 | + config = read_config(inipath) |
| 36 | + assert config.tls_cert_mode == "acme" |
| 37 | + |
| 38 | + |
| 39 | +def test_external_tls_overrides_domain_detection(tmp_path): |
| 40 | + """tls_external_cert_and_key overrides underscore-domain self-signed detection.""" |
| 41 | + inipath = tmp_path / "chatmail.ini" |
| 42 | + write_initial_config( |
| 43 | + inipath, |
| 44 | + "_test.example.org", |
| 45 | + overrides={ |
| 46 | + "tls_external_cert_and_key": "/certs/fullchain.pem /certs/privkey.pem", |
| 47 | + }, |
| 48 | + ) |
| 49 | + config = read_config(inipath) |
| 50 | + assert config.tls_cert_mode == "external" |
| 51 | + assert config.tls_cert_path == "/certs/fullchain.pem" |
| 52 | + assert config.tls_key_path == "/certs/privkey.pem" |
| 53 | + |
| 54 | + |
| 55 | +def test_external_tls_bad_format_raises(tmp_path): |
| 56 | + """Raises ValueError if not exactly two space-separated paths.""" |
| 57 | + inipath = tmp_path / "chatmail.ini" |
| 58 | + write_initial_config( |
| 59 | + inipath, |
| 60 | + "chat.example.org", |
| 61 | + overrides={ |
| 62 | + "tls_external_cert_and_key": "/only/one/path.pem", |
| 63 | + }, |
| 64 | + ) |
| 65 | + with pytest.raises(ValueError, match="two space-separated"): |
| 66 | + read_config(inipath) |
| 67 | + |
| 68 | + |
| 69 | +def test_external_tls_three_paths_raises(tmp_path): |
| 70 | + """Raises ValueError if three paths given.""" |
| 71 | + inipath = tmp_path / "chatmail.ini" |
| 72 | + write_initial_config( |
| 73 | + inipath, |
| 74 | + "chat.example.org", |
| 75 | + overrides={ |
| 76 | + "tls_external_cert_and_key": "/a /b /c", |
| 77 | + }, |
| 78 | + ) |
| 79 | + with pytest.raises(ValueError, match="two space-separated"): |
| 80 | + read_config(inipath) |
| 81 | + |
| 82 | + |
| 83 | +def test_external_tls_no_dclogin_url(tmp_path, capsys, monkeypatch): |
| 84 | + """External mode should not generate dclogin URLs (real certs, not self-signed).""" |
| 85 | + inipath = tmp_path / "chatmail.ini" |
| 86 | + write_initial_config( |
| 87 | + inipath, |
| 88 | + "chat.example.org", |
| 89 | + overrides={ |
| 90 | + "tls_external_cert_and_key": "/certs/fullchain.pem /certs/privkey.pem", |
| 91 | + }, |
| 92 | + ) |
| 93 | + monkeypatch.setattr(chatmaild.newemail, "CONFIG_PATH", str(inipath)) |
| 94 | + chatmaild.newemail.print_new_account() |
| 95 | + out, _ = capsys.readouterr() |
| 96 | + lines = out.split("\n") |
| 97 | + dic = json.loads(lines[2]) |
| 98 | + assert "dclogin_url" not in dic |
0 commit comments