Add Tests
[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 ap_id by user nickname and list id" do
118 nickname = "foo"
119 list_id = 42
120
121 expected = Pleroma.Web.Endpoint.url() <> "/users/#{nickname}/lists/#{list_id}"
122
123 assert Pleroma.List.ap_id(%Pleroma.User{nickname: nickname}, list_id) == expected
124 assert Pleroma.List.ap_id({nickname, list_id}) == expected
125 end
126
127 test "get by ap_id" do
128 user = insert(:user)
129 {:ok, list} = Pleroma.List.create("foo", user)
130 ap_id = Pleroma.List.ap_id(user, list.id)
131
132 assert Pleroma.List.get_by_ap_id(ap_id) == list
133 end
134
135 test "memberships" do
136 user = insert(:user)
137 member = insert(:user)
138 {:ok, list} = Pleroma.List.create("foo", user)
139 {:ok, list} = Pleroma.List.follow(list, member)
140
141 list_ap_id = Pleroma.List.ap_id(user, list.id)
142
143 assert Pleroma.List.memberships(member) == [list_ap_id]
144 end
145 end