Backend support to get used media size for users and actors
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
6a1cd42d2c
commit
b11d35cbec
|
@ -98,4 +98,52 @@ defmodule Mobilizon.GraphQL.Resolvers.Picture do
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_picture(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
def remove_picture(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Return the total media size for an actor
|
||||||
|
"""
|
||||||
|
@spec actor_size(map(), map(), map()) ::
|
||||||
|
{:ok, integer()} | {:error, :unauthorized} | {:error, :unauthenticated}
|
||||||
|
def actor_size(%Actor{id: actor_id}, _args, %{
|
||||||
|
context: %{current_user: %User{} = user}
|
||||||
|
}) do
|
||||||
|
if can_get_actor_size?(user, actor_id) do
|
||||||
|
{:ok, Media.media_size_for_actor(actor_id)}
|
||||||
|
else
|
||||||
|
{:error, :unauthorized}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def actor_size(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Return the total media size for a local user
|
||||||
|
"""
|
||||||
|
@spec user_size(map(), map(), map()) ::
|
||||||
|
{:ok, integer()} | {:error, :unauthorized} | {:error, :unauthenticated}
|
||||||
|
def user_size(%User{id: user_id}, _args, %{
|
||||||
|
context: %{current_user: %User{} = logged_user}
|
||||||
|
}) do
|
||||||
|
if can_get_user_size?(logged_user, user_id) do
|
||||||
|
{:ok, Media.media_size_for_user(user_id)}
|
||||||
|
else
|
||||||
|
{:error, :unauthorized}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def user_size(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
||||||
|
|
||||||
|
@spec can_get_user_size?(User.t(), integer()) :: boolean()
|
||||||
|
defp can_get_actor_size?(%User{role: role} = user, actor_id) do
|
||||||
|
role in [:moderator, :administrator] || owns_actor?(User.owns_actor(user, actor_id))
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec owns_actor?({:is_owned, Actor.t() | nil}) :: boolean()
|
||||||
|
defp owns_actor?({:is_owned, %Actor{} = _actor}), do: true
|
||||||
|
defp owns_actor?({:is_owned, _}), do: false
|
||||||
|
|
||||||
|
@spec can_get_user_size?(User.t(), integer()) :: boolean()
|
||||||
|
defp can_get_user_size?(%User{role: role, id: logged_user_id}, user_id) do
|
||||||
|
user_id == logged_user_id || role in [:moderator, :administrator]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,6 +37,8 @@ defmodule Mobilizon.GraphQL.Schema.ActorInterface do
|
||||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||||
|
|
||||||
|
field(:media_size, :integer, description: "The total size of the media from this actor")
|
||||||
|
|
||||||
resolve_type(fn
|
resolve_type(fn
|
||||||
%Actor{type: :Person}, _ ->
|
%Actor{type: :Person}, _ ->
|
||||||
:person
|
:person
|
||||||
|
|
|
@ -3,6 +3,7 @@ defmodule Mobilizon.GraphQL.Schema.Actors.ApplicationType do
|
||||||
Schema representation for Group.
|
Schema representation for Group.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
alias Mobilizon.GraphQL.Resolvers.Picture
|
||||||
use Absinthe.Schema.Notation
|
use Absinthe.Schema.Notation
|
||||||
|
|
||||||
@desc """
|
@desc """
|
||||||
|
@ -34,5 +35,10 @@ defmodule Mobilizon.GraphQL.Schema.Actors.ApplicationType do
|
||||||
field(:followers, list_of(:follower), description: "List of followers")
|
field(:followers, list_of(:follower), description: "List of followers")
|
||||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||||
|
|
||||||
|
field(:media_size, :integer,
|
||||||
|
resolve: &Picture.actor_size/3,
|
||||||
|
description: "The total size of the media from this actor"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
|
||||||
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
|
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
|
||||||
|
|
||||||
alias Mobilizon.Addresses
|
alias Mobilizon.Addresses
|
||||||
alias Mobilizon.GraphQL.Resolvers.{Discussion, Group, Member, Post, Resource, Todos}
|
alias Mobilizon.GraphQL.Resolvers.{Discussion, Group, Member, Picture, Post, Resource, Todos}
|
||||||
alias Mobilizon.GraphQL.Schema
|
alias Mobilizon.GraphQL.Schema
|
||||||
|
|
||||||
import_types(Schema.Actors.MemberType)
|
import_types(Schema.Actors.MemberType)
|
||||||
|
@ -52,6 +52,11 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
|
||||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||||
|
|
||||||
|
field(:media_size, :integer,
|
||||||
|
resolve: &Picture.actor_size/3,
|
||||||
|
description: "The total size of the media from this actor"
|
||||||
|
)
|
||||||
|
|
||||||
# This one should have a privacy setting
|
# This one should have a privacy setting
|
||||||
field :organized_events, :paginated_event_list do
|
field :organized_events, :paginated_event_list do
|
||||||
arg(:after_datetime, :datetime,
|
arg(:after_datetime, :datetime,
|
||||||
|
|
|
@ -7,7 +7,7 @@ defmodule Mobilizon.GraphQL.Schema.Actors.PersonType do
|
||||||
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
|
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
|
||||||
|
|
||||||
alias Mobilizon.Events
|
alias Mobilizon.Events
|
||||||
alias Mobilizon.GraphQL.Resolvers.Person
|
alias Mobilizon.GraphQL.Resolvers.{Person, Picture}
|
||||||
alias Mobilizon.GraphQL.Schema
|
alias Mobilizon.GraphQL.Schema
|
||||||
|
|
||||||
import_types(Schema.Events.FeedTokenType)
|
import_types(Schema.Events.FeedTokenType)
|
||||||
|
@ -49,6 +49,11 @@ defmodule Mobilizon.GraphQL.Schema.Actors.PersonType do
|
||||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||||
|
|
||||||
|
field(:media_size, :integer,
|
||||||
|
resolve: &Picture.actor_size/3,
|
||||||
|
description: "The total size of the media from this actor"
|
||||||
|
)
|
||||||
|
|
||||||
field(:feed_tokens, list_of(:feed_token),
|
field(:feed_tokens, list_of(:feed_token),
|
||||||
resolve: dataloader(Events),
|
resolve: dataloader(Events),
|
||||||
description: "A list of the feed tokens for this person"
|
description: "A list of the feed tokens for this person"
|
||||||
|
|
|
@ -7,7 +7,7 @@ defmodule Mobilizon.GraphQL.Schema.UserType do
|
||||||
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
|
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
|
||||||
|
|
||||||
alias Mobilizon.Events
|
alias Mobilizon.Events
|
||||||
alias Mobilizon.GraphQL.Resolvers.User
|
alias Mobilizon.GraphQL.Resolvers.{Picture, User}
|
||||||
alias Mobilizon.GraphQL.Schema
|
alias Mobilizon.GraphQL.Schema
|
||||||
|
|
||||||
import_types(Schema.SortType)
|
import_types(Schema.SortType)
|
||||||
|
@ -120,6 +120,11 @@ defmodule Mobilizon.GraphQL.Schema.UserType do
|
||||||
arg(:limit, :integer, default_value: 10, description: "The limit of user media per page")
|
arg(:limit, :integer, default_value: 10, description: "The limit of user media per page")
|
||||||
resolve(&User.user_medias/3)
|
resolve(&User.user_medias/3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
field(:media_size, :integer,
|
||||||
|
resolve: &Picture.user_size/3,
|
||||||
|
description: "The total size of all the media from this user (from all their actors)"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@desc "The list of roles an user can have"
|
@desc "The list of roles an user can have"
|
||||||
|
|
|
@ -37,6 +37,16 @@ defmodule Mobilizon.Media do
|
||||||
|> Repo.one()
|
|> Repo.one()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
List the paginated picture for an actor
|
||||||
|
"""
|
||||||
|
@spec pictures_for_actor(integer | String.t(), integer | nil, integer | nil) :: Page.t()
|
||||||
|
def pictures_for_actor(actor_id, page, limit) do
|
||||||
|
actor_id
|
||||||
|
|> pictures_for_actor_query()
|
||||||
|
|> Page.build_page(page, limit)
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
List the paginated picture for user
|
List the paginated picture for user
|
||||||
"""
|
"""
|
||||||
|
@ -47,6 +57,32 @@ defmodule Mobilizon.Media do
|
||||||
|> Page.build_page(page, limit)
|
|> Page.build_page(page, limit)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Calculate the sum of media size used by the user
|
||||||
|
"""
|
||||||
|
@spec media_size_for_actor(integer | String.t()) :: integer()
|
||||||
|
def media_size_for_actor(actor_id) do
|
||||||
|
actor_id
|
||||||
|
|> pictures_for_actor_query()
|
||||||
|
|> select([:file])
|
||||||
|
|> Repo.all()
|
||||||
|
|> Enum.map(& &1.file.size)
|
||||||
|
|> Enum.sum()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Calculate the sum of media size used by the user
|
||||||
|
"""
|
||||||
|
@spec media_size_for_user(integer | String.t()) :: integer()
|
||||||
|
def media_size_for_user(user_id) do
|
||||||
|
user_id
|
||||||
|
|> pictures_for_user_query()
|
||||||
|
|> select([:file])
|
||||||
|
|> Repo.all()
|
||||||
|
|> Enum.map(& &1.file.size)
|
||||||
|
|> Enum.sum()
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a picture.
|
Creates a picture.
|
||||||
"""
|
"""
|
||||||
|
@ -97,6 +133,13 @@ defmodule Mobilizon.Media do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec pictures_for_actor_query(integer() | String.t()) :: Ecto.Query.t()
|
||||||
|
defp pictures_for_actor_query(actor_id) do
|
||||||
|
Picture
|
||||||
|
|> join(:inner, [p], a in Actor, on: p.actor_id == a.id)
|
||||||
|
|> where([_p, a], a.id == ^actor_id)
|
||||||
|
end
|
||||||
|
|
||||||
@spec pictures_for_user_query(integer() | String.t()) :: Ecto.Query.t()
|
@spec pictures_for_user_query(integer() | String.t()) :: Ecto.Query.t()
|
||||||
defp pictures_for_user_query(user_id) do
|
defp pictures_for_user_query(user_id) do
|
||||||
Picture
|
Picture
|
||||||
|
|
|
@ -10,6 +10,9 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||||
|
|
||||||
alias Mobilizon.Web.Endpoint
|
alias Mobilizon.Web.Endpoint
|
||||||
|
|
||||||
|
@default_picture_details %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||||
|
@default_picture_path "test/fixtures/picture.png"
|
||||||
|
|
||||||
setup %{conn: conn} do
|
setup %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
actor = insert(:actor, user: user)
|
actor = insert(:actor, user: user)
|
||||||
|
@ -30,6 +33,21 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@upload_picture_mutation """
|
||||||
|
mutation UploadPicture($name: String!, $alt: String, $file: Upload!) {
|
||||||
|
uploadPicture(
|
||||||
|
name: $name
|
||||||
|
alt: $alt
|
||||||
|
file: $file
|
||||||
|
) {
|
||||||
|
url
|
||||||
|
name
|
||||||
|
content_type
|
||||||
|
size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
describe "Resolver: Get picture" do
|
describe "Resolver: Get picture" do
|
||||||
test "picture/3 returns the information on a picture", %{conn: conn} do
|
test "picture/3 returns the information on a picture", %{conn: conn} do
|
||||||
%Picture{id: id} = picture = insert(:picture)
|
%Picture{id: id} = picture = insert(:picture)
|
||||||
|
@ -59,21 +77,6 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "Resolver: Upload picture" do
|
describe "Resolver: Upload picture" do
|
||||||
@upload_picture_mutation """
|
|
||||||
mutation UploadPicture($name: String!, $alt: String, $file: Upload!) {
|
|
||||||
uploadPicture(
|
|
||||||
name: $name
|
|
||||||
alt: $alt
|
|
||||||
file: $file
|
|
||||||
) {
|
|
||||||
url
|
|
||||||
name
|
|
||||||
content_type
|
|
||||||
size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
test "upload_picture/3 uploads a new picture", %{conn: conn, user: user} do
|
test "upload_picture/3 uploads a new picture", %{conn: conn, user: user} do
|
||||||
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||||
|
|
||||||
|
@ -185,4 +188,304 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||||
assert hd(res["errors"])["status_code"] == 401
|
assert hd(res["errors"])["status_code"] == 401
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "Resolver: Get actor media size" do
|
||||||
|
@actor_media_size_query """
|
||||||
|
query LoggedPerson {
|
||||||
|
loggedPerson {
|
||||||
|
id
|
||||||
|
mediaSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
test "with own actor", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
insert(:actor, user: user)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @actor_media_size_query)
|
||||||
|
|
||||||
|
assert res["data"]["loggedPerson"]["mediaSize"] == 0
|
||||||
|
|
||||||
|
res = upload_picture(conn, user)
|
||||||
|
assert res["data"]["uploadPicture"]["size"] == 10_097
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @actor_media_size_query)
|
||||||
|
|
||||||
|
assert res["data"]["loggedPerson"]["mediaSize"] == 10_097
|
||||||
|
|
||||||
|
res =
|
||||||
|
upload_picture(
|
||||||
|
conn,
|
||||||
|
user,
|
||||||
|
"test/fixtures/image.jpg",
|
||||||
|
Map.put(@default_picture_details, :file, "image.jpg")
|
||||||
|
)
|
||||||
|
|
||||||
|
assert res["data"]["uploadPicture"]["size"] == 13_227
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @actor_media_size_query)
|
||||||
|
|
||||||
|
assert res["data"]["loggedPerson"]["mediaSize"] == 23_324
|
||||||
|
end
|
||||||
|
|
||||||
|
@list_actors_query """
|
||||||
|
query ListPersons($preferredUsername: String) {
|
||||||
|
persons(preferredUsername: $preferredUsername) {
|
||||||
|
total,
|
||||||
|
elements {
|
||||||
|
id
|
||||||
|
mediaSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
test "as a moderator", %{conn: conn} do
|
||||||
|
moderator = insert(:user, role: :moderator)
|
||||||
|
user = insert(:user)
|
||||||
|
actor = insert(:actor, user: user)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(moderator)
|
||||||
|
|> AbsintheHelpers.graphql_query(
|
||||||
|
query: @list_actors_query,
|
||||||
|
variables: %{preferredUsername: actor.preferred_username}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert is_nil(res["errors"])
|
||||||
|
assert hd(res["data"]["persons"]["elements"])["mediaSize"] == 0
|
||||||
|
|
||||||
|
upload_picture(conn, user)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(moderator)
|
||||||
|
|> AbsintheHelpers.graphql_query(
|
||||||
|
query: @list_actors_query,
|
||||||
|
variables: %{preferredUsername: actor.preferred_username}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert is_nil(res["errors"])
|
||||||
|
assert hd(res["data"]["persons"]["elements"])["mediaSize"] == 10_097
|
||||||
|
end
|
||||||
|
|
||||||
|
@event_organizer_media_query """
|
||||||
|
query Event($uuid: UUID!) {
|
||||||
|
event(uuid: $uuid) {
|
||||||
|
id
|
||||||
|
organizerActor {
|
||||||
|
id
|
||||||
|
mediaSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
test "as a different user", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
event = insert(:event)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(
|
||||||
|
query: @event_organizer_media_query,
|
||||||
|
variables: %{uuid: event.uuid}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert hd(res["errors"])["message"] == "unauthorized"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "without being logged-in", %{conn: conn} do
|
||||||
|
event = insert(:event)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> AbsintheHelpers.graphql_query(
|
||||||
|
query: @event_organizer_media_query,
|
||||||
|
variables: %{uuid: event.uuid}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert hd(res["errors"])["message"] == "unauthenticated"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "Resolver: Get user media size" do
|
||||||
|
@user_media_size_query """
|
||||||
|
query LoggedUser {
|
||||||
|
loggedUser {
|
||||||
|
id
|
||||||
|
mediaSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
@change_default_actor_mutation """
|
||||||
|
mutation ChangeDefaultActor($preferredUsername: String!) {
|
||||||
|
changeDefaultActor(preferredUsername: $preferredUsername) {
|
||||||
|
defaultActor {
|
||||||
|
id
|
||||||
|
preferredUsername
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
test "with own user", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
insert(:actor, user: user)
|
||||||
|
actor_2 = insert(:actor, user: user)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @user_media_size_query)
|
||||||
|
|
||||||
|
assert res["errors"] == nil
|
||||||
|
assert res["data"]["loggedUser"]["mediaSize"] == 0
|
||||||
|
|
||||||
|
res = upload_picture(conn, user)
|
||||||
|
assert res["data"]["uploadPicture"]["size"] == 10_097
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @user_media_size_query)
|
||||||
|
|
||||||
|
assert res["data"]["loggedUser"]["mediaSize"] == 10_097
|
||||||
|
|
||||||
|
res =
|
||||||
|
upload_picture(
|
||||||
|
conn,
|
||||||
|
user,
|
||||||
|
"test/fixtures/image.jpg",
|
||||||
|
Map.put(@default_picture_details, :file, "image.jpg")
|
||||||
|
)
|
||||||
|
|
||||||
|
assert res["data"]["uploadPicture"]["size"] == 13_227
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @user_media_size_query)
|
||||||
|
|
||||||
|
assert res["data"]["loggedUser"]["mediaSize"] == 23_324
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(
|
||||||
|
query: @change_default_actor_mutation,
|
||||||
|
variables: %{preferredUsername: actor_2.preferred_username}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert is_nil(res["errors"])
|
||||||
|
|
||||||
|
res =
|
||||||
|
upload_picture(
|
||||||
|
conn,
|
||||||
|
user,
|
||||||
|
"test/fixtures/image.jpg",
|
||||||
|
Map.put(@default_picture_details, :file, "image.jpg")
|
||||||
|
)
|
||||||
|
|
||||||
|
assert res["data"]["uploadPicture"]["size"] == 13_227
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @user_media_size_query)
|
||||||
|
|
||||||
|
assert res["data"]["loggedUser"]["mediaSize"] == 36_551
|
||||||
|
end
|
||||||
|
|
||||||
|
@list_users_query """
|
||||||
|
query ListUsers($email: String) {
|
||||||
|
users(email: $email) {
|
||||||
|
total,
|
||||||
|
elements {
|
||||||
|
id
|
||||||
|
mediaSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
test "as a moderator", %{conn: conn} do
|
||||||
|
moderator = insert(:user, role: :moderator)
|
||||||
|
user = insert(:user)
|
||||||
|
insert(:actor, user: user)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(moderator)
|
||||||
|
|> AbsintheHelpers.graphql_query(
|
||||||
|
query: @list_users_query,
|
||||||
|
variables: %{email: user.email}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert is_nil(res["errors"])
|
||||||
|
assert hd(res["data"]["users"]["elements"])["mediaSize"] == 0
|
||||||
|
|
||||||
|
res = upload_picture(conn, user)
|
||||||
|
assert is_nil(res["errors"])
|
||||||
|
assert res["data"]["uploadPicture"]["size"] == 10_097
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> auth_conn(moderator)
|
||||||
|
|> AbsintheHelpers.graphql_query(
|
||||||
|
query: @list_users_query,
|
||||||
|
variables: %{email: user.email}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert is_nil(res["errors"])
|
||||||
|
assert hd(res["data"]["users"]["elements"])["mediaSize"] == 10_097
|
||||||
|
end
|
||||||
|
|
||||||
|
test "without being logged-in", %{conn: conn} do
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> AbsintheHelpers.graphql_query(query: @user_media_size_query)
|
||||||
|
|
||||||
|
assert hd(res["errors"])["message"] == "You need to be logged-in to view current user"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec upload_picture(Plug.Conn.t(), Mobilizon.Users.User.t(), String.t(), map()) :: map()
|
||||||
|
defp upload_picture(
|
||||||
|
conn,
|
||||||
|
user,
|
||||||
|
picture_path \\ @default_picture_path,
|
||||||
|
picture_details \\ @default_picture_details
|
||||||
|
) do
|
||||||
|
map = %{
|
||||||
|
"query" => @upload_picture_mutation,
|
||||||
|
"variables" => picture_details,
|
||||||
|
picture_details.file => %Plug.Upload{
|
||||||
|
path: picture_path,
|
||||||
|
filename: picture_details.file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> auth_conn(user)
|
||||||
|
|> put_req_header("content-type", "multipart/form-data")
|
||||||
|
|> post(
|
||||||
|
"/api",
|
||||||
|
map
|
||||||
|
)
|
||||||
|
|> json_response(200)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue