Moar coverage

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-11-27 17:54:54 +01:00
parent b99625afab
commit da378633ac
13 changed files with 287 additions and 84 deletions

View File

@ -222,13 +222,17 @@ defmodule Mobilizon.Actors.Actor do
If actor A and C both follow actor B, actor B's followers are A and C If actor A and C both follow actor B, actor B's followers are A and C
""" """
def get_followers(%Actor{id: actor_id} = _actor) do def get_followers(%Actor{id: actor_id} = _actor, page \\ 1, limit \\ 10) do
start = (page - 1) * limit
Repo.all( Repo.all(
from( from(
a in Actor, a in Actor,
join: f in Follower, join: f in Follower,
on: a.id == f.actor_id, on: a.id == f.actor_id,
where: f.target_actor_id == ^actor_id where: f.target_actor_id == ^actor_id,
limit: ^limit,
offset: ^start
) )
) )
end end
@ -238,13 +242,17 @@ defmodule Mobilizon.Actors.Actor do
If actor A follows actor B and C, actor A's followings are B and B If actor A follows actor B and C, actor A's followings are B and B
""" """
def get_followings(%Actor{id: actor_id} = _actor) do def get_followings(%Actor{id: actor_id} = _actor, page \\ 1, limit \\ 10) do
start = (page - 1) * limit
Repo.all( Repo.all(
from( from(
a in Actor, a in Actor,
join: f in Follower, join: f in Follower,
on: a.id == f.target_actor_id, on: a.id == f.target_actor_id,
where: f.actor_id == ^actor_id where: f.actor_id == ^actor_id,
limit: ^limit,
offset: ^start
) )
) )
end end
@ -271,10 +279,8 @@ defmodule Mobilizon.Actors.Actor do
) )
end end
@spec follow(struct(), struct(), boolean()) :: Follower.t() | {:error, String.t()} @spec follow(struct(), struct(), boolean()) :: Follower.t() | {:error, String.t()}
def follow(%Actor{} = follower, %Actor{} = followed, approved \\ true) do def follow(%Actor{} = followed, %Actor{} = follower, 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
@ -298,9 +304,12 @@ defmodule Mobilizon.Actors.Actor do
end end
@spec following?(struct(), struct()) :: boolean() @spec following?(struct(), struct()) :: boolean()
def following?(%Actor{id: follower_actor_id} = _follower_actor, %Actor{followers: followers} = _followed) do def following?(
%Actor{id: follower_actor_id} = _follower_actor,
%Actor{followers: followers} = _followed
) do
followers followers
|> Enum.map(&(&1.actor_id)) |> Enum.map(& &1.actor_id)
|> Enum.member?(follower_actor_id) |> Enum.member?(follower_actor_id)
end end
end end

View File

@ -367,7 +367,7 @@ defmodule Mobilizon.Actors do
def get_local_actor_by_name_with_everything(name) do def get_local_actor_by_name_with_everything(name) do
actor = Repo.one(from(a in Actor, where: a.preferred_username == ^name and is_nil(a.domain))) actor = Repo.one(from(a in Actor, where: a.preferred_username == ^name and is_nil(a.domain)))
Repo.preload(actor, :organized_events) Repo.preload(actor, [:organized_events, :followers, :followings])
end end
@spec get_actor_by_name_with_everything(String.t(), atom() | nil) :: Actor.t() @spec get_actor_by_name_with_everything(String.t(), atom() | nil) :: Actor.t()

View File

@ -23,7 +23,10 @@ defmodule Mobilizon.Actors.Service.ResetPassword do
) do ) do
{:ok, user} {:ok, user}
else else
_err -> {:error, %Ecto.Changeset{errors: [password: {"registration.error.password_too_short", _}]}} ->
{:error, :password_too_short}
err ->
{:error, :invalid_token} {:error, :invalid_token}
end end
end end

View File

@ -889,7 +889,7 @@ defmodule Mobilizon.Events do
def get_comment_full_from_uuid(uuid) do def get_comment_full_from_uuid(uuid) do
with %Comment{} = comment <- Repo.get_by!(Comment, uuid: uuid) do with %Comment{} = comment <- Repo.get_by!(Comment, uuid: uuid) do
Repo.preload(comment, [:actor, :attributed_to]) Repo.preload(comment, [:actor, :attributed_to, :in_reply_to_comment])
end end
end end

View File

@ -69,9 +69,8 @@ defmodule MobilizonWeb.ActivityPubController do
end end
def following(conn, %{"name" => name, "page" => page}) do def following(conn, %{"name" => name, "page" => page}) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do with {page, ""} = Integer.parse(page),
{page, _} = Integer.parse(page) %Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
conn conn
|> put_resp_header("content-type", "application/activity+json") |> put_resp_header("content-type", "application/activity+json")
|> json(ActorView.render("following.json", %{actor: actor, page: page})) |> json(ActorView.render("following.json", %{actor: actor, page: page}))
@ -79,7 +78,7 @@ defmodule MobilizonWeb.ActivityPubController do
end end
def following(conn, %{"name" => name}) do def following(conn, %{"name" => name}) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do with %Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
conn conn
|> put_resp_header("content-type", "application/activity+json") |> put_resp_header("content-type", "application/activity+json")
|> json(ActorView.render("following.json", %{actor: actor})) |> json(ActorView.render("following.json", %{actor: actor}))
@ -87,9 +86,8 @@ defmodule MobilizonWeb.ActivityPubController do
end end
def followers(conn, %{"name" => name, "page" => page}) do def followers(conn, %{"name" => name, "page" => page}) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do with {page, ""} = Integer.parse(page),
{page, _} = Integer.parse(page) %Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
conn conn
|> put_resp_header("content-type", "application/activity+json") |> put_resp_header("content-type", "application/activity+json")
|> json(ActorView.render("followers.json", %{actor: actor, page: page})) |> json(ActorView.render("followers.json", %{actor: actor, page: page}))
@ -97,7 +95,7 @@ defmodule MobilizonWeb.ActivityPubController do
end end
def followers(conn, %{"name" => name}) do def followers(conn, %{"name" => name}) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do with %Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
conn conn
|> put_resp_header("content-type", "application/activity+json") |> put_resp_header("content-type", "application/activity+json")
|> json(ActorView.render("followers.json", %{actor: actor})) |> json(ActorView.render("followers.json", %{actor: actor}))
@ -157,6 +155,6 @@ defmodule MobilizonWeb.ActivityPubController do
def errors(conn, _e) do def errors(conn, _e) do
conn conn
|> put_status(500) |> put_status(500)
|> json("error") |> json("Unknown Error")
end end
end end

View File

@ -87,12 +87,13 @@ defmodule MobilizonWeb.Resolvers.User do
""" """
def send_reset_password(_parent, %{email: email, locale: locale}, _resolution) do def send_reset_password(_parent, %{email: email, locale: locale}, _resolution) do
with {:ok, user} <- Actors.get_user_by_email(email, false), with {:ok, user} <- Actors.get_user_by_email(email, false),
{:ok, email} <- {:ok, %Bamboo.Email{} = _email_html} <-
Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user, locale) do Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user, locale) do
{:ok, email} {:ok, email}
else else
{:error, :user_not_found} -> {:error, :user_not_found} ->
{:error, "No user to validate with this email was found"} # TODO : implement rate limits for this endpoint
{:error, "No user with this email was found"}
{:error, :email_too_soon} -> {:error, :email_too_soon} ->
{:error, "You requested again a confirmation email too soon"} {:error, "You requested again a confirmation email too soon"}

View File

@ -47,7 +47,7 @@ defmodule MobilizonWeb.ActivityPub.ActorView do
def render("following.json", %{actor: actor, page: page}) do def render("following.json", %{actor: actor, page: page}) do
actor actor
|> Actor.get_followings() |> Actor.get_followings(page)
|> collection(actor.following_url, page) |> collection(actor.following_url, page)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Utils.make_json_ld_header())
end end
@ -66,7 +66,7 @@ defmodule MobilizonWeb.ActivityPub.ActorView do
def render("followers.json", %{actor: actor, page: page}) do def render("followers.json", %{actor: actor, page: page}) do
actor actor
|> Actor.get_followers() |> Actor.get_followers(page)
|> collection(actor.followers_url, page) |> collection(actor.followers_url, page)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Utils.make_json_ld_header())
end end
@ -77,7 +77,8 @@ defmodule MobilizonWeb.ActivityPub.ActorView do
%{ %{
"id" => actor.followers_url, "id" => actor.followers_url,
"type" => "OrderedCollection", "type" => "OrderedCollection",
"totalItems" => length(followers), # TODO put me back
# "totalItems" => length(followers),
"first" => collection(followers, actor.followers_url, 1) "first" => collection(followers, actor.followers_url, 1)
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Utils.make_json_ld_header())
@ -148,22 +149,22 @@ defmodule MobilizonWeb.ActivityPub.ActorView do
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Utils.make_json_ld_header())
end end
def collection(collection, iri, page, total \\ nil) do def collection(collection, iri, page, _total \\ nil) do
offset = (page - 1) * 10 items = Enum.map(collection, fn account -> account.url end)
items = Enum.slice(collection, offset, 10)
items = Enum.map(items, fn account -> account.url end)
total = total || length(collection)
map = %{ # TODO : Add me back
# total = total || length(collection)
%{
"id" => "#{iri}?page=#{page}", "id" => "#{iri}?page=#{page}",
"type" => "OrderedCollectionPage", "type" => "OrderedCollectionPage",
"partOf" => iri, "partOf" => iri,
"totalItems" => total, # "totalItems" => total,
"orderedItems" => items "orderedItems" => items
} }
if offset < total do # if offset < total do
Map.put(map, "next", "#{iri}?page=#{page + 1}") # Map.put(map, "next", "#{iri}?page=#{page + 1}")
end # end
end end
end end

View File

@ -154,7 +154,7 @@ defmodule Mobilizon.Service.ActivityPub do
end end
def follow(%Actor{} = follower, %Actor{} = followed, _activity_id \\ nil, local \\ true) do def follow(%Actor{} = follower, %Actor{} = followed, _activity_id \\ nil, local \\ true) do
with {:ok, follow} <- Actor.follow(follower, followed, true), with {:ok, follow} <- Actor.follow(followed, follower, true),
data <- make_follow_data(follower, followed, follow.id), data <- make_follow_data(follower, followed, follow.id),
{:ok, activity} <- insert(data, local), {:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
@ -251,8 +251,7 @@ defmodule Mobilizon.Service.ActivityPub do
followers = followers =
if actor.followers_url in activity.recipients do if actor.followers_url in activity.recipients do
{:ok, followers} = Actor.get_followers(actor) Actor.get_followers(actor) |> Enum.filter(fn follower -> is_nil(follower.domain) end)
followers |> Enum.filter(fn follower -> is_nil(follower.domain) end)
else else
[] []
end end

View File

@ -585,23 +585,23 @@ defmodule Mobilizon.ActorsTest do
actor = Actors.get_actor_with_everything!(actor.id) actor = Actors.get_actor_with_everything!(actor.id)
target_actor = Actors.get_actor_with_everything!(target_actor.id) target_actor = Actors.get_actor_with_everything!(target_actor.id)
{:ok, follower} = Actor.follow(actor, target_actor) {:ok, follower} = Actor.follow(target_actor, actor)
assert follower.actor.id == actor.id assert follower.actor.id == actor.id
# Referesh followers/followings # Referesh followers/followings
actor = Actors.get_actor_with_everything!(actor.id) actor = Actors.get_actor_with_everything!(actor.id)
target_actor = Actors.get_actor_with_everything!(target_actor.id) target_actor = Actors.get_actor_with_everything!(target_actor.id)
assert target_actor.followers |> Enum.map(&(&1.actor_id)) == [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] assert actor.followings |> Enum.map(& &1.target_actor_id) == [target_actor.id]
# Test if actor is already following target actor # Test if actor is already following target actor
{:error, msg} = Actor.follow(actor, target_actor) {:error, msg} = Actor.follow(target_actor, actor)
assert msg =~ "already following" assert msg =~ "already following"
# Test if target actor is suspended # Test if target actor is suspended
target_actor = %{target_actor | suspended: true} target_actor = %{target_actor | suspended: true}
{:error, msg} = Actor.follow(actor, target_actor) {:error, msg} = Actor.follow(target_actor, actor)
assert msg =~ "suspended" assert msg =~ "suspended"
end end
end end

View File

@ -3,6 +3,7 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
import Mobilizon.Factory import Mobilizon.Factory
alias MobilizonWeb.ActivityPub.{ActorView, ObjectView} alias MobilizonWeb.ActivityPub.{ActorView, ObjectView}
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils alias Mobilizon.Service.ActivityPub.Utils
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
@ -47,6 +48,32 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
end end
end end
describe "/comments/:uuid" do
test "it returns a json representation of the comment", %{conn: conn} do
comment = insert(:comment)
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get("/comments/#{comment.uuid}")
assert json_response(conn, 200) ==
ObjectView.render("comment.json", %{comment: comment |> Utils.make_comment_data()})
end
# TODO !
# test "it returns 404 for non-public comments", %{conn: conn} do
# event = insert(:event, public: false)
# conn =
# conn
# |> put_req_header("accept", "application/activity+json")
# |> get("/events/#{event.uuid}")
# assert json_response(conn, 404)
# end
end
describe "/@:preferred_username/inbox" do describe "/@:preferred_username/inbox" do
test "it inserts an incoming event into the database", %{conn: conn} do test "it inserts an incoming event into the database", %{conn: conn} do
use_cassette "activity_pub_controller/mastodon-post-activity_actor_call" do use_cassette "activity_pub_controller/mastodon-post-activity_actor_call" do
@ -91,46 +118,88 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
end end
end end
# describe "/actors/:nickname/followers" do describe "/@actor/followers" do
# test "it returns the followers in a collection", %{conn: conn} do test "it returns the followers in a collection", %{conn: conn} do
# user = insert(:user) actor = insert(:actor)
# user_two = insert(:user) actor2 = insert(:actor)
# User.follow(user, user_two) Actor.follow(actor, actor2)
#
# result = result =
# conn conn
# |> get("/users/#{user_two.nickname}/followers") |> get("/@#{actor.preferred_username}/followers")
# |> json_response(200) |> json_response(200)
#
# assert result["first"]["orderedItems"] == [user.ap_id] assert result["first"]["orderedItems"] == [actor2.url]
# end end
#
# test "it works for more than 10 users", %{conn: conn} do test "it works for more than 10 actors", %{conn: conn} do
# user = insert(:user) actor = insert(:actor)
#
# Enum.each(1..15, fn _ -> Enum.each(1..15, fn _ ->
# other_user = insert(:user) other_actor = insert(:actor)
# User.follow(other_user, user) Actor.follow(actor, other_actor)
# end) end)
#
# result = result =
# conn conn
# |> get("/users/#{user.nickname}/followers") |> get("/@#{actor.preferred_username}/followers")
# |> json_response(200) |> json_response(200)
#
# assert length(result["first"]["orderedItems"]) == 10 assert length(result["first"]["orderedItems"]) == 10
# assert result["first"]["totalItems"] == 15 # assert result["first"]["totalItems"] == 15
# assert result["totalItems"] == 15 # assert result["totalItems"] == 15
#
# result = result =
# conn conn
# |> get("/users/#{user.nickname}/followers?page=2") |> get("/@#{actor.preferred_username}/followers?page=2")
# |> json_response(200) |> json_response(200)
#
# assert length(result["orderedItems"]) == 5 assert length(result["orderedItems"]) == 5
# assert result["totalItems"] == 15 # assert result["totalItems"] == 15
# end end
# end end
describe "/@actor/following" do
test "it returns the followings in a collection", %{conn: conn} do
actor = insert(:actor)
actor2 = insert(:actor)
Actor.follow(actor, actor2)
result =
conn
|> get("/@#{actor2.preferred_username}/following")
|> json_response(200)
assert result["first"]["orderedItems"] == [actor.url]
end
test "it works for more than 10 actors", %{conn: conn} do
actor = insert(:actor)
Enum.each(1..15, fn _ ->
other_actor = insert(:actor)
Actor.follow(other_actor, actor)
end)
result =
conn
|> get("/@#{actor.preferred_username}/following")
|> json_response(200)
assert length(result["first"]["orderedItems"]) == 10
# assert result["first"]["totalItems"] == 15
# assert result["totalItems"] == 15
result =
conn
|> get("/@#{actor.preferred_username}/following?page=2")
|> json_response(200)
assert length(result["orderedItems"]) == 5
# assert result["totalItems"] == 15
end
end
# #
# describe "/@:preferred_username/following" do # describe "/@:preferred_username/following" do
# test "it returns the following in a collection", %{conn: conn} do # test "it returns the following in a collection", %{conn: conn} do

View File

@ -1,8 +1,15 @@
defmodule MobilizonWeb.PageControllerTest do defmodule MobilizonWeb.PageControllerTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
import Mobilizon.Factory
test "GET /", %{conn: conn} do test "GET /", %{conn: conn} do
conn = get(conn, "/") conn = get(conn, "/")
assert html_response(conn, 200) assert html_response(conn, 200)
end end
test "GET /@actor", %{conn: conn} do
actor = insert(:actor)
conn = get(conn, "/@#{actor.preferred_username}")
assert html_response(conn, 200)
end
end end

View File

@ -1,6 +1,7 @@
defmodule MobilizonWeb.Resolvers.UserResolverTest do defmodule MobilizonWeb.Resolvers.UserResolverTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.{User, Actor}
alias MobilizonWeb.AbsintheHelpers alias MobilizonWeb.AbsintheHelpers
import Mobilizon.Factory import Mobilizon.Factory
use Bamboo.Test use Bamboo.Test
@ -234,4 +235,114 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
assert hd(json_response(res, 200)["errors"])["message"] == assert hd(json_response(res, 200)["errors"])["message"] ==
"No user to validate with this email was found" "No user to validate with this email was found"
end end
test "test send_reset_password/3 with valid email", context do
user = insert(:user)
mutation = """
mutation {
sendResetPassword(
email: "#{user.email}"
)
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["sendResetPassword"] == user.email
end
test "test send_reset_password/3 with invalid email", context do
mutation = """
mutation {
sendResetPassword(
email: "oh no"
)
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "No user with this email was found"
end
test "test reset_password/3 with valid email", context do
%User{} = user = insert(:user)
%Actor{} = insert(:actor, user: user)
{:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user)
%User{reset_password_token: reset_password_token} = Mobilizon.Actors.get_user!(user.id)
mutation = """
mutation {
resetPassword(
token: "#{reset_password_token}",
password: "new password"
) {
user {
id
}
}
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["resetPassword"]["user"]["id"] == to_string(user.id)
end
test "test reset_password/3 with a password too short", context do
%User{} = user = insert(:user)
{:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user)
%User{reset_password_token: reset_password_token} = Mobilizon.Actors.get_user!(user.id)
mutation = """
mutation {
resetPassword(
token: "#{reset_password_token}",
password: "new"
) {
user {
id
}
}
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "password_too_short"
end
test "test reset_password/3 with an invalid token", context do
%User{} = user = insert(:user)
{:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user)
%User{} = Mobilizon.Actors.get_user!(user.id)
mutation = """
mutation {
resetPassword(
token: "not good",
password: "new"
) {
user {
id
}
}
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "invalid_token"
end
end end

View File

@ -23,9 +23,13 @@ defmodule Mobilizon.Factory do
%Mobilizon.Actors.Actor{ %Mobilizon.Actors.Actor{
preferred_username: preferred_username, preferred_username: preferred_username,
domain: nil, domain: nil,
followers: [],
followings: [],
keys: pem, keys: pem,
type: :Person, type: :Person,
url: MobilizonWeb.Endpoint.url() <> "/@#{preferred_username}", url: MobilizonWeb.Endpoint.url() <> "/@#{preferred_username}",
followers_url: MobilizonWeb.Endpoint.url() <> "/@#{preferred_username}/followers",
following_url: MobilizonWeb.Endpoint.url() <> "/@#{preferred_username}/following",
user: nil user: nil
} }
end end
@ -81,6 +85,7 @@ defmodule Mobilizon.Factory do
actor: build(:actor), actor: build(:actor),
event: build(:event), event: build(:event),
uuid: uuid, uuid: uuid,
in_reply_to_comment: nil,
url: "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}" url: "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}"
} }
end end