Django tests : doesn't work for testing case for is_staff user
That seems to be a silly question explained by some stupid error, but I could not figure it out for the moment.
General situation :
I have a little Djano app used as small database for some items.
Items could be viewed, edit or deleted.
View is available for all users, but edit/delete only for Staff users.
It was implemented and during manual test it works fine (if I'm not staff user I get 403 for edit/delete pages, then if I become staff user : I could edit/delete).
But the automatic test Fail and I could not figure why.
Here is the test code :
def createTestUserAndLogin(self):
self.user = User.objects.create_user(username='testuser', password='qwerty')
login = self.client.login(username='testuser', password='qwerty')
def createTestStaffUserAndLogin(self):
self.user = User.objects.create_user(username='staffuser', password='qwerty_staff', is_staff=True)
login = self.client.login(username='testuser', password='qwerty_staff')
self.assertTrue(self.user.is_staff, "User is not staff")
def test_delete_item_as_non_staff_user(self):
test_serial = "AA1111"
new_item = Item(serial_number=test_serial)
new_item.save()
self.createTestUserAndLogin()
url = reverse("delete_item", kwargs={'pk':new_item.id})
resp = self.client.get(url)
self.assertEqual(resp.status_code, 403)
self.client.logout()
def test_delete_item_as_staff_user(self):
test_serial = "AA1111"
new_item = Item(serial_number=test_serial)
new_item.save()
# Could delete with Staff user
self.createTestStaffUserAndLogin()
self.assertTrue(self.user.is_staff, "User is not staff")
url = reverse("delete_item", kwargs={'pk':new_item.id})
resp = self.client.get(url)
self.assertNotEqual(resp.status_code, 403)
confirm_data = {'delete': 'yes'}
resp = self.client.post(url, confirm_data)
self.assertEqual(Item.objects.all().count(), 0)
If I launch manage.py test on these tests I get error :
in test_delete_item_as_staff_user
self.assertNotEqual(resp.status_code, 403)
AssertionError: 403 == 403
At first moment, I though that may be there is some problem with creating Staff user, but self.assertTrue(self.user.is_staff, "User is not staff")
pass, so created user is really Staff.
So I have 2 questions.
- is there something wrong with my test ?
- but if my test is ok... how could I debug it to find what is wrong with my app ? Manually I could set breakpoints and search for something, but the problem that manual test works just fine.