def test_default_user_can_not_change_data(): gp = GroupPermissions() gp.setup_groups_and_permissions() u1 = G(User) request = mock.Mock(method="POST", user=u1) perm_obj = CanEditOrReadOnly() assert perm_obj.has_object_permission(request, None, None) is False
def test_logging_happens_when_verbose_is_true(capsys): gp = GroupPermissions() gp.verbose = True gp.log('Hello World') output, _ = capsys.readouterr() assert 'Hello World\n' == output
def test_logging_doesnt_happen_when_verbose_is_false(capsys): gp = GroupPermissions() gp.verbose = False gp.log('Hello World') output, _ = capsys.readouterr() assert '' == output
def create_groups_with_permissions(codenames): g1, _ = Group.objects.get_or_create(name="Test Group 1") g2, _ = Group.objects.get_or_create(name="Test Group 2") gp = GroupPermissions() with gp.groups(g1, g2): gp.add_permissions(Group, *codenames) return (g1, g2)
def test_editor_can_change_data(): gp = GroupPermissions() gp.setup_groups_and_permissions() u1 = G(User) edit_perm = Permission.objects.get(codename='edit_logframe') u1.user_permissions.add(edit_perm) request = mock.Mock(method="POST", user=u1) perm_obj = CanEditOrReadOnly() assert perm_obj.has_object_permission(request, None, None) is True
def test_is_editable_succeeds_on_editors(): gp = GroupPermissions() gp.setup_groups_and_permissions() edit_perm = Permission.objects.get(codename='edit_logframe') u1 = G(User) u1.user_permissions.add(edit_perm) mixin = AptivateDataBaseMixin() mixin.request = mock.Mock() mixin.request.user = u1 assert mixin.is_editable() is True
def test_deleting_all_group_permissions(): any_model = Group # for example codenames = ['i_can_do_stuff', 'u_can_do_more_stuff'] create_expected_permissions(any_model, codenames) g1, g2 = create_groups_with_permissions(codenames) gp = GroupPermissions() gp.delete_all_group_permissions() assert list(g1.permissions.all()) == [] assert list(g2.permissions.all()) == []
def test_perm_name_returns_permission_name(): # This returns the permission name in the format that would be used when # checking if a user has it - app_label.codename. This format is coded into # the GroupPermissions.perm_name method. permission = get_first_custom_group_permission() assert 'contacts.{0}'.format(permission.codename) == GroupPermissions.perm_name(permission)
def test_group_permissions_get_perm_retrieves_permission_with_given_name(): expected_permission = get_first_custom_group_permission() permission_name = GroupPermissions.custom_permissions[0][0] permission = GroupPermissions.get_perm(permission_name) assert expected_permission == permission
def test_add_perms(): g1, _ = Group.objects.get_or_create(name="Test Group 1") g2, _ = Group.objects.get_or_create(name="Test Group 2") any_model = Group # for example content_type = ContentType.objects.get_for_model(any_model) codenames = ['a_do_stuff', 'b_do_more_stuff'] expected_permissions = [] for name in codenames: perm, _ = Permission.objects.get_or_create(name=name, codename=name, content_type=content_type) expected_permissions.append(perm) gp = GroupPermissions() with gp.groups(g1, g2): gp.add_permissions(any_model, *codenames) assert list(g1.permissions.all()) == expected_permissions assert list(g2.permissions.all()) == expected_permissions
def test_has_permission_returns_true_when_user_has_permission(): user = User.objects.create(business_email="*****@*****.**") users_group = Group.objects.create(name='test_group') user.groups.add(users_group) permission = get_first_custom_group_permission() users_group.permissions.add(permission) assert GroupPermissions.has_perm(user, permission.codename)
def test_has_permission_returns_false_when_user_doesnt_have_permission(): user = User.objects.create(business_email="*****@*****.**") permission = get_first_custom_group_permission() assert not GroupPermissions.has_perm(user, permission.codename)
def handle_noargs(self, **options): verbose = int(options.get('verbosity', 1)) > 1 try: GroupPermissions(verbose=verbose).setup_groups_and_permissions() except ObjectDoesNotExist as e: raise CommandError(e.message)