def test_spam_with_many_response(create_revision, akismet_wiki_user,
                                 akismet_wiki_user_2, user_client,
                                 enable_akismet_submissions,
                                 akismet_mock_requests):
    submission = RevisionAkismetSubmission(
        type="ham",
        sender=akismet_wiki_user_2,
        revision=create_revision
    )
    submission.save()

    # Check that one RevisionAkismetSubmission instance exists.
    ras = RevisionAkismetSubmission.objects.filter(revision=create_revision)
    assert ras.count() == 1

    # Create another Akismet revision via the endpoint.
    url = reverse('wiki.submit_akismet_spam', locale='en-US')
    response = user_client.post(url, data={'revision': create_revision.id})
    assert response.status_code == 201
    assert 'max-age=0' in response['Cache-Control']
    assert 'no-cache' in response['Cache-Control']
    assert 'no-store' in response['Cache-Control']
    assert 'must-revalidate' in response['Cache-Control']
    data = json.loads(response.content)
    assert len(data) == 2
    assert data[0]['type'] == 'ham'
    assert data[0]['sender'] == akismet_wiki_user_2.username
    assert data[1]['type'] == 'spam'
    assert data[1]['sender'] == akismet_wiki_user.username

    # Check that the Akismet endpoints were called.
    assert akismet_mock_requests.called
    assert akismet_mock_requests.call_count == 2
Exemple #2
0
def test_spam_with_many_response(create_revision, akismet_wiki_user,
                                 akismet_wiki_user_2, user_client,
                                 enable_akismet_submissions,
                                 akismet_mock_requests):
    submission = RevisionAkismetSubmission(type="ham",
                                           sender=akismet_wiki_user_2,
                                           revision=create_revision)
    submission.save()

    # Check that one RevisionAkismetSubmission instance exists.
    ras = RevisionAkismetSubmission.objects.filter(revision=create_revision)
    assert ras.count() == 1

    # Create another Akismet revision via the endpoint.
    url = reverse('wiki.submit_akismet_spam', locale='en-US')
    response = user_client.post(url, data={'revision': create_revision.id})
    assert response.status_code == 201
    assert 'max-age=0' in response['Cache-Control']
    assert 'no-cache' in response['Cache-Control']
    assert 'no-store' in response['Cache-Control']
    assert 'must-revalidate' in response['Cache-Control']
    data = json.loads(response.content)
    assert len(data) == 2
    assert data[0]['type'] == 'ham'
    assert data[0]['sender'] == akismet_wiki_user_2.username
    assert data[1]['type'] == 'spam'
    assert data[1]['sender'] == akismet_wiki_user.username

    # Check that the Akismet endpoints were called.
    assert akismet_mock_requests.called
    assert akismet_mock_requests.call_count == 2
    def handle(self, *args, **options):
        dry_run = options['dry_run']

        # first get the deleted document logs for the last n days
        ttl = timezone.now() - timedelta(days=options['days'])
        logged_deletions = DocumentDeletionLog.objects.filter(
            timestamp__gte=ttl,
        ).filter(
            # They use "spam" or "junk"
            # deleting spam revisions;
            # the spam makes me cry.  -- willkg
            models.Q(reason__icontains='spam') |
            models.Q(reason__icontains='junk')
        )
        count = logged_deletions.count()
        self.stdout.write('Checking %s deleted document logs' % count)

        sender = get_user_model().objects.get(username=options['username'])
        self.stdout.write(u'Submitting spam to Akismet as user %s' % sender)

        akismet = Akismet()

        if not akismet.ready:
            raise CommandError('Akismet client is not ready')

        for i, logged_deletion in enumerate(logged_deletions.iterator(), 1):
            self.stdout.write('%d/%d: ' % (i, count), ending='')
            # get the deleted document in question
            document = Document.admin_objects.filter(
                locale=logged_deletion.locale,
                slug=logged_deletion.slug,
            ).first()

            if document is None:
                # no document found with that locale and slug,
                # probably purged at some point
                self.stderr.write(u'Ignoring locale %s and slug %s' %
                                  (logged_deletion.locale,
                                   logged_deletion.slug))
                continue

            if not document.deleted:
                # guess the document got undeleted at some point again,
                # ignoring..
                self.stderr.write(u'Ignoring undeleted document %s' % document)
                continue

            if not document.current_revision:
                # no current revision found, which means something is fishy
                # but we can't submit it as spam since we don't have a revision
                self.stderr.write(u'Ignoring document %s without current '
                                  u'revision' % document.pk)
                continue

            params = revision_akismet_parameters(document.current_revision)
            if dry_run:
                # we're in dry-run, so let's continue okay?
                self.stdout.write(u'Not submitting current revision %s of '
                                  u'document %s because of dry-mode' %
                                  (document.current_revision.pk, document.pk))
                continue
            try:
                akismet.submit_spam(**params)
            except AkismetError as exc:
                self.stderr.write(u'Akismet error while submitting current '
                                  u'revision %s of document %s: %s' %
                                  (document.current_revision.pk, document.pk,
                                   exc.debug_help))
            else:
                self.stdout.write(u'Successfully submitted current '
                                  u'revision %s of document %s' %
                                  (document.current_revision.pk, document.pk))
                submission = RevisionAkismetSubmission(
                    revision=document.current_revision,
                    sender=sender,
                    type=RevisionAkismetSubmission.SPAM_TYPE,
                )
                submission.save()
Exemple #4
0
    def handle(self, *args, **options):
        dry_run = options['dry_run']

        # first get the deleted document logs for the last n days
        ttl = timezone.now() - timedelta(days=options['days'])
        logged_deletions = DocumentDeletionLog.objects.filter(
            # They use "spam"
            # deleting spam revisions;
            # the spam makes me cry.  -- willkg
            timestamp__gte=ttl,
            reason__icontains='spam'
        )
        count = logged_deletions.count()
        self.stdout.write('Checking %s deleted document logs' % count)

        sender = get_user_model().objects.get(username=options['username'])
        self.stdout.write('Submitting spam to Akismet as user %s' % sender)

        akismet = Akismet()

        if not akismet.ready:
            raise CommandError('Akismet client is not ready')

        for i, logged_deletion in enumerate(logged_deletions.iterator(), 1):
            self.stdout.write('%d/%d: ' % (i, count), ending='')
            # get the deleted document in question
            document = Document.admin_objects.filter(
                locale=logged_deletion.locale,
                slug=logged_deletion.slug,
            ).first()

            if document is None:
                # no document found with that locale and slug,
                # probably purged at some point
                self.stderr.write('Ignoring locale %s and slug %s' %
                                  (logged_deletion.locale,
                                   logged_deletion.slug))
                continue

            if not document.deleted:
                # guess the document got undeleted at some point again,
                # ignoring..
                self.stderr.write('Ignoring undeleted document %s' % document)
                continue

            if not document.current_revision:
                # no current revision found, which means something is fishy
                # but we can't submit it as spam since we don't have a revision
                self.stderr.write('Ignoring document %s without current '
                                  'revision' % document.pk)
                continue

            akismet_data = AkismetHistoricalData(document.current_revision)
            params = akismet_data.parameters
            if dry_run:
                # we're in dry-run, so let's continue okay?
                self.stdout.write('Not submitting current revision %s of '
                                  'document %s because of dry-mode' %
                                  (document.current_revision.pk, document.pk))
                continue
            try:
                akismet.submit_spam(**params)
            except AkismetError as exc:
                self.stderr.write('Akismet error while submitting current '
                                  'revision %s of document %s: %s' %
                                  (document.current_revision.pk, document.pk,
                                   exc.debug_help))
            else:
                self.stdout.write('Successfully submitted current '
                                  'revision %s of document %s' %
                                  (document.current_revision.pk, document.pk))
                submission = RevisionAkismetSubmission(
                    revision=document.current_revision,
                    sender=sender,
                    type=RevisionAkismetSubmission.SPAM_TYPE,
                )
                submission.save()