1 defmodule Pleroma.UserTest do
2 alias Pleroma.Builders.UserBuilder
3 alias Pleroma.{User, Repo, Activity}
4 alias Pleroma.Web.OStatus
5 alias Pleroma.Web.Websub.WebsubClientSubscription
6 alias Pleroma.Web.CommonAPI
12 test "ap_id returns the activity pub id for the user" do
13 user = UserBuilder.build()
15 expected_ap_id = "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
17 assert expected_ap_id == User.ap_id(user)
20 test "ap_followers returns the followers collection for the user" do
21 user = UserBuilder.build()
23 expected_followers_collection = "#{User.ap_id(user)}/followers"
25 assert expected_followers_collection == User.ap_followers(user)
28 test "follow takes a user and another user" do
30 followed = insert(:user)
32 {:ok, user} = User.follow(user, followed)
34 user = Repo.get(User, user.id)
36 followed = User.get_by_ap_id(followed.ap_id)
37 assert followed.info["follower_count"] == 1
39 assert User.ap_followers(followed) in user.following
42 test "can't follow a deactivated users" do
44 followed = insert(:user, info: %{"deactivated" => true})
46 {:error, _} = User.follow(user, followed)
49 # This is a somewhat useless test.
50 # test "following a remote user will ensure a websub subscription is present" do
51 # user = insert(:user)
52 # {:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
54 # assert followed.local == false
56 # {:ok, user} = User.follow(user, followed)
57 # assert User.ap_followers(followed) in user.following
59 # query = from w in WebsubClientSubscription,
60 # where: w.topic == ^followed.info["topic"]
61 # websub = Repo.one(query)
66 test "unfollow takes a user and another user" do
67 followed = insert(:user)
68 user = insert(:user, %{following: [User.ap_followers(followed)]})
70 {:ok, user, _activity} = User.unfollow(user, followed)
72 user = Repo.get(User, user.id)
74 assert user.following == []
77 test "unfollow doesn't unfollow yourself" do
80 {:error, _} = User.unfollow(user, user)
82 user = Repo.get(User, user.id)
83 assert user.following == [user.ap_id]
86 test "test if a user is following another user" do
87 followed = insert(:user)
88 user = insert(:user, %{following: [User.ap_followers(followed)]})
90 assert User.following?(user, followed)
91 refute User.following?(followed, user)
94 describe "user registration" do
100 password_confirmation: "test",
101 email: "email@example.com"
104 test "it requires an email, name, nickname and password, bio is optional" do
107 |> Enum.each(fn key ->
108 params = Map.delete(@full_user_data, key)
109 changeset = User.register_changeset(%User{}, params)
111 assert if key == :bio, do: changeset.valid?, else: not changeset.valid?
115 test "it sets the password_hash, ap_id and following fields" do
116 changeset = User.register_changeset(%User{}, @full_user_data)
118 assert changeset.valid?
120 assert is_binary(changeset.changes[:password_hash])
121 assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
123 assert changeset.changes[:following] == [
124 User.ap_followers(%User{nickname: @full_user_data.nickname})
127 assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
131 describe "fetching a user from nickname or trying to build one" do
132 test "gets an existing user" do
134 fetched_user = User.get_or_fetch_by_nickname(user.nickname)
136 assert user == fetched_user
139 test "gets an existing user, case insensitive" do
140 user = insert(:user, nickname: "nick")
141 fetched_user = User.get_or_fetch_by_nickname("NICK")
143 assert user == fetched_user
146 test "fetches an external user via ostatus if no user exists" do
147 fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
148 assert fetched_user.nickname == "shp@social.heldscal.la"
151 test "returns nil if no user could be fetched" do
152 fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
153 assert fetched_user == nil
156 test "returns nil for nonexistant local user" do
157 fetched_user = User.get_or_fetch_by_nickname("nonexistant")
158 assert fetched_user == nil
162 test "returns an ap_id for a user" do
165 assert User.ap_id(user) ==
166 Pleroma.Web.Router.Helpers.o_status_url(
167 Pleroma.Web.Endpoint,
173 test "returns an ap_followers link for a user" do
176 assert User.ap_followers(user) ==
177 Pleroma.Web.Router.Helpers.o_status_url(
178 Pleroma.Web.Endpoint,
184 describe "remote user creation changeset" do
190 info: %{some: "info"},
191 avatar: %{some: "avatar"}
194 test "it confirms validity" do
195 cs = User.remote_user_creation(@valid_remote)
199 test "it sets the follower_adress" do
200 cs = User.remote_user_creation(@valid_remote)
201 # remote users get a fake local follower address
202 assert cs.changes.follower_address ==
203 User.ap_followers(%User{nickname: @valid_remote[:nickname]})
206 test "it enforces the fqn format for nicknames" do
207 cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
208 assert cs.changes.local == false
209 assert cs.changes.avatar
213 test "it has required fields" do
214 [:name, :nickname, :ap_id]
215 |> Enum.each(fn field ->
216 cs = User.remote_user_creation(Map.delete(@valid_remote, field))
221 test "it restricts some sizes" do
222 [bio: 5000, name: 100]
223 |> Enum.each(fn {field, size} ->
224 string = String.pad_leading(".", size)
225 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
228 string = String.pad_leading(".", size + 1)
229 cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
235 describe "followers and friends" do
236 test "gets all followers for a given user" do
238 follower_one = insert(:user)
239 follower_two = insert(:user)
240 not_follower = insert(:user)
242 {:ok, follower_one} = User.follow(follower_one, user)
243 {:ok, follower_two} = User.follow(follower_two, user)
245 {:ok, res} = User.get_followers(user)
247 assert Enum.member?(res, follower_one)
248 assert Enum.member?(res, follower_two)
249 refute Enum.member?(res, not_follower)
252 test "gets all friends (followed users) for a given user" do
254 followed_one = insert(:user)
255 followed_two = insert(:user)
256 not_followed = insert(:user)
258 {:ok, user} = User.follow(user, followed_one)
259 {:ok, user} = User.follow(user, followed_two)
261 {:ok, res} = User.get_friends(user)
263 followed_one = User.get_by_ap_id(followed_one.ap_id)
264 followed_two = User.get_by_ap_id(followed_two.ap_id)
265 assert Enum.member?(res, followed_one)
266 assert Enum.member?(res, followed_two)
267 refute Enum.member?(res, not_followed)
271 describe "updating note and follower count" do
272 test "it sets the info->note_count property" do
275 user = User.get_by_ap_id(note.data["actor"])
277 assert user.info["note_count"] == nil
279 {:ok, user} = User.update_note_count(user)
281 assert user.info["note_count"] == 1
284 test "it increases the info->note_count property" do
286 user = User.get_by_ap_id(note.data["actor"])
288 assert user.info["note_count"] == nil
290 {:ok, user} = User.increase_note_count(user)
292 assert user.info["note_count"] == 1
294 {:ok, user} = User.increase_note_count(user)
296 assert user.info["note_count"] == 2
299 test "it sets the info->follower_count property" do
301 follower = insert(:user)
303 User.follow(follower, user)
305 assert user.info["follower_count"] == nil
307 {:ok, user} = User.update_follower_count(user)
309 assert user.info["follower_count"] == 1
314 test "it blocks people" do
316 blocked_user = insert(:user)
318 refute User.blocks?(user, blocked_user)
320 {:ok, user} = User.block(user, blocked_user)
322 assert User.blocks?(user, blocked_user)
325 test "it unblocks users" do
327 blocked_user = insert(:user)
329 {:ok, user} = User.block(user, blocked_user)
330 {:ok, user} = User.unblock(user, blocked_user)
332 refute User.blocks?(user, blocked_user)
336 test "get recipients from activity" do
337 actor = insert(:user)
338 user = insert(:user, local: true)
339 user_two = insert(:user, local: false)
340 addressed = insert(:user, local: true)
341 addressed_remote = insert(:user, local: false)
344 CommonAPI.post(actor, %{
345 "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
348 assert [addressed] == User.get_recipients_from_activity(activity)
350 {:ok, user} = User.follow(user, actor)
351 {:ok, _user_two} = User.follow(user_two, actor)
352 recipients = User.get_recipients_from_activity(activity)
353 assert length(recipients) == 2
354 assert user in recipients
355 assert addressed in recipients
358 test ".deactivate deactivates a user" do
360 assert false == !!user.info["deactivated"]
361 {:ok, user} = User.deactivate(user)
362 assert true == user.info["deactivated"]
365 test ".delete deactivates a user, all follow relationships and all create activities" do
367 followed = insert(:user)
368 follower = insert(:user)
370 {:ok, user} = User.follow(user, followed)
371 {:ok, follower} = User.follow(follower, user)
373 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
374 {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
376 {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
377 {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
378 {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
380 :ok = User.delete(user)
382 followed = Repo.get(User, followed.id)
383 follower = Repo.get(User, follower.id)
384 user = Repo.get(User, user.id)
386 assert user.info["deactivated"]
388 refute User.following?(user, followed)
389 refute User.following?(followed, follower)
391 # TODO: Remove favorites, repeats, delete activities.
393 refute Repo.get(Activity, activity.id)
396 test "get_public_key_for_ap_id fetches a user that's not in the db" do
397 assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
400 test "insert or update a user from given data" do
401 user = insert(:user, %{nickname: "nick@name.de"})
402 data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
404 assert {:ok, %User{}} = User.insert_or_update_user(data)