Example #1
0
    def test_validate_og_lead_image_invalid_dimensions(self):
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_DIMENSIONS

        # HACK: change image scale to test the validator
        image = get_content_image(self.obj, scale='preview')
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_DIMENSIONS)
Example #2
0
    def test_validate_og_lead_image_invalid_aspect_ratio(self):
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_ASPECT_RATIO

        set_image_field(self.obj, load_image(768, 768, 'JPG'), 'image/jpeg')
        image = get_content_image(self.obj)
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_ASPECT_RATIO)
Example #3
0
    def test_validate_og_lead_image_invalid_aspect_ratio(self):
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_ASPECT_RATIO

        set_image_field(self.obj, load_image(768, 768, 'JPG'), 'image/jpeg')
        image = get_content_image(self.obj)
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_ASPECT_RATIO)
Example #4
0
    def test_validate_og_lead_image_invalid_dimensions(self):
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_DIMENSIONS

        # HACK: change image scale to test the validator
        image = get_content_image(self.obj, scale='preview')
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_DIMENSIONS)
Example #5
0
    def test_validate_og_lead_image_invalid_mime_type(self):
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_MIME_TYPE

        image = get_content_image(self.obj)
        # HACK: change image MIME type to test the validator
        image.mimetype = 'image/tiff'
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_MIME_TYPE)
Example #6
0
    def test_validate_og_lead_image_invalid_mime_type(self):
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_MIME_TYPE

        image = get_content_image(self.obj)
        # HACK: change image MIME type to test the validator
        image.mimetype = 'image/tiff'
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_MIME_TYPE)
Example #7
0
    def test_validate_og_lead_image_invalid_size(self):
        from sc.social.like.config import OG_LEAD_IMAGE_MAX_SIZE
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_SIZE

        image = get_content_image(self.obj)
        # HACK: change image scale data to test the validator
        image.data.data = get_random_string(OG_LEAD_IMAGE_MAX_SIZE + 1)
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_SIZE)
Example #8
0
    def test_validate_og_lead_image_invalid_size(self):
        from sc.social.like.config import OG_LEAD_IMAGE_MAX_SIZE
        from sc.social.like.utils import MSG_INVALID_OG_LEAD_IMAGE_SIZE

        image = get_content_image(self.obj)
        # HACK: change image scale data to test the validator
        image.data.data = get_random_string(OG_LEAD_IMAGE_MAX_SIZE + 1)
        with self.assertRaises(ValueError):
            validate_og_lead_image(image)

        # test validation message
        try:
            validate_og_lead_image(image)
        except ValueError as e:
            self.assertEqual(str(e), MSG_INVALID_OG_LEAD_IMAGE_SIZE)
Example #9
0
def check_sharing_best_practices(obj, event):
    """Check if content follows social networks sharing best practices
    as defined by Twitter and Facebook.
    """
    record = ISocialLikeSettings.__identifier__ + '.validation_enabled'
    validation_enabled = api.portal.get_registry_record(record, default=False)
    if not validation_enabled:
        return

    try:
        review_state = api.content.get_state(obj)
    except WorkflowException:
        # images and files have no associated workflow by default
        review_state = 'published'

    if review_state not in ('published', ):
        return  # no need to validate

    request = obj.REQUEST

    title = getattr(obj, 'title', '')
    try:
        validate_og_title(title)
    except ValueError as e:
        api.portal.show_message(message=str(e),
                                request=request,
                                type='warning')

    description = getattr(obj, 'description', '')
    try:
        validate_og_description(description)
    except ValueError as e:
        api.portal.show_message(message=str(e),
                                request=request,
                                type='warning')

    image = get_content_image(obj)
    try:
        validate_og_lead_image(image)
    except ValueError as e:
        api.portal.show_message(message=str(e),
                                request=request,
                                type='warning')
Example #10
0
def check_sharing_best_practices(obj, event):
    """Check if content follows social networks sharing best practices
    as defined by Twitter and Facebook.
    """
    record = ISocialLikeSettings.__identifier__ + '.validation_enabled'
    validation_enabled = api.portal.get_registry_record(record, default=False)
    if not validation_enabled:
        return

    try:
        review_state = api.content.get_state(obj)
    except WorkflowException:
        # images and files have no associated workflow by default
        review_state = 'published'

    if review_state not in ('published', ):
        return  # no need to validate

    request = obj.REQUEST

    title = getattr(obj, 'title', '')
    try:
        validate_og_title(title)
    except ValueError as e:
        api.portal.show_message(message=str(e), request=request, type='warning')

    description = getattr(obj, 'description', '')
    try:
        validate_og_description(description)
    except ValueError as e:
        api.portal.show_message(message=str(e), request=request, type='warning')

    image = get_content_image(obj)
    try:
        validate_og_lead_image(image)
    except ValueError as e:
        api.portal.show_message(message=str(e), request=request, type='warning')
Example #11
0
 def test_validate_og_lead_image_valid(self):
     image = get_content_image(self.obj)
     self.assertTrue(validate_og_lead_image(image))
Example #12
0
 def test_validate_og_lead_image_no_image(self):
     self.assertTrue(validate_og_lead_image(None))
Example #13
0
 def test_validate_og_lead_image_valid(self):
     image = get_content_image(self.obj)
     self.assertTrue(validate_og_lead_image(image))
Example #14
0
 def test_validate_og_lead_image_no_image(self):
     self.assertTrue(validate_og_lead_image(None))