Add logging for changed slug in devhub & API (#21221)

This commit is contained in:
Greg Sherman 2023-10-02 04:36:03 -04:00 коммит произвёл GitHub
Родитель 12b36475af
Коммит 3fec0feffb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 43 добавлений и 0 удалений

Просмотреть файл

@ -1216,6 +1216,9 @@ class AddonSerializer(AMOModelSerializer):
if instance.has_listed_versions() if instance.has_listed_versions()
else None else None
) )
old_slug = instance.slug
if 'icon' in validated_data: if 'icon' in validated_data:
self._save_icon(validated_data['icon']) self._save_icon(validated_data['icon'])
instance = super().update(instance, validated_data) instance = super().update(instance, validated_data)
@ -1238,6 +1241,10 @@ class AddonSerializer(AMOModelSerializer):
# When updating, always return the version we just created in the # When updating, always return the version we just created in the
# representation if there was one. # representation if there was one.
self.fields['version'].write_only = False self.fields['version'].write_only = False
if 'slug' in validated_data:
ActivityLog.create(
amo.LOG.ADDON_SLUG_CHANGED, instance, old_slug, instance.slug
)
self.log(instance, validated_data) self.log(instance, validated_data)
return instance return instance

Просмотреть файл

@ -1996,6 +1996,22 @@ class TestAddonViewSetUpdate(AddonViewSetCreateUpdateMixin, TestCase):
) )
assert response.status_code == 200, response.content assert response.status_code == 200, response.content
def test_set_slug_log(self):
self.addon.update(slug='first-slug')
response = self.request(
data={'slug': 'second-slug'},
)
log_entry = ActivityLog.objects.filter(
action=amo.LOG.ADDON_SLUG_CHANGED.id
).latest('pk')
assert response.status_code == 200, response.content
assert log_entry.user == self.user
assert log_entry.arguments == [self.addon, 'first-slug', 'second-slug']
assert 'slug from first-slug to second-slug' in str(log_entry)
assert str(self.user.id) in str(log_entry)
def test_set_extra_data(self): def test_set_extra_data(self):
self.addon.description = 'Existing description' self.addon.description = 'Existing description'
self.addon.save() self.addon.save()
@ -2043,6 +2059,7 @@ class TestAddonViewSetUpdate(AddonViewSetCreateUpdateMixin, TestCase):
amo.LOG.ADD_VERSION.id, amo.LOG.ADD_VERSION.id,
amo.LOG.LOG_IN.id, amo.LOG.LOG_IN.id,
amo.LOG.LOG_IN_API_TOKEN.id, amo.LOG.LOG_IN_API_TOKEN.id,
amo.LOG.ADDON_SLUG_CHANGED.id,
) )
).get() ).get()
assert alog.user == self.user assert alog.user == self.user

Просмотреть файл

@ -954,6 +954,14 @@ class CLEAR_ADMIN_REVIEW_THEME(_LOG):
admin_event = True admin_event = True
class ADDON_SLUG_CHANGED(_LOG):
id = 182
format = _('{user_responsible} changed {addon} slug from {0} to {1}.')
short = _('Addon slug changed')
keep = True
show_user_to_developer = True
LOGS = [x for x in vars().values() if isclass(x) and issubclass(x, _LOG) and x != _LOG] LOGS = [x for x in vars().values() if isclass(x) and issubclass(x, _LOG) and x != _LOG]
# Make sure there's no duplicate IDs. # Make sure there's no duplicate IDs.
assert len(LOGS) == len({log.id for log in LOGS}) assert len(LOGS) == len({log.id for log in LOGS})

Просмотреть файл

@ -137,6 +137,12 @@ class BaseTestEditDescribe(BaseTestEdit):
doc = pq(response.content) doc = pq(response.content)
assert doc('form').attr('action') != old_edit assert doc('form').attr('action') != old_edit
activity_log = ActivityLog.objects.latest('pk')
assert activity_log.action == amo.LOG.ADDON_SLUG_CHANGED.id
assert activity_log.user == self.user
assert activity_log.arguments == [self.addon, self.addon.slug, 'valid']
assert f'slug from {self.addon.slug} to valid' in str(activity_log)
def test_edit_as_developer(self): def test_edit_as_developer(self):
self.client.force_login(UserProfile.objects.get(email='regular@mozilla.com')) self.client.force_login(UserProfile.objects.get(email='regular@mozilla.com'))
data = self.get_dict() data = self.get_dict()

Просмотреть файл

@ -906,7 +906,12 @@ def addons_section(request, addon_id, addon, section, editable=False):
else: else:
ActivityLog.create(amo.LOG.EDIT_PROPERTIES, addon) ActivityLog.create(amo.LOG.EDIT_PROPERTIES, addon)
if valid_slug != addon.slug:
ActivityLog.create(
amo.LOG.ADDON_SLUG_CHANGED, addon, valid_slug, addon.slug
)
valid_slug = addon.slug valid_slug = addon.slug
if cat_form: if cat_form:
if cat_form.is_valid(): if cat_form.is_valid():
cat_form.save() cat_form.save()