test_web_update.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """Tests for the web application update flow."""
  2. from unittest.mock import Mock
  3. from routes import main_routes
  4. from utils import updateSquidStats
  5. def _dashboard_context():
  6. return {
  7. "grouped_connections": {},
  8. "valid_users": {},
  9. "squid_version": "test",
  10. "squid_info_stats": {},
  11. "page_icon": "favicon.ico",
  12. "page_title": "Test dashboard",
  13. "build_time_ms": 0,
  14. "connection_count": 0,
  15. "system_type": "test",
  16. "proxy_statuses": [],
  17. "multi_proxy": False,
  18. }
  19. def test_dashboard_renders_web_update_button(client, monkeypatch):
  20. """The desktop web update button and its update handler are present."""
  21. monkeypatch.setattr(
  22. main_routes, "_get_dashboard_context", lambda: (_dashboard_context(), None)
  23. )
  24. client.application.jinja_env.globals["csrf_token"] = lambda: "test-token"
  25. response = client.get("/")
  26. assert response.status_code == 200
  27. html = response.get_data(as_text=True)
  28. assert 'id="web-update-btn"' in html
  29. assert 'onclick="confirmWebUpdate()"' in html
  30. assert "fetch('/update'" in html
  31. assert "const restartMessage" in html
  32. assert "toastr.success(restartMessage)" in html
  33. assert "window.location.reload(), 2500" in html
  34. def test_update_status_returns_web_update_state(client, monkeypatch):
  35. """The endpoint used by the button exposes the latest web version state."""
  36. monkeypatch.setattr(
  37. main_routes,
  38. "check_squid_update",
  39. lambda: {"available": False, "current": "6.0", "latest": None},
  40. )
  41. web_status = {
  42. "available": True,
  43. "current": "2.6.1",
  44. "latest": "2.7.0",
  45. "error": None,
  46. }
  47. monkeypatch.setattr(main_routes, "check_web_update", lambda _version: web_status)
  48. main_routes._update_status_cache = {"data": None, "timestamp": 0}
  49. response = client.get("/api/update-status")
  50. assert response.status_code == 200
  51. assert response.get_json()["web"] == web_status
  52. def test_update_web_redirects_with_success_flash(client, monkeypatch):
  53. """A successful update delegates to the updater and reports success."""
  54. updater = Mock(return_value=True)
  55. monkeypatch.setattr(main_routes, "updateSquidStats", updater)
  56. response = client.post("/update")
  57. assert response.status_code == 302
  58. assert response.headers["Location"].endswith("/")
  59. updater.assert_called_once_with()
  60. with client.session_transaction() as session:
  61. assert any(
  62. category == "success" and "actualizado correctamente" in message
  63. for category, message in session["_flashes"]
  64. )
  65. def test_update_web_redirects_with_error_flash(client, monkeypatch):
  66. """A failed update reports an error instead of claiming success."""
  67. updater = Mock(return_value=False)
  68. monkeypatch.setattr(main_routes, "updateSquidStats", updater)
  69. response = client.post("/update")
  70. assert response.status_code == 302
  71. updater.assert_called_once_with()
  72. with client.session_transaction() as session:
  73. assert any(
  74. category == "error" and "Error al actualizar SquidStats" in message
  75. for category, message in session["_flashes"]
  76. )
  77. def test_update_web_handles_updater_exception(client, monkeypatch):
  78. """An updater exception is converted into an error flash and redirect."""
  79. updater = Mock(side_effect=RuntimeError("update failed"))
  80. monkeypatch.setattr(main_routes, "updateSquidStats", updater)
  81. response = client.post("/update")
  82. assert response.status_code == 302
  83. updater.assert_called_once_with()
  84. with client.session_transaction() as session:
  85. assert any(
  86. category == "error" and "Error inesperado" in message
  87. for category, message in session["_flashes"]
  88. )
  89. def test_check_web_update_detects_newer_release(monkeypatch):
  90. """A newer GitHub release is reported as available."""
  91. response = Mock()
  92. response.json.return_value = {"tag_name": "v2.7.0"}
  93. response.raise_for_status.return_value = None
  94. monkeypatch.setattr(
  95. updateSquidStats.requests, "get", lambda *args, **kwargs: response
  96. )
  97. monkeypatch.setattr(
  98. updateSquidStats,
  99. "_WEB_UPDATE_CACHE",
  100. {"data": None, "timestamp": 0},
  101. )
  102. result = updateSquidStats.check_web_update("2.6.1", force_refresh=True)
  103. assert result == {
  104. "available": True,
  105. "current": "2.6.1",
  106. "latest": "v2.7.0",
  107. "error": None,
  108. }
  109. def test_check_web_update_handles_github_failure(monkeypatch):
  110. """A GitHub failure hides the indicator and exposes an error state."""
  111. def fail_request(*args, **kwargs):
  112. raise RuntimeError("GitHub unavailable")
  113. monkeypatch.setattr(updateSquidStats.requests, "get", fail_request)
  114. monkeypatch.setattr(
  115. updateSquidStats,
  116. "_WEB_UPDATE_CACHE",
  117. {"data": None, "timestamp": 0},
  118. )
  119. result = updateSquidStats.check_web_update("2.6.1", force_refresh=True)
  120. assert result["available"] is False
  121. assert result["current"] == "2.6.1"
  122. assert result["latest"] is None
  123. assert result["error"] == "No se pudo verificar la actualización de SquidStats"