2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.ActorsTest do
|
|
|
|
use Mobilizon.DataCase
|
2019-12-03 11:29:51 +01:00
|
|
|
use Oban.Testing, repo: Mobilizon.Storage.Repo
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
import Mox
|
2019-09-09 00:52:49 +02:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
alias Mobilizon.{Actors, Config, Discussions, Events, Users}
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Actors.{Actor, Bot, Follower, Member}
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Events.Event
|
2021-11-13 18:45:01 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Medias.File, as: FileModel
|
2021-11-13 18:45:01 +01:00
|
|
|
alias Mobilizon.Service.HTTP.ActivityPub.Mock
|
2020-01-23 00:55:07 +01:00
|
|
|
alias Mobilizon.Service.Workers
|
2019-09-09 00:52:49 +02:00
|
|
|
alias Mobilizon.Storage.Page
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.Upload.Uploader
|
2020-01-23 21:59:50 +01:00
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
describe "actors" do
|
2018-07-27 10:45:35 +02:00
|
|
|
@valid_attrs %{
|
|
|
|
summary: "some description",
|
2018-08-24 11:34:00 +02:00
|
|
|
name: "Bobby Blank",
|
2018-07-27 10:45:35 +02:00
|
|
|
domain: "some domain",
|
|
|
|
keys: "some keypair",
|
|
|
|
suspended: true,
|
|
|
|
uri: "some uri",
|
|
|
|
url: "some url",
|
2022-05-06 19:30:52 +02:00
|
|
|
preferred_username: "some_username"
|
2018-07-27 10:45:35 +02:00
|
|
|
}
|
|
|
|
@update_attrs %{
|
|
|
|
summary: "some updated description",
|
|
|
|
name: "some updated name",
|
|
|
|
domain: "some updated domain",
|
|
|
|
keys: "some updated keys",
|
|
|
|
suspended: false,
|
|
|
|
uri: "some updated uri",
|
|
|
|
url: "some updated url",
|
2022-05-06 19:30:52 +02:00
|
|
|
preferred_username: "some_updated_username"
|
2018-07-27 10:45:35 +02:00
|
|
|
}
|
|
|
|
@invalid_attrs %{
|
|
|
|
summary: nil,
|
|
|
|
name: nil,
|
|
|
|
domain: nil,
|
|
|
|
keys: nil,
|
|
|
|
suspended: nil,
|
|
|
|
uri: nil,
|
|
|
|
url: nil,
|
2019-02-25 18:35:00 +01:00
|
|
|
preferred_username: "never"
|
2018-07-27 10:45:35 +02:00
|
|
|
}
|
2018-05-30 18:59:13 +02:00
|
|
|
|
2018-08-03 11:08:51 +02:00
|
|
|
@remote_account_url "https://social.tcit.fr/users/tcit"
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
setup do
|
|
|
|
user = insert(:user)
|
2019-02-21 18:11:49 +01:00
|
|
|
actor = insert(:actor, user: user, preferred_username: "tcit")
|
2018-05-30 18:59:13 +02:00
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
{:ok, actor: actor}
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
test "list_actors/0 returns all actors", %{actor: %Actor{id: actor_id}} do
|
2020-06-11 19:13:21 +02:00
|
|
|
assert %Page{total: 1, elements: [%Actor{id: id}]} = Actors.list_actors()
|
|
|
|
assert id == actor_id
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
test "get_actor!/1 returns the actor with given id", %{actor: %Actor{id: actor_id} = actor} do
|
|
|
|
assert actor_id == Actors.get_actor!(actor.id).id
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
test "get_actor_for_user/1 returns the actor for an user", %{
|
|
|
|
actor: %{user: user, id: actor_id} = _actor
|
|
|
|
} do
|
2019-03-05 17:23:05 +01:00
|
|
|
assert actor_id == Users.get_actor_for_user(user).id
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "get_actor_for_user/1 returns the actor for an user with no default actor defined" do
|
|
|
|
user = insert(:user)
|
2018-11-12 18:17:53 +01:00
|
|
|
actor_id = insert(:actor, user: user).id
|
2019-03-05 17:23:05 +01:00
|
|
|
assert actor_id == Users.get_actor_for_user(user).id
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
test "get_actor_with_preload/1 returns the actor with its organized events", %{
|
2018-08-01 14:45:18 +02:00
|
|
|
actor: actor
|
|
|
|
} do
|
2019-09-09 00:52:49 +02:00
|
|
|
assert Actors.get_actor_with_preload(actor.id).organized_events == []
|
2018-08-01 14:45:18 +02:00
|
|
|
event = insert(:event, organizer_actor: actor)
|
2018-11-12 18:17:53 +01:00
|
|
|
|
|
|
|
event_found_id =
|
2019-09-09 00:52:49 +02:00
|
|
|
Actors.get_actor_with_preload(actor.id).organized_events |> hd |> Map.get(:id)
|
2018-11-12 18:17:53 +01:00
|
|
|
|
|
|
|
assert event_found_id == event.id
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
test "get_actor_by_name/1 returns a local actor", %{
|
|
|
|
actor: %Actor{id: actor_id, preferred_username: preferred_username}
|
|
|
|
} do
|
|
|
|
actor_found_id = Actors.get_actor_by_name(preferred_username).id
|
|
|
|
assert actor_found_id == actor_id
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
|
|
|
|
2018-08-03 11:08:51 +02:00
|
|
|
test "get_actor_by_name/1 returns a remote actor" do
|
2021-11-13 18:45:01 +01:00
|
|
|
tcit_social_tcit =
|
|
|
|
"test/fixtures/mastodon-tcit-tcit.json" |> File.read!() |> Jason.decode!()
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: @remote_account_url}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: tcit_social_tcit}}
|
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%Actor{
|
|
|
|
id: actor_id,
|
|
|
|
preferred_username: preferred_username,
|
|
|
|
domain: domain,
|
|
|
|
avatar: %FileModel{name: picture_name} = _picture
|
|
|
|
} = _actor} = ActivityPubActor.get_or_fetch_actor_by_url(@remote_account_url)
|
|
|
|
|
|
|
|
assert picture_name == "a28c50ce5f2b13fd.jpg"
|
|
|
|
|
|
|
|
%Actor{
|
|
|
|
id: actor_found_id,
|
|
|
|
avatar: %FileModel{name: picture_name} = _picture
|
|
|
|
} = Actors.get_actor_by_name("#{preferred_username}@#{domain}")
|
|
|
|
|
|
|
|
assert actor_found_id == actor_id
|
|
|
|
assert picture_name == "a28c50ce5f2b13fd.jpg"
|
2018-08-03 11:08:51 +02:00
|
|
|
end
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
test "get_local_actor_by_name_with_preload!/1 returns the local actor with its organized events",
|
2018-08-03 10:19:28 +02:00
|
|
|
%{
|
|
|
|
actor: actor
|
|
|
|
} do
|
2019-09-09 00:52:49 +02:00
|
|
|
assert Actors.get_local_actor_by_name_with_preload(actor.preferred_username).organized_events ==
|
2018-08-03 10:19:28 +02:00
|
|
|
[]
|
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
event = insert(:event, organizer_actor: actor)
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
event_found_id =
|
2019-09-09 00:52:49 +02:00
|
|
|
Actors.get_local_actor_by_name_with_preload(actor.preferred_username).organized_events
|
2018-11-12 18:17:53 +01:00
|
|
|
|> hd
|
|
|
|
|> Map.get(:id)
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
assert event_found_id == event.id
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
test "get_actor_by_name_with_preload!/1 returns the local actor with its organized events",
|
2018-08-03 10:19:28 +02:00
|
|
|
%{
|
|
|
|
actor: actor
|
|
|
|
} do
|
2019-09-09 00:52:49 +02:00
|
|
|
assert Actors.get_actor_by_name_with_preload(actor.preferred_username).organized_events ==
|
2018-08-03 10:19:28 +02:00
|
|
|
[]
|
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
event = insert(:event, organizer_actor: actor)
|
2018-11-12 18:17:53 +01:00
|
|
|
|
|
|
|
event_found_id =
|
2019-09-09 00:52:49 +02:00
|
|
|
Actors.get_actor_by_name_with_preload(actor.preferred_username).organized_events
|
2018-11-12 18:17:53 +01:00
|
|
|
|> hd
|
|
|
|
|> Map.get(:id)
|
|
|
|
|
|
|
|
assert event_found_id == event.id
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
test "get_actor_by_name_with_preload!/1 returns the remote actor with its organized events" do
|
2021-11-13 18:45:01 +01:00
|
|
|
tcit_social_tcit =
|
|
|
|
"test/fixtures/mastodon-tcit-tcit.json" |> File.read!() |> Jason.decode!()
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: @remote_account_url}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: tcit_social_tcit}}
|
|
|
|
end)
|
|
|
|
|
|
|
|
with {:ok, %Actor{} = actor} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(@remote_account_url) do
|
|
|
|
assert Actors.get_actor_by_name_with_preload(
|
|
|
|
"#{actor.preferred_username}@#{actor.domain}"
|
|
|
|
).organized_events == []
|
|
|
|
|
|
|
|
event = insert(:event, organizer_actor: actor)
|
|
|
|
|
|
|
|
event_found_id =
|
|
|
|
Actors.get_actor_by_name_with_preload("#{actor.preferred_username}@#{actor.domain}").organized_events
|
|
|
|
|> hd
|
|
|
|
|> Map.get(:id)
|
|
|
|
|
|
|
|
assert event_found_id == event.id
|
2018-11-13 12:23:37 +01:00
|
|
|
end
|
2018-08-03 11:08:51 +02:00
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
test "test list_local_actor_by_username/1 returns local actors with similar usernames", %{
|
2018-08-03 10:19:28 +02:00
|
|
|
actor: actor
|
|
|
|
} do
|
2019-02-21 18:11:49 +01:00
|
|
|
actor2 = insert(:actor, preferred_username: "tcit")
|
2019-09-11 03:16:37 +02:00
|
|
|
[%Actor{id: actor_found_id} | tail] = Actors.list_local_actor_by_username("tcit")
|
2018-11-12 18:17:53 +01:00
|
|
|
%Actor{id: actor2_found_id} = hd(tail)
|
2019-02-21 18:11:49 +01:00
|
|
|
assert MapSet.new([actor_found_id, actor2_found_id]) == MapSet.new([actor.id, actor2.id])
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
|
|
|
|
2021-11-06 10:09:54 +01:00
|
|
|
test "test search_actors/4 returns actors with similar usernames",
|
2019-09-09 00:52:49 +02:00
|
|
|
%{actor: %Actor{id: actor_id}} do
|
2021-11-13 18:45:01 +01:00
|
|
|
tcit_social_tcit =
|
|
|
|
"test/fixtures/mastodon-tcit-tcit.json" |> File.read!() |> Jason.decode!()
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: @remote_account_url}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: tcit_social_tcit}}
|
|
|
|
end)
|
|
|
|
|
|
|
|
with {:ok, %Actor{id: actor2_id}} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(@remote_account_url) do
|
|
|
|
%Page{total: 2, elements: actors} =
|
|
|
|
Actors.search_actors("tcit",
|
|
|
|
actor_type: :Person,
|
|
|
|
minimum_visibility: :private
|
|
|
|
)
|
|
|
|
|
|
|
|
actors_ids = actors |> Enum.map(& &1.id)
|
|
|
|
|
|
|
|
assert MapSet.new(actors_ids) == MapSet.new([actor2_id, actor_id])
|
2018-11-13 12:23:37 +01:00
|
|
|
end
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
|
|
|
|
2021-11-06 10:09:54 +01:00
|
|
|
test "test search_actors/4 returns actors with similar names" do
|
|
|
|
%{total: 0, elements: actors} = Actors.search_actors("ohno", actor_type: :Person)
|
2019-04-12 15:04:32 +02:00
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
assert actors == []
|
|
|
|
end
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
test "create_actor/1 with valid data creates a actor" do
|
|
|
|
assert {:ok, %Actor{} = actor} = Actors.create_actor(@valid_attrs)
|
2018-06-14 17:25:55 +02:00
|
|
|
assert actor.summary == "some description"
|
2018-08-24 11:34:00 +02:00
|
|
|
assert actor.name == "Bobby Blank"
|
2018-05-30 18:59:13 +02:00
|
|
|
assert actor.domain == "some domain"
|
2018-06-14 17:25:55 +02:00
|
|
|
assert actor.keys == "some keypair"
|
2018-05-30 18:59:13 +02:00
|
|
|
assert actor.suspended
|
2022-05-06 19:30:52 +02:00
|
|
|
assert actor.preferred_username == "some_username"
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
test "create_actor/1 with empty data returns error changeset" do
|
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.create_actor()
|
|
|
|
end
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
test "create_actor/1 with invalid data returns error changeset" do
|
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.create_actor(@invalid_attrs)
|
|
|
|
end
|
|
|
|
|
2019-06-05 18:29:39 +02:00
|
|
|
test "update_actor/2 with valid data updates the actor", %{
|
|
|
|
actor: %Actor{} = actor
|
|
|
|
} do
|
|
|
|
assert {:ok, actor} =
|
|
|
|
Actors.update_actor(
|
|
|
|
actor,
|
|
|
|
@update_attrs
|
|
|
|
)
|
|
|
|
|
|
|
|
assert %Actor{} = actor
|
|
|
|
assert actor.summary == "some updated description"
|
|
|
|
assert actor.name == "some updated name"
|
|
|
|
assert actor.keys == "some updated keys"
|
|
|
|
refute actor.suspended
|
|
|
|
end
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
test "update_actor/2 with valid data updates the actor and its media files", %{
|
2019-06-05 18:29:39 +02:00
|
|
|
actor: %Actor{avatar: %{url: avatar_url}, banner: %{url: banner_url}} = actor
|
|
|
|
} do
|
|
|
|
%URI{path: "/media/" <> avatar_path} = URI.parse(avatar_url)
|
|
|
|
%URI{path: "/media/" <> banner_path} = URI.parse(banner_url)
|
|
|
|
|
|
|
|
assert File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> avatar_path
|
|
|
|
)
|
|
|
|
|
|
|
|
assert File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> banner_path
|
|
|
|
)
|
|
|
|
|
|
|
|
file = %Plug.Upload{
|
|
|
|
content_type: "image/jpg",
|
|
|
|
path: Path.absname("test/fixtures/image.jpg"),
|
|
|
|
filename: "image.jpg"
|
|
|
|
}
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
{:ok, data} = Mobilizon.Web.Upload.store(file)
|
2019-06-05 18:29:39 +02:00
|
|
|
|
|
|
|
assert {:ok, actor} =
|
|
|
|
Actors.update_actor(
|
|
|
|
actor,
|
2019-10-25 17:43:37 +02:00
|
|
|
Map.put(@update_attrs, :avatar, %{name: file.filename, url: data.url})
|
2019-06-05 18:29:39 +02:00
|
|
|
)
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
assert %Actor{} = actor
|
2018-06-14 17:25:55 +02:00
|
|
|
assert actor.summary == "some updated description"
|
|
|
|
assert actor.name == "some updated name"
|
|
|
|
assert actor.keys == "some updated keys"
|
2018-05-30 18:59:13 +02:00
|
|
|
refute actor.suspended
|
2019-06-05 18:29:39 +02:00
|
|
|
|
|
|
|
refute File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> avatar_path
|
|
|
|
)
|
|
|
|
|
|
|
|
assert File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> banner_path
|
|
|
|
)
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
test "update_actor/2 with invalid data returns error changeset", %{actor: actor} do
|
2018-05-30 18:59:13 +02:00
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.update_actor(actor, @invalid_attrs)
|
2018-08-01 14:45:18 +02:00
|
|
|
actor_fetched = Actors.get_actor!(actor.id)
|
2021-09-10 11:36:05 +02:00
|
|
|
assert actor.id == actor_fetched.id
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
|
2019-06-05 18:29:39 +02:00
|
|
|
test "delete_actor/1 deletes the actor", %{
|
|
|
|
actor: %Actor{avatar: %{url: avatar_url}, banner: %{url: banner_url}, id: actor_id} = actor
|
|
|
|
} do
|
2021-09-24 16:46:42 +02:00
|
|
|
%Event{} = event1 = insert(:event, organizer_actor: actor)
|
2019-12-03 11:29:51 +01:00
|
|
|
insert(:event, organizer_actor: actor)
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
%Comment{} = comment1 = insert(:comment, actor: actor)
|
2019-12-03 11:29:51 +01:00
|
|
|
insert(:comment, actor: actor)
|
|
|
|
|
2019-06-05 18:29:39 +02:00
|
|
|
%URI{path: "/media/" <> avatar_path} = URI.parse(avatar_url)
|
|
|
|
%URI{path: "/media/" <> banner_path} = URI.parse(banner_url)
|
|
|
|
|
|
|
|
assert File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> avatar_path
|
|
|
|
)
|
|
|
|
|
|
|
|
assert File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> banner_path
|
|
|
|
)
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
assert {:ok, %Oban.Job{}} = Actors.delete_actor(actor)
|
|
|
|
|
|
|
|
assert_enqueued(
|
2020-01-23 00:55:07 +01:00
|
|
|
worker: Workers.Background,
|
2020-08-27 11:53:24 +02:00
|
|
|
args: %{
|
|
|
|
"actor_id" => actor.id,
|
|
|
|
"op" => "delete_actor",
|
|
|
|
"author_id" => nil,
|
|
|
|
"suspension" => false,
|
|
|
|
"reserve_username" => true
|
|
|
|
}
|
2019-12-03 11:29:51 +01:00
|
|
|
)
|
|
|
|
|
2022-03-23 11:38:25 +01:00
|
|
|
assert %{success: 1, snoozed: 0, failure: 0, discard: 0} ==
|
|
|
|
Oban.drain_queue(queue: :background)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
assert %Actor{
|
|
|
|
name: nil,
|
|
|
|
summary: nil,
|
|
|
|
suspended: true,
|
|
|
|
avatar: nil,
|
|
|
|
banner: nil,
|
|
|
|
user_id: nil
|
|
|
|
} = Actors.get_actor(actor_id)
|
|
|
|
|
|
|
|
assert {:error, :event_not_found} = Events.get_event(event1.id)
|
2020-07-09 17:24:28 +02:00
|
|
|
assert %Comment{deleted_at: deleted_at} = Discussions.get_comment(comment1.id)
|
2019-12-03 11:29:51 +01:00
|
|
|
refute is_nil(deleted_at)
|
|
|
|
|
|
|
|
refute File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-12-03 11:29:51 +01:00
|
|
|
"/" <> avatar_path
|
|
|
|
)
|
2019-06-05 18:29:39 +02:00
|
|
|
|
|
|
|
refute File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> banner_path
|
|
|
|
)
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "groups" do
|
2018-07-27 10:45:35 +02:00
|
|
|
@valid_attrs %{
|
|
|
|
summary: "some description",
|
|
|
|
suspended: true,
|
2022-05-06 19:30:52 +02:00
|
|
|
preferred_username: "some_title",
|
2018-07-27 10:45:35 +02:00
|
|
|
name: "Some Title"
|
|
|
|
}
|
|
|
|
@update_attrs %{
|
|
|
|
summary: "some updated description",
|
|
|
|
suspended: false,
|
2022-05-06 19:30:52 +02:00
|
|
|
preferred_username: "some_updated_title",
|
2018-07-27 10:45:35 +02:00
|
|
|
name: "Some Updated Title"
|
|
|
|
}
|
2018-05-30 18:59:13 +02:00
|
|
|
@invalid_attrs %{summary: nil, suspended: nil, preferred_username: nil, name: nil}
|
|
|
|
|
|
|
|
test "create_group/1 with valid data creates a group" do
|
2020-02-18 08:57:00 +01:00
|
|
|
%Actor{id: actor_id} = insert(:actor)
|
|
|
|
|
|
|
|
assert {:ok, %Actor{} = group} =
|
|
|
|
Actors.create_group(Map.put(@valid_attrs, :creator_actor_id, actor_id))
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
assert group.summary == "some description"
|
|
|
|
refute group.suspended
|
2022-05-06 19:30:52 +02:00
|
|
|
assert group.preferred_username == "some_title"
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2019-02-25 18:35:00 +01:00
|
|
|
test "create_group/1 with an existing profile username fails" do
|
|
|
|
_actor = insert(:actor, preferred_username: @valid_attrs.preferred_username)
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:error,
|
2020-11-17 19:14:55 +01:00
|
|
|
%Ecto.Changeset{
|
|
|
|
errors: [preferred_username: {"This username is already taken.", []}]
|
2021-09-24 16:46:42 +02:00
|
|
|
}} = Actors.create_group(@valid_attrs)
|
2019-02-25 18:35:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_group/1 with an existing group username fails" do
|
2020-02-18 08:57:00 +01:00
|
|
|
%Actor{id: actor_id} = insert(:actor)
|
|
|
|
attrs = Map.put(@valid_attrs, :creator_actor_id, actor_id)
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:ok, %Actor{}} = Actors.create_group(attrs)
|
2019-02-25 18:35:00 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:error,
|
2020-11-17 19:14:55 +01:00
|
|
|
%Ecto.Changeset{
|
|
|
|
errors: [preferred_username: {"This username is already taken.", []}]
|
2021-09-24 16:46:42 +02:00
|
|
|
}} = Actors.create_group(attrs)
|
2019-02-25 18:35:00 +01:00
|
|
|
end
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
test "create_group/1 with invalid data returns error changeset" do
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.create_group(@invalid_attrs)
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
describe "bots" do
|
|
|
|
@valid_attrs %{source: "some source", type: "some type"}
|
|
|
|
@update_attrs %{source: "some updated source", type: "some updated type"}
|
|
|
|
@invalid_attrs %{source: nil, type: nil}
|
|
|
|
|
|
|
|
test "get_bot!/1 returns the bot with given id" do
|
2018-11-12 18:17:53 +01:00
|
|
|
%Bot{id: bot_id} = bot = insert(:bot)
|
|
|
|
assert bot_id == Actors.get_bot!(bot.id).id
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_bot/1 with valid data creates a bot" do
|
2018-07-27 10:45:35 +02:00
|
|
|
attrs =
|
|
|
|
@valid_attrs
|
|
|
|
|> Map.merge(%{actor_id: insert(:actor).id})
|
|
|
|
|> Map.merge(%{user_id: insert(:user).id})
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
assert {:ok, %Bot{} = bot} = Actors.create_bot(attrs)
|
2018-05-30 14:27:21 +02:00
|
|
|
assert bot.source == "some source"
|
|
|
|
assert bot.type == "some type"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_bot/1 with invalid data returns error changeset" do
|
2019-07-23 18:06:22 +02:00
|
|
|
case Actors.create_bot(@invalid_attrs) do
|
|
|
|
{:error, %Ecto.Changeset{}} ->
|
|
|
|
assert true
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
_ ->
|
|
|
|
assert false
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "update_bot/2 with valid data updates the bot" do
|
2018-11-12 18:17:53 +01:00
|
|
|
with bot <- insert(:bot),
|
|
|
|
{:ok, %Bot{source: source, type: type}} <- Actors.update_bot(bot, @update_attrs) do
|
|
|
|
assert source == "some updated source"
|
|
|
|
assert type == "some updated type"
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "update_bot/2 with invalid data returns error changeset" do
|
2018-11-12 18:17:53 +01:00
|
|
|
bot = insert(:bot)
|
2018-05-30 14:27:21 +02:00
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.update_bot(bot, @invalid_attrs)
|
2021-09-24 16:46:42 +02:00
|
|
|
assert bot.id == Actors.get_bot!(bot.id).id
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_bot/1 deletes the bot" do
|
2018-11-12 18:17:53 +01:00
|
|
|
bot = insert(:bot)
|
2018-05-30 14:27:21 +02:00
|
|
|
assert {:ok, %Bot{}} = Actors.delete_bot(bot)
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> Actors.get_bot!(bot.id) end
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
|
|
|
|
describe "followers" do
|
2019-07-30 16:40:59 +02:00
|
|
|
@valid_attrs %{approved: true}
|
|
|
|
@update_attrs %{approved: false}
|
|
|
|
@invalid_attrs %{approved: nil}
|
2018-08-01 14:45:18 +02:00
|
|
|
|
|
|
|
setup do
|
|
|
|
actor = insert(:actor)
|
|
|
|
target_actor = insert(:actor)
|
2018-08-03 10:16:22 +02:00
|
|
|
{:ok, actor: actor, target_actor: target_actor}
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
defp create_test_follower(%{actor: actor, target_actor: target_actor}) do
|
2018-08-03 10:16:22 +02:00
|
|
|
insert(:follower, actor: actor, target_actor: target_actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get_follower!/1 returns the follower with given id", context do
|
2018-11-12 18:17:53 +01:00
|
|
|
follower = create_test_follower(context)
|
2021-09-24 16:46:42 +02:00
|
|
|
assert follower.id == Actors.get_follower!(follower.id).id
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_follower/1 with valid data creates a follower", %{
|
|
|
|
actor: actor,
|
|
|
|
target_actor: target_actor
|
|
|
|
} do
|
|
|
|
valid_attrs =
|
|
|
|
@valid_attrs
|
|
|
|
|> Map.put(:actor_id, actor.id)
|
|
|
|
|> Map.put(:target_actor_id, target_actor.id)
|
|
|
|
|
|
|
|
assert {:ok, %Follower{} = follower} = Actors.create_follower(valid_attrs)
|
|
|
|
assert follower.approved == true
|
2018-08-03 10:16:22 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
assert %{total: 1, elements: followings} = Actors.build_followings_for_actor(actor)
|
|
|
|
followings_ids = Enum.map(followings, & &1.id)
|
|
|
|
assert followings_ids == [target_actor.id]
|
|
|
|
assert %{total: 1, elements: followers} = Actors.build_followers_for_actor(target_actor)
|
|
|
|
followers_ids = Enum.map(followers, & &1.id)
|
|
|
|
assert followers_ids == [actor.id]
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_follower/1 with valid data but same actors fails to create a follower", %{
|
|
|
|
actor: actor,
|
|
|
|
target_actor: target_actor
|
|
|
|
} do
|
2018-11-12 18:17:53 +01:00
|
|
|
create_test_follower(%{actor: actor, target_actor: target_actor})
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
valid_attrs =
|
|
|
|
@valid_attrs
|
|
|
|
|> Map.put(:actor_id, actor.id)
|
|
|
|
|> Map.put(:target_actor_id, target_actor.id)
|
|
|
|
|
|
|
|
assert {:error, _follower} = Actors.create_follower(valid_attrs)
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_follower/1 with invalid data returns error changeset", %{
|
|
|
|
actor: actor,
|
|
|
|
target_actor: target_actor
|
|
|
|
} do
|
|
|
|
invalid_attrs =
|
|
|
|
@invalid_attrs
|
|
|
|
|> Map.put(:actor_id, actor.id)
|
|
|
|
|> Map.put(:target_actor_id, target_actor.id)
|
|
|
|
|
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.create_follower(invalid_attrs)
|
|
|
|
end
|
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
test "update_follower/2 with valid data updates the follower", context do
|
2018-11-12 18:17:53 +01:00
|
|
|
follower = create_test_follower(context)
|
2018-08-01 14:45:18 +02:00
|
|
|
assert {:ok, follower} = Actors.update_follower(follower, @update_attrs)
|
|
|
|
assert %Follower{} = follower
|
|
|
|
assert follower.approved == false
|
|
|
|
end
|
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
test "update_follower/2 with invalid data returns error changeset", context do
|
2018-11-12 18:17:53 +01:00
|
|
|
follower = create_test_follower(context)
|
2018-08-01 14:45:18 +02:00
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.update_follower(follower, @invalid_attrs)
|
2021-09-24 16:46:42 +02:00
|
|
|
assert follower.id == Actors.get_follower!(follower.id).id
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
test "delete_follower/1 deletes the follower", context do
|
2018-11-12 18:17:53 +01:00
|
|
|
follower = create_test_follower(context)
|
2018-08-01 14:45:18 +02:00
|
|
|
assert {:ok, %Follower{}} = Actors.delete_follower(follower)
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> Actors.get_follower!(follower.id) end
|
|
|
|
end
|
|
|
|
|
2018-11-27 14:02:51 +01:00
|
|
|
test "follow/3 makes an actor follow another", %{actor: actor, target_actor: target_actor} do
|
|
|
|
# Preloading followers/followings
|
2019-09-09 00:52:49 +02:00
|
|
|
actor = Actors.get_actor_with_preload(actor.id)
|
|
|
|
target_actor = Actors.get_actor_with_preload(target_actor.id)
|
2018-11-27 14:02:51 +01:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
{:ok, follower} = Actors.follow(target_actor, actor)
|
2018-11-27 14:02:51 +01:00
|
|
|
assert follower.actor.id == actor.id
|
|
|
|
|
|
|
|
# Referesh followers/followings
|
2019-09-09 00:52:49 +02:00
|
|
|
actor = Actors.get_actor_with_preload(actor.id)
|
|
|
|
target_actor = Actors.get_actor_with_preload(target_actor.id)
|
2018-11-27 14:02:51 +01:00
|
|
|
|
2018-11-27 17:54:54 +01:00
|
|
|
assert target_actor.followers |> Enum.map(& &1.actor_id) == [actor.id]
|
|
|
|
assert actor.followings |> Enum.map(& &1.target_actor_id) == [target_actor.id]
|
2018-11-27 14:02:51 +01:00
|
|
|
|
|
|
|
# Test if actor is already following target actor
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:error, :already_following} = Actors.follow(target_actor, actor)
|
2018-11-27 14:02:51 +01:00
|
|
|
|
|
|
|
# Test if target actor is suspended
|
|
|
|
target_actor = %{target_actor | suspended: true}
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:error, :followed_suspended} = Actors.follow(target_actor, actor)
|
2018-11-27 14:02:51 +01:00
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
describe "members" do
|
2019-03-01 17:11:28 +01:00
|
|
|
@valid_attrs %{role: :member}
|
|
|
|
@update_attrs %{role: :not_approved}
|
|
|
|
@invalid_attrs %{role: nil}
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
setup do
|
|
|
|
actor = insert(:actor)
|
|
|
|
group = insert(:group)
|
|
|
|
{:ok, actor: actor, group: group}
|
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
defp create_test_member(%{actor: actor, group: group}) do
|
2018-08-24 11:34:00 +02:00
|
|
|
insert(:member, actor: actor, parent: group)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get_member!/1 returns the member with given id", context do
|
2018-11-12 18:17:53 +01:00
|
|
|
member = create_test_member(context)
|
2021-09-24 16:46:42 +02:00
|
|
|
assert member.id == Actors.get_member!(member.id).id
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create_member/1 with valid data creates a member", %{
|
|
|
|
actor: actor,
|
|
|
|
group: group
|
|
|
|
} do
|
|
|
|
valid_attrs =
|
|
|
|
@valid_attrs
|
|
|
|
|> Map.put(:actor_id, actor.id)
|
|
|
|
|> Map.put(:parent_id, group.id)
|
|
|
|
|
|
|
|
assert {:ok, %Member{} = member} = Actors.create_member(valid_attrs)
|
2019-03-01 17:11:28 +01:00
|
|
|
assert member.role == :member
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
assert [group] = Actors.list_groups_member_of(actor)
|
2021-09-24 16:46:42 +02:00
|
|
|
assert %Page{elements: members, total: 1} = Actors.list_members_for_group(group)
|
|
|
|
members_ids = Enum.map(members, & &1.id)
|
|
|
|
assert members_ids == [member.id]
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
test "create_member/1 with valid data but same actors just updates the member", %{
|
2018-08-24 11:34:00 +02:00
|
|
|
actor: actor,
|
|
|
|
group: group
|
|
|
|
} do
|
2020-08-14 11:32:23 +02:00
|
|
|
%Member{id: member_id, url: member_url} = create_test_member(%{actor: actor, group: group})
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
attrs =
|
|
|
|
%{}
|
2018-08-24 11:34:00 +02:00
|
|
|
|> Map.put(:actor_id, actor.id)
|
|
|
|
|> Map.put(:parent_id, group.id)
|
2020-08-14 11:32:23 +02:00
|
|
|
|> Map.put(:role, :member)
|
|
|
|
|
|
|
|
assert {:ok,
|
|
|
|
%Member{
|
|
|
|
id: updated_member_id,
|
|
|
|
role: updated_member_role,
|
|
|
|
actor_id: actor_id,
|
|
|
|
parent_id: parent_id,
|
|
|
|
url: url
|
|
|
|
}} = Actors.create_member(attrs)
|
|
|
|
|
|
|
|
assert updated_member_role == :member
|
|
|
|
assert actor_id == actor.id
|
|
|
|
assert parent_id == group.id
|
|
|
|
|
|
|
|
assert url == member_url
|
|
|
|
assert updated_member_id == member_id
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
test "create_member/1 with invalid data returns error changeset" do
|
2018-08-24 11:34:00 +02:00
|
|
|
invalid_attrs =
|
|
|
|
@invalid_attrs
|
|
|
|
|> Map.put(:actor_id, nil)
|
|
|
|
|> Map.put(:parent_id, nil)
|
|
|
|
|
|
|
|
assert {:error, %Ecto.Changeset{}} = Actors.create_member(invalid_attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "update_member/2 with valid data updates the member", context do
|
2018-11-12 18:17:53 +01:00
|
|
|
member = create_test_member(context)
|
2018-08-24 11:34:00 +02:00
|
|
|
assert {:ok, member} = Actors.update_member(member, @update_attrs)
|
|
|
|
assert %Member{} = member
|
2019-03-01 17:11:28 +01:00
|
|
|
assert member.role == :not_approved
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# This can't happen, since attrs are optional
|
|
|
|
# test "update_member/2 with invalid data returns error changeset", context do
|
|
|
|
# member = create_member(context)
|
|
|
|
# assert {:error, %Ecto.Changeset{}} = Actors.update_member(member, @invalid_attrs)
|
|
|
|
# assert member = Actors.get_member!(member.id)
|
|
|
|
# end
|
|
|
|
|
|
|
|
test "delete_member/1 deletes the member", context do
|
2018-11-12 18:17:53 +01:00
|
|
|
member = create_test_member(context)
|
2018-08-24 11:34:00 +02:00
|
|
|
assert {:ok, %Member{}} = Actors.delete_member(member)
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> Actors.get_member!(member.id) end
|
|
|
|
end
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|