This commit is contained in:
Olivier Larchevêque 2013-08-10 22:14:06 -04:00
parent 61c29f1a3e
commit 369544071a
6 changed files with 117 additions and 6 deletions

View File

@ -1 +1,2 @@
from configuration import ConfigurationTest
from use import UseTest

View File

@ -5,7 +5,7 @@ from django.test.client import Client
from django.contrib.auth.models import User
from secretquestions.models import Question
from secretquestions.models import Question, Answer, crypt_answer
class SecretQuestionTest(TestCase):
@ -25,6 +25,20 @@ class SecretQuestionTest(TestCase):
def create_questions(self):
self.question1 = Question.objects.create(text="question1")
self.question1.save()
self.question2 = Question.objects.create(text="question2")
self.question2.save()
self.question3 = Question.objects.create(text="question3")
self.question3.save()
self.questions = (self.question1, self.question2, self.question3)
def create_answers(self):
self.answer1 = Answer.objects.create(question=self.question1,
secret=crypt_answer('one'), user=self.user)
self.answer1.save()
self.answer2 = Answer.objects.create(question=self.question2,
secret=crypt_answer('two'), user=self.user)
self.answer2.save()
self.answer3 = Answer.objects.create(question=self.question3,
secret=crypt_answer('three'), user=self.user)
self.answer3.save()

View File

@ -1,10 +1,16 @@
from django.conf.urls.defaults import patterns, include
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/(.*)', include(admin.site.urls)),
(r'^accounts/', include('registration.urls')),
(r'^secret/', include('secretquestions.urls')),)
(r'^admin/(.*)', include(admin.site.urls)),
(r'^accounts/', include('registration.urls')),
(r'^secret/', include('secretquestions.urls')),
url(r'^test_public/',
'secretquestions.tests.views.public',
name='sq_test_public'),
url(r'^test_private/',
'secretquestions.tests.views.private',
name='sq_test_private'),)

View File

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from secretquestions.tests.common import SecretQuestionTest
class UseTest(SecretQuestionTest):
"""
TestCase for accessing guarded pages.
"""
def test_access_public_for_anonymous(self):
"""
Check if you try to access public page as anonymous
"""
url = reverse('sq_test_public')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTrue('OK' in response.content)
def test_access_public_for_authenticated(self):
"""
Check if you try to access public page as authenticated
"""
self.assertEqual(self.client.login(username=self.username,
password=self.password), True)
url = reverse('sq_test_public')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTrue('OK' in response.content)
def test_access_private_for_anonymous(self):
"""
Check if you try to access private page as anonymous,
you are asking for username
"""
url = reverse('sq_test_private')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertFalse('OK' in response.content)
self.assertTrue('username' in response.content)
def test_access_private_for_authenticated(self):
"""
Check if you try to access private page as authenticated,
you are asking for first question
"""
self.create_answers()
self.assertEqual(self.client.login(username=self.username,
password=self.password), True)
url = reverse('sq_test_private')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertFalse('OK' in response.content)
self.assertFalse('username' in response.content)
def test_access_private_without_question(self):
"""
Check if you try to access private page as authenticated,
you don't have set any secret questions yet
you are redirected to the page
"""
self.assertEqual(self.client.login(username=self.username,
password=self.password), True)
url = reverse('sq_test_private')
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
self.assertFalse('OK' in response.content)
self.assertFalse('username' in response.content)

View File

@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from django.http import HttpResponse
from secretquestions.decorators import secret_questions_required
def public(request):
return HttpResponse("OK")
@secret_questions_required
def private(request):
return HttpResponse("OK")

View File

@ -48,6 +48,7 @@ class SecretQuestionWizard(FormWizard):
self.step_mapping = {}
if request.user.is_authenticated():
self.user = request.user
form_list = []
else:
if request.POST:
@ -62,7 +63,7 @@ class SecretQuestionWizard(FormWizard):
for answer in self.user.secret_answers.all():
self.step_mapping[len(form_list)] = answer
form_list.append(QuestionForm)
super(SecretQuestionWizard, self).__init__(form_list)
def get_form(self, step, data=None):
@ -90,3 +91,8 @@ class SecretQuestionWizard(FormWizard):
request.session[SQ_SESSION_KEY] = (token, path, datetime.now())
return redirect(url)
def __call__(self, request, *args, **kwargs):
if len(self.form_list) == 0:
return self.done(request, self.form_list)
return super(SecretQuestionWizard, self).__call__(request, *args, **kwargs)