Merge branch 'support/issue_442' into 'develop'
[akkoma] / test / list_test.exs
1 defmodule Pleroma.ListTest do
2 alias Pleroma.Repo
3 use Pleroma.DataCase
4
5 import Pleroma.Factory
6
7 test "creating a list" do
8 user = insert(:user)
9 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
10 %Pleroma.List{title: title} = Pleroma.List.get(list.id, user)
11 assert title == "title"
12 end
13
14 test "getting a list not belonging to the user" do
15 user = insert(:user)
16 other_user = insert(:user)
17 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
18 ret = Pleroma.List.get(list.id, other_user)
19 assert is_nil(ret)
20 end
21
22 test "adding an user to a list" do
23 user = insert(:user)
24 other_user = insert(:user)
25 {:ok, list} = Pleroma.List.create("title", user)
26 {:ok, %{following: following}} = Pleroma.List.follow(list, other_user)
27 assert [other_user.follower_address] == following
28 end
29
30 test "removing an user from a list" do
31 user = insert(:user)
32 other_user = insert(:user)
33 {:ok, list} = Pleroma.List.create("title", user)
34 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
35 {:ok, %{following: following}} = Pleroma.List.unfollow(list, other_user)
36 assert [] == following
37 end
38
39 test "renaming a list" do
40 user = insert(:user)
41 {:ok, list} = Pleroma.List.create("title", user)
42 {:ok, %{title: title}} = Pleroma.List.rename(list, "new")
43 assert "new" == title
44 end
45
46 test "deleting a list" do
47 user = insert(:user)
48 {:ok, list} = Pleroma.List.create("title", user)
49 {:ok, list} = Pleroma.List.delete(list)
50 assert is_nil(Repo.get(Pleroma.List, list.id))
51 end
52
53 test "getting users in a list" do
54 user = insert(:user)
55 other_user = insert(:user)
56 third_user = insert(:user)
57 {:ok, list} = Pleroma.List.create("title", user)
58 {:ok, list} = Pleroma.List.follow(list, other_user)
59 {:ok, list} = Pleroma.List.follow(list, third_user)
60 {:ok, following} = Pleroma.List.get_following(list)
61 assert other_user in following
62 assert third_user in following
63 end
64
65 test "getting all lists by an user" do
66 user = insert(:user)
67 other_user = insert(:user)
68 {:ok, list_one} = Pleroma.List.create("title", user)
69 {:ok, list_two} = Pleroma.List.create("other title", user)
70 {:ok, list_three} = Pleroma.List.create("third title", other_user)
71 lists = Pleroma.List.for_user(user, %{})
72 assert list_one in lists
73 assert list_two in lists
74 refute list_three in lists
75 end
76
77 test "getting all lists the user is a member of" do
78 user = insert(:user)
79 other_user = insert(:user)
80 {:ok, list_one} = Pleroma.List.create("title", user)
81 {:ok, list_two} = Pleroma.List.create("other title", user)
82 {:ok, list_three} = Pleroma.List.create("third title", other_user)
83 {:ok, list_one} = Pleroma.List.follow(list_one, other_user)
84 {:ok, list_two} = Pleroma.List.follow(list_two, other_user)
85 {:ok, list_three} = Pleroma.List.follow(list_three, user)
86
87 lists = Pleroma.List.get_lists_from_activity(%Pleroma.Activity{actor: other_user.ap_id})
88 assert list_one in lists
89 assert list_two in lists
90 refute list_three in lists
91 end
92
93 test "getting own lists a given user belongs to" do
94 owner = insert(:user)
95 not_owner = insert(:user)
96 member_1 = insert(:user)
97 member_2 = insert(:user)
98 {:ok, owned_list} = Pleroma.List.create("owned", owner)
99 {:ok, not_owned_list} = Pleroma.List.create("not owned", not_owner)
100 {:ok, owned_list} = Pleroma.List.follow(owned_list, member_1)
101 {:ok, owned_list} = Pleroma.List.follow(owned_list, member_2)
102 {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_1)
103 {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_2)
104
105 lists_1 = Pleroma.List.get_lists_account_belongs(owner, member_1.id)
106 assert owned_list in lists_1
107 refute not_owned_list in lists_1
108 lists_2 = Pleroma.List.get_lists_account_belongs(owner, member_2.id)
109 assert owned_list in lists_2
110 refute not_owned_list in lists_2
111 end
112 end