from tests.unit.app.app_route_helpers import * def test_nextcloud_mail_sync(monkeypatch) -> None: ctx = AuthContext(username="alice", email="", groups=["dev"], claims={}) client = _client(monkeypatch, ctx) monkeypatch.setattr(app_module.keycloak_admin, "ready", lambda: True) monkeypatch.setattr(app_module.nextcloud, "sync_mail", lambda *args, **kwargs: {"status": "ok"}) resp = client.post( "/api/account/nextcloud/mail/sync", headers={"Authorization": "Bearer token"}, json={"wait": True}, ) assert resp.status_code == 200 payload = resp.json() assert payload["status"] == "ok" def test_nextcloud_mail_sync_error(monkeypatch) -> None: ctx = AuthContext(username="alice", email="", groups=["dev"], claims={}) client = _client(monkeypatch, ctx) monkeypatch.setattr(app_module.keycloak_admin, "ready", lambda: True) monkeypatch.setattr(app_module.nextcloud, "sync_mail", lambda *args, **kwargs: (_ for _ in ()).throw(RuntimeError("fail"))) resp = client.post( "/api/account/nextcloud/mail/sync", headers={"Authorization": "Bearer token"}, json={"wait": True}, ) assert resp.status_code == 502 def test_nextcloud_mail_sync_bad_json(monkeypatch) -> None: ctx = AuthContext(username="alice", email="", groups=["dev"], claims={}) client = _client(monkeypatch, ctx) monkeypatch.setattr(app_module.keycloak_admin, "ready", lambda: True) monkeypatch.setattr(app_module.nextcloud, "sync_mail", lambda *args, **kwargs: {"status": "ok"}) resp = client.post( "/api/account/nextcloud/mail/sync", headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, data="{bad}", ) assert resp.status_code == 200 def test_nextcloud_mail_sync_unconfigured(monkeypatch) -> None: ctx = AuthContext(username="alice", email="", groups=["dev"], claims={}) client = _client(monkeypatch, ctx) monkeypatch.setattr(app_module.keycloak_admin, "ready", lambda: False) resp = client.post( "/api/account/nextcloud/mail/sync", headers={"Authorization": "Bearer token"}, json={"wait": True}, ) assert resp.status_code == 503 def test_nextcloud_mail_sync_missing_username(monkeypatch) -> None: ctx = AuthContext(username="", email="", groups=["dev"], claims={}) client = _client(monkeypatch, ctx) monkeypatch.setattr(app_module.keycloak_admin, "ready", lambda: True) resp = client.post( "/api/account/nextcloud/mail/sync", headers={"Authorization": "Bearer token"}, json={"wait": True}, ) assert resp.status_code == 400 def test_nextcloud_mail_sync_http_exception(monkeypatch) -> None: ctx = AuthContext(username="alice", email="", groups=["dev"], claims={}) client = _client(monkeypatch, ctx) monkeypatch.setattr(app_module.keycloak_admin, "ready", lambda: True) monkeypatch.setattr( app_module.nextcloud, "sync_mail", lambda *args, **kwargs: (_ for _ in ()).throw(HTTPException(status_code=409, detail="conflict")), ) resp = client.post( "/api/account/nextcloud/mail/sync", headers={"Authorization": "Bearer token"}, json={"wait": True}, ) assert resp.status_code == 409 def test_nextcloud_mail_sync_handles_storage_error(monkeypatch) -> None: ctx = AuthContext(username="alice", email="", groups=["dev"], claims={}) client = _client(monkeypatch, ctx) monkeypatch.setattr(app_module.keycloak_admin, "ready", lambda: True) monkeypatch.setattr(app_module.nextcloud, "sync_mail", lambda *args, **kwargs: {"status": "ok"}) monkeypatch.setattr( app_module.storage, "record_task_run", lambda *args, **kwargs: (_ for _ in ()).throw(RuntimeError("fail")), ) resp = client.post( "/api/account/nextcloud/mail/sync", headers={"Authorization": "Bearer token"}, json={"wait": True}, ) assert resp.status_code == 200