Пример #1
0
 def test_name_all_locales_cannot_be_empty(self):
     data = {'name': {'fr': u'', 'en-US': u''}}
     serializer = CollectionSerializer(instance=self.collection,
                                       data=data,
                                       partial=True)
     eq_(serializer.is_valid(), False)
     ok_('name' in serializer.errors)
Пример #2
0
 def test_wrong_default_language_serialization(self):
     # The following is wrong because we only accept the 'en-us' form.
     data = {'default_language': u'en_US'}
     serializer = CollectionSerializer(instance=self.collection, data=data,
                                       partial=True)
     eq_(serializer.is_valid(), False)
     ok_('default_language' in serializer.errors)
Пример #3
0
 def test_translation_deserialization(self):
     data = {'name': u'¿Dónde está la biblioteca?'}
     serializer = CollectionSerializer(instance=self.collection,
                                       data=data,
                                       partial=True)
     eq_(serializer.errors, {})
     ok_(serializer.is_valid())
Пример #4
0
 def test_translation_deserialization_multiples_locales(self):
     data = {'name': {'fr': u'Chat grincheux…', 'en-US': u'Grumpy Cat...'}}
     serializer = CollectionSerializer(instance=self.collection,
                                       data=data,
                                       partial=True)
     eq_(serializer.errors, {})
     ok_(serializer.is_valid())
Пример #5
0
 def test_translation_deserialization(self):
     data = {
         'name': u'¿Dónde está la biblioteca?'
     }
     serializer = CollectionSerializer(instance=self.collection, data=data,
                                       partial=True)
     eq_(serializer.errors, {})
     ok_(serializer.is_valid())
Пример #6
0
 def test_wrong_default_language_serialization(self):
     # The following is wrong because we only accept the 'en-us' form.
     data = {'default_language': u'en_US'}
     serializer = CollectionSerializer(instance=self.collection,
                                       data=data,
                                       partial=True)
     eq_(serializer.is_valid(), False)
     ok_('default_language' in serializer.errors)
Пример #7
0
 def test_name_required(self):
     data = {
         'description': u'some description',
         'collection_type': u'1'
     }
     serializer = CollectionSerializer(instance=self.collection, data=data)
     eq_(serializer.is_valid(), False)
     ok_('name' in serializer.errors)
Пример #8
0
 def test_name_cannot_be_empty(self):
     data = {'name': u''}
     serializer = CollectionSerializer(instance=self.collection,
                                       data=data,
                                       partial=True)
     eq_(serializer.is_valid(), False)
     ok_('name' in serializer.errors)
     eq_(serializer.errors['name'],
         [u'The field must have a length of at least 1 characters.'])
Пример #9
0
 def test_name_one_locale_must_be_non_empty(self):
     data = {
         'name': {
             'fr': u'',
             'en-US': u'Non-Empty Name'
         }
     }
     serializer = CollectionSerializer(instance=self.collection, data=data,
                                       partial=True)
     eq_(serializer.is_valid(), True)
Пример #10
0
 def test_name_cannot_be_empty(self):
     data = {
         'name': u''
     }
     serializer = CollectionSerializer(instance=self.collection, data=data,
                                       partial=True)
     eq_(serializer.is_valid(), False)
     ok_('name' in serializer.errors)
     eq_(serializer.errors['name'],
         [u'The field must have a length of at least 1 characters.'])
Пример #11
0
 def test_name_all_locales_cannot_be_empty(self):
     data = {
         'name': {
             'fr': u'',
             'en-US': u''
         }
     }
     serializer = CollectionSerializer(instance=self.collection, data=data,
                                       partial=True)
     eq_(serializer.is_valid(), False)
     ok_('name' in serializer.errors)
Пример #12
0
 def test_translation_deserialization_multiples_locales(self):
     data = {
         'name': {
             'fr': u'Chat grincheux…',
             'en-US': u'Grumpy Cat...'
         }
     }
     serializer = CollectionSerializer(instance=self.collection, data=data,
                                       partial=True)
     eq_(serializer.errors, {})
     ok_(serializer.is_valid())
Пример #13
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
    def setUp(self):
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer()

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), [
            'id', 'name', 'description', 'apps', 'collection_type', 'category',
            'region', 'carrier', 'author', 'is_public'
        ])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        self.test_to_native(apps=apps)
Пример #14
0
 def setUp(self):
     minimal_context = {
         'request': RequestFactory().get('/whatever')
     }
     self.collection = Collection.objects.create(**self.collection_data)
     self.serializer = CollectionSerializer(self.collection,
                                            context=minimal_context)
Пример #15
0
 def collections(self, request, collection_type=None):
     filters = request.GET.dict()
     if collection_type is not None:
         qs = Collection.public.filter(collection_type=collection_type)
     else:
         qs = Collection.public.all()
     filterset = CollectionFilterSetWithFallback(filters, queryset=qs)
     serializer = CollectionSerializer(filterset,
                                       context={'request': request})
     return serializer.data
Пример #16
0
 def collections(self, request, collection_type=None, limit=1):
     filters = request.GET.dict()
     filters.setdefault('region', self.get_region(request).slug)
     if collection_type is not None:
         qs = Collection.public.filter(collection_type=collection_type)
     else:
         qs = Collection.public.all()
     qs = CollectionFilterSetWithFallback(filters, queryset=qs).qs
     serializer = CollectionSerializer(qs[:limit],
                                       context={'request': request})
     return serializer.data, getattr(qs, 'filter_fallback', None)
Пример #17
0
 def setUp(self):
     self.create_switch('rocketfuel')
     super(TestCollectionViewSet, self).setUp()
     self.serializer = CollectionSerializer()
     self.collection_data = {
         'collection_type': COLLECTIONS_TYPE_BASIC,
         'description': u'A cöllection of my favorite games',
         'name': u'My Favorite Gamés',
         'author': u'My Àuthør',
         'is_public': True,
     }
     self.collection = Collection.objects.create(**self.collection_data)
     self.apps = [amo.tests.app_factory() for n in xrange(1, 5)]
     self.list_url = reverse('collections-list')
Пример #18
0
 def collections(self, request, collection_type=None, limit=1):
     filters = request.GET.dict()
     region = self.get_region(request)
     if region:
         filters.setdefault('region', region.slug)
     if collection_type is not None:
         qs = Collection.public.filter(collection_type=collection_type)
     else:
         qs = Collection.public.all()
     qs = CollectionFilterSetWithFallback(filters, queryset=qs).qs
     preview_mode = filters.get('preview', False)
     serializer = CollectionSerializer(qs[:limit], many=True, context={
         'request': request,
         'view': self,
         'use-es-for-apps': not preview_mode
     })
     return serializer.data, getattr(qs, 'filter_fallback', None)
Пример #19
0
 def setUp(self):
     self.create_switch('rocketfuel')
     super(TestCollectionViewSetUnique, self).setUp()
     self.serializer = CollectionSerializer()
     self.category = Category.objects.create(type=amo.ADDON_WEBAPP,
         name='Grumpy', slug='grumpy-cat')
     self.collection_data = {
         'collection_type': COLLECTIONS_TYPE_FEATURED,
         'name': 'Featured Apps are cool',
         'slug': 'featured-apps-are-cool',
         'description': 'Featured Apps really are the bomb',
         'region': mkt.regions.SPAIN.id,
         'carrier': mkt.carriers.TELEFONICA.id,
         'category': self.category,
         'is_public': True,
     }
     self.collection = Collection.objects.create(**self.collection_data)
     self.list_url = reverse('collections-list')
     self.grant_permission(self.profile, 'Apps:Publisher')
Пример #20
0
class FeedItemSerializer(URLSerializerMixin, serializers.ModelSerializer):
    """Thin wrappers around apps w/ metadata related to its feature in feed."""
    carrier = SlugChoiceField(required=False,
                              choices_dict=mkt.carriers.CARRIER_MAP)
    region = SlugChoiceField(required=False,
                             choices_dict=mkt.regions.REGION_LOOKUP)
    category = SlugModelChoiceField(
        required=False,
        queryset=Category.objects.filter(type=amo.ADDON_WEBAPP))
    item_type = serializers.SerializerMethodField('get_item_type')

    # Types of objects that are allowed to be a feed item.
    collection = SplitField(relations.PrimaryKeyRelatedField(required=False),
                            CollectionSerializer())

    class Meta:
        fields = ('carrier', 'category', 'collection', 'id', 'item_type',
                  'region', 'url')
        item_types = ('collection', )
        model = FeedItem
        url_basename = 'feeditems'

    def validate(self, attrs):
        """
        Ensure that at least one object type is specified.
        """
        item_changed = any(k for k in self.Meta.item_types
                           if k in attrs.keys())
        num_defined = sum(1 for item in self.Meta.item_types
                          if attrs.get(item))
        if item_changed and num_defined != 1:
            message = ('A valid value for exactly one of the following '
                       'parameters must be defined: %s' %
                       ','.join(self.Meta.item_types))
            raise serializers.ValidationError(message)
        return attrs

    def get_item_type(self, obj):
        for item_type in self.Meta.item_types:
            if getattr(obj, item_type):
                return item_type
        return
Пример #21
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
    def setUp(self):
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer()

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(
            data.keys(),
            [
                "id",
                "name",
                "description",
                "apps",
                "collection_type",
                "category",
                "region",
                "carrier",
                "author",
                "is_public",
            ],
        )
        for order, app in enumerate(apps):
            eq_(data["apps"][order]["slug"], app.app_slug)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        self.test_to_native(apps=apps)
Пример #22
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):

    def setUp(self):
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer()

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), ['id', 'name', 'description', 'apps',
                                          'collection_type'])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        self.test_to_native(apps=apps)
Пример #23
0
 def test_name_one_locale_must_be_non_empty(self):
     data = {'name': {'fr': u'', 'en-US': u'Non-Empty Name'}}
     serializer = CollectionSerializer(instance=self.collection,
                                       data=data,
                                       partial=True)
     eq_(serializer.is_valid(), True)
Пример #24
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
    def setUp(self):
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer()

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(
            data.keys(),
            [
                "id",
                "name",
                "description",
                "apps",
                "collection_type",
                "category",
                "region",
                "carrier",
                "author",
                "slug",
                "is_public",
                "default_language",
            ],
        )
        for order, app in enumerate(apps):
            eq_(data["apps"][order]["slug"], app.app_slug)
        return data

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {"default_language": u"en_US"}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.is_valid(), False)
        ok_("default_language" in serializer.errors)

    def test_translation_deserialization(self):
        data = {"name": u"¿Dónde está la biblioteca?"}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {"name": {"fr": u"Chat grincheux…", "en-US": u"Grumpy Cat..."}}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data["apps"][0].keys()
        ok_("name" in keys)
        ok_("id" in keys)
Пример #25
0
 def setUp(self):
     self.collection = Collection.objects.create(**self.collection_data)
     self.serializer = CollectionSerializer()
Пример #26
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):

    def setUp(self):
        minimal_context = {
            'request': RequestFactory().get('/whatever')
        }
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer(context=minimal_context)

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), ['apps', 'author', 'background_color',
                                          'carrier', 'category',
                                          'collection_type', 'default_language',
                                          'description', 'id', 'image',
                                          'is_public', 'name', 'region',
                                          'slug', 'text_color'])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)
        return data

    def test_image(self):
        data = self.serializer.to_native(self.collection)
        eq_(data['image'], None)
        try:
            os.makedirs(os.path.dirname(self.collection.image_path()))
        except OSError:
            pass
        open(self.collection.image_path(), 'w').write("some data")
        data = self.serializer.to_native(self.collection)
        eq_(data['image'], reverse('collection-image-detail',
                                   kwargs={'pk': self.collection.id}))

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {'default_language': u'en_US'}
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('default_language' in serializer.errors)

    def test_translation_deserialization(self):
        data = {
            'name': u'¿Dónde está la biblioteca?'
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {
            'name': {
                'fr': u'Chat grincheux…',
                'en-US': u'Grumpy Cat...'
            }
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_empty_choice_deserialization(self):
        # Build data from existing object.
        data = self.serializer.to_native(self.collection)
        data.pop('id')
        # Emulate empty values passed via POST.
        data.update({'carrier': '', 'region': ''})

        instance = self.serializer.from_native(data, None)
        eq_(self.serializer.errors, {})
        ok_(self.serializer.is_valid())
        eq_(instance.region, None)
        eq_(instance.carrier, None)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data['apps'][0].keys()
        ok_('name' in keys)
        ok_('id' in keys)
Пример #27
0
 def setUp(self):
     minimal_context = {'request': RequestFactory().get('/whatever')}
     self.collection = Collection.objects.create(**self.collection_data)
     self.serializer = CollectionSerializer(context=minimal_context)
Пример #28
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):

    def setUp(self):
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer()

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), ['apps', 'author', 'background_color',
                                          'carrier', 'category',
                                          'collection_type', 'default_language',
                                          'description', 'id', 'is_public',
                                          'name', 'region', 'slug',
                                          'text_color'])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)
        return data

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {'default_language': u'en_US'}
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('default_language' in serializer.errors)

    def test_translation_deserialization(self):
        data = {
            'name': u'¿Dónde está la biblioteca?'
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {
            'name': {
                'fr': u'Chat grincheux…',
                'en-US': u'Grumpy Cat...'
            }
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data['apps'][0].keys()
        ok_('name' in keys)
        ok_('id' in keys)
Пример #29
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
    def setUp(self):
        minimal_context = {"request": RequestFactory().get("/whatever")}
        minimal_context["request"].user = AnonymousUser()
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer(self.collection, context=minimal_context)

    def test_metadata_is_serialized_to_json(self):
        ok_(json.dumps(self.serializer.metadata()))

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(
            data.keys(),
            [
                "apps",
                "author",
                "background_color",
                "carrier",
                "category",
                "collection_type",
                "default_language",
                "description",
                "id",
                "image",
                "is_public",
                "name",
                "region",
                "slug",
                "text_color",
            ],
        )
        for order, app in enumerate(apps):
            eq_(data["apps"][order]["slug"], app.app_slug)
        return data

    def test_to_native_operator(self):
        self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
        data = self.serializer.to_native(self.collection)
        ok_("can_be_hero" in data.keys())

    @override_settings(STATIC_URL="https://testserver-cdn/")
    def test_image(self):
        data = self.serializer.to_native(self.collection)
        eq_(data["image"], None)
        self.collection.update(image_hash="bbbbbb")
        data = self.serializer.to_native(self.collection)
        self.assertApiUrlEqual(
            data["image"],
            "/rocketfuel/collections/%s/image.png?bbbbbb" % self.collection.pk,
            scheme="https",
            netloc="testserver-cdn",
        )

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {"default_language": u"en_US"}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.is_valid(), False)
        ok_("default_language" in serializer.errors)

    def test_name_required(self):
        data = {"description": u"some description", "collection_type": u"1"}
        serializer = CollectionSerializer(instance=self.collection, data=data)
        eq_(serializer.is_valid(), False)
        ok_("name" in serializer.errors)

    def test_name_cannot_be_empty(self):
        data = {"name": u""}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.is_valid(), False)
        ok_("name" in serializer.errors)
        eq_(serializer.errors["name"], [u"The field must have a length of at least 1 characters."])

    def test_name_all_locales_cannot_be_empty(self):
        data = {"name": {"fr": u"", "en-US": u""}}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.is_valid(), False)
        ok_("name" in serializer.errors)

    def test_name_one_locale_must_be_non_empty(self):
        data = {"name": {"fr": u"", "en-US": u"Non-Empty Name"}}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.is_valid(), True)

    def test_translation_deserialization(self):
        data = {"name": u"¿Dónde está la biblioteca?"}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {"name": {"fr": u"Chat grincheux…", "en-US": u"Grumpy Cat..."}}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_empty_choice_deserialization(self):
        # Build data from existing object.
        data = self.serializer.to_native(self.collection)
        data.pop("id")
        # Emulate empty values passed via POST.
        data.update({"carrier": "", "region": ""})

        instance = self.serializer.from_native(data, None)
        eq_(self.serializer.errors, {})
        ok_(self.serializer.is_valid())
        eq_(instance.region, None)
        eq_(instance.carrier, None)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data["apps"][0].keys()
        ok_("name" in keys)
        ok_("id" in keys)

    def validate(self, **kwargs):
        return self.serializer.validate(kwargs)

    def test_validation_operatorshelf_category(self):
        category = "games"
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_BASIC, category=category))
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR))
        with self.assertRaises(serializers.ValidationError):
            self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR, category=category)
Пример #30
0
 def setUp(self):
     minimal_context = {"request": RequestFactory().get("/whatever")}
     minimal_context["request"].user = AnonymousUser()
     self.collection = Collection.objects.create(**self.collection_data)
     self.serializer = CollectionSerializer(self.collection, context=minimal_context)
Пример #31
0
 def test_name_required(self):
     data = {"description": u"some description", "collection_type": u"1"}
     serializer = CollectionSerializer(instance=self.collection, data=data)
     eq_(serializer.is_valid(), False)
     ok_("name" in serializer.errors)
Пример #32
0
 def test_name_cannot_be_empty(self):
     data = {"name": u""}
     serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
     eq_(serializer.is_valid(), False)
     ok_("name" in serializer.errors)
     eq_(serializer.errors["name"], [u"The field must have a length of at least 1 characters."])
Пример #33
0
 def test_name_all_locales_cannot_be_empty(self):
     data = {"name": {"fr": u"", "en-US": u""}}
     serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
     eq_(serializer.is_valid(), False)
     ok_("name" in serializer.errors)
Пример #34
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
    def setUp(self):
        minimal_context = {"request": RequestFactory().get("/whatever")}
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer(self.collection, context=minimal_context)

    def test_metadata_is_serialized_to_json(self):
        ok_(json.dumps(self.serializer.metadata()))

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(
            data.keys(),
            [
                "apps",
                "author",
                "background_color",
                "carrier",
                "category",
                "collection_type",
                "default_language",
                "description",
                "id",
                "image",
                "is_public",
                "name",
                "region",
                "slug",
                "text_color",
            ],
        )
        for order, app in enumerate(apps):
            eq_(data["apps"][order]["slug"], app.app_slug)
        return data

    def test_to_native_operator(self):
        self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
        data = self.serializer.to_native(self.collection)
        ok_("can_be_hero" in data.keys())

    @patch("mkt.collections.serializers.build_id", "bbbbbb")
    def test_image(self):
        data = self.serializer.to_native(self.collection)
        eq_(data["image"], None)
        self.collection.update(has_image=True)
        data = self.serializer.to_native(self.collection)
        self.assertApiUrlEqual(data["image"], "/rocketfuel/collections/%s/image.png?bbbbbb" % self.collection.pk)

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {"default_language": u"en_US"}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.is_valid(), False)
        ok_("default_language" in serializer.errors)

    def test_translation_deserialization(self):
        data = {"name": u"¿Dónde está la biblioteca?"}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {"name": {"fr": u"Chat grincheux…", "en-US": u"Grumpy Cat..."}}
        serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_empty_choice_deserialization(self):
        # Build data from existing object.
        data = self.serializer.to_native(self.collection)
        data.pop("id")
        # Emulate empty values passed via POST.
        data.update({"carrier": "", "region": ""})

        instance = self.serializer.from_native(data, None)
        eq_(self.serializer.errors, {})
        ok_(self.serializer.is_valid())
        eq_(instance.region, None)
        eq_(instance.carrier, None)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data["apps"][0].keys()
        ok_("name" in keys)
        ok_("id" in keys)

    def validate(self, **kwargs):
        return self.serializer.validate(kwargs)

    def test_validation_operatorshelf_category(self):
        category = Category.objects.create(name="BastaCorp", slug="basta", type=amo.ADDON_WEBAPP)
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_BASIC, category=category))
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR))
        with self.assertRaises(serializers.ValidationError):
            self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR, category=category)
Пример #35
0
 def test_translation_deserialization_multiples_locales(self):
     data = {"name": {"fr": u"Chat grincheux…", "en-US": u"Grumpy Cat..."}}
     serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
     eq_(serializer.errors, {})
     ok_(serializer.is_valid())
Пример #36
0
 def setUp(self):
     self.collection = Collection.objects.create(**self.collection_data)
     self.serializer = CollectionSerializer()
Пример #37
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):

    def setUp(self):
        minimal_context = {
            'request': RequestFactory().get('/whatever')
        }
        minimal_context['request'].user = AnonymousUser()
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer(self.collection,
                                               context=minimal_context)

    def test_metadata_is_serialized_to_json(self):
        ok_(json.dumps(self.serializer.metadata()))

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), [
            'apps', 'author', 'background_color', 'carrier', 'category',
            'collection_type', 'default_language', 'description', 'id',
            'image', 'is_public', 'name', 'region', 'slug', 'text_color'
        ])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)
        return data

    def test_to_native_operator(self):
        self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
        data = self.serializer.to_native(self.collection)
        ok_('can_be_hero' in data.keys())

    @override_settings(STATIC_URL='https://testserver-cdn/')
    def test_image(self):
        data = self.serializer.to_native(self.collection)
        eq_(data['image'], None)
        self.collection.update(image_hash='bbbbbb')
        data = self.serializer.to_native(self.collection)
        self.assertApiUrlEqual(data['image'],
            '/rocketfuel/collections/%s/image.png?bbbbbb' % self.collection.pk,
            scheme='https', netloc='testserver-cdn')

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {'default_language': u'en_US'}
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('default_language' in serializer.errors)

    def test_name_required(self):
        data = {
            'description': u'some description',
            'collection_type': u'1'
        }
        serializer = CollectionSerializer(instance=self.collection, data=data)
        eq_(serializer.is_valid(), False)
        ok_('name' in serializer.errors)

    def test_name_cannot_be_empty(self):
        data = {
            'name': u''
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('name' in serializer.errors)
        eq_(serializer.errors['name'],
            [u'The field must have a length of at least 1 characters.'])

    def test_name_all_locales_cannot_be_empty(self):
        data = {
            'name': {
                'fr': u'',
                'en-US': u''
            }
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('name' in serializer.errors)

    def test_name_one_locale_must_be_non_empty(self):
        data = {
            'name': {
                'fr': u'',
                'en-US': u'Non-Empty Name'
            }
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.is_valid(), True)

    def test_translation_deserialization(self):
        data = {
            'name': u'¿Dónde está la biblioteca?'
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {
            'name': {
                'fr': u'Chat grincheux…',
                'en-US': u'Grumpy Cat...'
            }
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_empty_choice_deserialization(self):
        # Build data from existing object.
        data = self.serializer.to_native(self.collection)
        data.pop('id')
        # Emulate empty values passed via POST.
        data.update({'carrier': '', 'region': ''})

        instance = self.serializer.from_native(data, None)
        eq_(self.serializer.errors, {})
        ok_(self.serializer.is_valid())
        eq_(instance.region, None)
        eq_(instance.carrier, None)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data['apps'][0].keys()
        ok_('name' in keys)
        ok_('id' in keys)

    def validate(self, **kwargs):
        return self.serializer.validate(kwargs)

    def test_validation_operatorshelf_category(self):
        category = 'games'
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_BASIC,
                          category=category))
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR))
        with self.assertRaises(serializers.ValidationError):
            self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR,
                          category=category)
Пример #38
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
    def setUp(self):
        minimal_context = {'request': RequestFactory().get('/whatever')}
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer(self.collection,
                                               context=minimal_context)

    def test_metadata_is_serialized_to_json(self):
        ok_(json.dumps(self.serializer.metadata()))

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), [
            'apps', 'author', 'background_color', 'carrier', 'category',
            'collection_type', 'default_language', 'description', 'id',
            'image', 'is_public', 'name', 'region', 'slug', 'text_color'
        ])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)
        return data

    def test_to_native_operator(self):
        self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
        data = self.serializer.to_native(self.collection)
        ok_('can_be_hero' in data.keys())

    @override_settings(STATIC_URL='https://testserver-cdn/')
    def test_image(self):
        data = self.serializer.to_native(self.collection)
        eq_(data['image'], None)
        self.collection.update(image_hash='bbbbbb')
        data = self.serializer.to_native(self.collection)
        self.assertApiUrlEqual(data['image'],
                               '/rocketfuel/collections/%s/image.png?bbbbbb' %
                               self.collection.pk,
                               scheme='https',
                               netloc='testserver-cdn')

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {'default_language': u'en_US'}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('default_language' in serializer.errors)

    def test_name_required(self):
        data = {'description': u'some description', 'collection_type': u'1'}
        serializer = CollectionSerializer(instance=self.collection, data=data)
        eq_(serializer.is_valid(), False)
        ok_('name' in serializer.errors)

    def test_name_cannot_be_empty(self):
        data = {'name': u''}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('name' in serializer.errors)
        eq_(serializer.errors['name'],
            [u'The field must have a length of at least 1 characters.'])

    def test_name_all_locales_cannot_be_empty(self):
        data = {'name': {'fr': u'', 'en-US': u''}}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('name' in serializer.errors)

    def test_name_one_locale_must_be_non_empty(self):
        data = {'name': {'fr': u'', 'en-US': u'Non-Empty Name'}}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.is_valid(), True)

    def test_translation_deserialization(self):
        data = {'name': u'¿Dónde está la biblioteca?'}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {'name': {'fr': u'Chat grincheux…', 'en-US': u'Grumpy Cat...'}}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_empty_choice_deserialization(self):
        # Build data from existing object.
        data = self.serializer.to_native(self.collection)
        data.pop('id')
        # Emulate empty values passed via POST.
        data.update({'carrier': '', 'region': ''})

        instance = self.serializer.from_native(data, None)
        eq_(self.serializer.errors, {})
        ok_(self.serializer.is_valid())
        eq_(instance.region, None)
        eq_(instance.carrier, None)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data['apps'][0].keys()
        ok_('name' in keys)
        ok_('id' in keys)

    def validate(self, **kwargs):
        return self.serializer.validate(kwargs)

    def test_validation_operatorshelf_category(self):
        category = Category.objects.create(name='BastaCorp',
                                           slug='basta',
                                           type=amo.ADDON_WEBAPP)
        ok_(
            self.validate(collection_type=COLLECTIONS_TYPE_BASIC,
                          category=category))
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR))
        with self.assertRaises(serializers.ValidationError):
            self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR,
                          category=category)
Пример #39
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
    def setUp(self):
        minimal_context = {'request': RequestFactory().get('/whatever')}
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer(context=minimal_context)

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), [
            'apps', 'author', 'background_color', 'carrier', 'category',
            'collection_type', 'default_language', 'description', 'id',
            'image', 'is_public', 'name', 'region', 'slug', 'text_color'
        ])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)
        return data

    def test_image(self):
        data = self.serializer.to_native(self.collection)
        eq_(data['image'], None)
        self.collection.update(has_image=True)
        data = self.serializer.to_native(self.collection)
        eq_(
            data['image'],
            reverse('collection-image-detail',
                    kwargs={'pk': self.collection.id}))

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {'default_language': u'en_US'}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('default_language' in serializer.errors)

    def test_translation_deserialization(self):
        data = {'name': u'¿Dónde está la biblioteca?'}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {'name': {'fr': u'Chat grincheux…', 'en-US': u'Grumpy Cat...'}}
        serializer = CollectionSerializer(instance=self.collection,
                                          data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_empty_choice_deserialization(self):
        # Build data from existing object.
        data = self.serializer.to_native(self.collection)
        data.pop('id')
        # Emulate empty values passed via POST.
        data.update({'carrier': '', 'region': ''})

        instance = self.serializer.from_native(data, None)
        eq_(self.serializer.errors, {})
        ok_(self.serializer.is_valid())
        eq_(instance.region, None)
        eq_(instance.carrier, None)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data['apps'][0].keys()
        ok_('name' in keys)
        ok_('id' in keys)
Пример #40
0
 def test_name_required(self):
     data = {'description': u'some description', 'collection_type': u'1'}
     serializer = CollectionSerializer(instance=self.collection, data=data)
     eq_(serializer.is_valid(), False)
     ok_('name' in serializer.errors)
Пример #41
0
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):

    def setUp(self):
        minimal_context = {
            'request': RequestFactory().get('/whatever')
        }
        self.collection = Collection.objects.create(**self.collection_data)
        self.serializer = CollectionSerializer(self.collection,
                                               context=minimal_context)

    def test_to_native(self, apps=None):
        if apps:
            for app in apps:
                self.collection.add_app(app)
        else:
            apps = []

        data = self.serializer.to_native(self.collection)
        for name, value in self.collection_data.iteritems():
            eq_(self.collection_data[name], data[name])
        self.assertSetEqual(data.keys(), [
            'apps', 'author', 'background_color', 'carrier', 'category',
            'collection_type', 'default_language', 'description', 'id',
            'image', 'is_public', 'name', 'region', 'slug', 'text_color'
        ])
        for order, app in enumerate(apps):
            eq_(data['apps'][order]['slug'], app.app_slug)
        return data

    def test_to_native_operator(self):
        self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
        data = self.serializer.to_native(self.collection)
        ok_('can_be_hero' in data.keys())

    def test_image(self):
        data = self.serializer.to_native(self.collection)
        eq_(data['image'], None)
        self.collection.update(has_image=True)
        data = self.serializer.to_native(self.collection)
        self.assertApiUrlEqual(data['image'],
            '/rocketfuel/collections/%s/image.png' % self.collection.pk)

    def test_wrong_default_language_serialization(self):
        # The following is wrong because we only accept the 'en-us' form.
        data = {'default_language': u'en_US'}
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.is_valid(), False)
        ok_('default_language' in serializer.errors)

    def test_translation_deserialization(self):
        data = {
            'name': u'¿Dónde está la biblioteca?'
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_translation_deserialization_multiples_locales(self):
        data = {
            'name': {
                'fr': u'Chat grincheux…',
                'en-US': u'Grumpy Cat...'
            }
        }
        serializer = CollectionSerializer(instance=self.collection, data=data,
                                          partial=True)
        eq_(serializer.errors, {})
        ok_(serializer.is_valid())

    def test_empty_choice_deserialization(self):
        # Build data from existing object.
        data = self.serializer.to_native(self.collection)
        data.pop('id')
        # Emulate empty values passed via POST.
        data.update({'carrier': '', 'region': ''})

        instance = self.serializer.from_native(data, None)
        eq_(self.serializer.errors, {})
        ok_(self.serializer.is_valid())
        eq_(instance.region, None)
        eq_(instance.carrier, None)

    def test_to_native_with_apps(self):
        apps = [amo.tests.app_factory() for n in xrange(1, 5)]
        data = self.test_to_native(apps=apps)
        keys = data['apps'][0].keys()
        ok_('name' in keys)
        ok_('id' in keys)

    def validate(self, **kwargs):
        return self.serializer.validate(kwargs)

    def test_validation_operatorshelf_category(self):
        category = Category.objects.create(name='BastaCorp', slug='basta',
                                           type=amo.ADDON_WEBAPP)
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_BASIC,
                          category=category))
        ok_(self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR))
        with self.assertRaises(serializers.ValidationError):
            self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR,
                          category=category)
Пример #42
0
 def test_name_one_locale_must_be_non_empty(self):
     data = {"name": {"fr": u"", "en-US": u"Non-Empty Name"}}
     serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
     eq_(serializer.is_valid(), True)