I am writing tests for my Django site. One of the tests--in part--tests the permissions of the user. My app uses the Django builtin auth system. Running this test by itself succeeds, running the entire suite fails because the group that used to have the necessary permissions has no permissions by the time the permissions are checked. Here is the test that fails:
def test_navbar(self):
user = User.objects.get(username='test2')
print 'Active' if user.is_active else 'Inactive'
print 'Has Perm' if user.has_perm('auth.add_user') else 'No Perm'
print user.groups.all()
print 'overseers permissions:', Group.objects.get(name='overseers').permissions.values('codename')
# ... Tests for unauthenticated users
# ... Tests for authenticated, unprivileged users
# Privileged users (overseers)
self.logout_using_popup()
self.login_using_popup('test2', 'test2password')
self.selenium.get('{}{}'.format(self.live_server_url, reverse('platformdemand:userlist')))
links = self.selenium.find_elements_by_css_selector('.navbar-nav > li')
activeli = self.find_element_containing_value(links, 'class', 'active')
# This assertion fails because the last get was redirected
# to a login page where none of the navbar li-s are active.
assert len(activeli) == 1, len(activeli)
activelink = activeli[0].find_element_by_tag_name('a')
expected = '{}{}'.format(self.live_server_url, reverse('platformdemand:userlist'))
assert activelink.get_attribute('href') == expected, activelink.get_attribute('href')
The output when running the test by itself is:
Active
Has Perm
[<Group: overseers>]
overseers permissions: [{'codename': u'add_user'}, {'codename': u'change_user'}, {'codename': u'delete_user'}, {'codename': u'add_assembly'}, {'codename': u'change_assembly'}, {'codename': u'delete_assembly'}, {'codename': u'add_assemblyrequest'}, {'codename': u'change_assemblyrequest'}, {'codename': u'delete_assemblyrequest'}, {'codename': u'add_assemblytype'}, {'codename': u'change_assemblytype'}, {'codename': u'delete_assemblytype'}, {'codename': u'add_component'}, {'codename': u'change_component'}, {'codename': u'delete_component'}, {'codename': u'add_componentrequest'}, {'codename': u'change_componentrequest'}, {'codename': u'delete_componentrequest'}, {'codename': u'add_componenttype'}, {'codename': u'change_componenttype'}, '...(remaining elements truncated)...']
Showing that the group has the permissions. Running the test in the suite gives:
Active
No Perm
[<Group: overseers>]
overseers permissions: []
Showing that the group has lost it's permissions. I suspect that this might have to do with this section of the documentation: Advanced features of TransactionTestCase. It mentions that "post_migrate is fired before each test to create the content types and permissions for each model in available apps, in case they’re missing." I suspect that this causes all permissions to be removed (to be recreated later), and then the groups' relations to the permissions are lost. That page mentions that post_migrate is not fired when available_apps is set. Unfortunately setting that to only include my app seems to prevent my fixture (which includes some users and groups) from installing.
I attempted to search the Django source code for more information about what was happening for post_migrate, but I couldn't find any post_migrate Signal receivers. And for all I know, post_migrate has nothing to do with what I am seeing.
Why are my groups losing their permissions between tests?
Aucun commentaire:
Enregistrer un commentaire