forked from potsda.mn/mobilizon
Add test to the follow backend
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
824b1a969e
commit
b99625afab
|
@ -49,7 +49,8 @@ defmodule Mobilizon.Actors.Actor do
|
|||
field(:avatar_url, :string)
|
||||
field(:banner_url, :string)
|
||||
# field(:openness, Mobilizon.Actors.ActorOpennesssEnum, default: :moderated)
|
||||
many_to_many(:followers, Actor, join_through: Follower)
|
||||
has_many(:followers, Follower, foreign_key: :target_actor_id)
|
||||
has_many(:followings, Follower, foreign_key: :actor_id)
|
||||
has_many(:organized_events, Event, foreign_key: :organizer_actor_id)
|
||||
many_to_many(:memberships, Actor, join_through: Member)
|
||||
belongs_to(:user, User)
|
||||
|
@ -270,7 +271,10 @@ defmodule Mobilizon.Actors.Actor do
|
|||
)
|
||||
end
|
||||
|
||||
|
||||
@spec follow(struct(), struct(), boolean()) :: Follower.t() | {:error, String.t()}
|
||||
def follow(%Actor{} = follower, %Actor{} = followed, approved \\ true) do
|
||||
|
||||
with {:suspended, false} <- {:suspended, followed.suspended},
|
||||
# Check if followed has blocked follower
|
||||
{:already_following, false} <- {:already_following, following?(follower, followed)} do
|
||||
|
@ -293,7 +297,10 @@ defmodule Mobilizon.Actors.Actor do
|
|||
})
|
||||
end
|
||||
|
||||
def following?(%Actor{} = follower, %Actor{followers: followers}) do
|
||||
Enum.member?(followers, follower)
|
||||
@spec following?(struct(), struct()) :: boolean()
|
||||
def following?(%Actor{id: follower_actor_id} = _follower_actor, %Actor{followers: followers} = _followed) do
|
||||
followers
|
||||
|> Enum.map(&(&1.actor_id))
|
||||
|> Enum.member?(follower_actor_id)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -73,7 +73,7 @@ defmodule Mobilizon.Actors do
|
|||
|
||||
def get_actor_with_everything!(id) do
|
||||
actor = Repo.get!(Actor, id)
|
||||
Repo.preload(actor, :organized_events)
|
||||
Repo.preload(actor, [:organized_events, :followers, :followings])
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -899,9 +899,12 @@ defmodule Mobilizon.Actors do
|
|||
|
||||
"""
|
||||
def create_follower(attrs \\ %{}) do
|
||||
%Follower{}
|
||||
|> Follower.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
with {:ok, %Follower{} = follower} <-
|
||||
%Follower{}
|
||||
|> Follower.changeset(attrs)
|
||||
|> Repo.insert() do
|
||||
{:ok, Repo.preload(follower, [:actor, :target_actor])}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
|
@ -579,6 +579,31 @@ defmodule Mobilizon.ActorsTest do
|
|||
follower = create_test_follower(context)
|
||||
assert %Ecto.Changeset{} = Actors.change_follower(follower)
|
||||
end
|
||||
|
||||
test "follow/3 makes an actor follow another", %{actor: actor, target_actor: target_actor} do
|
||||
# Preloading followers/followings
|
||||
actor = Actors.get_actor_with_everything!(actor.id)
|
||||
target_actor = Actors.get_actor_with_everything!(target_actor.id)
|
||||
|
||||
{:ok, follower} = Actor.follow(actor, target_actor)
|
||||
assert follower.actor.id == actor.id
|
||||
|
||||
# Referesh followers/followings
|
||||
actor = Actors.get_actor_with_everything!(actor.id)
|
||||
target_actor = Actors.get_actor_with_everything!(target_actor.id)
|
||||
|
||||
assert target_actor.followers |> Enum.map(&(&1.actor_id)) == [actor.id]
|
||||
assert actor.followings |> Enum.map(&(&1.target_actor_id)) == [target_actor.id]
|
||||
|
||||
# Test if actor is already following target actor
|
||||
{:error, msg} = Actor.follow(actor, target_actor)
|
||||
assert msg =~ "already following"
|
||||
|
||||
# Test if target actor is suspended
|
||||
target_actor = %{target_actor | suspended: true}
|
||||
{:error, msg} = Actor.follow(actor, target_actor)
|
||||
assert msg =~ "suspended"
|
||||
end
|
||||
end
|
||||
|
||||
describe "members" do
|
||||
|
|
Loading…
Reference in a new issue