def clean(self):
        data = {
            prop: value for prop, value in self.cleaned_data.items()
            if prop in self.fields_to_akismet_comment_check}
        request_meta = getattr(self.request, 'META', {})

        # Find out if there is existing metadata that's been spam checked.
        addon_listed_versions = self.instance.versions.filter(
            channel=amo.RELEASE_CHANNEL_LISTED)
        if self.version:
            # If this is in the submission flow, exclude version in progress.
            addon_listed_versions = addon_listed_versions.exclude(
                id=self.version.id)
        existing_data = (
            fetch_existing_translations_from_addon(
                self.instance, self.fields_to_akismet_comment_check)
            if addon_listed_versions.exists() else ())

        reports = get_addon_akismet_reports(
            user=getattr(self.request, 'user', None),
            user_agent=request_meta.get('HTTP_USER_AGENT'),
            referrer=request_meta.get('HTTP_REFERER'),
            addon=self.instance,
            data=data,
            existing_data=existing_data)
        error_msg = ugettext('The text entered has been flagged as spam.')
        error_if_spam = waffle.switch_is_active('akismet-addon-action')
        for prop, report in reports:
            is_spam = report.is_spam
            if error_if_spam and is_spam:
                self.add_error(prop, forms.ValidationError(error_msg))
        return super(AkismetSpamCheckFormMixin, self).clean()
 def test_upload_with_addon(self):
     # Give addon some existing metadata.
     addon = addon_factory()
     user = user_factory()
     upload = FileUpload.objects.create(addon=addon)
     # summary is parsed but it's in existing_data - i.e. should have
     # been spam checked previous it so will be ignored.
     self.parse_addon_mock.return_value = {
         'name': u'fóó',
         'summary': u'summáry'
     }
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(user,
                                               user_agent,
                                               referrer,
                                               upload=upload,
                                               existing_data=[u'summáry'])
     # only one, no summary because it's in existing data
     assert len(reports) == 1
     self.create_for_addon_mock.assert_called_with(upload=upload,
                                                   addon=addon,
                                                   user=user,
                                                   property_name='name',
                                                   property_value=u'fóó',
                                                   user_agent=user_agent,
                                                   referrer=referrer)
     self.create_for_addon_mock.assert_called_once()
Beispiel #3
0
    def clean(self):
        data = {
            prop: value for prop, value in self.cleaned_data.items()
            if prop in self.fields_to_akismet_comment_check}
        request_meta = getattr(self.request, 'META', {})

        # Find out if there is existing metadata that's been spam checked.
        addon_listed_versions = self.instance.versions.filter(
            channel=amo.RELEASE_CHANNEL_LISTED)
        if self.version:
            # If this is in the submission flow, exclude version in progress.
            addon_listed_versions = addon_listed_versions.exclude(
                id=self.version.id)
        existing_data = (
            fetch_existing_translations_from_addon(
                self.instance, self.fields_to_akismet_comment_check)
            if addon_listed_versions.exists() else ())

        reports = get_addon_akismet_reports(
            user=getattr(self.request, 'user', None),
            user_agent=request_meta.get('HTTP_USER_AGENT'),
            referrer=request_meta.get('HTTP_REFERER'),
            addon=self.instance,
            data=data,
            existing_data=existing_data)
        error_msg = ugettext('The text entered has been flagged as spam.')
        error_if_spam = waffle.switch_is_active('akismet-addon-action')
        for prop, report in reports:
            is_spam = report.is_spam
            if error_if_spam and is_spam:
                self.add_error(prop, forms.ValidationError(error_msg))
        return super(AkismetSpamCheckFormMixin, self).clean()
Beispiel #4
0
 def test_upload_with_addon(self):
     # Give addon some existing metadata.
     addon = addon_factory(summary=u'summáry')
     user = user_factory()
     upload = FileUpload.objects.create(addon=addon)
     # description is parsed but should have spam checked it so will ignore
     self.parse_addon_mock.return_value = {
         'name': u'fóó',
         'description': u'summáry'
     }
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(user,
                                               user_agent,
                                               referrer,
                                               upload=upload)
     assert len(reports) == 1  # only one, no description
     self.create_for_addon_mock.assert_called_with(upload=upload,
                                                   addon=addon,
                                                   user=user,
                                                   property_name='name',
                                                   property_value=u'fóó',
                                                   user_agent=user_agent,
                                                   referrer=referrer)
     self.create_for_addon_mock.assert_called_once()
Beispiel #5
0
 def test_broken_upload(self):
     user = user_factory()
     upload = FileUpload.objects.create()
     self.parse_addon_mock.side_effect = ValidationError('foo')
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(
         user, user_agent, referrer, upload=upload)
     assert reports == []
     self.create_for_addon_mock.assert_not_called()
Beispiel #6
0
 def test_broken_upload(self):
     user = user_factory()
     upload = self.get_upload('webextension.xpi')
     self.parse_addon_mock.side_effect = ValidationError('foo')
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(
         user, user_agent, referrer, upload=upload)
     assert reports == []
     self.create_for_addon_mock.assert_not_called()
 def test_addon_update(self):
     addon = addon_factory(summary=u'¡Ochó!', default_locale='es-AR')
     user = user_factory()
     existing_data = utils.fetch_existing_translations_from_addon(
         addon, ('summary', 'name', 'description'))
     # check fetch_existing_translations_from_addon worked okay
     assert existing_data == {text_type(addon.name), u'¡Ochó!'}
     cleaned_data = {
         'description': {
             'en-US': u'fóó',
             'fr': u'lé foo',
             'de': '',  # should be ignored because empty
             'es-ES': u'¡Ochó!'  # ignored because in exist_data
         },
         'name': {
             'en-GB': u'just one name',
             'fr': None,
         },
     }
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(user,
                                               user_agent,
                                               referrer,
                                               addon=addon,
                                               data=cleaned_data,
                                               existing_data=existing_data)
     assert len(reports) == 3
     assert self.create_for_addon_mock.call_count == 3
     calls = [
         mock.call(upload=None,
                   addon=addon,
                   user=user,
                   property_name='name',
                   property_value=u'just one name',
                   user_agent=user_agent,
                   referrer=referrer),
         mock.call(upload=None,
                   addon=addon,
                   user=user,
                   property_name='description',
                   property_value=u'fóó',
                   user_agent=user_agent,
                   referrer=referrer),
         mock.call(upload=None,
                   addon=addon,
                   user=user,
                   property_name='description',
                   property_value=u'lé foo',
                   user_agent=user_agent,
                   referrer=referrer)
     ]
     self.create_for_addon_mock.assert_has_calls(calls, any_order=True)
Beispiel #8
0
 def test_upload(self):
     user = user_factory()
     upload = self.get_upload('webextension.xpi')
     self.parse_addon_mock.return_value = {'description': u'fóó'}
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(
         user, user_agent, referrer, upload=upload)
     assert len(reports) == 1
     self.create_for_addon_mock.assert_called_with(
         upload=upload, addon=None, user=user, property_name='description',
         property_value=u'fóó', user_agent=user_agent, referrer=referrer)
     self.create_for_addon_mock.assert_called_once()
Beispiel #9
0
 def test_upload(self):
     user = user_factory()
     upload = FileUpload.objects.create()
     self.parse_addon_mock.return_value = {'description': u'fóó'}
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(
         user, user_agent, referrer, upload=upload)
     assert len(reports) == 1
     self.create_for_addon_mock.assert_called_with(
         upload=upload, addon=None, user=user, property_name='description',
         property_value=u'fóó', user_agent=user_agent, referrer=referrer)
     self.create_for_addon_mock.assert_called_once()
Beispiel #10
0
 def test_addon_update(self):
     addon = addon_factory(summary=u'¡Ochó!', default_locale='es-AR')
     user = user_factory()
     cleaned_data = {
         'description': {
             'en-US': u'fóó',
             'fr': u'lé foo',
             'de': '',  # should be ignored because empty
             'es-ES': u'¡Ochó!'  # should be ignored because exists already
         },
         'name': {
             'en-GB': u'just one name',
             'fr': None,
         },
     }
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(user,
                                               user_agent,
                                               referrer,
                                               addon=addon,
                                               data=cleaned_data)
     assert len(reports) == 3
     assert self.create_for_addon_mock.call_count == 3
     calls = [
         mock.call(upload=None,
                   addon=addon,
                   user=user,
                   property_name='name',
                   property_value=u'just one name',
                   user_agent=user_agent,
                   referrer=referrer),
         mock.call(upload=None,
                   addon=addon,
                   user=user,
                   property_name='description',
                   property_value=u'fóó',
                   user_agent=user_agent,
                   referrer=referrer),
         mock.call(upload=None,
                   addon=addon,
                   user=user,
                   property_name='description',
                   property_value=u'lé foo',
                   user_agent=user_agent,
                   referrer=referrer)
     ]
     self.create_for_addon_mock.assert_has_calls(calls, any_order=True)
Beispiel #11
0
 def clean(self):
     data = {
         prop: value
         for prop, value in self.cleaned_data.items()
         if prop in self.fields_to_akismet_comment_check
     }
     request_meta = getattr(self.request, 'META', {})
     reports = get_addon_akismet_reports(
         user=getattr(self.request, 'user', None),
         user_agent=request_meta.get('HTTP_USER_AGENT'),
         referrer=request_meta.get('HTTP_REFERER'),
         addon=self.instance,
         data=data)
     if any((report.comment_check() for report in reports)):
         raise forms.ValidationError(
             ugettext('The text entered has been flagged as spam.'))
     return super(AkismetSpamCheckFormMixin, self).clean()
Beispiel #12
0
 def test_upload_locales(self):
     addon = addon_factory(summary=u'¡Ochó!', default_locale='es-AR')
     user = user_factory()
     upload = FileUpload.objects.create(addon=addon)
     self.parse_addon_mock.return_value = {
         'description': {
             'en-US': u'fóó',
             'fr': u'lé foo',
             'de': '',  # should be ignored because empty
             'es-ES': u'¡Ochó!'  # should be ignored because exists already
         },
         'name': u'just one name',
         'summary': None,  # should also be ignored because None
     }
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(user,
                                               user_agent,
                                               referrer,
                                               upload=upload)
     assert len(reports) == 3
     assert self.create_for_addon_mock.call_count == 3
     calls = [
         mock.call(upload=upload,
                   addon=addon,
                   user=user,
                   property_name='name',
                   property_value=u'just one name',
                   user_agent=user_agent,
                   referrer=referrer),
         mock.call(upload=upload,
                   addon=addon,
                   user=user,
                   property_name='description',
                   property_value=u'fóó',
                   user_agent=user_agent,
                   referrer=referrer),
         mock.call(upload=upload,
                   addon=addon,
                   user=user,
                   property_name='description',
                   property_value=u'lé foo',
                   user_agent=user_agent,
                   referrer=referrer)
     ]
     self.create_for_addon_mock.assert_has_calls(calls, any_order=True)
Beispiel #13
0
 def test_upload_with_addon(self):
     # Give addon some existing metadata.
     addon = addon_factory()
     user = user_factory()
     upload = self.get_upload('webextension.xpi', addon=addon)
     # summary is parsed but it's in existing_data - i.e. should have
     # been spam checked previous it so will be ignored.
     self.parse_addon_mock.return_value = {
         'name': u'fóó', 'summary': u'summáry'}
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(
         user, user_agent, referrer, upload=upload,
         existing_data=[u'summáry'])
     # only one, no summary because it's in existing data
     assert len(reports) == 1
     self.create_for_addon_mock.assert_called_with(
         upload=upload, addon=addon, user=user, property_name='name',
         property_value=u'fóó', user_agent=user_agent, referrer=referrer)
     self.create_for_addon_mock.assert_called_once()
Beispiel #14
0
 def test_addon_update(self):
     addon = addon_factory(summary=u'¡Ochó!', default_locale='es-AR')
     user = user_factory()
     existing_data = utils.fetch_existing_translations_from_addon(
         addon, ('summary', 'name', 'description'))
     # check fetch_existing_translations_from_addon worked okay
     assert existing_data == {text_type(addon.name), u'¡Ochó!'}
     cleaned_data = {
         'description': {
             'en-US': u'fóó',
             'fr': u'lé foo',
             'de': '',  # should be ignored because empty
             'es-ES': u'¡Ochó!'  # ignored because in exist_data
         },
         'name': {
             'en-GB': u'just one name',
             'fr': None,
         },
     }
     user_agent = 'Mr User/Agent'
     referrer = 'http://foo.baa/'
     reports = utils.get_addon_akismet_reports(
         user, user_agent, referrer, addon=addon, data=cleaned_data,
         existing_data=existing_data)
     assert len(reports) == 3
     assert self.create_for_addon_mock.call_count == 3
     calls = [
         mock.call(
             upload=None, addon=addon, user=user,
             property_name='name', property_value=u'just one name',
             user_agent=user_agent, referrer=referrer),
         mock.call(
             upload=None, addon=addon, user=user,
             property_name='description', property_value=u'fóó',
             user_agent=user_agent, referrer=referrer),
         mock.call(
             upload=None, addon=addon, user=user,
             property_name='description', property_value=u'lé foo',
             user_agent=user_agent, referrer=referrer)]
     self.create_for_addon_mock.assert_has_calls(calls, any_order=True)
 def test_waffle_off(self):
     reports = utils.get_addon_akismet_reports(None, '', '')
     assert reports == []
     self.create_for_addon_mock.assert_not_called()
Beispiel #16
0
 def test_waffle_off(self):
     reports = utils.get_addon_akismet_reports(
         None, '', '')
     assert reports == []
     self.create_for_addon_mock.assert_not_called()