0

Here's what I'm trying to do.

  1. Use a mixin to validate ownership of an object.

  2. Test the detail url to make sure that request.user == obj.owner

I expect the detail test to pass with a 200 assertion. But it's giving me a 302. But when I do a print from the mixing the request.user and owner are the same.

Here's my mixin:

from django.contrib.auth.mixins import UserPassesTestMixin

class IsOwnerMixin(UserPassesTestMixin):
    """
    a custom mixin that checks to see if the user is the owner of the object
    """

    def test_func(self):

        # get the object

        obj = self.get_object()

        # if the obj.user == the logged in user they can see it otherwise boo!

        if self.request.user == obj.owner:
            return True
        else:
            return False

Here's my View:

class AwesomeDetail(LoginRequiredMixin, IsOwnerMixin, DetailView):
    """
    An awesome detail
    """
    model = models.Awesome
    template_name = "awesomeness/detail.html"

Here's my Test:

from django.test import TestCase, RequestFactory
from django.test.client import Client
from django.contrib.auth.models import AnonymousUser, User, Group
from project.awesomness import views, models

class UrlsViewsTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = User.objects.create_user(id='1', username='name', email='email@email.com', password='top_secret')
        self.awesome = models.Awesome.objects.create(id='2', owner=self.user)
        self.not_owner = User.objects.create_user(id='3', username='trouble', email='otheremail@otheremail.com', password='top_secret')

    def test_awesome_detail(self):
        """
        Test the awesome detail URL
        """
        request = self.factory.get('/awesome/2/')
        request.user = self.user
        response = views.AwesomeDetail.as_view()(request, pk=2)
        self.assertEqual(response.status_code, 200)

    def test_awesome_not_owner(self):
        """
        Test the awesome detail with a user that is not the owner
        """
        request = self.factory.get('/awesome/2/')
        request.user = self.not_owner
        response = views.AwesomeDetail.as_view()(request, pk=2)
        self.assertEqual(response.status_code, 302)

    def test_awesome_detail_anonymous_user(self):
        """
        Test the awesome detail with a user that is anonymous
        """
        request = self.factory.get('/awesome/2/')
        request.user = AnonymousUser()
        response = views.AwesomeDetail.as_view()(request, pk=2)
        self.assertEqual(response.status_code, 302)

And finally, here's the result:

Creating test database for alias 'default'...
F..
======================================================================
FAIL: test_awesome_detail (project.awesomeness.tests.UrlsViewsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/dave/sandbox/project/project/awesomeness/tests.py", line 25, in test_awesome_detail
    self.assertEqual(response.status_code, 200)
AssertionError: 302 != 200

----------------------------------------------------------------------
Ran 3 tests in 0.202s

FAILED (failures=1)
Destroying test database for alias 'default'...
Dave Merwin
  • 1,382
  • 2
  • 22
  • 44
  • try to print the response and see what it returns. In the mixin, print `request.user` , `obj.owner` and `request.user==obj.owner` . It might give you some ideas. – sudshekhar Mar 12 '16 at 20:56
  • Hmm, this all works for me. Can you also try printing the response to see where it is redirecting to? – Daniel Roseman Mar 12 '16 at 21:20
  • print(response) It's going to the right place and including the right next. print(self.request.user == obj.owner) is returning False. It should be true. However, print(self.request.user) matches print(obj.owner) when they are printed individually – Dave Merwin Mar 12 '16 at 21:24
  • Actually wait, it's not returning anything now. – Dave Merwin Mar 12 '16 at 21:26
  • Ok, if I remove all of the tests EXCEPT the detail the print statements are all blank. So, it looks like the MixIn isn't getting run on the first run through? – Dave Merwin Mar 12 '16 at 21:31

0 Answers0