From 9780fc9eedb382dc4769bd576e2e16a1c589358d Mon Sep 17 00:00:00 2001 From: Camille Daniel Date: Thu, 29 Jul 2021 11:37:02 +0200 Subject: [PATCH] Enable group deletion --- canaille/groups.py | 14 ++++++++ canaille/static/js/{profile.js => confirm.js} | 0 canaille/templates/base.html | 6 ++-- canaille/templates/group.html | 34 ++++++++++++++++++- canaille/templates/groups.html | 3 ++ canaille/templates/profile.html | 2 +- canaille/templates/users.html | 2 ++ tests/test_groups.py | 15 ++++++-- 8 files changed, 68 insertions(+), 8 deletions(-) rename canaille/static/js/{profile.js => confirm.js} (100%) diff --git a/canaille/groups.py b/canaille/groups.py index abb55904..b369d939 100644 --- a/canaille/groups.py +++ b/canaille/groups.py @@ -44,6 +44,16 @@ def create_group(user): @moderator_needed() def group(user, groupname): group = Group.get(groupname) or abort(404) + + if request.method == "GET" or request.form.get("action") == "edit": + return edit_group(group) + + if request.form.get("action") == "delete": + return delete_group(group) + + abort(400) + +def edit_group(group): form = GroupForm(request.form or None, data={"name": group.name}) form["name"].render_kw["disabled"] = "true" @@ -60,3 +70,7 @@ def group(user, groupname): members=group.get_members() ) +def delete_group(group): + flash(_("The group %(group)s has been sucessfully deleted", group=group.name), "success") + group.delete() + return redirect(url_for("groups.groups")) diff --git a/canaille/static/js/profile.js b/canaille/static/js/confirm.js similarity index 100% rename from canaille/static/js/profile.js rename to canaille/static/js/confirm.js diff --git a/canaille/templates/base.html b/canaille/templates/base.html index ef4e43ac..62dd8b2c 100644 --- a/canaille/templates/base.html +++ b/canaille/templates/base.html @@ -48,9 +48,9 @@ {% trans %}Users{% endtrans %} - - {% trans %}Groups{% endtrans %} + href="{{ url_for('groups.groups') }}"> + + {% trans %}Groups{% endtrans %} {% endif %} {% if user.admin %} diff --git a/canaille/templates/group.html b/canaille/templates/group.html index 19856847..20602c42 100644 --- a/canaille/templates/group.html +++ b/canaille/templates/group.html @@ -2,7 +2,29 @@ {% import 'fomanticui.j2' as sui %} {% import 'flask.j2' as flask %} +{% block script %} + +{% endblock %} + {% block content %} +{% if edited_group %} + +{% endif %} + {% if edited_group %}

{% trans %}Members{% endtrans %}

@@ -46,13 +68,23 @@ > {{ form.hidden_tag() if form.hidden_tag }} {{ sui.render_field(form.name) }} - + {% if edited_group %} + + {% endif %}
{% endblock %} diff --git a/canaille/templates/groups.html b/canaille/templates/groups.html index 94d87286..1eff5f5f 100644 --- a/canaille/templates/groups.html +++ b/canaille/templates/groups.html @@ -1,6 +1,9 @@ {% extends 'base.html' %} +{% import 'flask.j2' as flask %} {% block content %} +{{ flask.messages() }} +
{% trans %}Add a group{% endtrans %}
diff --git a/canaille/templates/profile.html b/canaille/templates/profile.html index 2bd6414a..0dbfcf3e 100644 --- a/canaille/templates/profile.html +++ b/canaille/templates/profile.html @@ -3,7 +3,7 @@ {% import 'flask.j2' as flask %} {% block script %} - + {% endblock %} {% block content %} diff --git a/canaille/templates/users.html b/canaille/templates/users.html index 3669b215..432d332a 100644 --- a/canaille/templates/users.html +++ b/canaille/templates/users.html @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% import 'flask.j2' as flask %} {% block style %} @@ -12,6 +13,7 @@ {% endblock %} {% block content %} +{{ flask.messages() }}
{% trans %}Add a user{% endtrans %} diff --git a/tests/test_groups.py b/tests/test_groups.py index abbb7291..70fd625a 100644 --- a/tests/test_groups.py +++ b/tests/test_groups.py @@ -33,7 +33,7 @@ def test_set_groups(app, slapd_connection, user, foo_group, bar_group): assert user.dn in foo_dns assert user.dn not in bar_dns -def test_group_creation(testclient, slapd_connection, logged_moderator, foo_group): +def test_moderator_can_create_edit_and_delete_group(testclient, slapd_connection, logged_moderator, foo_group): # The group does not exist res = testclient.get("/groups", status=200) with testclient.app.app_context(): @@ -59,7 +59,7 @@ def test_group_creation(testclient, slapd_connection, logged_moderator, foo_grou res = testclient.get("/groups/bar", status=200) res.form["name"] = "bar2" - res = res.form.submit(status=200) + res = res.form.submit(name="action", value="edit", status=200) with testclient.app.app_context(): bar_group = Group.get("bar", conn=slapd_connection) @@ -69,4 +69,13 @@ def test_group_creation(testclient, slapd_connection, logged_moderator, foo_grou for member in members: assert member.name in res.text - # TODO: Group is deleted + # Group is deleted + res = res.form.submit(name="action", value="delete", status=302).follow(status=200) + with testclient.app.app_context(): + assert Group.get("bar", conn=slapd_connection) is None + assert "The group bar has been sucessfully deleted" in res.text + +def test_simple_user_cannot_view_or_edit_groups(testclient, slapd_connection, logged_user, foo_group): + testclient.get("/groups", status=403) + testclient.get("/groups/add", status=403) + testclient.get("/groups/foo", status=403)