"""/api/me carries whether this instance can actually send email. It drives the operator warnings on the Configuration and Access-requests screens. Admin-only, because it is an operator concern and a regular user has nothing to do with it. """ from app import email as email_mod _PW = "correct horse battery" def _signed_in(client, make_user, **kw): """/api/me resolves its user through optional_current_user, which the as_user override does not cover — so sign in for real and read the bootstrap payload the SPA actually gets.""" user = make_user(password=_PW, **kw) assert ( client.post("/auth/password-login", json={"email": user.email, "password": _PW}).status_code == 200 ) r = client.get("/api/me") assert r.status_code == 200 and r.json()["authenticated"] is True return r.json() def test_an_admin_is_told_when_email_is_not_configured(client, make_user, monkeypatch): monkeypatch.setattr(email_mod, "email_enabled", lambda db=None: False) me = _signed_in(client, make_user, email="admin@example.com", role="admin") assert me["email_configured"] is False def test_an_admin_sees_nothing_wrong_when_email_works(client, make_user, monkeypatch): monkeypatch.setattr(email_mod, "email_enabled", lambda db=None: True) me = _signed_in(client, make_user, email="admin@example.com", role="admin") assert me["email_configured"] is True def test_a_regular_user_is_never_warned_about_the_mail_setup(client, make_user, monkeypatch): # Not their problem, and the flag must not become a way to probe the instance's configuration. monkeypatch.setattr(email_mod, "email_enabled", lambda db=None: False) me = _signed_in(client, make_user, email="plain@example.com") assert me["email_configured"] is True def test_the_flag_reuses_the_request_session(client, make_user, monkeypatch): # /api/me is polled once a minute per open tab; reading the SMTP settings decrypts a Fernet # value, so it must not also check out a second pooled connection each time. seen: list[object] = [] monkeypatch.setattr(email_mod, "email_enabled", lambda db=None: seen.append(db) or True) _signed_in(client, make_user, email="admin@example.com", role="admin") assert seen and seen[0] is not None