I'm writing tests for locallibrary app following the tutorial on MDN. Both classes that test views share similar login code, but it only works in one class
I tried finding spelling errors and comping both classes
Code snippets:
Login throws FAIL:
class AuthorCreateViewTest(TestCase):
def setUp(self):
# Create two users
test_user1 = User.objects.create_user(username='testuser1', password='fjo&*&d3h')
test_user2 = User.objects.create_user(username='testuser2', password='J9cdj8we9')
test_user1.save()
# Give test_user2 permission to renew books.
permission = Permission.objects.get(name='Set book as returned')
test_user2.user_permissions.add(permission)
test_user2.save()
def test_view_url_accessible_by_name(self):
login = self.client.login(username='testuser2', password='J9cdj8we9')
response = self.client.get(reverse('author-create'))
self.assertEqual(response.status_code, 200)
Login works:
class RenewBookInstancesViewTest(TestCase):
def setUp(self):
# Create two users
test_user1 = User.objects.create_user(username='testuser1', password='fjo&*&d3h')
test_user2 = User.objects.create_user(username='testuser2', password='J9cdj8we9')
test_user1.save()
test_user2.save()
# Give test_user2 permission to renew books.
permission = Permission.objects.get(name='Set book as returned')
test_user2.user_permissions.add(permission)
test_user2.save()
def test_logged_in_with_permission_borrowed_book(self):
login = self.client.login(username='testuser2', password='J9cdj8we9')
response = self.client.get(reverse('renew-book-librarian', kwargs={'pk':self.test_bookinstance2.pk}))
# Check that it lets us login - this is our book and we have the right permissions.
self.assertEqual(response.status_code, 200)
Here is the github link with all code in classes
Incorrect Permissions Being Set
The permission is wrong here:
class AuthorCreateViewTest(TestCase):
def setUp(self):
# Create two users
test_user1 = User.objects.create_user(username='testuser1', password='fjo&*&d3h')
test_user2 = User.objects.create_user(username='testuser2', password='J9cdj8we9')
test_user1.save()
# Give test_user2 permission to renew books.
permission = Permission.objects.get(name='Set book as returned')
test_user2.user_permissions.add(permission)
test_user2.save()
because in this test, which is the first that fails
def test_logged_in_with_correct_permission(self):
login = self.client.login(username='testuser4', password='9dj2q8f')
response = self.client.get(reverse('author-create'))
self.assertEqual(response.status_code, 200)
you are redirecting to author-create
, but the permission there is NOT Set book as returned
, rather Add or modify book records and information about them
, thus change it to this:
class AuthorCreateViewTest(TestCase):
def setUp(self):
# Create two users
test_user3 = User.objects.create_user(username='testuser3', password='nd392dn')
test_user4 = User.objects.create_user(username='testuser4', password='9dj2q8f')
# Give test_user2 permission to renew books.
# permission = Permission.objects.get(name='Set book as returned')
# test_user4.user_permissions.add(permission)
permission = Permission.objects.get(name='Add or modify book records and information about them')
test_user4.user_permissions.add(permission)
Also, as an aside, both create_user
and add
save by default, so it is not necessary to do test_user4.save()
in these. I'm not sure why the MDN tutorial is showing that.