Commit f786ff76 authored by Sofia Papagiannaki's avatar Sofia Papagiannaki

astakos: Fix approval_terms view

Fix crash when request user is not logged in.
Add tests
parent 92516cc4
......@@ -9,7 +9,7 @@
{% if approval_terms_form %}
<div class="section">
<form action="{% url latest_terms %}" method="post" class="login innerlabels">{% csrf_token %}
<form name="approval_terms_form" action="{% url latest_terms %}" method="post" class="login innerlabels">{% csrf_token %}
{% with approval_terms_form as form %}
{% include "im/form_render.html" %}
{% endwith %}
......
......@@ -31,8 +31,15 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import astakos.im.messages as astakos_messages
from astakos.im.models import ApprovalTerms
from astakos.im.tests.common import *
from django.core import urlresolvers
from django.utils.translation import ugettext as _
import os
class TestViews(TestCase):
......@@ -48,3 +55,63 @@ class TestViews(TestCase):
r = self.client.get(reverse('api_access_config'))
self.assertContains(r, user.auth_token)
class TestApprovalTerms(TestCase):
def tearDown(self):
os.remove('terms')
ApprovalTerms.objects.get(location='terms').delete()
def test_approval_terms(self):
r = self.client.get(reverse('latest_terms'), follow=True)
self.assertEqual(r.status_code, 200)
self.assertContains(r, _(astakos_messages.NO_APPROVAL_TERMS))
r = self.client.post(reverse('latest_terms'), follow=True)
self.assertEqual(r.status_code, 200)
self.assertContains(r, _(astakos_messages.NO_APPROVAL_TERMS))
# add terms
f = open('terms', 'w+')
f.write('This are some terms')
f.close()
terms = ApprovalTerms(location='terms')
terms.save()
self.user = get_local_user('user@synnefo.org')
self.assertTrue(not self.user.signed_terms)
self.assertTrue(self.user.date_signed_terms is None)
self.user_client = get_user_client(self.user.username)
r = self.client.get(reverse('latest_terms'))
self.assertEqual(r.status_code, 200)
self.assertTemplateUsed(r, 'im/approval_terms.html')
# assert there is no form
self.assertNotContains(r, 'I agree with the terms')
r = self.client.post(reverse('latest_terms'), follow=False)
self.assertEqual(r.status_code, 302)
# assert redirect to login
self.assertTrue('Location' in r)
self.assertTrue(r['Location'].find(reverse('login')) != -1)
r = self.user_client.get(reverse('latest_terms'), follow=True)
self.assertEqual(r.status_code, 200)
self.assertTemplateUsed(r, 'im/approval_terms.html')
# assert there is form
self.assertContains(r, 'I agree with the terms')
r = self.user_client.post(reverse('latest_terms'), follow=True)
self.assertEqual(r.status_code, 200)
self.assertFormError(r, 'approval_terms_form', 'has_signed_terms',
_(astakos_messages.SIGN_TERMS))
r = self.user_client.post(reverse('latest_terms'),
{'has_signed_terms': True},
follow=True)
self.assertEqual(r.status_code, 200)
user = AstakosUser.objects.get(username=self.user.username)
self.assertTrue(user.signed_terms)
......@@ -663,29 +663,48 @@ def activate(request, greeting_email_template_name='im/welcome_email.txt',
return response
@login_required
def _approval_terms_post(request, template_name, terms, extra_context):
next = restrict_next(
request.POST.get('next'),
domain=settings.COOKIE_DOMAIN
)
if not next:
next = reverse('index')
form = SignApprovalTermsForm(request.POST, instance=request.user)
if not form.is_valid():
return render_response(template_name,
terms=terms,
approval_terms_form=form,
context_instance=get_context(request,
extra_context))
user = form.save()
return HttpResponseRedirect(next)
@require_http_methods(["GET", "POST"])
@cookie_fix
def approval_terms(request, term_id=None,
template_name='im/approval_terms.html', extra_context=None):
extra_context = extra_context or {}
term = None
terms_record = None
terms = None
if not term_id:
try:
term = ApprovalTerms.objects.order_by('-id')[0]
terms_record = ApprovalTerms.objects.order_by('-id')[0]
except IndexError:
pass
else:
try:
term = ApprovalTerms.objects.get(id=term_id)
terms_record = ApprovalTerms.objects.get(id=term_id)
except ApprovalTerms.DoesNotExist, e:
pass
if not term:
if not terms_record:
messages.error(request, _(astakos_messages.NO_APPROVAL_TERMS))
return HttpResponseRedirect(reverse('index'))
try:
f = open(term.location, 'r')
f = open(terms_record.location, 'r')
except IOError:
messages.error(request, _(astakos_messages.GENERIC_ERROR))
return render_response(
......@@ -695,21 +714,8 @@ def approval_terms(request, term_id=None,
terms = f.read()
if request.method == 'POST':
next = restrict_next(
request.POST.get('next'),
domain=settings.COOKIE_DOMAIN
)
if not next:
next = reverse('index')
form = SignApprovalTermsForm(request.POST, instance=request.user)
if not form.is_valid():
return render_response(template_name,
terms=terms,
approval_terms_form=form,
context_instance=get_context(request,
extra_context))
user = form.save()
return HttpResponseRedirect(next)
return _approval_terms_post(request, template_name, terms,
extra_context)
else:
form = None
if request.user.is_authenticated() and not request.user.signed_terms:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment