From 016ab5a9c999ed0adfa1fefd799ba0963cfba0ca Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 23 Dec 2020 18:45:05 +0100 Subject: [PATCH] Remove dead code, don't try to cover unnecessary things Signed-off-by: Yohann D'ANELLO --- apps/permission/templatetags/perms.py | 41 ------------------- .../tests/test_permission_queries.py | 2 +- apps/permission/views.py | 2 +- apps/treasury/tables.py | 3 -- apps/treasury/tests/test_treasury.py | 7 +--- note_kfet/settings/secrets_example.py | 7 ++-- tox.ini | 2 +- 7 files changed, 9 insertions(+), 55 deletions(-) diff --git a/apps/permission/templatetags/perms.py b/apps/permission/templatetags/perms.py index 335721a1..7841f400 100644 --- a/apps/permission/templatetags/perms.py +++ b/apps/permission/templatetags/perms.py @@ -5,7 +5,6 @@ from django.contrib.auth.models import AnonymousUser from django.contrib.contenttypes.models import ContentType from django.template.defaultfilters import stringfilter from django import template -from note.models import Transaction from note_kfet.middlewares import get_current_authenticated_user, get_current_session from permission.backends import PermissionBackend @@ -25,21 +24,6 @@ def not_empty_model_list(model_name): return qs.exists() -@stringfilter -def not_empty_model_change_list(model_name): - """ - Return True if and only if the current user has right to change any object of the given model. - """ - user = get_current_authenticated_user() - session = get_current_session() - if user is None or isinstance(user, AnonymousUser): - return False - elif user.is_superuser and session.get("permission_mask", -1) >= 42: - return True - qs = model_list(model_name, "change") - return qs.exists() - - @stringfilter def model_list(model_name, t="view", fetch=True): """ @@ -68,33 +52,8 @@ def has_perm(perm, obj): return PermissionBackend.check_perm(get_current_authenticated_user(), perm, obj) -def can_create_transaction(): - """ - :return: True iff the authenticated user can create a transaction. - """ - user = get_current_authenticated_user() - session = get_current_session() - if user is None or isinstance(user, AnonymousUser): - return False - elif user.is_superuser and session.get("permission_mask", -1) >= 42: - return True - if session.get("can_create_transaction", None): - return session.get("can_create_transaction", None) == 1 - - empty_transaction = Transaction( - source=user.note, - destination=user.note, - quantity=1, - amount=0, - reason="Check permissions", - ) - session["can_create_transaction"] = PermissionBackend.check_perm(user, "note.add_transaction", empty_transaction) - return session.get("can_create_transaction") == 1 - - register = template.Library() register.filter('not_empty_model_list', not_empty_model_list) -register.filter('not_empty_model_change_list', not_empty_model_change_list) register.filter('model_list', model_list) register.filter('model_list_length', model_list_length) register.filter('has_perm', has_perm) diff --git a/apps/permission/tests/test_permission_queries.py b/apps/permission/tests/test_permission_queries.py index fdd530a5..95b8b7b1 100644 --- a/apps/permission/tests/test_permission_queries.py +++ b/apps/permission/tests/test_permission_queries.py @@ -78,7 +78,7 @@ class PermissionQueryTestCase(TestCase): query = instanced.query model = perm.model.model_class() model.objects.filter(query).all() - except (FieldError, AttributeError, ValueError, TypeError, JSONDecodeError): + except (FieldError, AttributeError, ValueError, TypeError, JSONDecodeError): # pragma: no cover print("Query error for permission", perm) print("Query:", perm.query) if instanced.query: diff --git a/apps/permission/views.py b/apps/permission/views.py index d77133d6..9ff9b50d 100644 --- a/apps/permission/views.py +++ b/apps/permission/views.py @@ -85,7 +85,7 @@ class ProtectedCreateView(LoginRequiredMixin, CreateView): If not, a 403 error is displayed. """ - def get_sample_object(self): + def get_sample_object(self): # pragma: no cover """ return a sample instance of the Model. It should be valid (can be stored properly in database), but must not collide with existing data. diff --git a/apps/treasury/tables.py b/apps/treasury/tables.py index 44415061..77d39a0e 100644 --- a/apps/treasury/tables.py +++ b/apps/treasury/tables.py @@ -109,9 +109,6 @@ class SpecialTransactionTable(tables.Table): 'a': {'class': 'btn btn-primary btn-danger'} }, ) - def render_id(self, record): - return record.specialtransactionproxy.pk - def render_amount(self, value): return pretty_money(value) diff --git a/apps/treasury/tests/test_treasury.py b/apps/treasury/tests/test_treasury.py index e51054b6..98fb2249 100644 --- a/apps/treasury/tests/test_treasury.py +++ b/apps/treasury/tests/test_treasury.py @@ -370,11 +370,8 @@ class TestSogeCredits(TestCase): response = self.client.get(reverse("treasury:manage_soge_credit", args=(soge_credit.pk,))) self.assertEqual(response.status_code, 200) - try: - self.client.post(reverse("treasury:manage_soge_credit", args=(soge_credit.pk,)), data=dict(delete=True)) - raise AssertionError("It is not possible to delete the soge credit until the note is not credited.") - except ValidationError: - pass + self.assertRaises(ValidationError, self.client.post, + reverse("treasury:manage_soge_credit", args=(soge_credit.pk,)), data=dict(delete=True)) SpecialTransaction.objects.create( source=NoteSpecial.objects.get(special_type="Carte bancaire"), diff --git a/note_kfet/settings/secrets_example.py b/note_kfet/settings/secrets_example.py index 656e558b..61d92359 100644 --- a/note_kfet/settings/secrets_example.py +++ b/note_kfet/settings/secrets_example.py @@ -1,12 +1,13 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -# CAS OPTIONAL_APPS = [ -# 'debug_toolbar' + # 'cas_server', + # 'debug_toolbar', + # 'django_extensions', ] -# When a server error occured, send an email to these addresses +# When a server error occurred, send an email to these addresses ADMINS = ( ('Note Kfet', 'notekfet@example.com'), ) diff --git a/tox.ini b/tox.ini index 1c32a412..d972e34b 100644 --- a/tox.ini +++ b/tox.ini @@ -15,7 +15,7 @@ deps = -r{toxinidir}/requirements.txt coverage commands = - coverage run --omit='*migrations*,apps/scripts*' --source=apps,note_kfet ./manage.py test apps/ + coverage run --omit='apps/scripts*,*_example.py,note_kfet/wsgi.py' --source=apps,note_kfet ./manage.py test apps/ coverage report -m [testenv:linters]