Testing client's force_login method doesn't seem to be logging in.

474 views
Skip to first unread message

Quentin Fulsher

unread,
Mar 26, 2017, 5:35:08 PM3/26/17
to Django users
I'm trying to write a test case for the viewing permissions on a certain webpage. I'm trying to get the client to force_login as a user to test if they can get the view or not. The issue is that my client doesn't seem to be logged in. No matter who I try to login as (There are a few different types of users to test) I get redirected to the login page. The view is decorated with  login_required so this makes a little bit of sense but I'm still not sure why its happening.

Here's the code for my test case:

class AvailabilityListTestCase(TestCase):
   
def setUp(self):
       
self.admin = User.objects.create(username='admin', email='ad...@sb.com', is_superuser=True, is_staff=True)
       
self.admin.set_password('admin')
       
self.student = User.objects.create(username='student', email='stu...@sb.com')
       
self.student.set_password('student')
       
self.student2 = User.objects.create(username='student2', email='stud...@sb.com')
       
self.student2.set_password('student2')
       
self.tutor = User.objects.create(username='tutor', email='tu...@sb.com')
       
self.tutor.set_password('tutor')
       
self.tutor2 = User.objects.create(username='tutor2', email='tut...@sb.com')
       
self.tutor2.set_password('tutor2')
       
self.student.tutor = self.tutor
       
self.c = Client()
       
self.url = '/user/tutor/availabilities/'


   
def test_anon_access(self):
        response
= self.c.get(self.url)
       
self.assertRedirects(response, '/user/login/?next=/user/tutor/availabilities/')


   
def test_owner_access(self):
       
self.c.force_login(self.tutor)
        response
= self.c.get(self.url)
       
self.assertEqual(200, response.status_code)


Here's the view:

@login_required
def list_availabilities(request, username):
    curr_user
= request.user
   
if not curr_user.is_staff or curr_user.username != username:
       
return HttpResponseForbidden()
    requested_user
= get_object_or_404(User.objects.all(), username=username)
    requested_user_profile
= get_user_profile(requested_user)
    availabilities
= requested_user_profile.availability_set.all()
    formatted_list
= []
   
for availability in availabilities:
        formatted_list
.append(
           
(
                availability
.title,
               
# Format meaning: `12h:minute am/pm`
                availability
.start_time.strftime("%l:%M %p"),
                availability
.end_time.strftime("%l:%M %p"),
           
)
       
)
   
return render(
        request
,
       
'appointments/availability_list.html',
       
{
           
'tutor_username':username,
           
'formatted_list':formatted_list,
       
}
   
)



Thanks!

Quentin Fulsher

unread,
Mar 26, 2017, 5:41:49 PM3/26/17
to Django users
Actually I just fixed it, You have to save the model after you set the password.
Reply all
Reply to author
Forward
0 new messages