add ut
authorOlivier Larchevêque <olivier.larcheveque@gmail.com>
Sun, 11 Aug 2013 02:14:06 +0000 (22:14 -0400)
committerOlivier Larchevêque <olivier.larcheveque@gmail.com>
Sun, 11 Aug 2013 02:14:06 +0000 (22:14 -0400)
secretquestions/tests/__init__.py
secretquestions/tests/common.py
secretquestions/tests/urls.py
secretquestions/tests/use.py [new file with mode: 0644]
secretquestions/tests/views.py [new file with mode: 0644]
secretquestions/views.py

index c48b029..617f330 100644 (file)
@@ -1 +1,2 @@
 from configuration import ConfigurationTest
+from use import UseTest
index da63625..1e3685b 100644 (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()
index 7a851c7..b0dd12e 100644 (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'),)
diff --git a/secretquestions/tests/use.py b/secretquestions/tests/use.py
new file mode 100644 (file)
index 0000000..6f9b1d9
--- /dev/null
@@ -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)
diff --git a/secretquestions/tests/views.py b/secretquestions/tests/views.py
new file mode 100644 (file)
index 0000000..4d1d191
--- /dev/null
@@ -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")
index 10285bb..a2524a6 100644 (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)