return is_all_versions property in Block serializer (#20858)

This commit is contained in:
Andrew Williamson 2023-06-21 15:52:03 +01:00 коммит произвёл GitHub
Родитель 6a183ea40e
Коммит 367a0849a6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 46 добавлений и 1 удалений

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

@ -31,3 +31,4 @@ This endpoint returns an add-on Block from the blocklist, specified by guid or i
:>json string|null reason: Why the add-on needed to be blocked.
:>json object|null url: A url to the report/request that detailed why the add-on should potentially be blocked. Typically a bug report on bugzilla.mozilla.org. (See :ref:`Outgoing Links <api-overview-outgoing>`)
:>json string versions[]: The versions of this add-on that are blocked.
:>json boolean is_all_versions: Are all versions of this add-on blocked. If ``False``, some versions are not blocked.

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

@ -463,6 +463,7 @@ These are `v5` specific changes - `v4` changes apply also.
* 2023-06-01: renamed add-ons search endpoint sort by ratings parameter to ``sort=ratings``, ``sort=rating`` is still supported for backwards-compatibility. https://github.com/mozilla/addons-server/issues/20763
* 2023-06-06: added the /addons/browser-mappings/ endpoint. https://github.com/mozilla/addons-server/issues/20798
* 2023-06-22: added ``versions`` to blocklist block endpoint. https://github.com/mozilla/addons-server/issues/20748
* 2023-07-06: added ``is_all_versions`` to blocklist block endpoint. https://github.com/mozilla/addons-server/issues/20857
.. _`#11380`: https://github.com/mozilla/addons-server/issues/11380/
.. _`#11379`: https://github.com/mozilla/addons-server/issues/11379/

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

@ -1,7 +1,9 @@
from rest_framework import fields
from olympia import amo
from olympia.api.fields import OutgoingURLField, TranslationSerializerField
from olympia.api.serializers import AMOModelSerializer
from olympia.versions.models import Version
from .models import Block
@ -10,6 +12,7 @@ class BlockSerializer(AMOModelSerializer):
addon_name = TranslationSerializerField(source='addon.name')
url = OutgoingURLField()
versions = fields.SerializerMethodField()
is_all_versions = fields.SerializerMethodField()
class Meta:
model = Block
@ -24,6 +27,7 @@ class BlockSerializer(AMOModelSerializer):
'reason',
'url',
'versions',
'is_all_versions',
)
def get_versions(self, obj):
@ -32,3 +36,13 @@ class BlockSerializer(AMOModelSerializer):
'version__version', flat=True
)
)
def get_is_all_versions(self, obj):
cannot_upload_new_versions = not obj.addon or obj.addon.status in (
amo.STATUS_DISABLED,
amo.STATUS_DELETED,
)
unblocked_versions_qs = Version.unfiltered.filter(
addon__addonguid__guid=obj.guid, file__is_signed=True
).exclude(blockversion__id__isnull=False)
return cannot_upload_new_versions and not unblocked_versions_qs.exists()

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

@ -1,4 +1,5 @@
from olympia.amo.tests import TestCase, addon_factory, user_factory
from olympia import amo
from olympia.amo.tests import TestCase, addon_factory, user_factory, version_factory
from olympia.amo.urlresolvers import get_outgoing_url
from ..models import Block, BlockVersion
@ -29,6 +30,7 @@ class TestBlockSerializer(TestCase):
'outgoing': get_outgoing_url('https://goo.gol'),
},
'versions': [],
'is_all_versions': True,
'created': self.block.created.isoformat()[:-7] + 'Z',
'modified': self.block.modified.isoformat()[:-7] + 'Z',
}
@ -41,3 +43,30 @@ class TestBlockSerializer(TestCase):
serializer = BlockSerializer(instance=self.block)
assert serializer.data['addon_name'] == {'en-US': 'Addón náme'}
assert serializer.data['versions'] == [self.block.addon.current_version.version]
def test_is_all_versions(self):
# no add-on so True
assert BlockSerializer(instance=self.block).data['is_all_versions'] is True
addon = addon_factory(
guid=self.block.guid, name='Addón náme', file_kw={'is_signed': True}
)
BlockVersion.objects.create(block=self.block, version=addon.current_version)
version_factory(addon=addon, file_kw={'is_signed': False}) # not signed
self.block.refresh_from_db()
del self.block.addon
# add-on with only one signed version - so all blocked - but not disabled
assert BlockSerializer(instance=self.block).data['is_all_versions'] is False
addon.update(status=amo.STATUS_DISABLED)
del self.block.addon
# now the add-on is disabled with all signed versions blocked
assert BlockSerializer(instance=self.block).data['is_all_versions'] is True
# but if there was another signed version, even deleted on a deleted addon...
old_addon = addon_factory(file_kw={'is_signed': True})
old_addon.current_version.delete()
old_addon.delete()
old_addon.addonguid.update(guid=addon.guid)
# ... the guid isn't completely blocked
assert BlockSerializer(instance=self.block).data['is_all_versions'] is False