2023-04-09 11:34:38 +00:00
|
|
|
from canaille.core.models import Group
|
|
|
|
from canaille.core.models import User
|
|
|
|
from canaille.core.populate import fake_groups
|
|
|
|
from canaille.core.populate import fake_users
|
2021-04-08 15:38:13 +00:00
|
|
|
|
2021-06-03 10:28:45 +00:00
|
|
|
|
2021-04-08 15:38:13 +00:00
|
|
|
def test_no_group(app, slapd_connection):
|
2023-03-07 13:49:44 +00:00
|
|
|
assert Group.query() == []
|
2021-04-08 15:38:13 +00:00
|
|
|
|
2021-06-03 10:28:45 +00:00
|
|
|
|
2023-02-25 17:11:19 +00:00
|
|
|
def test_group_list_pagination(testclient, logged_admin, foo_group):
|
|
|
|
res = testclient.get("/groups")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("1 items")
|
2023-02-25 17:11:19 +00:00
|
|
|
|
|
|
|
groups = fake_groups(25)
|
|
|
|
|
|
|
|
res = testclient.get("/groups")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("26 items")
|
2023-02-25 17:11:19 +00:00
|
|
|
group_name = res.pyquery(
|
|
|
|
".groups tbody tr:nth-of-type(1) td:nth-of-type(2) a"
|
|
|
|
).text()
|
|
|
|
assert group_name
|
|
|
|
|
2023-03-09 19:46:04 +00:00
|
|
|
form = res.forms["next"]
|
|
|
|
form["page"] = 2
|
|
|
|
res = form.submit()
|
|
|
|
assert group_name not in res.pyquery(
|
|
|
|
".groups tbody tr td:nth-of-type(2) a"
|
|
|
|
).text().split(" ")
|
2023-02-25 17:11:19 +00:00
|
|
|
for group in groups:
|
|
|
|
group.delete()
|
|
|
|
|
|
|
|
res = testclient.get("/groups")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("1 items")
|
2023-02-25 17:11:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_group_list_bad_pages(testclient, logged_admin):
|
|
|
|
res = testclient.get("/groups")
|
2023-03-09 19:46:04 +00:00
|
|
|
form = res.forms["next"]
|
2023-02-25 17:11:19 +00:00
|
|
|
testclient.post(
|
2023-03-28 18:30:29 +00:00
|
|
|
"/groups", {"csrf_token": form["csrf_token"].value, "page": "2"}, status=404
|
2023-02-25 17:11:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
res = testclient.get("/groups")
|
2023-03-09 19:46:04 +00:00
|
|
|
form = res.forms["next"]
|
2023-02-25 17:11:19 +00:00
|
|
|
testclient.post(
|
2023-03-28 18:30:29 +00:00
|
|
|
"/groups", {"csrf_token": form["csrf_token"].value, "page": "-1"}, status=404
|
2023-02-25 17:11:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-04-07 19:03:11 +00:00
|
|
|
def test_group_deletion(testclient, slapd_server, slapd_connection):
|
|
|
|
user = User(
|
|
|
|
formatted_name="foobar",
|
|
|
|
family_name="foobar",
|
|
|
|
user_name="foobar",
|
|
|
|
email="foo@bar.com",
|
|
|
|
)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
group = Group(
|
|
|
|
members=[user],
|
|
|
|
display_name="foobar",
|
|
|
|
)
|
|
|
|
group.save()
|
|
|
|
assert user.groups == [group]
|
|
|
|
|
|
|
|
group.delete()
|
2023-04-08 19:34:09 +00:00
|
|
|
user.reload()
|
2023-04-07 19:03:11 +00:00
|
|
|
assert not user.groups
|
|
|
|
|
|
|
|
user.delete()
|
|
|
|
|
|
|
|
|
2023-03-07 17:29:18 +00:00
|
|
|
def test_group_list_search(testclient, logged_admin, foo_group, bar_group):
|
|
|
|
res = testclient.get("/groups")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("2 items")
|
|
|
|
res.mustcontain(foo_group.display_name)
|
|
|
|
res.mustcontain(bar_group.display_name)
|
2023-03-07 17:29:18 +00:00
|
|
|
|
|
|
|
form = res.forms["search"]
|
|
|
|
form["query"] = "oo"
|
|
|
|
res = form.submit()
|
|
|
|
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("1 items")
|
|
|
|
res.mustcontain(foo_group.display_name)
|
|
|
|
res.mustcontain(no=bar_group.display_name)
|
2023-03-07 17:29:18 +00:00
|
|
|
|
|
|
|
|
2022-05-19 10:36:39 +00:00
|
|
|
def test_set_groups(app, user, foo_group, bar_group):
|
2023-04-07 22:31:22 +00:00
|
|
|
assert user in foo_group.members
|
2023-03-11 18:12:58 +00:00
|
|
|
assert user.groups[0] == foo_group
|
2021-06-03 10:00:04 +00:00
|
|
|
|
2023-04-07 22:31:22 +00:00
|
|
|
user.groups = [foo_group, bar_group]
|
2021-07-29 14:00:21 +00:00
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
bar_group.reload()
|
2023-04-07 22:31:22 +00:00
|
|
|
assert user in bar_group.members
|
2023-03-11 18:12:58 +00:00
|
|
|
assert user.groups[1] == bar_group
|
2021-06-03 10:00:04 +00:00
|
|
|
|
2023-04-07 22:31:22 +00:00
|
|
|
user.groups = [foo_group]
|
2021-07-29 14:00:21 +00:00
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
foo_group.reload()
|
|
|
|
bar_group.reload()
|
2023-04-07 22:31:22 +00:00
|
|
|
assert user in foo_group.members
|
|
|
|
assert user not in bar_group.members
|
2021-07-01 16:21:20 +00:00
|
|
|
|
2021-07-29 14:00:21 +00:00
|
|
|
|
2022-05-19 10:36:39 +00:00
|
|
|
def test_set_groups_with_leading_space_in_user_id_attribute(app, foo_group):
|
2022-03-02 17:31:48 +00:00
|
|
|
user = User(
|
2023-02-05 17:57:18 +00:00
|
|
|
formatted_name=" Doe", # leading space in id attribute
|
|
|
|
family_name="Doe",
|
|
|
|
user_name="user2",
|
|
|
|
email="john@doe.com",
|
2022-03-02 17:31:48 +00:00
|
|
|
)
|
2022-05-08 14:31:17 +00:00
|
|
|
user.save()
|
2022-03-02 17:31:48 +00:00
|
|
|
|
2023-04-07 22:31:22 +00:00
|
|
|
user.groups = [foo_group]
|
2022-03-02 17:31:48 +00:00
|
|
|
|
2023-04-07 22:31:22 +00:00
|
|
|
assert user in foo_group.members
|
2022-03-02 17:31:48 +00:00
|
|
|
|
2023-04-07 22:31:22 +00:00
|
|
|
user.groups = []
|
2022-03-02 17:31:48 +00:00
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
foo_group.reload()
|
2023-04-07 22:31:22 +00:00
|
|
|
assert user.id not in foo_group.members
|
2022-03-02 17:31:48 +00:00
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
user.delete()
|
2022-05-18 09:31:26 +00:00
|
|
|
|
2022-03-02 17:31:48 +00:00
|
|
|
|
2021-07-29 14:00:21 +00:00
|
|
|
def test_moderator_can_create_edit_and_delete_group(
|
2022-05-19 10:36:39 +00:00
|
|
|
testclient, logged_moderator, foo_group
|
2021-07-29 14:00:21 +00:00
|
|
|
):
|
2021-07-01 16:21:20 +00:00
|
|
|
# The group does not exist
|
|
|
|
res = testclient.get("/groups", status=200)
|
2022-05-08 14:31:17 +00:00
|
|
|
assert Group.get("bar") is None
|
|
|
|
assert Group.get("foo") == foo_group
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain(no="bar")
|
|
|
|
res.mustcontain("foo")
|
2021-07-01 16:21:20 +00:00
|
|
|
|
|
|
|
# Fill the form for a new group
|
|
|
|
res = testclient.get("/groups/add", status=200)
|
2023-02-26 21:23:57 +00:00
|
|
|
form = res.forms["creategroupform"]
|
2023-02-05 18:39:52 +00:00
|
|
|
form["display_name"] = "bar"
|
2023-02-26 21:23:57 +00:00
|
|
|
form["description"] = "yolo"
|
2021-07-01 16:21:20 +00:00
|
|
|
|
|
|
|
# Group has been created
|
2023-02-26 21:23:57 +00:00
|
|
|
res = form.submit(status=302).follow(status=200)
|
2021-07-01 16:21:20 +00:00
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
bar_group = Group.get("bar")
|
2023-02-05 18:39:52 +00:00
|
|
|
assert bar_group.display_name == "bar"
|
2022-05-08 14:31:17 +00:00
|
|
|
assert bar_group.description == ["yolo"]
|
2023-04-07 22:31:22 +00:00
|
|
|
assert bar_group.members == [
|
2023-03-11 18:12:58 +00:00
|
|
|
logged_moderator
|
2022-05-08 14:31:17 +00:00
|
|
|
] # Group cannot be empty so creator is added in it
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("bar")
|
2021-07-01 16:21:20 +00:00
|
|
|
|
|
|
|
# Group name can not be edited
|
|
|
|
res = testclient.get("/groups/bar", status=200)
|
2023-02-26 21:23:57 +00:00
|
|
|
form = res.forms["editgroupform"]
|
2023-02-05 18:39:52 +00:00
|
|
|
form["display_name"] = "bar2"
|
2023-02-26 21:23:57 +00:00
|
|
|
form["description"] = ["yolo2"]
|
2021-07-01 16:21:20 +00:00
|
|
|
|
2023-02-26 21:23:57 +00:00
|
|
|
res = form.submit(name="action", value="edit").follow()
|
2021-07-01 16:21:20 +00:00
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
bar_group = Group.get("bar")
|
2023-02-05 18:39:52 +00:00
|
|
|
assert bar_group.display_name == "bar"
|
2022-05-08 14:31:17 +00:00
|
|
|
assert bar_group.description == ["yolo2"]
|
|
|
|
assert Group.get("bar2") is None
|
2023-04-07 22:31:22 +00:00
|
|
|
members = bar_group.members
|
2022-05-08 14:31:17 +00:00
|
|
|
for member in members:
|
2023-04-07 20:45:42 +00:00
|
|
|
res.mustcontain(member.formatted_name[0])
|
2021-07-29 14:00:21 +00:00
|
|
|
|
2021-07-29 09:37:02 +00:00
|
|
|
# Group is deleted
|
2023-02-26 21:23:57 +00:00
|
|
|
res = form.submit(name="action", value="delete", status=302)
|
2022-05-08 14:31:17 +00:00
|
|
|
assert Group.get("bar") is None
|
2023-01-28 18:02:00 +00:00
|
|
|
assert ("success", "The group bar has been sucessfully deleted") in res.flashes
|
2021-07-29 09:37:02 +00:00
|
|
|
|
2021-07-29 14:00:21 +00:00
|
|
|
|
2022-05-19 10:36:39 +00:00
|
|
|
def test_cannot_create_already_existing_group(testclient, logged_moderator, foo_group):
|
2023-03-28 18:30:29 +00:00
|
|
|
res = testclient.get("/groups/add")
|
|
|
|
res = testclient.post(
|
|
|
|
"/groups/add",
|
|
|
|
{"csrf_token": res.form["csrf_token"].value, "display_name": "foo"},
|
|
|
|
status=200,
|
|
|
|
)
|
2021-07-29 14:00:21 +00:00
|
|
|
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("Group creation failed.")
|
|
|
|
res.mustcontain("The group 'foo' already exists")
|
2021-07-29 14:00:21 +00:00
|
|
|
|
|
|
|
|
2022-12-06 17:36:07 +00:00
|
|
|
def test_invalid_group(testclient, logged_moderator):
|
|
|
|
testclient.get("/groups/invalid", status=404)
|
|
|
|
|
|
|
|
|
2022-05-19 10:36:39 +00:00
|
|
|
def test_simple_user_cannot_view_or_edit_groups(testclient, logged_user, foo_group):
|
2021-07-29 09:37:02 +00:00
|
|
|
testclient.get("/groups", status=403)
|
|
|
|
testclient.get("/groups/add", status=403)
|
|
|
|
testclient.get("/groups/foo", status=403)
|
2021-10-29 12:19:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_members_filters_non_existent_user(
|
2022-05-19 10:36:39 +00:00
|
|
|
testclient, logged_moderator, foo_group, user
|
2021-10-29 12:19:46 +00:00
|
|
|
):
|
|
|
|
# an LDAP group can be inconsistent by containing members which doesn't exist
|
2023-02-05 17:57:18 +00:00
|
|
|
non_existent_user = User(formatted_name="foo", family_name="bar")
|
2023-03-08 22:53:53 +00:00
|
|
|
foo_group.member = foo_group.member + [non_existent_user]
|
2022-05-08 14:31:17 +00:00
|
|
|
foo_group.save()
|
2021-10-29 12:19:46 +00:00
|
|
|
|
2023-04-07 22:31:22 +00:00
|
|
|
foo_group.members
|
2021-10-29 12:19:46 +00:00
|
|
|
|
2023-03-08 22:53:53 +00:00
|
|
|
assert foo_group.member == [user, non_existent_user]
|
2021-10-29 12:19:46 +00:00
|
|
|
|
|
|
|
testclient.get("/groups/foo", status=200)
|
2022-12-06 17:36:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_form_request(testclient, logged_moderator, foo_group):
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-02-26 21:23:57 +00:00
|
|
|
form = res.forms["editgroupform"]
|
|
|
|
res = form.submit(name="action", value="invalid-action", status=400)
|
2022-12-06 17:36:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_edition_failed(testclient, logged_moderator, foo_group):
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-02-26 21:23:57 +00:00
|
|
|
form = res.forms["editgroupform"]
|
2023-03-28 18:30:29 +00:00
|
|
|
form["display_name"] = ""
|
2023-02-26 21:23:57 +00:00
|
|
|
res = form.submit(name="action", value="edit")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("Group edition failed.")
|
2023-04-08 19:34:09 +00:00
|
|
|
foo_group.reload()
|
2023-02-05 18:39:52 +00:00
|
|
|
assert foo_group.display_name == "foo"
|
2023-02-25 17:11:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_user_list_pagination(testclient, logged_admin, foo_group):
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("1 items")
|
2023-02-25 17:11:19 +00:00
|
|
|
|
|
|
|
users = fake_users(25)
|
|
|
|
for user in users:
|
2023-04-07 22:31:22 +00:00
|
|
|
foo_group.members = foo_group.members + [user]
|
2023-02-25 17:11:19 +00:00
|
|
|
foo_group.save()
|
|
|
|
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("26 items")
|
2023-02-25 17:11:19 +00:00
|
|
|
user_name = res.pyquery(".users tbody tr:nth-of-type(1) td:nth-of-type(2) a").text()
|
|
|
|
assert user_name
|
|
|
|
|
2023-03-09 19:46:04 +00:00
|
|
|
form = res.forms["next"]
|
|
|
|
form["page"] = 2
|
|
|
|
res = form.submit()
|
|
|
|
assert user_name not in res.pyquery(".users tr td:nth-of-type(2) a").text().split(
|
|
|
|
" "
|
|
|
|
)
|
2023-02-25 17:11:19 +00:00
|
|
|
for user in users:
|
|
|
|
user.delete()
|
|
|
|
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("1 items")
|
2023-02-25 17:11:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_user_list_bad_pages(testclient, logged_admin, foo_group):
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-03-09 19:46:04 +00:00
|
|
|
form = res.forms["next"]
|
2023-02-25 17:11:19 +00:00
|
|
|
testclient.post(
|
2023-03-28 18:30:29 +00:00
|
|
|
"/groups/foo", {"csrf_token": form["csrf_token"].value, "page": "2"}, status=404
|
2023-02-25 17:11:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-03-09 19:46:04 +00:00
|
|
|
form = res.forms["next"]
|
2023-02-25 17:11:19 +00:00
|
|
|
testclient.post(
|
2023-03-28 18:30:29 +00:00
|
|
|
"/groups/foo",
|
|
|
|
{"csrf_token": form["csrf_token"].value, "page": "-1"},
|
|
|
|
status=404,
|
2023-02-25 17:11:19 +00:00
|
|
|
)
|
2023-03-07 17:29:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_user_list_search(testclient, logged_admin, foo_group, user, moderator):
|
2023-04-07 22:31:22 +00:00
|
|
|
foo_group.members = foo_group.members + [logged_admin, moderator]
|
2023-03-07 17:29:18 +00:00
|
|
|
foo_group.save()
|
|
|
|
|
|
|
|
res = testclient.get("/groups/foo")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("3 items")
|
2023-04-07 20:45:42 +00:00
|
|
|
res.mustcontain(user.formatted_name[0])
|
|
|
|
res.mustcontain(moderator.formatted_name[0])
|
2023-03-07 17:29:18 +00:00
|
|
|
|
|
|
|
form = res.forms["search"]
|
|
|
|
form["query"] = "ohn"
|
|
|
|
res = form.submit()
|
|
|
|
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("1 items")
|
2023-04-07 20:45:42 +00:00
|
|
|
res.mustcontain(user.formatted_name[0])
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain(no=logged_admin.name)
|
2023-04-07 20:45:42 +00:00
|
|
|
res.mustcontain(no=moderator.formatted_name[0])
|