Merge branch 'hex-flake-id' into 'develop'
[akkoma] / test / list_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ListTest do
6 alias Pleroma.Repo
7 use Pleroma.DataCase
8
9 import Pleroma.Factory
10
11 test "creating a list" do
12 user = insert(:user)
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"
16 end
17
18 test "validates title" do
19 user = insert(:user)
20
21 assert {:error, changeset} = Pleroma.List.create("", user)
22 assert changeset.errors == [title: {"can't be blank", [validation: :required]}]
23 end
24
25 test "getting a list not belonging to the user" do
26 user = insert(:user)
27 other_user = insert(:user)
28 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
29 ret = Pleroma.List.get(list.id, other_user)
30 assert is_nil(ret)
31 end
32
33 test "adding an user to a list" do
34 user = insert(:user)
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
39 end
40
41 test "removing an user from a list" do
42 user = insert(:user)
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
48 end
49
50 test "renaming a list" do
51 user = insert(:user)
52 {:ok, list} = Pleroma.List.create("title", user)
53 {:ok, %{title: title}} = Pleroma.List.rename(list, "new")
54 assert "new" == title
55 end
56
57 test "deleting a list" do
58 user = insert(:user)
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))
62 end
63
64 test "getting users in a list" do
65 user = insert(:user)
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
74 end
75
76 test "getting all lists by an user" do
77 user = insert(:user)
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
86 end
87
88 test "getting all lists the user is a member of" do
89 user = insert(:user)
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)
97
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
102 end
103
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)
115
116 lists_1 = Pleroma.List.get_lists_account_belongs(owner, member_1.id)
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.id)
120 assert owned_list in lists_2
121 refute not_owned_list in lists_2
122 end
123
124 test "get by ap_id" do
125 user = insert(:user)
126 {:ok, list} = Pleroma.List.create("foo", user)
127 assert Pleroma.List.get_by_ap_id(list.ap_id) == list
128 end
129
130 test "memberships" do
131 user = insert(:user)
132 member = insert(:user)
133 {:ok, list} = Pleroma.List.create("foo", user)
134 {:ok, list} = Pleroma.List.follow(list, member)
135
136 assert Pleroma.List.memberships(member) == [list.ap_id]
137 end
138
139 test "member?" do
140 user = insert(:user)
141 member = insert(:user)
142
143 {:ok, list} = Pleroma.List.create("foo", user)
144 {:ok, list} = Pleroma.List.follow(list, member)
145
146 assert Pleroma.List.member?(list, member)
147 refute Pleroma.List.member?(list, user)
148 end
149 end