Merge branch 'develop' into default-db-name
[akkoma] / test / pleroma / web / admin_api / search_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.SearchTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 alias Pleroma.Web.AdminAPI.Search
9
10 import Pleroma.Factory
11
12 describe "search for admin" do
13 test "it ignores case" do
14 insert(:user, nickname: "papercoach")
15 insert(:user, nickname: "CanadaPaperCoach")
16
17 {:ok, _results, count} =
18 Search.user(%{
19 query: "paper",
20 local: false,
21 page: 1,
22 page_size: 50
23 })
24
25 assert count == 2
26 end
27
28 test "it returns local/external users" do
29 insert(:user, local: true)
30 insert(:user, local: false)
31 insert(:user, local: false)
32
33 {:ok, _results, local_count} =
34 Search.user(%{
35 query: "",
36 local: true
37 })
38
39 {:ok, _results, external_count} =
40 Search.user(%{
41 query: "",
42 external: true
43 })
44
45 assert local_count == 1
46 assert external_count == 2
47 end
48
49 test "it returns active/deactivated users" do
50 insert(:user, is_active: false)
51 insert(:user, is_active: false)
52 insert(:user, is_active: true)
53
54 {:ok, _results, active_count} =
55 Search.user(%{
56 query: "",
57 active: true
58 })
59
60 {:ok, _results, deactivated_count} =
61 Search.user(%{
62 query: "",
63 deactivated: true
64 })
65
66 assert active_count == 1
67 assert deactivated_count == 2
68 end
69
70 test "it returns specific user" do
71 insert(:user)
72 insert(:user)
73 user = insert(:user, nickname: "bob", local: true, is_active: true)
74
75 {:ok, _results, total_count} = Search.user(%{query: ""})
76
77 {:ok, [^user], count} =
78 Search.user(%{
79 query: "Bo",
80 active: true,
81 local: true
82 })
83
84 assert total_count == 3
85 assert count == 1
86 end
87
88 test "it returns user by domain" do
89 insert(:user)
90 insert(:user)
91 user = insert(:user, nickname: "some@domain.com")
92
93 {:ok, _results, total} = Search.user()
94 {:ok, [^user], count} = Search.user(%{query: "domain.com"})
95 assert total == 3
96 assert count == 1
97 end
98
99 test "it return user by full nickname" do
100 insert(:user)
101 insert(:user)
102 user = insert(:user, nickname: "some@domain.com")
103
104 {:ok, _results, total} = Search.user()
105 {:ok, [^user], count} = Search.user(%{query: "some@domain.com"})
106 assert total == 3
107 assert count == 1
108 end
109
110 test "it returns admin user" do
111 admin = insert(:user, is_admin: true)
112 insert(:user)
113 insert(:user)
114
115 {:ok, _results, total} = Search.user()
116 {:ok, [^admin], count} = Search.user(%{is_admin: true})
117 assert total == 3
118 assert count == 1
119 end
120
121 test "it returns moderator user" do
122 moderator = insert(:user, is_moderator: true)
123 insert(:user)
124 insert(:user)
125
126 {:ok, _results, total} = Search.user()
127 {:ok, [^moderator], count} = Search.user(%{is_moderator: true})
128 assert total == 3
129 assert count == 1
130 end
131
132 test "it returns users with tags" do
133 user1 = insert(:user, tags: ["first"])
134 user2 = insert(:user, tags: ["second"])
135 insert(:user)
136 insert(:user)
137
138 {:ok, _results, total} = Search.user()
139 {:ok, users, count} = Search.user(%{tags: ["first", "second"]})
140 assert total == 4
141 assert count == 2
142 assert user1 in users
143 assert user2 in users
144 end
145
146 test "it returns users by actor_types" do
147 user_service = insert(:user, actor_type: "Service")
148 user_application = insert(:user, actor_type: "Application")
149 user1 = insert(:user)
150 user2 = insert(:user)
151
152 {:ok, [^user_service], 1} = Search.user(%{actor_types: ["Service"]})
153 {:ok, [^user_application], 1} = Search.user(%{actor_types: ["Application"]})
154 {:ok, [^user2, ^user1], 2} = Search.user(%{actor_types: ["Person"]})
155
156 {:ok, [^user2, ^user1, ^user_service], 3} =
157 Search.user(%{actor_types: ["Person", "Service"]})
158 end
159
160 test "it returns user by display name" do
161 user = insert(:user, name: "Display name")
162 insert(:user)
163 insert(:user)
164
165 {:ok, _results, total} = Search.user()
166 {:ok, [^user], count} = Search.user(%{name: "display"})
167
168 assert total == 3
169 assert count == 1
170 end
171
172 test "it returns user by email" do
173 user = insert(:user, email: "some@example.com")
174 insert(:user)
175 insert(:user)
176
177 {:ok, _results, total} = Search.user()
178 {:ok, [^user], count} = Search.user(%{email: "some@example.com"})
179
180 assert total == 3
181 assert count == 1
182 end
183
184 test "it returns unapproved user" do
185 unapproved = insert(:user, is_approved: false)
186 insert(:user)
187 insert(:user)
188
189 {:ok, _results, total} = Search.user()
190 {:ok, [^unapproved], count} = Search.user(%{need_approval: true})
191 assert total == 3
192 assert count == 1
193 end
194
195 test "it returns unconfirmed user" do
196 unconfirmed = insert(:user, is_confirmed: false)
197 insert(:user)
198 insert(:user)
199
200 {:ok, _results, total} = Search.user()
201 {:ok, [^unconfirmed], count} = Search.user(%{unconfirmed: true})
202 assert total == 3
203 assert count == 1
204 end
205
206 # Note: as in Mastodon, `is_discoverable` doesn't anyhow relate to user searchability
207 test "it returns non-discoverable users" do
208 insert(:user)
209 insert(:user, is_discoverable: false)
210
211 {:ok, _results, total} = Search.user()
212
213 assert total == 2
214 end
215 end
216 end