Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
bug 1260197 - update tests to match previous code; add tests for unte…
Browse files Browse the repository at this point in the history
…sted sections of translate.py
  • Loading branch information
dchukhin committed Aug 22, 2016
1 parent 6302314 commit bc15424
Showing 1 changed file with 132 additions and 33 deletions.
165 changes: 132 additions & 33 deletions kuma/wiki/tests/test_views.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import base64
import datetime
import HTMLParser
import json
import time
from urllib import urlencode
Expand Down Expand Up @@ -2287,42 +2288,67 @@ def test_quick_review(self):
eq_(data_dict['expected_tags'], review_tags)

@pytest.mark.midair
def test_edit_midair_collisions(self, is_ajax=False):
def test_edit_midair_collisions(self, is_ajax=False, translate_locale=None):
"""Tests midair collisions for non-ajax submissions."""
self.client.login(username='admin', password='testpass')

# Post a new document.
data = new_document_data()
resp = self.client.post(reverse('wiki.create'), data)
doc = Document.objects.get(slug=data['slug'])
# This is the url to post new revisions for the rest of this test
posting_url = reverse('wiki.edit', args=[doc.slug])

# Edit #1 starts...
resp = self.client.get(reverse('wiki.edit',
args=[doc.slug]))
resp = self.client.get(
reverse('wiki.edit', args=[doc.slug])
)
page = pq(resp.content)
rev_id1 = page.find('input[name="current_rev"]').attr('value')

# Edit #2 starts...
resp = self.client.get(reverse('wiki.edit',
args=[doc.slug]))
resp = self.client.get(
reverse('wiki.edit', args=[doc.slug])
)
page = pq(resp.content)
rev_id2 = page.find('input[name="current_rev"]').attr('value')

# Edit #2 submits successfully
# Update data for the POST we are about to attempt
data.update({
'form-type': 'rev',
'content': 'This edit got there first',
'current_rev': rev_id2
})
# If this is a translation test, then create a translation and a
# revision on it. Then update data.
if 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
)
rev_id1 = rev_id2 = translation_rev.id
posting_url = reverse(
'wiki.edit',
args=[translation_rev.document.slug],
locale=translate_locale
)
data.update({
'title': translation.title,
'locale': translation.locale,
'slug': translation.slug,
'current_rev': rev_id2
})

# Edit #2 submits successfully
if is_ajax:
resp = self.client.post(
reverse('wiki.edit', args=[doc.slug]),
posting_url,
data, HTTP_X_REQUESTED_WITH='XMLHttpRequest'
)
eq_(False, json.loads(resp.content)['error'])
else:
resp = self.client.post(reverse('wiki.edit',
args=[doc.slug]), data)
resp = self.client.post(posting_url, data)
eq_(302, resp.status_code)

# Edit #1 submits, but receives a mid-aired notification
Expand All @@ -2331,40 +2357,53 @@ def test_edit_midair_collisions(self, is_ajax=False):
'content': 'This edit gets mid-aired',
'current_rev': rev_id1
})

if is_ajax:
resp = self.client.post(
reverse('wiki.edit', args=[doc.slug]),
posting_url,
data,
HTTP_X_REQUESTED_WITH='XMLHttpRequest'
)
else:
resp = self.client.post(reverse('wiki.edit',
args=[doc.slug]), data)
resp = self.client.post(posting_url, data)

# The url of the document's history
locale = translate_locale if translate_locale else doc.locale
doc_path = translation.slug if translate_locale else doc.slug
history_url = reverse(
'wiki.document_revisions',
kwargs={'document_path': doc.slug}, locale=doc.locale)
midair_collission_error = (unicode(MIDAIR_COLLISION) % {'url': history_url}).encode('utf-8')
'wiki.document_revisions', kwargs={'document_path': doc_path}, locale=locale
)
# The midair collission error, with the document url
midair_collission_error = (unicode(
MIDAIR_COLLISION) % {'url': history_url}
).encode('utf-8')

if is_ajax:
location_of_error = json.loads(resp.content)['error_message']
else:
location_of_error = resp.content
# If this is not an ajax post, then the error comes back in escaped
# html. We unescape the resp.content, but not all of it, since that
# causes ascii errors.
start_of_error = resp.content.index(midair_collission_error[0:20])
# Add an some extra characters to the end, since the unescaped length
# is a little less than the escaped length
end_of_error = start_of_error + len(midair_collission_error) + 20
location_of_error = HTMLParser.HTMLParser().unescape(
resp.content[start_of_error: end_of_error]
)
ok_(midair_collission_error in location_of_error,
"Midair collision message should appear")

@pytest.mark.midair
def test_edit_midair_collisions_ajax(self):
"""Tests midair collisions for ajax submissions."""
self.test_edit_midair_collisions(self, is_ajax=True)
self.test_edit_midair_collisions(is_ajax=True)

@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):
def test_edit_spam_ajax(self, mock_requests, mock_akismet_method, translate_locale=None):
"""Tests attempted spam edits that occur on Ajax POSTs."""
# Note: Akismet is enabled by the Flag overrides

Expand All @@ -2380,9 +2419,30 @@ def test_edit_spam_ajax(self, mock_requests, mock_akismet_method):
data = new_document_data()
# Create a revision on the document
revision(save=True, document=doc)
# This is the url to post new revisions for the rest of this test
posting_url = reverse('wiki.edit', args=[doc.slug])

# If this is a translation test, then create a translation and a revision on it
if 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
)
# rev_id = translation_rev.id
posting_url = reverse(
'wiki.edit',
args=[translation_rev.document.slug],
locale=translate_locale
)

resp = self.client.get(reverse('wiki.edit',
args=[doc.slug]))
# Get the rev id
resp = self.client.get(posting_url)
page = pq(resp.content)
rev_id = page.find('input[name="current_rev"]').attr('value')

Expand All @@ -2393,22 +2453,30 @@ def test_edit_spam_ajax(self, mock_requests, mock_akismet_method):
'current_rev': rev_id
})
resp = self.client.post(
reverse('wiki.edit', args=[doc.slug]),
posting_url,
data,
HTTP_X_REQUESTED_WITH='XMLHttpRequest'
)

spam_message = render_to_string('wiki/includes/spam_error.html')
# The spam message is generated without a locale during this test, so we
# replace the url with a url with a locale
no_locale = reverse('wiki.document', args=['MDN/Contribute/Does_this_belong']).encode('utf-8')
yes_locale = reverse('wiki.document', args=['MDN/Contribute/Does_this_belong'], force_locale=True).encode('utf-8')
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)
# Spam message appears in the JsonResponse content
ok_(spam_message in json.loads(resp.content)['error_message'],
"Spam message should appear")

def test_multiple_edits_ajax(self):
def test_multiple_edits_ajax(self, translate_locale=None):
"""Tests multiple sequential attempted valid edits that occur as Ajax POSTs."""

self.client.login(username='admin', password='testpass')
Expand All @@ -2417,38 +2485,52 @@ def test_multiple_edits_ajax(self):
data = new_document_data()
resp = self.client.post(reverse('wiki.create'), data)
doc = Document.objects.get(slug=data['slug'])
# This is the url to post new revisions for the rest of this test
if translate_locale:
posting_url = reverse('wiki.edit', args=[doc.slug], locale=translate_locale)
else:
posting_url = reverse('wiki.edit', args=[doc.slug])

if translate_locale:
# Post a new translation on doc
translate_url = reverse(
'wiki.translate',
args=[data['slug']],
locale=settings.WIKI_DEFAULT_LANGUAGE
) + '?tolocale={}'.format(translate_locale)
self.client.post(translate_url, data, follow=True)
data.update({'locale': translate_locale})

# Edit #1
resp = self.client.get(reverse('wiki.edit',
args=[doc.slug]))
resp = self.client.get(posting_url)
page = pq(resp.content)
rev_id1 = page.find('input[name="current_rev"]').attr('value')

# Edit #1 submits successfully
data.update({
'form-type': 'rev',
'content': 'Edit #1',
'current_rev': rev_id1
})
resp1 = self.client.post(
reverse('wiki.edit', args=[doc.slug]),
posting_url,
data,
HTTP_X_REQUESTED_WITH='XMLHttpRequest'
)

# Edit #2
resp = self.client.get(reverse('wiki.edit',
args=[doc.slug]))
resp = self.client.get(posting_url)
page = pq(resp.content)
rev_id2 = page.find('input[name="current_rev"]').attr('value')

# Edit #2 submits successfully
data.update({
'form-type': 'rev',
'content': 'This edit got there first',
'content': 'Edit #2',
'current_rev': rev_id2
})
resp2 = self.client.post(
reverse('wiki.edit', args=[doc.slug]),
posting_url,
data,
HTTP_X_REQUESTED_WITH='XMLHttpRequest'
)
Expand All @@ -2458,6 +2540,23 @@ def test_multiple_edits_ajax(self):
eq_(json.loads(resp.content)['error'], False)
ok_('error_message' not in json.loads(resp.content).keys())

def test_multiple_translation_edits_ajax(self):
"""Tests multiple sequential valid transalation edits that occur as Ajax POSTs."""
self.test_multiple_edits_ajax(translate_locale='es')

# test translation fails as well
def test_translation_midair_collission(self):
"""Tests midair collisions for non-ajax translation revisions."""
self.test_edit_midair_collisions(is_ajax=False, translate_locale='az')

def test_translation_midair_collission_ajax(self):
"""Tests midair collisions for ajax translation revisions."""
self.test_edit_midair_collisions(is_ajax=True, translate_locale='af')

def test_translation_spam_ajax(self):
"""Tests attempted translation spam edits that occur on Ajax POSTs."""
self.test_edit_spam_ajax(translate_locale='ru')

@pytest.mark.toc
def test_toc_toggle_off(self):
"""Toggling of table of contents in revisions"""
Expand Down

0 comments on commit bc15424

Please sign in to comment.