﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28140	Add convenience method to add  `Permission`s to a `User`	Flavio Curella	nobody	"When writing tests checking for permissions, it's commonplace to test if a user can access a particular view, check for failure, add the necessary permission, then check again for success:

{{{
def mytest(self):
    user = User.objects.create_user(
            username='regularuser',
            password='regularuser',
    )
    response = self.client.get('/mymodel/create/')
    # Usually redirects to some other page, such as `/login/`
    self.assertNotEqual(response.status_code, 200)

    # then we fetch the 'myapp.add_mymodel' permission and add it to `user`
    my_permission = Permission.objects.get_by_natural_key('add_mymodel', 'myapp', 'mymodel')
    user.user_permissions.add(mypermission)

    response = self.client.get('/mymodel/create/')
    # now it works
    self.assertEqual(response.status_code, 200)

}}}

I'm wondering if we should have a more convenient way to add permissions. I'm thinking of adding a `add_perm` method to the `User` model, that mirrors the already existing [https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.has_perm `has_perm`] so that instead of

{{{
    my_permission = Permission.objects.get_by_natural_key('add_mymodel', 'myapp', 'mymodel')
    user.user_permissions.add(mypermission)

    response = self.client.get('/mymodel/create/')
    self.assertEqual(response.status_code, 200)
}}}

we can have:

{{{
    user.add_perm('myapp.add_mymodel')
    response = self.client.get('/mymodel/create/')
    self.assertEqual(response.status_code, 200)
}}}
"	New feature	new	contrib.auth	1.11	Normal				Someday/Maybe	0	0	0	0	0	0
