Merge branch 'release/2.2.0' into 'stable'
[akkoma] / test / pleroma / web / admin_api / search_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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, deactivated: true)
51 insert(:user, deactivated: true)
52 insert(:user, deactivated: false)
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, deactivated: false)
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 user by display name" do
147 user = insert(:user, name: "Display name")
148 insert(:user)
149 insert(:user)
150
151 {:ok, _results, total} = Search.user()
152 {:ok, [^user], count} = Search.user(%{name: "display"})
153
154 assert total == 3
155 assert count == 1
156 end
157
158 test "it returns user by email" do
159 user = insert(:user, email: "some@example.com")
160 insert(:user)
161 insert(:user)
162
163 {:ok, _results, total} = Search.user()
164 {:ok, [^user], count} = Search.user(%{email: "some@example.com"})
165
166 assert total == 3
167 assert count == 1
168 end
169
170 test "it returns unapproved user" do
171 unapproved = insert(:user, approval_pending: true)
172 insert(:user)
173 insert(:user)
174
175 {:ok, _results, total} = Search.user()
176 {:ok, [^unapproved], count} = Search.user(%{need_approval: true})
177 assert total == 3
178 assert count == 1
179 end
180
181 test "it returns non-discoverable users" do
182 insert(:user)
183 insert(:user, discoverable: false)
184
185 {:ok, _results, total} = Search.user()
186
187 assert total == 2
188 end
189 end
190 end