Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions moonmining/models/owners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions moonmining/tests/models/test_owners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading