1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ListTest do
7 use Pleroma.DataCase, async: true
11 test "creating a list" do
13 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
14 %Pleroma.List{title: title} = Pleroma.List.get(list.id, user)
15 assert title == "title"
18 test "validates title" do
21 assert {:error, changeset} = Pleroma.List.create("", user)
22 assert changeset.errors == [title: {"can't be blank", [validation: :required]}]
25 test "getting a list not belonging to the user" do
27 other_user = insert(:user)
28 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
29 ret = Pleroma.List.get(list.id, other_user)
33 test "adding an user to a list" do
35 other_user = insert(:user)
36 {:ok, list} = Pleroma.List.create("title", user)
37 {:ok, %{following: following}} = Pleroma.List.follow(list, other_user)
38 assert [other_user.follower_address] == following
41 test "removing an user from a list" do
43 other_user = insert(:user)
44 {:ok, list} = Pleroma.List.create("title", user)
45 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
46 {:ok, %{following: following}} = Pleroma.List.unfollow(list, other_user)
47 assert [] == following
50 test "renaming a list" do
52 {:ok, list} = Pleroma.List.create("title", user)
53 {:ok, %{title: title}} = Pleroma.List.rename(list, "new")
57 test "deleting a list" do
59 {:ok, list} = Pleroma.List.create("title", user)
60 {:ok, list} = Pleroma.List.delete(list)
61 assert is_nil(Repo.get(Pleroma.List, list.id))
64 test "getting users in a list" do
66 other_user = insert(:user)
67 third_user = insert(:user)
68 {:ok, list} = Pleroma.List.create("title", user)
69 {:ok, list} = Pleroma.List.follow(list, other_user)
70 {:ok, list} = Pleroma.List.follow(list, third_user)
71 {:ok, following} = Pleroma.List.get_following(list)
72 assert other_user in following
73 assert third_user in following
76 test "getting all lists by an user" do
78 other_user = insert(:user)
79 {:ok, list_one} = Pleroma.List.create("title", user)
80 {:ok, list_two} = Pleroma.List.create("other title", user)
81 {:ok, list_three} = Pleroma.List.create("third title", other_user)
82 lists = Pleroma.List.for_user(user, %{})
83 assert list_one in lists
84 assert list_two in lists
85 refute list_three in lists
88 test "getting all lists the user is a member of" do
90 other_user = insert(:user)
91 {:ok, list_one} = Pleroma.List.create("title", user)
92 {:ok, list_two} = Pleroma.List.create("other title", user)
93 {:ok, list_three} = Pleroma.List.create("third title", other_user)
94 {:ok, list_one} = Pleroma.List.follow(list_one, other_user)
95 {:ok, list_two} = Pleroma.List.follow(list_two, other_user)
96 {:ok, list_three} = Pleroma.List.follow(list_three, user)
98 lists = Pleroma.List.get_lists_from_activity(%Pleroma.Activity{actor: other_user.ap_id})
99 assert list_one in lists
100 assert list_two in lists
101 refute list_three in lists
104 test "getting own lists a given user belongs to" do
105 owner = insert(:user)
106 not_owner = insert(:user)
107 member_1 = insert(:user)
108 member_2 = insert(:user)
109 {:ok, owned_list} = Pleroma.List.create("owned", owner)
110 {:ok, not_owned_list} = Pleroma.List.create("not owned", not_owner)
111 {:ok, owned_list} = Pleroma.List.follow(owned_list, member_1)
112 {:ok, owned_list} = Pleroma.List.follow(owned_list, member_2)
113 {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_1)
114 {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_2)
116 lists_1 = Pleroma.List.get_lists_account_belongs(owner, member_1)
117 assert owned_list in lists_1
118 refute not_owned_list in lists_1
119 lists_2 = Pleroma.List.get_lists_account_belongs(owner, member_2)
120 assert owned_list in lists_2
121 refute not_owned_list in lists_2
124 test "get by ap_id" do
126 {:ok, list} = Pleroma.List.create("foo", user)
127 assert Pleroma.List.get_by_ap_id(list.ap_id) == list
130 test "memberships" do
132 member = insert(:user)
133 {:ok, list} = Pleroma.List.create("foo", user)
134 {:ok, list} = Pleroma.List.follow(list, member)
136 assert Pleroma.List.memberships(member) == [list.ap_id]
141 member = insert(:user)
143 {:ok, list} = Pleroma.List.create("foo", user)
144 {:ok, list} = Pleroma.List.follow(list, member)
146 assert Pleroma.List.member?(list, member)
147 refute Pleroma.List.member?(list, user)