+ describe "updating note and follower count" do
+ test "it sets the info->note_count property" do
+ note = insert(:note)
+
+ user = User.get_by_ap_id(note.data["actor"])
+
+ assert user.info.note_count == 0
+
+ {:ok, user} = User.update_note_count(user)
+
+ assert user.info.note_count == 1
+ end
+
+ test "it increases the info->note_count property" do
+ note = insert(:note)
+ user = User.get_by_ap_id(note.data["actor"])
+
+ assert user.info.note_count == 0
+
+ {:ok, user} = User.increase_note_count(user)
+
+ assert user.info.note_count == 1
+
+ {:ok, user} = User.increase_note_count(user)
+
+ assert user.info.note_count == 2
+ end
+
+ test "it decreases the info->note_count property" do
+ note = insert(:note)
+ user = User.get_by_ap_id(note.data["actor"])
+
+ assert user.info.note_count == 0
+
+ {:ok, user} = User.increase_note_count(user)
+
+ assert user.info.note_count == 1
+
+ {:ok, user} = User.decrease_note_count(user)
+
+ assert user.info.note_count == 0
+
+ {:ok, user} = User.decrease_note_count(user)
+
+ assert user.info.note_count == 0
+ end
+
+ test "it sets the info->follower_count property" do
+ user = insert(:user)
+ follower = insert(:user)
+
+ User.follow(follower, user)
+
+ assert user.info.follower_count == 0
+
+ {:ok, user} = User.update_follower_count(user)
+
+ assert user.info.follower_count == 1
+ end
+ end
+
+ describe "blocks" do
+ test "it blocks people" do
+ user = insert(:user)
+ blocked_user = insert(:user)
+
+ refute User.blocks?(user, blocked_user)
+
+ {:ok, user} = User.block(user, blocked_user)
+
+ assert User.blocks?(user, blocked_user)
+ end
+
+ test "it unblocks users" do
+ user = insert(:user)
+ blocked_user = insert(:user)
+
+ {:ok, user} = User.block(user, blocked_user)
+ {:ok, user} = User.unblock(user, blocked_user)
+
+ refute User.blocks?(user, blocked_user)
+ end
+
+ test "blocks tear down cyclical follow relationships" do
+ blocker = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, blocker} = User.follow(blocker, blocked)
+ {:ok, blocked} = User.follow(blocked, blocker)
+
+ assert User.following?(blocker, blocked)
+ assert User.following?(blocked, blocker)
+
+ {:ok, blocker} = User.block(blocker, blocked)
+ blocked = Repo.get(User, blocked.id)
+
+ assert User.blocks?(blocker, blocked)
+
+ refute User.following?(blocker, blocked)
+ refute User.following?(blocked, blocker)
+ end
+
+ test "blocks tear down blocker->blocked follow relationships" do
+ blocker = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, blocker} = User.follow(blocker, blocked)
+
+ assert User.following?(blocker, blocked)
+ refute User.following?(blocked, blocker)
+
+ {:ok, blocker} = User.block(blocker, blocked)
+ blocked = Repo.get(User, blocked.id)
+
+ assert User.blocks?(blocker, blocked)
+
+ refute User.following?(blocker, blocked)
+ refute User.following?(blocked, blocker)
+ end
+
+ test "blocks tear down blocked->blocker follow relationships" do
+ blocker = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, blocked} = User.follow(blocked, blocker)
+
+ refute User.following?(blocker, blocked)
+ assert User.following?(blocked, blocker)
+
+ {:ok, blocker} = User.block(blocker, blocked)
+ blocked = Repo.get(User, blocked.id)
+
+ assert User.blocks?(blocker, blocked)
+
+ refute User.following?(blocker, blocked)
+ refute User.following?(blocked, blocker)
+ end
+ end
+
+ describe "domain blocking" do
+ test "blocks domains" do
+ user = insert(:user)
+ collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
+
+ {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
+
+ assert User.blocks?(user, collateral_user)
+ end
+
+ test "unblocks domains" do
+ user = insert(:user)
+ collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
+
+ {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
+ {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
+
+ refute User.blocks?(user, collateral_user)
+ end
+ end
+
+ test "get recipients from activity" do
+ actor = insert(:user)
+ user = insert(:user, local: true)
+ user_two = insert(:user, local: false)
+ addressed = insert(:user, local: true)
+ addressed_remote = insert(:user, local: false)
+
+ {:ok, activity} =
+ CommonAPI.post(actor, %{
+ "status" => "hey @#{addressed.nickname} @#{addressed_remote.nickname}"
+ })
+
+ assert [addressed] == User.get_recipients_from_activity(activity)
+
+ {:ok, user} = User.follow(user, actor)
+ {:ok, _user_two} = User.follow(user_two, actor)
+ recipients = User.get_recipients_from_activity(activity)
+ assert length(recipients) == 2
+ assert user in recipients
+ assert addressed in recipients
+ end
+
+ test ".deactivate can de-activate then re-activate a user" do
+ user = insert(:user)
+ assert false == user.info.deactivated
+ {:ok, user} = User.deactivate(user)
+ assert true == user.info.deactivated
+ {:ok, user} = User.deactivate(user, false)
+ assert false == user.info.deactivated
+ end
+
+ test ".delete deactivates a user, all follow relationships and all create activities" do
+ user = insert(:user)
+ followed = insert(:user)
+ follower = insert(:user)
+
+ {:ok, user} = User.follow(user, followed)
+ {:ok, follower} = User.follow(follower, user)
+
+ {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
+ {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
+
+ {:ok, _, _} = CommonAPI.favorite(activity_two.id, user)
+ {:ok, _, _} = CommonAPI.favorite(activity.id, follower)
+ {:ok, _, _} = CommonAPI.repeat(activity.id, follower)
+
+ {:ok, _} = User.delete(user)
+
+ followed = Repo.get(User, followed.id)
+ follower = Repo.get(User, follower.id)
+ user = Repo.get(User, user.id)
+
+ assert user.info.deactivated
+
+ refute User.following?(user, followed)
+ refute User.following?(followed, follower)
+
+ # TODO: Remove favorites, repeats, delete activities.
+
+ refute Repo.get(Activity, activity.id)
+ end
+
+ test "get_public_key_for_ap_id fetches a user that's not in the db" do
+ assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
+ end
+
+ test "insert or update a user from given data" do
+ user = insert(:user, %{nickname: "nick@name.de"})
+ data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname}
+
+ assert {:ok, %User{}} = User.insert_or_update_user(data)
+ end
+
+ describe "per-user rich-text filtering" do
+ test "html_filter_policy returns nil when rich-text is enabled" do
+ user = insert(:user)
+
+ assert nil == User.html_filter_policy(user)
+ end
+
+ test "html_filter_policy returns TwitterText scrubber when rich-text is disabled" do
+ user = insert(:user, %{info: %{no_rich_text: true}})
+
+ assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
+ end
+ end
+
+ describe "caching" do
+ test "invalidate_cache works" do
+ user = insert(:user)
+ _user_info = User.get_cached_user_info(user)
+
+ User.invalidate_cache(user)
+
+ {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
+ {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
+ {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
+ end
+
+ test "User.delete() plugs any possible zombie objects" do
+ user = insert(:user)
+
+ {:ok, _} = User.delete(user)
+
+ {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
+
+ assert cached_user != user
+
+ {:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
+
+ assert cached_user != user
+ end
+ end
+
+ describe "User.search" do
+ test "finds a user, ranking by similarity" do
+ _user = insert(:user, %{name: "lain"})
+ _user_two = insert(:user, %{name: "ean"})
+ _user_three = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
+ user_four = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
+
+ assert user_four ==
+ User.search("lain@ple") |> List.first() |> Map.put(:search_distance, nil)
+ end
+ end
+end