diff --git a/backend/app/routers/system.py b/backend/app/routers/system.py index 294c7e7..b2cac38 100644 --- a/backend/app/routers/system.py +++ b/backend/app/routers/system.py @@ -354,7 +354,7 @@ def test_jira_connection( """ from app.services.jira_service import ( get_admin_jira_client, get_jira_url, has_admin_jira_configured, - get_admin_jira_email, + get_admin_jira_email, get_admin_jira_api_token, is_jira_enabled, ) jira_url = get_jira_url(db) @@ -362,12 +362,21 @@ def test_jira_connection( return {"status": "error", "message": "Jira URL is not configured.", "jira_url": ""} if not has_admin_jira_configured(db): + # has_admin_jira_configured() bundles 4 independent checks into one + # bool — report exactly which one(s) are actually missing instead of + # always blaming "Admin Email and Admin API Token" regardless of + # which check failed (e.g. URL/email/token can all be set while the + # "Enable Jira Integration" toggle itself is still off). + missing = [] + if not is_jira_enabled(db): + missing.append("Jira integration is not enabled (toggle 'Enable Jira Integration' above)") + if not get_admin_jira_email(db): + missing.append("Admin Email is not set") + if not get_admin_jira_api_token(db): + missing.append("Admin API Token is not set") return { "status": "error", - "message": ( - "Admin Jira credentials not configured. " - "Set the Admin Email and Admin API Token in the fields above." - ), + "message": "Cannot connect: " + "; ".join(missing) + ".", "jira_url": jira_url, } diff --git a/backend/tests/test_jira_test_connection.py b/backend/tests/test_jira_test_connection.py new file mode 100644 index 0000000..a578b6a --- /dev/null +++ b/backend/tests/test_jira_test_connection.py @@ -0,0 +1,54 @@ +"""Tests for POST /system/jira-test's diagnostic message. + +Regression coverage for a real QA-reported bug: has_admin_jira_configured() +bundles four independent checks (enabled, url, admin_email, admin_api_token) +into one bool, but the error message always blamed "Admin Email and Admin +API Token" regardless of which check actually failed. An admin who had set +url/email/token correctly but never flipped the "Enable Jira Integration" +toggle saw a misleading message pointing at the wrong fields. +""" + +from app.models.system_config import SystemConfig + + +def _set_config(db, key, value): + db.add(SystemConfig(key=key, value=value)) + + +def test_jira_test_reports_disabled_toggle_specifically(client, db, auth_headers): + """url/email/token all set, but jira.enabled is missing entirely — + the message must call out the toggle, not email/token.""" + _set_config(db, "jira.url", "https://example.atlassian.net") + _set_config(db, "jira.admin_email", "admin@example.com") + _set_config(db, "jira.admin_api_token", "some-real-token") + db.commit() + + resp = client.post("/api/v1/system/jira-test", headers=auth_headers) + assert resp.status_code == 200 + body = resp.json() + assert body["status"] == "error" + assert "not enabled" in body["message"] + assert "Admin Email" not in body["message"] + assert "Admin API Token" not in body["message"] + + +def test_jira_test_reports_missing_email_and_token_when_actually_missing(client, db, auth_headers): + _set_config(db, "jira.url", "https://example.atlassian.net") + _set_config(db, "jira.enabled", "true") + db.commit() + + resp = client.post("/api/v1/system/jira-test", headers=auth_headers) + assert resp.status_code == 200 + body = resp.json() + assert body["status"] == "error" + assert "Admin Email is not set" in body["message"] + assert "Admin API Token is not set" in body["message"] + assert "not enabled" not in body["message"] + + +def test_jira_test_missing_url_reports_url_specifically(client, db, auth_headers): + resp = client.post("/api/v1/system/jira-test", headers=auth_headers) + assert resp.status_code == 200 + body = resp.json() + assert body["status"] == "error" + assert "Jira URL is not configured" in body["message"]