Actually I just fixed it, You have to save the model after you set the 
password.

On Sunday, March 26, 2017 at 2:35:08 PM UTC-7, Quentin Fulsher wrote:
>
> 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='
> stud...@sb.com')
>         self.student.set_password('student')
>         self.student2 = User.objects.create(username='student2', email='
> stude...@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!
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c0244ed1-5fd6-4525-b13e-7b772a69b3ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to