On May 28, 9:23 am, irum <[email protected]> wrote:

<snip>

> 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
> ='[email protected]', 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

What is `c` in the second-last line of the test? Are you perhaps
instantiating a separate test client somewhere? Show the actual code
you're running.

Also note you don't need to define self.client in setUp(), as TestCase
does that already:
http://docs.djangoproject.com/en/1.2/topics/testing/#default-test-client
--
DR.

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

Reply via email to