Refactor media upload
Use Upload Media logic from Pleroma Backend changes for picture upload Move AS <-> Model conversion to separate module Front changes Downgrade apollo-client: https://github.com/Akryum/vue-apollo/issues/577 Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -6,12 +6,11 @@
|
||||
defmodule MobilizonWeb.ActivityPubControllerTest do
|
||||
use MobilizonWeb.ConnCase
|
||||
import Mobilizon.Factory
|
||||
alias MobilizonWeb.ActivityPub.{ActorView, ObjectView}
|
||||
alias MobilizonWeb.ActivityPub.ActorView
|
||||
alias MobilizonWeb.PageView
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Utils
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||
alias MobilizonWeb.Endpoint
|
||||
|
||||
181
test/mobilizon_web/media_proxy_test.exs
Normal file
181
test/mobilizon_web/media_proxy_test.exs
Normal file
@@ -0,0 +1,181 @@
|
||||
# Portions of this file are derived from Pleroma:
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/media_proxy_test.ex
|
||||
|
||||
defmodule MobilizonWeb.MediaProxyTest do
|
||||
use ExUnit.Case
|
||||
import MobilizonWeb.MediaProxy
|
||||
alias MobilizonWeb.MediaProxyController
|
||||
|
||||
setup do
|
||||
enabled = Mobilizon.CommonConfig.get([:media_proxy, :enabled])
|
||||
on_exit(fn -> Mobilizon.CommonConfig.put([:media_proxy, :enabled], enabled) end)
|
||||
:ok
|
||||
end
|
||||
|
||||
describe "when enabled" do
|
||||
setup do
|
||||
Mobilizon.CommonConfig.put([:media_proxy, :enabled], true)
|
||||
:ok
|
||||
end
|
||||
|
||||
test "ignores invalid url" do
|
||||
assert url(nil) == nil
|
||||
assert url("") == nil
|
||||
end
|
||||
|
||||
test "ignores relative url" do
|
||||
assert url("/local") == "/local"
|
||||
assert url("/") == "/"
|
||||
end
|
||||
|
||||
test "ignores local url" do
|
||||
local_url = MobilizonWeb.Endpoint.url() <> "/hello"
|
||||
local_root = MobilizonWeb.Endpoint.url()
|
||||
assert url(local_url) == local_url
|
||||
assert url(local_root) == local_root
|
||||
end
|
||||
|
||||
test "encodes and decodes URL" do
|
||||
url = "https://pleroma.soykaf.com/static/logo.png"
|
||||
encoded = url(url)
|
||||
|
||||
assert String.starts_with?(
|
||||
encoded,
|
||||
Mobilizon.CommonConfig.get([:media_proxy, :base_url], MobilizonWeb.Endpoint.url())
|
||||
)
|
||||
|
||||
assert String.ends_with?(encoded, "/logo.png")
|
||||
|
||||
assert decode_result(encoded) == url
|
||||
end
|
||||
|
||||
test "encodes and decodes URL without a path" do
|
||||
url = "https://pleroma.soykaf.com"
|
||||
encoded = url(url)
|
||||
assert decode_result(encoded) == url
|
||||
end
|
||||
|
||||
test "encodes and decodes URL without an extension" do
|
||||
url = "https://pleroma.soykaf.com/path/"
|
||||
encoded = url(url)
|
||||
assert String.ends_with?(encoded, "/path")
|
||||
assert decode_result(encoded) == url
|
||||
end
|
||||
|
||||
test "encodes and decodes URL and ignores query params for the path" do
|
||||
url = "https://pleroma.soykaf.com/static/logo.png?93939393939&bunny=true"
|
||||
encoded = url(url)
|
||||
assert String.ends_with?(encoded, "/logo.png")
|
||||
assert decode_result(encoded) == url
|
||||
end
|
||||
|
||||
test "ensures urls are url-encoded" do
|
||||
assert decode_result(url("https://pleroma.social/Hello world.jpg")) ==
|
||||
"https://pleroma.social/Hello%20world.jpg"
|
||||
|
||||
assert decode_result(url("https://pleroma.social/Hello%20world.jpg")) ==
|
||||
"https://pleroma.social/Hello%20world.jpg"
|
||||
end
|
||||
|
||||
test "validates signature" do
|
||||
secret_key_base = Mobilizon.CommonConfig.get([MobilizonWeb.Endpoint, :secret_key_base])
|
||||
|
||||
on_exit(fn ->
|
||||
Mobilizon.CommonConfig.put([MobilizonWeb.Endpoint, :secret_key_base], secret_key_base)
|
||||
end)
|
||||
|
||||
encoded = url("https://pleroma.social")
|
||||
|
||||
Mobilizon.CommonConfig.put(
|
||||
[MobilizonWeb.Endpoint, :secret_key_base],
|
||||
"00000000000000000000000000000000000000000000000"
|
||||
)
|
||||
|
||||
[_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
|
||||
assert decode_url(sig, base64) == {:error, :invalid_signature}
|
||||
end
|
||||
|
||||
test "filename_matches matches url encoded paths" do
|
||||
assert MediaProxyController.filename_matches(
|
||||
true,
|
||||
"/Hello%20world.jpg",
|
||||
"http://pleroma.social/Hello world.jpg"
|
||||
) == :ok
|
||||
|
||||
assert MediaProxyController.filename_matches(
|
||||
true,
|
||||
"/Hello%20world.jpg",
|
||||
"http://pleroma.social/Hello%20world.jpg"
|
||||
) == :ok
|
||||
end
|
||||
|
||||
test "filename_matches matches non-url encoded paths" do
|
||||
assert MediaProxyController.filename_matches(
|
||||
true,
|
||||
"/Hello world.jpg",
|
||||
"http://pleroma.social/Hello%20world.jpg"
|
||||
) == :ok
|
||||
|
||||
assert MediaProxyController.filename_matches(
|
||||
true,
|
||||
"/Hello world.jpg",
|
||||
"http://pleroma.social/Hello world.jpg"
|
||||
) == :ok
|
||||
end
|
||||
|
||||
test "uses the configured base_url" do
|
||||
base_url = Mobilizon.CommonConfig.get([:media_proxy, :base_url])
|
||||
|
||||
if base_url do
|
||||
on_exit(fn ->
|
||||
Mobilizon.CommonConfig.put([:media_proxy, :base_url], base_url)
|
||||
end)
|
||||
end
|
||||
|
||||
Mobilizon.CommonConfig.put([:media_proxy, :base_url], "https://cache.pleroma.social")
|
||||
|
||||
url = "https://pleroma.soykaf.com/static/logo.png"
|
||||
encoded = url(url)
|
||||
|
||||
assert String.starts_with?(encoded, Mobilizon.CommonConfig.get([:media_proxy, :base_url]))
|
||||
end
|
||||
|
||||
# https://git.pleroma.social/pleroma/pleroma/issues/580
|
||||
test "encoding S3 links (must preserve `%2F`)" do
|
||||
url =
|
||||
"https://s3.amazonaws.com/example/test.png?X-Amz-Credential=your-access-key-id%2F20130721%2Fus-east-1%2Fs3%2Faws4_request"
|
||||
|
||||
encoded = url(url)
|
||||
assert decode_result(encoded) == url
|
||||
end
|
||||
end
|
||||
|
||||
describe "when disabled" do
|
||||
setup do
|
||||
enabled = Mobilizon.CommonConfig.get([:media_proxy, :enabled])
|
||||
|
||||
if enabled do
|
||||
Mobilizon.CommonConfig.put([:media_proxy, :enabled], false)
|
||||
|
||||
on_exit(fn ->
|
||||
Mobilizon.CommonConfig.put([:media_proxy, :enabled], enabled)
|
||||
:ok
|
||||
end)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
test "does not encode remote urls" do
|
||||
assert url("https://google.fr") == "https://google.fr"
|
||||
end
|
||||
end
|
||||
|
||||
defp decode_result(encoded) do
|
||||
[_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
|
||||
{:ok, decoded} = decode_url(sig, base64)
|
||||
decoded
|
||||
end
|
||||
end
|
||||
44
test/mobilizon_web/plugs/uploaded_media_plug_test.exs
Normal file
44
test/mobilizon_web/plugs/uploaded_media_plug_test.exs
Normal file
@@ -0,0 +1,44 @@
|
||||
# Portions of this file are derived from Pleroma:
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/plugs/uploaded_media_plug_test.ex
|
||||
|
||||
defmodule MobilizonWeb.Plugs.UploadedMediaPlugTest do
|
||||
use MobilizonWeb.ConnCase
|
||||
alias MobilizonWeb.Upload
|
||||
|
||||
defp upload_file(context) do
|
||||
Mobilizon.DataCase.ensure_local_uploader(context)
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "nice_tf.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
[%{"href" => attachment_url} | _] = data["url"]
|
||||
[attachment_url: attachment_url]
|
||||
end
|
||||
|
||||
setup_all :upload_file
|
||||
|
||||
test "does not send Content-Disposition header when name param is not set", %{
|
||||
attachment_url: attachment_url
|
||||
} do
|
||||
conn = get(build_conn(), attachment_url)
|
||||
refute Enum.any?(conn.resp_headers, &(elem(&1, 0) == "content-disposition"))
|
||||
end
|
||||
|
||||
test "sends Content-Disposition header when name param is set", %{
|
||||
attachment_url: attachment_url
|
||||
} do
|
||||
conn = get(build_conn(), attachment_url <> "?name=\"cofe\".gif")
|
||||
|
||||
assert Enum.any?(
|
||||
conn.resp_headers,
|
||||
&(&1 == {"content-disposition", "filename=\"\\\"cofe\\\".gif\""})
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -84,6 +84,135 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
|
||||
assert json_response(res, 200)["data"]["createEvent"]["title"] == "come to my event"
|
||||
end
|
||||
|
||||
test "create_event/3 creates an event with an attached picture", %{
|
||||
conn: conn,
|
||||
actor: actor,
|
||||
user: user
|
||||
} do
|
||||
mutation = """
|
||||
mutation {
|
||||
createEvent(
|
||||
title: "come to my event",
|
||||
description: "it will be fine",
|
||||
begins_on: "#{
|
||||
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
|
||||
}",
|
||||
organizer_actor_id: "#{actor.id}",
|
||||
category: "birthday",
|
||||
picture: {
|
||||
picture: {
|
||||
name: "picture for my event",
|
||||
alt: "A very sunny landscape",
|
||||
file: "event.jpg"
|
||||
}
|
||||
}
|
||||
) {
|
||||
title,
|
||||
uuid,
|
||||
picture {
|
||||
name,
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
map = %{
|
||||
"query" => mutation,
|
||||
"event.jpg" => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: "event.jpg"
|
||||
}
|
||||
}
|
||||
|
||||
res =
|
||||
conn
|
||||
|> auth_conn(user)
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post("/api", map)
|
||||
|
||||
assert json_response(res, 200)["data"]["createEvent"]["title"] == "come to my event"
|
||||
|
||||
assert json_response(res, 200)["data"]["createEvent"]["picture"]["name"] ==
|
||||
"picture for my event"
|
||||
end
|
||||
|
||||
test "create_event/3 creates an event with an picture URL", %{
|
||||
conn: conn,
|
||||
actor: actor,
|
||||
user: user
|
||||
} do
|
||||
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
|
||||
mutation = """
|
||||
mutation { uploadPicture(
|
||||
name: "#{picture.name}",
|
||||
alt: "#{picture.alt}",
|
||||
file: "#{picture.file}"
|
||||
) {
|
||||
id,
|
||||
url,
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
map = %{
|
||||
"query" => mutation,
|
||||
picture.file => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: picture.file
|
||||
}
|
||||
}
|
||||
|
||||
res =
|
||||
conn
|
||||
|> auth_conn(user)
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post(
|
||||
"/api",
|
||||
map
|
||||
)
|
||||
|
||||
assert json_response(res, 200)["data"]["uploadPicture"]["name"] == picture.name
|
||||
picture_id = json_response(res, 200)["data"]["uploadPicture"]["id"]
|
||||
|
||||
mutation = """
|
||||
mutation {
|
||||
createEvent(
|
||||
title: "come to my event",
|
||||
description: "it will be fine",
|
||||
begins_on: "#{
|
||||
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
|
||||
}",
|
||||
organizer_actor_id: "#{actor.id}",
|
||||
category: "birthday",
|
||||
picture: {
|
||||
picture_id: "#{picture_id}"
|
||||
}
|
||||
) {
|
||||
title,
|
||||
uuid,
|
||||
picture {
|
||||
name,
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
res =
|
||||
conn
|
||||
|> auth_conn(user)
|
||||
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
||||
|
||||
assert json_response(res, 200)["data"]["createEvent"]["title"] == "come to my event"
|
||||
|
||||
assert json_response(res, 200)["data"]["createEvent"]["picture"]["name"] == picture.name
|
||||
|
||||
assert json_response(res, 200)["data"]["createEvent"]["picture"]["url"]
|
||||
end
|
||||
|
||||
test "list_events/3 returns events", context do
|
||||
event = insert(:event)
|
||||
|
||||
|
||||
@@ -50,7 +50,9 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do
|
||||
query = """
|
||||
{
|
||||
loggedPerson {
|
||||
avatarUrl,
|
||||
avatar {
|
||||
url
|
||||
},
|
||||
preferredUsername,
|
||||
}
|
||||
}
|
||||
@@ -72,6 +74,9 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do
|
||||
|
||||
assert json_response(res, 200)["data"]["loggedPerson"]["preferredUsername"] ==
|
||||
actor.preferred_username
|
||||
|
||||
assert json_response(res, 200)["data"]["loggedPerson"]["avatar"]["url"] =~
|
||||
MobilizonWeb.Endpoint.url()
|
||||
end
|
||||
|
||||
test "create_person/3 creates a new identity", context do
|
||||
@@ -111,7 +116,9 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do
|
||||
query = """
|
||||
{
|
||||
identities {
|
||||
avatarUrl,
|
||||
avatar {
|
||||
url
|
||||
},
|
||||
preferredUsername,
|
||||
}
|
||||
}
|
||||
@@ -136,87 +143,156 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do
|
||||
|> MapSet.new() ==
|
||||
MapSet.new([actor.preferred_username, "new_identity"])
|
||||
end
|
||||
end
|
||||
|
||||
test "get_current_person/3 can return the events the person is going to", context do
|
||||
user = insert(:user)
|
||||
actor = insert(:actor, user: user)
|
||||
test "create_person/3 with an avatar and an banner creates a new identity", context do
|
||||
user = insert(:user)
|
||||
insert(:actor, user: user)
|
||||
|
||||
query = """
|
||||
{
|
||||
loggedPerson {
|
||||
goingToEvents {
|
||||
uuid,
|
||||
title
|
||||
mutation = """
|
||||
mutation {
|
||||
createPerson(
|
||||
preferredUsername: "new_identity",
|
||||
name: "secret person",
|
||||
summary: "no-one will know who I am",
|
||||
banner: {
|
||||
picture: {
|
||||
file: "landscape.jpg",
|
||||
name: "irish landscape",
|
||||
alt: "The beautiful atlantic way"
|
||||
}
|
||||
}
|
||||
) {
|
||||
id,
|
||||
preferredUsername
|
||||
avatar {
|
||||
id,
|
||||
url
|
||||
},
|
||||
banner {
|
||||
id,
|
||||
name,
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
map = %{
|
||||
"query" => mutation,
|
||||
"landscape.jpg" => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: "landscape.jpg"
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
||||
res =
|
||||
context.conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post("/api", map)
|
||||
|
||||
assert json_response(res, 200)["data"]["loggedPerson"]["goingToEvents"] == []
|
||||
assert json_response(res, 200)["data"]["createPerson"] == nil
|
||||
|
||||
event = insert(:event, %{organizer_actor: actor})
|
||||
insert(:participant, %{actor: actor, event: event})
|
||||
assert hd(json_response(res, 200)["errors"])["message"] ==
|
||||
"You need to be logged-in to create a new identity"
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post("/api", map)
|
||||
|
||||
assert json_response(res, 200)["data"]["loggedPerson"]["goingToEvents"] == [
|
||||
%{"title" => event.title, "uuid" => event.uuid}
|
||||
]
|
||||
end
|
||||
assert json_response(res, 200)["data"]["createPerson"]["preferredUsername"] ==
|
||||
"new_identity"
|
||||
|
||||
test "find_person/3 can return the events an identity is going to if it's the same actor",
|
||||
context do
|
||||
user = insert(:user)
|
||||
actor = insert(:actor, user: user)
|
||||
insert(:actor, user: user)
|
||||
actor_from_other_user = insert(:actor)
|
||||
assert json_response(res, 200)["data"]["createPerson"]["banner"]["id"]
|
||||
|
||||
query = """
|
||||
{
|
||||
person(preferredUsername: "#{actor.preferred_username}") {
|
||||
goingToEvents {
|
||||
uuid,
|
||||
title
|
||||
assert json_response(res, 200)["data"]["createPerson"]["banner"]["name"] ==
|
||||
"The beautiful atlantic way"
|
||||
|
||||
assert json_response(res, 200)["data"]["createPerson"]["banner"]["url"] =~
|
||||
MobilizonWeb.Endpoint.url() <> "/media/"
|
||||
end
|
||||
|
||||
test "get_current_person/3 can return the events the person is going to", context do
|
||||
user = insert(:user)
|
||||
actor = insert(:actor, user: user)
|
||||
|
||||
query = """
|
||||
{
|
||||
loggedPerson {
|
||||
goingToEvents {
|
||||
uuid,
|
||||
title
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
||||
|
||||
assert json_response(res, 200)["data"]["loggedPerson"]["goingToEvents"] == []
|
||||
|
||||
event = insert(:event, %{organizer_actor: actor})
|
||||
insert(:participant, %{actor: actor, event: event})
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
||||
|
||||
assert json_response(res, 200)["data"]["loggedPerson"]["goingToEvents"] == [
|
||||
%{"title" => event.title, "uuid" => event.uuid}
|
||||
]
|
||||
end
|
||||
|
||||
test "find_person/3 can return the events an identity is going to if it's the same actor",
|
||||
context do
|
||||
user = insert(:user)
|
||||
actor = insert(:actor, user: user)
|
||||
insert(:actor, user: user)
|
||||
actor_from_other_user = insert(:actor)
|
||||
|
||||
query = """
|
||||
{
|
||||
person(preferredUsername: "#{actor.preferred_username}") {
|
||||
goingToEvents {
|
||||
uuid,
|
||||
title
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
"""
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "person"))
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "person"))
|
||||
|
||||
assert json_response(res, 200)["data"]["person"]["goingToEvents"] == []
|
||||
assert json_response(res, 200)["data"]["person"]["goingToEvents"] == []
|
||||
|
||||
query = """
|
||||
{
|
||||
person(preferredUsername: "#{actor_from_other_user.preferred_username}") {
|
||||
goingToEvents {
|
||||
uuid,
|
||||
title
|
||||
}
|
||||
query = """
|
||||
{
|
||||
person(preferredUsername: "#{actor_from_other_user.preferred_username}") {
|
||||
goingToEvents {
|
||||
uuid,
|
||||
title
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
"""
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "person"))
|
||||
res =
|
||||
context.conn
|
||||
|> auth_conn(user)
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "person"))
|
||||
|
||||
assert json_response(res, 200)["data"]["person"]["goingToEvents"] == nil
|
||||
assert json_response(res, 200)["data"]["person"]["goingToEvents"] == nil
|
||||
|
||||
assert hd(json_response(res, 200)["errors"])["message"] ==
|
||||
"Actor id is not owned by authenticated user"
|
||||
assert hd(json_response(res, 200)["errors"])["message"] ==
|
||||
"Actor id is not owned by authenticated user"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
130
test/mobilizon_web/resolvers/picture_resolver_test.exs
Normal file
130
test/mobilizon_web/resolvers/picture_resolver_test.exs
Normal file
@@ -0,0 +1,130 @@
|
||||
defmodule MobilizonWeb.Resolvers.PictureResolverTest do
|
||||
alias MobilizonWeb.AbsintheHelpers
|
||||
use MobilizonWeb.ConnCase
|
||||
use Bamboo.Test
|
||||
alias Mobilizon.Media.Picture
|
||||
import Mobilizon.Factory
|
||||
|
||||
setup %{conn: conn} do
|
||||
user = insert(:user)
|
||||
|
||||
{:ok, conn: conn, user: user}
|
||||
end
|
||||
|
||||
describe "Resolver: Get picture" do
|
||||
test "picture/3 returns the information on a picture", context do
|
||||
%Picture{id: id} = picture = insert(:picture)
|
||||
|
||||
query = """
|
||||
{
|
||||
picture(id: "#{id}") {
|
||||
name,
|
||||
alt,
|
||||
url
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "picture"))
|
||||
|
||||
assert json_response(res, 200)["data"]["picture"]["name"] == picture.file.name
|
||||
|
||||
assert json_response(res, 200)["data"]["picture"]["url"] =~
|
||||
MobilizonWeb.Endpoint.url()
|
||||
end
|
||||
|
||||
test "picture/3 returns nothing on a non-existent picture", context do
|
||||
query = """
|
||||
{
|
||||
picture(id: "3") {
|
||||
name,
|
||||
alt,
|
||||
url
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
res =
|
||||
context.conn
|
||||
|> get("/api", AbsintheHelpers.query_skeleton(query, "picture"))
|
||||
|
||||
assert hd(json_response(res, 200)["errors"])["message"] ==
|
||||
"Picture with ID 3 was not found"
|
||||
end
|
||||
end
|
||||
|
||||
describe "Resolver: Upload picture" do
|
||||
test "upload_picture/3 uploads a new picture", %{conn: conn, user: user} do
|
||||
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
|
||||
mutation = """
|
||||
mutation { uploadPicture(
|
||||
name: "#{picture.name}",
|
||||
alt: "#{picture.alt}",
|
||||
file: "#{picture.file}"
|
||||
) {
|
||||
url,
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
map = %{
|
||||
"query" => mutation,
|
||||
picture.file => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: picture.file
|
||||
}
|
||||
}
|
||||
|
||||
res =
|
||||
conn
|
||||
|> auth_conn(user)
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post(
|
||||
"/api",
|
||||
map
|
||||
)
|
||||
|
||||
assert json_response(res, 200)["data"]["uploadPicture"]["name"] == picture.name
|
||||
assert json_response(res, 200)["data"]["uploadPicture"]["url"]
|
||||
end
|
||||
|
||||
test "upload_picture/3 forbids uploading if no auth", %{conn: conn} do
|
||||
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
|
||||
mutation = """
|
||||
mutation { uploadPicture(
|
||||
name: "#{picture.name}",
|
||||
alt: "#{picture.alt}",
|
||||
file: "#{picture.file}"
|
||||
) {
|
||||
url,
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
map = %{
|
||||
"query" => mutation,
|
||||
picture.file => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: picture.file
|
||||
}
|
||||
}
|
||||
|
||||
res =
|
||||
conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post(
|
||||
"/api",
|
||||
map
|
||||
)
|
||||
|
||||
assert hd(json_response(res, 200)["errors"])["message"] ==
|
||||
"You need to login to upload a picture"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -255,7 +255,9 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
|
||||
preferredUsername,
|
||||
name,
|
||||
summary,
|
||||
avatarUrl,
|
||||
avatar {
|
||||
url
|
||||
},
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -295,7 +297,9 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
|
||||
preferredUsername,
|
||||
name,
|
||||
summary,
|
||||
avatarUrl,
|
||||
avatar {
|
||||
url
|
||||
},
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -334,7 +338,9 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
|
||||
preferredUsername,
|
||||
name,
|
||||
summary,
|
||||
avatarUrl,
|
||||
avatar {
|
||||
url
|
||||
},
|
||||
}
|
||||
}
|
||||
"""
|
||||
@@ -357,7 +363,9 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
|
||||
preferredUsername,
|
||||
name,
|
||||
summary,
|
||||
avatarUrl,
|
||||
avatar {
|
||||
url
|
||||
},
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
173
test/mobilizon_web/upload_test.exs
Normal file
173
test/mobilizon_web/upload_test.exs
Normal file
@@ -0,0 +1,173 @@
|
||||
# Portions of this file are derived from Pleroma:
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/upload_test.ex
|
||||
|
||||
defmodule Mobilizon.UploadTest do
|
||||
alias MobilizonWeb.Upload
|
||||
use Mobilizon.DataCase
|
||||
|
||||
describe "Storing a file with the Local uploader" do
|
||||
setup [:ensure_local_uploader]
|
||||
|
||||
test "returns a media url" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "image.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
|
||||
assert %{"url" => [%{"href" => url}]} = data
|
||||
|
||||
assert String.starts_with?(url, MobilizonWeb.Endpoint.url() <> "/media/")
|
||||
end
|
||||
|
||||
test "returns a media url with configured base_url" do
|
||||
base_url = "https://cache.mobilizon.social"
|
||||
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "image.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file, base_url: base_url)
|
||||
|
||||
assert %{"url" => [%{"href" => url}]} = data
|
||||
|
||||
assert String.starts_with?(url, base_url <> "/media/")
|
||||
end
|
||||
|
||||
test "copies the file to the configured folder with deduping" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "an [image.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file, filters: [MobilizonWeb.Upload.Filter.Dedupe])
|
||||
|
||||
assert List.first(data["url"])["href"] ==
|
||||
MobilizonWeb.Endpoint.url() <>
|
||||
"/media/590523d60d3831ec92d05cdd871078409d5780903910efec5cd35ab1b0f19d11.jpg"
|
||||
end
|
||||
|
||||
test "copies the file to the configured folder without deduping" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "an [image.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
assert data["name"] == "an [image.jpg"
|
||||
end
|
||||
|
||||
test "fixes incorrect content type" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "application/octet-stream",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "an [image.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file, filters: [MobilizonWeb.Upload.Filter.Dedupe])
|
||||
assert hd(data["url"])["mediaType"] == "image/jpeg"
|
||||
end
|
||||
|
||||
test "adds missing extension" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "an [image"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
assert data["name"] == "an [image.jpg"
|
||||
end
|
||||
|
||||
test "fixes incorrect file extension" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "an [image.blah"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
assert data["name"] == "an [image.jpg"
|
||||
end
|
||||
|
||||
test "don't modify filename of an unknown type" do
|
||||
File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "text/plain",
|
||||
path: Path.absname("test/fixtures/test_tmp.txt"),
|
||||
filename: "test.txt"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
assert data["name"] == "test.txt"
|
||||
end
|
||||
|
||||
test "copies the file to the configured folder with anonymizing filename" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "an [image.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file, filters: [MobilizonWeb.Upload.Filter.AnonymizeFilename])
|
||||
|
||||
refute data["name"] == "an [image.jpg"
|
||||
end
|
||||
|
||||
test "escapes invalid characters in url" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: "an… image.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
[attachment_url | _] = data["url"]
|
||||
|
||||
assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
|
||||
end
|
||||
|
||||
test "escapes reserved uri characters" do
|
||||
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
|
||||
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image_tmp.jpg"),
|
||||
filename: ":?#[]@!$&\\'()*+,;=.jpg"
|
||||
}
|
||||
|
||||
{:ok, data} = Upload.store(file)
|
||||
[attachment_url | _] = data["url"]
|
||||
|
||||
assert Path.basename(attachment_url["href"]) ==
|
||||
"%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user