def test_is_editor_is_false_removes_permission_from_instance(self): org_owner_factory = OrganizationOwnerFactory() org_user = org_owner_factory.organization_user user = org_user.user org = org_user.organization assign(GUARDIAN_PERMISSION, user, org) # Confirm permission is True self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org)) form = CustomOrganizationUserForm(data={'is_admin': True}, instance=org_user) form.cleaned_data = {'is_editor': False} form.save() # Now it should be False self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org))
def test_is_editor_is_true_adds_permission_to_instance(self): org_owner_factory = OrganizationOwnerFactory() org_user = org_owner_factory.organization_user user = org_user.user org = org_user.organization # Check that permission is False self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org)) # Need to pass {'is_admin': True} for clean_is_admin to validate form = CustomOrganizationUserForm(data={'is_admin': True}, instance=org_user) form.cleaned_data = {'is_editor': True} form.save() # Now it should be True self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_user_type_editor_adds_permission_to_instance(self): """ When editor is selected, the correct permission is set for that user for that organization. """ org_owner = OrganizationOwnerFactory() org_user = org_owner.organization_user user = org_user.user org = org_user.organization # Check the user doesn't have the permission self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org)) # Need to pass {'is_admin': True} for clean_is_admin to validate form = CustomOrganizationUserForm(instance=org_user) form.cleaned_data = {'user_type': 'editor'} form.save() # Now they should have the permission self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_user_type_viewer_removes_permission_from_instance(self): """ When is_editor gets unticked, the permission is removed. Also implicitly tests is_editor form field's required property because for a BooleanField, False is empty. """ org_owner = OrganizationOwnerFactory() org_user = org_owner.organization_user user = org_user.user org = org_user.organization assign_perm(GUARDIAN_PERMISSION, user, org) # Confirm user has the permission self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org)) form = CustomOrganizationUserForm(instance=org_user) form.cleaned_data = {'user_type': 'viewer'} form.save() # Now they shouldn't have the permission self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org))