2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
2020-01-26 20:36:50 +00:00
|
|
|
use Mobilizon.Web.ConnCase
|
2019-05-22 12:12:11 +00:00
|
|
|
use Bamboo.Test
|
2020-01-26 19:34:25 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
alias Mobilizon.Media.Picture
|
|
|
|
|
|
|
|
alias Mobilizon.GraphQL.AbsintheHelpers
|
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
setup %{conn: conn} do
|
|
|
|
user = insert(:user)
|
2019-05-31 15:58:03 +00:00
|
|
|
actor = insert(:actor, user: user)
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2019-05-31 15:58:03 +00:00
|
|
|
{:ok, conn: conn, user: user, actor: actor}
|
2019-05-22 12:12:11 +00:00
|
|
|
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,
|
2019-06-03 15:13:47 +00:00
|
|
|
url,
|
|
|
|
content_type,
|
|
|
|
size
|
2019-05-22 12:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "picture"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["picture"]["name"] == picture.file.name
|
|
|
|
|
2019-06-03 15:13:47 +00:00
|
|
|
assert json_response(res, 200)["data"]["picture"]["content_type"] ==
|
|
|
|
picture.file.content_type
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["picture"]["size"] == 13_120
|
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
assert json_response(res, 200)["data"]["picture"]["url"] =~ Endpoint.url()
|
2019-05-22 12:12:11 +00:00
|
|
|
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
|
2019-05-31 15:58:03 +00:00
|
|
|
test "upload_picture/3 uploads a new picture", %{conn: conn, user: user, actor: actor} do
|
2019-05-22 12:12:11 +00:00
|
|
|
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation { uploadPicture(
|
|
|
|
name: "#{picture.name}",
|
|
|
|
alt: "#{picture.alt}",
|
2019-05-31 15:58:03 +00:00
|
|
|
file: "#{picture.file}",
|
|
|
|
actor_id: #{actor.id}
|
2019-05-22 12:12:11 +00:00
|
|
|
) {
|
|
|
|
url,
|
2019-06-03 15:13:47 +00:00
|
|
|
name,
|
|
|
|
content_type,
|
|
|
|
size
|
2019-05-22 12:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
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
|
2019-06-03 15:13:47 +00:00
|
|
|
assert json_response(res, 200)["data"]["uploadPicture"]["content_type"] == "image/png"
|
|
|
|
assert json_response(res, 200)["data"]["uploadPicture"]["size"] == 10_097
|
2019-05-22 12:12:11 +00:00
|
|
|
assert json_response(res, 200)["data"]["uploadPicture"]["url"]
|
|
|
|
end
|
|
|
|
|
2019-05-31 15:58:03 +00:00
|
|
|
test "upload_picture/3 forbids uploading if no auth", %{conn: conn, actor: actor} do
|
2019-05-22 12:12:11 +00:00
|
|
|
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation { uploadPicture(
|
|
|
|
name: "#{picture.name}",
|
|
|
|
alt: "#{picture.alt}",
|
2019-05-31 15:58:03 +00:00
|
|
|
file: "#{picture.file}",
|
|
|
|
actor_id: #{actor.id}
|
2019-05-22 12:12:11 +00:00
|
|
|
) {
|
|
|
|
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
|