Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
bug 648401, opening the gates
Browse files Browse the repository at this point in the history
  • Loading branch information
davedash committed Apr 8, 2011
1 parent 041de65 commit 7abdbcb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 219 deletions.
155 changes: 3 additions & 152 deletions apps/feedback/tests/test_release_views.py
@@ -1,5 +1,6 @@
import json

from nose import SkipTest
from nose.tools import eq_
from product_details.version_compare import Version
from pyquery import PyQuery as pq
Expand Down Expand Up @@ -33,6 +34,7 @@ def test_no_ua(self):
@enforce_ua
def test_beta(self):
"""Beta version on release page: redirect."""
raise SkipTest
r = self._get_page('3.6b2')
self.assertRedirects(r, reverse('feedback', channel='beta'), 302, 302)

Expand All @@ -49,6 +51,7 @@ def notest_old_release(self):
@enforce_ua
def test_old_release(self):
"""Pre Fx4: Old release: redirect to beta download page."""
raise SkipTest
r = self._get_page('3.5')
eq_(r.status_code, 302)
assert r['Location'].endswith(reverse('feedback.download',
Expand Down Expand Up @@ -91,155 +94,3 @@ def post_feedback(self, data, ajax=False, follow=True):

return self.client.post(reverse('feedback', channel='release'), data,
**options)

def test_feedback_loads(self):
"""No general errors on release feedback page."""
r = self.client.get(reverse('feedback', channel='release'),
HTTP_USER_AGENT=(FX_UA % '20.0'),
follow=True)
eq_(r.status_code, 200)
doc = pq(r.content)
# Find all three forms
eq_(doc('article form').length, 3)

def test_feedback_wrongtype(self):
"""Test that giving a wrong type generates a 400."""
# Wrong number
r = self.post_feedback(dict(type=99, follow=False, ajax=True))
eq_(r.status_code, 400)

# Not a number
r = self.post_feedback(dict(type='pancakes', follow=False, ajax=True))
eq_(r.status_code, 400)

# No type
r = self.post_feedback(dict(follow=False, ajax=True))
eq_(r.status_code, 400)

def test_rating(self):
"""Submit rating form with and without AJAX."""
data = {'type': OPINION_RATING.id}
for type in RATING_USAGE:
data[type.short] = RATING_CHOICES[type.id % len(RATING_CHOICES)][0]

for ajax in True, False:
r = self.post_feedback(data, follow=False, ajax=ajax)
if not ajax:
eq_(r.status_code, 302)
assert r['Location'].endswith(
reverse('feedback', channel='release') + '#thanks')
else:
eq_(r.status_code, 200)
eq_(r['Content-Type'], 'application/json')

# Check the content made it into the database.
latest = Opinion.objects.no_cache().order_by('-id')[0]
eq_(latest.ratings.count(), len(RATING_USAGE))
latest.delete()

def test_rating_errors(self):
# Empty POST: Count errors
for ajax in True, False:
r = self.post_feedback({'type': OPINION_RATING.id}, ajax=ajax)
if not ajax:
doc = pq(r.content)
eq_(doc('article#rate form .errorlist').text(),
'Please rate at least one item.')
else:
eq_(r.status_code, 400)
errors = json.loads(r.content)
eq_(len(errors), 1)

def test_rating_one_item(self):
data = {'type': OPINION_RATING.id}
type = RATING_USAGE[0]
data[type.short] = 1
r = self.post_feedback(data, ajax=True)
eq_(r.status_code, 200)
latest = Opinion.objects.no_cache().order_by('-id')[0]
eq_(latest.ratings.count(), 1)

def test_broken(self):
"""Submit broken website report with and without AJAX."""
# Empty POST: Count errors
for ajax in True, False:
r = self.post_feedback({'type': OPINION_BROKEN.id}, ajax=ajax)
if not ajax:
doc = pq(r.content)
eq_(doc('article#broken form .errorlist').length, 2)
else:
eq_(r.status_code, 400)
errors = json.loads(r.content)
assert 'url' in errors
assert 'description' in errors

# Submit actual form
data = {
'type': OPINION_BROKEN.id,
'url': 'http://example.com/broken',
'description': 'This does not work.',
}

for ajax in True, False:
r = self.post_feedback(data, follow=False, ajax=ajax)
if not ajax:
eq_(r.status_code, 302)
assert r['Location'].endswith(
reverse('feedback', channel='release') + '#thanks')
else:
eq_(r.status_code, 200)
eq_(r['Content-Type'], 'application/json')

# Check the content made it into the database.
latest = Opinion.objects.no_cache().order_by('-id')[0]
eq_(latest.description, data['description'])
eq_(latest.url, data['url'])
latest.delete()

def test_idea(self):
"""Submit idea with and without AJAX."""
# Empty POST: Count errors
for ajax in True, False:
r = self.post_feedback({'type': OPINION_IDEA.id}, ajax=ajax)
if not ajax:
doc = pq(r.content)
eq_(doc('article#idea form .errorlist').length, 1)
else:
eq_(r.status_code, 400)
errors = json.loads(r.content)
assert 'description' in errors

# Submit actual form
data = {
'type': OPINION_IDEA.id,
'description': 'This is an idea.',
}

for ajax in True, False:
r = self.post_feedback(data, follow=False, ajax=ajax)
if not ajax:
eq_(r.status_code, 302)
assert r['Location'].endswith(
reverse('feedback', channel='release') + '#thanks')
else:
eq_(r.status_code, 200)
eq_(r['Content-Type'], 'application/json')

# Check the content made it into the database.
latest = Opinion.objects.no_cache().order_by('-id')[0]
eq_(latest.description, data['description'])
latest.delete()

def test_max_length(self):
"""
Ensure description's max_length attribute is propagated correctly for
JS to pick up.
"""
r = self.client.get(reverse('feedback', channel='release'),
HTTP_USER_AGENT=(FX_UA % '20.0'),
follow=True)
doc = pq(r.content)
eq_(doc('#count-broken-desc').attr('data-max'),
str(MAX_FEEDBACK_LENGTH))
eq_(doc('#count-idea-desc').attr('data-max'),
str(MAX_IDEA_LENGTH))
65 changes: 2 additions & 63 deletions apps/feedback/views.py
Expand Up @@ -142,9 +142,9 @@ def give_feedback(request, ua, type):

@forward_mobile
@vary_on_headers('User-Agent')
@enforce_ua(beta=True)
@enforce_ua(beta=False)
@cache_page
def beta_feedback(request, ua):
def feedback(request, ua):
"""
The index page for beta version feedback, which shows links to the happy
and sad feedback pages.
Expand All @@ -154,67 +154,6 @@ def beta_feedback(request, ua):
return jingo.render(request, template)


@forward_mobile
@vary_on_headers('User-Agent')
@enforce_ua(beta=False)
@cache_page
@csrf_exempt
def release_feedback(request, ua):
"""The index page for release version feedback."""
data = {
'RATING_USAGE': input.RATING_USAGE,
'RATING_CHOICES': input.RATING_CHOICES,
}

if request.method == 'POST':
try:
type = int(request.POST.get('type'))
FormType = {
input.OPINION_RATING.id: RatingForm,
input.OPINION_BROKEN.id: BrokenWebsiteForm,
input.OPINION_IDEA.id: IdeaReleaseForm,
}[type]
except (TypeError, ValueError, KeyError):
return http.HttpResponseBadRequest(_('Invalid feedback type'))

form = FormType(request.POST)
if form.is_valid():
save_opinion_from_form(request, type, ua, form)

if request.is_ajax():
return http.HttpResponse(json.dumps('ok'),
mimetype='application/json')
else:
return http.HttpResponseRedirect(
reverse('feedback', channel='release') + '#thanks')

elif request.is_ajax():
# For AJAX request, return errors only.
return http.HttpResponseBadRequest(json.dumps(form.errors),
mimetype='application/json')

else:
# For non-AJAX, return form with errors, and blank other feedback
# forms.
data.update(
rating_form=(form if type == input.OPINION_RATING.id else
RatingForm()),
website_form=(form if type == input.OPINION_BROKEN.id else
BrokenWebsiteForm()),
idea_form=(form if type == input.OPINION_IDEA.id
else IdeaReleaseForm()))

else:
data.update(rating_form=RatingForm(), website_form=BrokenWebsiteForm(),
idea_form=IdeaReleaseForm())

template = 'feedback/%srelease_index.html' % (
'mobile/' if request.mobile_site else '')
return jingo.render(request, template, data)

feedback = negotiate(beta=beta_feedback, release=release_feedback)


@cache_page
def need_beta(request):
"""Encourage people to download a current beta version."""
Expand Down
4 changes: 2 additions & 2 deletions apps/input/tests/test_redirects.py
Expand Up @@ -8,8 +8,8 @@ class RedirectTests(InputTestCase):
def test_redirects(self):
redirect = lambda x: '/en-US/%s/%s' % (settings.DEFAULT_CHANNEL, x)
redirs = {
'/feedback': '/en-US/release/feedback',
'/thanks': '/en-US/%s/thanks' % settings.DEFAULT_CHANNEL,
'/feedback': '/en-US/feedback',
'/thanks': '/en-US/thanks',
'/themes': '/en-US/%s/themes' % settings.DEFAULT_CHANNEL,
'/sites': redirect('sites')
}
Expand Down
5 changes: 3 additions & 2 deletions settings.py
Expand Up @@ -100,7 +100,8 @@ def __new__(self):
SUPPORTED_NONLOCALES = ('media', 'admin')

# TODO: These will be ported to /feeedback/beta
SUPPORTED_NONCHANNELS = ('media', 'admin', 'happy', 'sad', 'about', 'idea')
SUPPORTED_NONCHANNELS = ('media', 'admin', 'feedback', 'happy', 'sad', 'about',
'idea', 'thanks')
DEFAULT_CHANNEL = 'release'

TEXT_DOMAIN = 'messages'
Expand Down Expand Up @@ -343,4 +344,4 @@ def JINJA_CONFIG():
## FEATURE FLAGS:
# Setting this to False allows feedback to be collected from any user agent.
# (good for testing)
ENFORCE_USER_AGENT = True
ENFORCE_USER_AGENT = False

0 comments on commit 7abdbcb

Please sign in to comment.