Merge remote-tracking branch 'pleroma/develop' into feature/addressable-lists
[akkoma] / test / list_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 "getting a list not belonging to the user" do
19 user = insert(:user)
20 other_user = insert(:user)
21 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
22 ret = Pleroma.List.get(list.id, other_user)
23 assert is_nil(ret)
24 end
25
26 test "adding an user to a list" do
27 user = insert(:user)
28 other_user = insert(:user)
29 {:ok, list} = Pleroma.List.create("title", user)
30 {:ok, %{following: following}} = Pleroma.List.follow(list, other_user)
31 assert [other_user.follower_address] == following
32 end
33
34 test "removing an user from a list" do
35 user = insert(:user)
36 other_user = insert(:user)
37 {:ok, list} = Pleroma.List.create("title", user)
38 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
39 {:ok, %{following: following}} = Pleroma.List.unfollow(list, other_user)
40 assert [] == following
41 end
42
43 test "renaming a list" do
44 user = insert(:user)
45 {:ok, list} = Pleroma.List.create("title", user)
46 {:ok, %{title: title}} = Pleroma.List.rename(list, "new")
47 assert "new" == title
48 end
49
50 test "deleting a list" do
51 user = insert(:user)
52 {:ok, list} = Pleroma.List.create("title", user)
53 {:ok, list} = Pleroma.List.delete(list)
54 assert is_nil(Repo.get(Pleroma.List, list.id))
55 end
56
57 test "getting users in a list" do
58 user = insert(:user)
59 other_user = insert(:user)
60 third_user = insert(:user)
61 {:ok, list} = Pleroma.List.create("title", user)
62 {:ok, list} = Pleroma.List.follow(list, other_user)
63 {:ok, list} = Pleroma.List.follow(list, third_user)
64 {:ok, following} = Pleroma.List.get_following(list)
65 assert other_user in following
66 assert third_user in following
67 end
68
69 test "getting all lists by an user" do
70 user = insert(:user)
71 other_user = insert(:user)
72 {:ok, list_one} = Pleroma.List.create("title", user)
73 {:ok, list_two} = Pleroma.List.create("other title", user)
74 {:ok, list_three} = Pleroma.List.create("third title", other_user)
75 lists = Pleroma.List.for_user(user, %{})
76 assert list_one in lists
77 assert list_two in lists
78 refute list_three in lists
79 end
80
81 test "getting all lists the user is a member of" do
82 user = insert(:user)
83 other_user = insert(:user)
84 {:ok, list_one} = Pleroma.List.create("title", user)
85 {:ok, list_two} = Pleroma.List.create("other title", user)
86 {:ok, list_three} = Pleroma.List.create("third title", other_user)
87 {:ok, list_one} = Pleroma.List.follow(list_one, other_user)
88 {:ok, list_two} = Pleroma.List.follow(list_two, other_user)
89 {:ok, list_three} = Pleroma.List.follow(list_three, user)
90
91 lists = Pleroma.List.get_lists_from_activity(%Pleroma.Activity{actor: other_user.ap_id})
92 assert list_one in lists
93 assert list_two in lists
94 refute list_three in lists
95 end
96
97 test "getting own lists a given user belongs to" do
98 owner = insert(:user)
99 not_owner = insert(:user)
100 member_1 = insert(:user)
101 member_2 = insert(:user)
102 {:ok, owned_list} = Pleroma.List.create("owned", owner)
103 {:ok, not_owned_list} = Pleroma.List.create("not owned", not_owner)
104 {:ok, owned_list} = Pleroma.List.follow(owned_list, member_1)
105 {:ok, owned_list} = Pleroma.List.follow(owned_list, member_2)
106 {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_1)
107 {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_2)
108
109 lists_1 = Pleroma.List.get_lists_account_belongs(owner, member_1.id)
110 assert owned_list in lists_1
111 refute not_owned_list in lists_1
112 lists_2 = Pleroma.List.get_lists_account_belongs(owner, member_2.id)
113 assert owned_list in lists_2
114 refute not_owned_list in lists_2
115 end
116
117 test "get by ap_id" do
118 user = insert(:user)
119 {:ok, list} = Pleroma.List.create("foo", user)
120 assert Pleroma.List.get_by_ap_id(list.ap_id) == list
121 end
122
123 test "memberships" do
124 user = insert(:user)
125 member = insert(:user)
126 {:ok, list} = Pleroma.List.create("foo", user)
127 {:ok, list} = Pleroma.List.follow(list, member)
128
129 assert Pleroma.List.memberships(member) == [list.ap_id]
130 end
131 end