Hi,
I am trying to test my pages that need authentication. But, I am not
able to test views that need authentication. I have tried following
threads about logging in test client in this group and django
documentation, but still cant figure out.

This my view I am trying to test:

def cauction(request)
    if not request.user.is_authenticated():
        print "redirecting" # printing this to check which if branch is
executing
        print request.user
        return HttpResponseRedirect('/login/?next=%s' % request.path)
    else:
         print "authenticated_branch"  # printing this to check which if
branch is executing
         errors = []
         if not request.method == 'POST':
            form = createAuction()
            return render_to_response('createauction.html', {'form' : form} )
         else:
            form = createAuction(request.POST)
            if form.is_valid():
               cd = form.cleaned_data
               uname = request.user.username
               a_title = cd['title']
               a_desc = cd['description']
               a_minprice = cd['min_price']
               a_end_date = cd['end_date']
               a_start_date = datetime.datetime.now()
               if not a_end_date > a_start_date:
                   e = _('End date should be greater than start date.')
                   errors.append(e)
               else:
                   dt = a_end_date - a_start_date
                   day = dt.days
                   total_secs=dt.seconds
                   secs = total_secs % 60
                   total_mins = total_secs / 60
                   mins = total_mins % 60
                   hours = total_mins / 60
                   th = hours + (day * 24)
                   if th > 72 or th == 72:
                      uname = request.user.username
                      form = confAuction()
                      return render_to_response('wizardTest.html', {'form' : 
form,
'a_title' : a_title, 'a_desc' : a_desc,'a_start-date' : a_start_date,
'a_end_date' : a_end_date, 'a_minprice' : a_minprice, 'uname' :
uname})
                   else:
                      e = _('Auction duration should be at least 72 hours')
                      errors.append(e)
            else:
              e = _('Enter Valid data')
              errors.append(e)
            return render_to_response('createauction.html', {'form' : form,
'errors' : errors})

This view is called by this pattern in urls.py

urlpatterns = patterns('',('^$', yaas_homepage) ,
                       (r'^createauction/$', cauction), ...)

This is my tests.py

import unittest
from django.test.client import Client
from django.test import Client, TestCase
from django.contrib.auth import authenticate
from django.contrib.auth.models import *
from django.core import mail
import datetime
import time


class SimpleTest(TestCase):
    fixtures = ['f1.json']

    def setUp(self): # Every test needs a client
        self.client = Client()

    def test_create(self):

        response = self.client.get('/createauction/')
        # Check that it takes to login page if not logged n
        self.failUnlessEqual(response.status_code, 302)
        self.assertRedirects(response, '/login/?next=/createauction/')


# i have created a user here again as some threads suggested that I
should make a user here.
        user = User.objects.create_user(username = 'myadmin2', email
='du...@dummy.com', password = 'testing2')
        uid = user.id
        user.is_staff = True
        user.save()

        print "trying to login..."

        login = self.client.login(username='myadmin2',
password='testing2')
        self.failUnless(login, True)
        self.assertEqual( login, True) # this test does not fail,
means user has logged in succefully and login is True

       enddate = datetime.datetime(2010, 06, 04, 3, 45, 50)

        post_data = {
             'title' : 'Title1',
             'desc' : 'description',
             'start-date' : datetime.datetime.now() ,
             'end_date' : enddate,
             'owner' : user.id,
             'price' : 60,
             'highbid' : 60,
             'banned' : False,
             'closed' :  True,

        }


        r = c.post('/createauction/', post_data)
        self.failUnlessEqual(r.status_code, 200) # this test fails, as
status_code returned is 302

These tests are run successully, meaning the user is redirected to
login page, implying that client never enters the else branch for
authenticated user in the 'cauction' view.
       self.failUnlessEqual(r.status_code, 302)
        self.assertRedirects(response, '/login/?next=/createauction/')

I have put in more than a day in this problem, trying to figure out
where I am going wrong.  I have tried to explain clearly here. Any one
has any idea?

Thanks,
irum


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to