'Tests attempted spam edits that occur on Ajax POSTs.'

@override_flag(SPAM_SUBMISSIONS_FLAG, active=True)
@override_flag(SPAM_CHECKS_FLAG, active=True)
@override_config(AKISMET_KEY='dashboard')
@requests_mock.mock()
@mock.patch('kuma.spam.akismet.Akismet.check_comment')
def test_edit_spam_ajax(self, mock_requests, mock_akismet_method, translate_locale=None):

    mock_requests.post(VERIFY_URL, content='valid')
    mock_akismet_method.return_value = True
    self.client.login(username='testuser', password='testpass')
    doc = document(save=True)
    data = new_document_data()
    revision(save=True, document=doc)
    posting_url = reverse('wiki.edit', args=[doc.slug])
    if translate_locale:
        data['locale'] = translate_locale
        translation = document(parent=doc, locale=translate_locale, save=True)
        translation_rev = revision(document=translation, based_on=translation.parent.current_or_latest_revision(), save=True)
        posting_url = reverse('wiki.edit', args=[translation_rev.document.slug], locale=translate_locale)
    resp = self.client.get(posting_url)
    page = pq(resp.content)
    rev_id = page.find('input[name="current_rev"]').attr('value')
    data.update({'form-type': 'rev', 'content': 'Spam    content', 'current_rev': rev_id})
    resp = self.client.post(posting_url, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    spam_message = render_to_string('wiki/includes/spam_error.html')
    args = ['MDN/Contribute/Does_this_belong']
    no_locale = reverse('wiki.document', args=args).encode('utf-8')
    if translate_locale:
        yes_locale = reverse('wiki.document', args=args, locale=translate_locale).encode('utf-8')
    else:
        yes_locale = reverse('wiki.document', args=args, force_locale=True).encode('utf-8')
    spam_message = spam_message.replace(no_locale, yes_locale)
    ok_((spam_message in json.loads(resp.content)['error_message']), 'Spam    message    should    appear')
