add tests

This commit is contained in:
Egor Kislitsyn 2019-04-12 16:38:32 +07:00
parent 46bd5c1d87
commit dd097a406b
2 changed files with 75 additions and 12 deletions

View File

@ -1159,13 +1159,16 @@ defmodule Pleroma.User do
def deactivate(%User{} = user, status \\ true) do def deactivate(%User{} = user, status \\ true) do
info_cng = User.Info.set_activation_status(user.info, status) info_cng = User.Info.set_activation_status(user.info, status)
with {:ok, user} <- with {:ok, friends} <- User.get_friends(user),
{:ok, followers} <- User.get_followers(user),
{:ok, user} <-
user user
|> change() |> change()
|> put_embed(:info, info_cng) |> put_embed(:info, info_cng)
|> update_and_set_cache(), |> update_and_set_cache() do
{:ok, friends} <- User.get_friends(user) do Enum.each(followers, &invalidate_cache(&1))
Enum.each(friends, &update_follower_count(&1)) Enum.each(friends, &update_follower_count(&1))
{:ok, user} {:ok, user}
end end
end end

View File

@ -5,8 +5,10 @@
defmodule Pleroma.UserTest do defmodule Pleroma.UserTest do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.UserBuilder
alias Pleroma.Notification
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
use Pleroma.DataCase use Pleroma.DataCase
@ -212,8 +214,8 @@ defmodule Pleroma.UserTest do
test "fetches correct profile for nickname beginning with number" do test "fetches correct profile for nickname beginning with number" do
# Use old-style integer ID to try to reproduce the problem # Use old-style integer ID to try to reproduce the problem
user = insert(:user, %{id: 1080}) user = insert(:user, %{id: 1080})
userwithnumbers = insert(:user, %{nickname: "#{user.id}garbage"}) user_with_numbers = insert(:user, %{nickname: "#{user.id}garbage"})
assert userwithnumbers == User.get_cached_by_nickname_or_id(userwithnumbers.nickname) assert user_with_numbers == User.get_cached_by_nickname_or_id(user_with_numbers.nickname)
end end
describe "user registration" do describe "user registration" do
@ -815,7 +817,8 @@ defmodule Pleroma.UserTest do
assert addressed in recipients assert addressed in recipients
end end
test ".deactivate can de-activate then re-activate a user" do describe ".deactivate" do
test "can de-activate then re-activate a user" do
user = insert(:user) user = insert(:user)
assert false == user.info.deactivated assert false == user.info.deactivated
{:ok, user} = User.deactivate(user) {:ok, user} = User.deactivate(user)
@ -824,6 +827,63 @@ defmodule Pleroma.UserTest do
assert false == user.info.deactivated assert false == user.info.deactivated
end end
test "hide a user from followers " do
user = insert(:user)
user2 = insert(:user)
{:ok, user} = User.follow(user, user2)
{:ok, _user} = User.deactivate(user)
info = User.get_cached_user_info(user2)
assert info.follower_count == 0
assert {:ok, []} = User.get_followers(user2)
end
test "hide a user from friends" do
user = insert(:user)
user2 = insert(:user)
{:ok, user2} = User.follow(user2, user)
assert User.following_count(user2) == 1
{:ok, _user} = User.deactivate(user)
info = User.get_cached_user_info(user2)
assert info.following_count == 0
assert User.following_count(user2) == 0
assert {:ok, []} = User.get_friends(user2)
end
test "hide a user's statuses from timelines and notifications" do
user = insert(:user)
user2 = insert(:user)
{:ok, user2} = User.follow(user2, user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}"})
[notification] = Notification.for_user(user2)
assert notification.activity == activity
assert [activity] == ActivityPub.fetch_public_activities(%{})
assert [activity] ==
ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
|> ActivityPub.contain_timeline(user2)
{:ok, _user} = User.deactivate(user)
assert [] == ActivityPub.fetch_public_activities(%{})
assert [] == Notification.for_user(user2)
assert [] ==
ActivityPub.fetch_activities([user2.ap_id | user2.following], %{"user" => user2})
|> ActivityPub.contain_timeline(user2)
end
end
test ".delete_user_activities deletes all create activities" do test ".delete_user_activities deletes all create activities" do
user = insert(:user) user = insert(:user)