Add test to the follow backend

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-11-27 14:02:51 +01:00
parent 824b1a969e
commit b99625afab
3 changed files with 42 additions and 7 deletions

View file

@ -49,7 +49,8 @@ defmodule Mobilizon.Actors.Actor do
field(:avatar_url, :string) field(:avatar_url, :string)
field(:banner_url, :string) field(:banner_url, :string)
# field(:openness, Mobilizon.Actors.ActorOpennesssEnum, default: :moderated) # 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) has_many(:organized_events, Event, foreign_key: :organizer_actor_id)
many_to_many(:memberships, Actor, join_through: Member) many_to_many(:memberships, Actor, join_through: Member)
belongs_to(:user, User) belongs_to(:user, User)
@ -270,7 +271,10 @@ defmodule Mobilizon.Actors.Actor do
) )
end end
@spec follow(struct(), struct(), boolean()) :: Follower.t() | {:error, String.t()}
def follow(%Actor{} = follower, %Actor{} = followed, approved \\ true) do def follow(%Actor{} = follower, %Actor{} = followed, approved \\ true) do
with {:suspended, false} <- {:suspended, followed.suspended}, with {:suspended, false} <- {:suspended, followed.suspended},
# Check if followed has blocked follower # Check if followed has blocked follower
{:already_following, false} <- {:already_following, following?(follower, followed)} do {:already_following, false} <- {:already_following, following?(follower, followed)} do
@ -293,7 +297,10 @@ defmodule Mobilizon.Actors.Actor do
}) })
end end
def following?(%Actor{} = follower, %Actor{followers: followers}) do @spec following?(struct(), struct()) :: boolean()
Enum.member?(followers, follower) 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
end end

View file

@ -73,7 +73,7 @@ defmodule Mobilizon.Actors do
def get_actor_with_everything!(id) do def get_actor_with_everything!(id) do
actor = Repo.get!(Actor, id) actor = Repo.get!(Actor, id)
Repo.preload(actor, :organized_events) Repo.preload(actor, [:organized_events, :followers, :followings])
end end
@doc """ @doc """
@ -899,9 +899,12 @@ defmodule Mobilizon.Actors do
""" """
def create_follower(attrs \\ %{}) do def create_follower(attrs \\ %{}) do
%Follower{} with {:ok, %Follower{} = follower} <-
|> Follower.changeset(attrs) %Follower{}
|> Repo.insert() |> Follower.changeset(attrs)
|> Repo.insert() do
{:ok, Repo.preload(follower, [:actor, :target_actor])}
end
end end
@doc """ @doc """

View file

@ -579,6 +579,31 @@ defmodule Mobilizon.ActorsTest do
follower = create_test_follower(context) follower = create_test_follower(context)
assert %Ecto.Changeset{} = Actors.change_follower(follower) assert %Ecto.Changeset{} = Actors.change_follower(follower)
end 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 end
describe "members" do describe "members" do