From 198d1de3a78317d846c52627f53f7e2bceb53501 Mon Sep 17 00:00:00 2001 From: ErikKalkoken Date: Fri, 24 Jul 2026 13:59:29 +0200 Subject: [PATCH] fix: keeps trying to sync when character is no longer valid --- moonmining/models/owners.py | 26 +++++++++++++++++++++++--- moonmining/tests/models/test_owners.py | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/moonmining/models/owners.py b/moonmining/models/owners.py index 6295755..0f2c0d1 100644 --- a/moonmining/models/owners.py +++ b/moonmining/models/owners.py @@ -2,6 +2,7 @@ import datetime as dt from collections import defaultdict +from http import HTTPStatus from typing import Iterable, List, Optional, Set, Tuple import yaml @@ -11,14 +12,14 @@ from django.utils.html import format_html from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ -from esi.exceptions import HTTPError +from esi.exceptions import HTTPClientError, HTTPError from esi.models import Token from eveuniverse.models import EveEntity, EveMoon, EveSolarSystem, EveType from allianceauth.authentication.models import CharacterOwnership from allianceauth.eveonline.models import EveCorporationInfo from allianceauth.services.hooks import get_extension_logger -from app_utils.allianceauth import notify_admins_throttled +from app_utils.allianceauth import notify_admins, notify_admins_throttled from app_utils.views import bootstrap_icon_plus_name_html from moonmining import __title__ @@ -164,7 +165,26 @@ def fetch_token(self) -> Token: def update_refineries_from_esi(self): """Update all refineries from ESI.""" logger.info("%s: Updating refineries...", self) - refinery_ids = self._fetch_refineries_from_esi() + try: + refinery_ids = self._fetch_refineries_from_esi() + except HTTPClientError as exc: + if exc.status_code == HTTPStatus.FORBIDDEN: + self.is_enabled = False + self.last_update_ok = False + self.save() + try: + character = str(self.character_ownership.character) + except AttributeError: + character = "?" + message = ( + f"Owner disabled: {self}\n" + f"Sync character is no longer valid: {character}" + f"Response from ESI: {exc}" + ) + title = "Moon Mining: Owner disabled" + notify_admins(title=title, message=message) + raise exc + for refinery_id in refinery_ids: try: self._update_or_create_refinery_from_esi(refinery_id) diff --git a/moonmining/tests/models/test_owners.py b/moonmining/tests/models/test_owners.py index 36fcac0..b4b2d9b 100644 --- a/moonmining/tests/models/test_owners.py +++ b/moonmining/tests/models/test_owners.py @@ -409,6 +409,30 @@ def test_should_continue_with_other_refineries_after_http_error( structure_2.refresh_from_db() self.assertEqual(structure_2.name, structure_2_name) + @pook.on + def test_should_abort_when_sync_character_invalid( + self, mock_nearest_celestial: Mock + ): + # given + owner = OwnerFactory() + corporation_id = owner.corporation.corporation_id + pook.get( + make_esi_url(f"corporations/{corporation_id}/structures"), + reply=403, + response_json={"error": "Character is not in corporation"}, + ) + + # when + with patch(MODELS_PATH + ".owners.notify_admins") as notify: + with self.assertRaises(HTTPError): + owner.update_refineries_from_esi() + + # then + owner.refresh_from_db() + self.assertFalse(owner.is_enabled) + self.assertFalse(owner.last_update_ok) + self.assertTrue(notify.called) + class TestOwner_UpdateExtractions(helpers.TestCaseWithClearCache): @pook.on