Track usage of media files and add a job to clean them
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -526,7 +526,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
organizer_actor_id: "#{actor.id}",
|
||||
category: "birthday",
|
||||
picture: {
|
||||
picture: {
|
||||
media: {
|
||||
name: "picture for my event",
|
||||
alt: "A very sunny landscape",
|
||||
file: "event.jpg",
|
||||
@@ -569,13 +569,13 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
actor: actor,
|
||||
user: user
|
||||
} do
|
||||
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
media = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
|
||||
mutation = """
|
||||
mutation { uploadPicture(
|
||||
name: "#{picture.name}",
|
||||
alt: "#{picture.alt}",
|
||||
file: "#{picture.file}"
|
||||
mutation { uploadMedia (
|
||||
name: "#{media.name}",
|
||||
alt: "#{media.alt}",
|
||||
file: "#{media.file}"
|
||||
) {
|
||||
id,
|
||||
url,
|
||||
@@ -586,9 +586,9 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
|
||||
map = %{
|
||||
"query" => mutation,
|
||||
picture.file => %Plug.Upload{
|
||||
media.file => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: picture.file
|
||||
filename: media.file
|
||||
}
|
||||
}
|
||||
|
||||
@@ -601,8 +601,8 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
map
|
||||
)
|
||||
|
||||
assert json_response(res, 200)["data"]["uploadPicture"]["name"] == picture.name
|
||||
picture_id = json_response(res, 200)["data"]["uploadPicture"]["id"]
|
||||
assert json_response(res, 200)["data"]["uploadMedia"]["name"] == media.name
|
||||
media_id = json_response(res, 200)["data"]["uploadMedia"]["id"]
|
||||
|
||||
mutation = """
|
||||
mutation {
|
||||
@@ -615,7 +615,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
organizer_actor_id: "#{actor.id}",
|
||||
category: "birthday",
|
||||
picture: {
|
||||
picture_id: "#{picture_id}"
|
||||
media_id: "#{media_id}"
|
||||
}
|
||||
) {
|
||||
title,
|
||||
@@ -635,7 +635,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
|
||||
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"]["name"] == media.name
|
||||
|
||||
assert json_response(res, 200)["data"]["createEvent"]["picture"]["url"]
|
||||
end
|
||||
@@ -943,7 +943,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
event_id: #{event.id},
|
||||
category: "birthday",
|
||||
picture: {
|
||||
picture: {
|
||||
media: {
|
||||
name: "picture for my event",
|
||||
alt: "A very sunny landscape",
|
||||
file: "event.jpg",
|
||||
@@ -1349,7 +1349,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|
||||
test "delete_event/3 should check the event can be deleted by the user", %{
|
||||
conn: conn,
|
||||
user: user,
|
||||
actor: actor
|
||||
actor: _actor
|
||||
} do
|
||||
actor2 = insert(:actor)
|
||||
event = insert(:event, organizer_actor: actor2)
|
||||
|
||||
@@ -218,8 +218,8 @@ defmodule Mobilizon.Web.Resolvers.GroupTest do
|
||||
$id: ID!
|
||||
$name: String
|
||||
$summary: String
|
||||
$avatar: PictureInput
|
||||
$banner: PictureInput
|
||||
$avatar: MediaInput
|
||||
$banner: MediaInput
|
||||
$visibility: GroupVisibility
|
||||
$physicalAddress: AddressInput
|
||||
) {
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
defmodule Mobilizon.GraphQL.Resolvers.MediaTest do
|
||||
use Mobilizon.Web.ConnCase
|
||||
use Bamboo.Test
|
||||
|
||||
import Mobilizon.Factory
|
||||
|
||||
alias Mobilizon.Media.Picture
|
||||
alias Mobilizon.Medias.Media
|
||||
|
||||
alias Mobilizon.GraphQL.AbsintheHelpers
|
||||
|
||||
alias Mobilizon.Web.Endpoint
|
||||
|
||||
@default_picture_details %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
@default_picture_path "test/fixtures/picture.png"
|
||||
@default_media_details %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
@default_media_path "test/fixtures/picture.png"
|
||||
|
||||
setup %{conn: conn} do
|
||||
user = insert(:user)
|
||||
@@ -20,9 +20,9 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
{:ok, conn: conn, user: user, actor: actor}
|
||||
end
|
||||
|
||||
@picture_query """
|
||||
query Picture($id: ID!) {
|
||||
picture(id: $id) {
|
||||
@media_query """
|
||||
query Media($id: ID!) {
|
||||
media(id: $id) {
|
||||
id
|
||||
name,
|
||||
alt,
|
||||
@@ -33,9 +33,9 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
}
|
||||
"""
|
||||
|
||||
@upload_picture_mutation """
|
||||
mutation UploadPicture($name: String!, $alt: String, $file: Upload!) {
|
||||
uploadPicture(
|
||||
@upload_media_mutation """
|
||||
mutation UploadMedia($name: String!, $alt: String, $file: Upload!) {
|
||||
uploadMedia(
|
||||
name: $name
|
||||
alt: $alt
|
||||
file: $file
|
||||
@@ -48,44 +48,44 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
}
|
||||
"""
|
||||
|
||||
describe "Resolver: Get picture" do
|
||||
test "picture/3 returns the information on a picture", %{conn: conn} do
|
||||
%Picture{id: id} = picture = insert(:picture)
|
||||
describe "Resolver: Get media" do
|
||||
test "media/3 returns the information on a media", %{conn: conn} do
|
||||
%Media{id: id} = media = insert(:media)
|
||||
|
||||
res =
|
||||
conn
|
||||
|> AbsintheHelpers.graphql_query(query: @picture_query, variables: %{id: id})
|
||||
|> AbsintheHelpers.graphql_query(query: @media_query, variables: %{id: id})
|
||||
|
||||
assert res["data"]["picture"]["name"] == picture.file.name
|
||||
assert res["data"]["media"]["name"] == media.file.name
|
||||
|
||||
assert res["data"]["picture"]["content_type"] ==
|
||||
picture.file.content_type
|
||||
assert res["data"]["media"]["content_type"] ==
|
||||
media.file.content_type
|
||||
|
||||
assert res["data"]["picture"]["size"] == 13_120
|
||||
assert res["data"]["media"]["size"] == 13_120
|
||||
|
||||
assert res["data"]["picture"]["url"] =~ Endpoint.url()
|
||||
assert res["data"]["media"]["url"] =~ Endpoint.url()
|
||||
end
|
||||
|
||||
test "picture/3 returns nothing on a non-existent picture", %{conn: conn} do
|
||||
test "media/3 returns nothing on a non-existent media", %{conn: conn} do
|
||||
res =
|
||||
conn
|
||||
|> AbsintheHelpers.graphql_query(query: @picture_query, variables: %{id: 3})
|
||||
|> AbsintheHelpers.graphql_query(query: @media_query, variables: %{id: 3})
|
||||
|
||||
assert hd(res["errors"])["message"] == "Resource not found"
|
||||
assert hd(res["errors"])["status_code"] == 404
|
||||
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"}
|
||||
describe "Resolver: Upload media" do
|
||||
test "upload_media/3 uploads a new media", %{conn: conn, user: user} do
|
||||
media = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
|
||||
map = %{
|
||||
"query" => @upload_picture_mutation,
|
||||
"variables" => picture,
|
||||
picture.file => %Plug.Upload{
|
||||
"query" => @upload_media_mutation,
|
||||
"variables" => media,
|
||||
media.file => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: picture.file
|
||||
filename: media.file
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,21 +99,21 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
)
|
||||
|> json_response(200)
|
||||
|
||||
assert res["data"]["uploadPicture"]["name"] == picture.name
|
||||
assert res["data"]["uploadPicture"]["content_type"] == "image/png"
|
||||
assert res["data"]["uploadPicture"]["size"] == 10_097
|
||||
assert res["data"]["uploadPicture"]["url"]
|
||||
assert res["data"]["uploadMedia"]["name"] == media.name
|
||||
assert res["data"]["uploadMedia"]["content_type"] == "image/png"
|
||||
assert res["data"]["uploadMedia"]["size"] == 10_097
|
||||
assert res["data"]["uploadMedia"]["url"]
|
||||
end
|
||||
|
||||
test "upload_picture/3 forbids uploading if no auth", %{conn: conn} do
|
||||
picture = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
test "upload_media/3 forbids uploading if no auth", %{conn: conn} do
|
||||
media = %{name: "my pic", alt: "represents something", file: "picture.png"}
|
||||
|
||||
map = %{
|
||||
"query" => @upload_picture_mutation,
|
||||
"variables" => picture,
|
||||
picture.file => %Plug.Upload{
|
||||
"query" => @upload_media_mutation,
|
||||
"variables" => media,
|
||||
media.file => %Plug.Upload{
|
||||
path: "test/fixtures/picture.png",
|
||||
filename: picture.file
|
||||
filename: media.file
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,43 +130,43 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "Resolver: Remove picture" do
|
||||
@remove_picture_mutation """
|
||||
mutation RemovePicture($id: ID!) {
|
||||
removePicture(id: $id) {
|
||||
describe "Resolver: Remove media" do
|
||||
@remove_media_mutation """
|
||||
mutation RemoveMedia($id: ID!) {
|
||||
removeMedia(id: $id) {
|
||||
id
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
test "Removes a previously uploaded picture", %{conn: conn, user: user, actor: actor} do
|
||||
%Picture{id: picture_id} = insert(:picture, actor: actor)
|
||||
test "Removes a previously uploaded media", %{conn: conn, user: user, actor: actor} do
|
||||
%Media{id: media_id} = insert(:media, actor: actor)
|
||||
|
||||
res =
|
||||
conn
|
||||
|> auth_conn(user)
|
||||
|> AbsintheHelpers.graphql_query(
|
||||
query: @remove_picture_mutation,
|
||||
variables: %{id: picture_id}
|
||||
query: @remove_media_mutation,
|
||||
variables: %{id: media_id}
|
||||
)
|
||||
|
||||
assert is_nil(res["errors"])
|
||||
assert res["data"]["removePicture"]["id"] == to_string(picture_id)
|
||||
assert res["data"]["removeMedia"]["id"] == to_string(media_id)
|
||||
|
||||
res =
|
||||
conn
|
||||
|> AbsintheHelpers.graphql_query(query: @picture_query, variables: %{id: picture_id})
|
||||
|> AbsintheHelpers.graphql_query(query: @media_query, variables: %{id: media_id})
|
||||
|
||||
assert hd(res["errors"])["message"] == "Resource not found"
|
||||
assert hd(res["errors"])["status_code"] == 404
|
||||
end
|
||||
|
||||
test "Removes nothing if picture is not found", %{conn: conn, user: user} do
|
||||
test "Removes nothing if media is not found", %{conn: conn, user: user} do
|
||||
res =
|
||||
conn
|
||||
|> auth_conn(user)
|
||||
|> AbsintheHelpers.graphql_query(
|
||||
query: @remove_picture_mutation,
|
||||
query: @remove_media_mutation,
|
||||
variables: %{id: 400}
|
||||
)
|
||||
|
||||
@@ -174,14 +174,14 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
assert hd(res["errors"])["status_code"] == 404
|
||||
end
|
||||
|
||||
test "Removes nothing if picture if not logged-in", %{conn: conn, actor: actor} do
|
||||
%Picture{id: picture_id} = insert(:picture, actor: actor)
|
||||
test "Removes nothing if media if not logged-in", %{conn: conn, actor: actor} do
|
||||
%Media{id: media_id} = insert(:media, actor: actor)
|
||||
|
||||
res =
|
||||
conn
|
||||
|> AbsintheHelpers.graphql_query(
|
||||
query: @remove_picture_mutation,
|
||||
variables: %{id: picture_id}
|
||||
query: @remove_media_mutation,
|
||||
variables: %{id: media_id}
|
||||
)
|
||||
|
||||
assert hd(res["errors"])["message"] == "You need to be logged in"
|
||||
@@ -210,8 +210,8 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
|
||||
assert res["data"]["loggedPerson"]["mediaSize"] == 0
|
||||
|
||||
res = upload_picture(conn, user)
|
||||
assert res["data"]["uploadPicture"]["size"] == 10_097
|
||||
res = upload_media(conn, user)
|
||||
assert res["data"]["uploadMedia"]["size"] == 10_097
|
||||
|
||||
res =
|
||||
conn
|
||||
@@ -221,14 +221,14 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
assert res["data"]["loggedPerson"]["mediaSize"] == 10_097
|
||||
|
||||
res =
|
||||
upload_picture(
|
||||
upload_media(
|
||||
conn,
|
||||
user,
|
||||
"test/fixtures/image.jpg",
|
||||
Map.put(@default_picture_details, :file, "image.jpg")
|
||||
Map.put(@default_media_details, :file, "image.jpg")
|
||||
)
|
||||
|
||||
assert res["data"]["uploadPicture"]["size"] == 13_227
|
||||
assert res["data"]["uploadMedia"]["size"] == 13_227
|
||||
|
||||
res =
|
||||
conn
|
||||
@@ -266,7 +266,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
assert is_nil(res["errors"])
|
||||
assert hd(res["data"]["persons"]["elements"])["mediaSize"] == 0
|
||||
|
||||
upload_picture(conn, user)
|
||||
upload_media(conn, user)
|
||||
|
||||
res =
|
||||
conn
|
||||
@@ -355,8 +355,8 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
assert res["errors"] == nil
|
||||
assert res["data"]["loggedUser"]["mediaSize"] == 0
|
||||
|
||||
res = upload_picture(conn, user)
|
||||
assert res["data"]["uploadPicture"]["size"] == 10_097
|
||||
res = upload_media(conn, user)
|
||||
assert res["data"]["uploadMedia"]["size"] == 10_097
|
||||
|
||||
res =
|
||||
conn
|
||||
@@ -366,14 +366,14 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
assert res["data"]["loggedUser"]["mediaSize"] == 10_097
|
||||
|
||||
res =
|
||||
upload_picture(
|
||||
upload_media(
|
||||
conn,
|
||||
user,
|
||||
"test/fixtures/image.jpg",
|
||||
Map.put(@default_picture_details, :file, "image.jpg")
|
||||
Map.put(@default_media_details, :file, "image.jpg")
|
||||
)
|
||||
|
||||
assert res["data"]["uploadPicture"]["size"] == 13_227
|
||||
assert res["data"]["uploadMedia"]["size"] == 13_227
|
||||
|
||||
res =
|
||||
conn
|
||||
@@ -393,14 +393,14 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
assert is_nil(res["errors"])
|
||||
|
||||
res =
|
||||
upload_picture(
|
||||
upload_media(
|
||||
conn,
|
||||
user,
|
||||
"test/fixtures/image.jpg",
|
||||
Map.put(@default_picture_details, :file, "image.jpg")
|
||||
Map.put(@default_media_details, :file, "image.jpg")
|
||||
)
|
||||
|
||||
assert res["data"]["uploadPicture"]["size"] == 13_227
|
||||
assert res["data"]["uploadMedia"]["size"] == 13_227
|
||||
|
||||
res =
|
||||
conn
|
||||
@@ -438,9 +438,9 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
assert is_nil(res["errors"])
|
||||
assert hd(res["data"]["users"]["elements"])["mediaSize"] == 0
|
||||
|
||||
res = upload_picture(conn, user)
|
||||
res = upload_media(conn, user)
|
||||
assert is_nil(res["errors"])
|
||||
assert res["data"]["uploadPicture"]["size"] == 10_097
|
||||
assert res["data"]["uploadMedia"]["size"] == 10_097
|
||||
|
||||
res =
|
||||
conn
|
||||
@@ -463,19 +463,19 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
|
||||
end
|
||||
end
|
||||
|
||||
@spec upload_picture(Plug.Conn.t(), Mobilizon.Users.User.t(), String.t(), map()) :: map()
|
||||
defp upload_picture(
|
||||
@spec upload_media(Plug.Conn.t(), Mobilizon.Users.User.t(), String.t(), map()) :: map()
|
||||
defp upload_media(
|
||||
conn,
|
||||
user,
|
||||
picture_path \\ @default_picture_path,
|
||||
picture_details \\ @default_picture_details
|
||||
media_path \\ @default_media_path,
|
||||
media_details \\ @default_media_details
|
||||
) do
|
||||
map = %{
|
||||
"query" => @upload_picture_mutation,
|
||||
"variables" => picture_details,
|
||||
picture_details.file => %Plug.Upload{
|
||||
path: picture_path,
|
||||
filename: picture_details.file
|
||||
"query" => @upload_media_mutation,
|
||||
"variables" => media_details,
|
||||
media_details.file => %Plug.Upload{
|
||||
path: media_path,
|
||||
filename: media_details.file
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PersonTest do
|
||||
name: "secret person",
|
||||
summary: "no-one will know who I am",
|
||||
banner: {
|
||||
picture: {
|
||||
media: {
|
||||
file: "landscape.jpg",
|
||||
name: "irish landscape",
|
||||
alt: "The beautiful atlantic way"
|
||||
@@ -274,7 +274,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PersonTest do
|
||||
name: "riri updated",
|
||||
summary: "summary updated",
|
||||
banner: {
|
||||
picture: {
|
||||
media: {
|
||||
file: "landscape.jpg",
|
||||
name: "irish landscape",
|
||||
alt: "The beautiful atlantic way"
|
||||
|
||||
@@ -9,7 +9,7 @@ defmodule Mobilizon.ActorsTest do
|
||||
alias Mobilizon.Actors.{Actor, Bot, Follower, Member}
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Media.File, as: FileModel
|
||||
alias Mobilizon.Medias.File, as: FileModel
|
||||
alias Mobilizon.Service.Workers
|
||||
alias Mobilizon.Storage.Page
|
||||
|
||||
|
||||
@@ -58,7 +58,8 @@ defmodule Mobilizon.DiscussionsTest do
|
||||
%Comment{} = comment = insert(:comment)
|
||||
assert {:error, %Ecto.Changeset{}} = Discussions.update_comment(comment, @invalid_attrs)
|
||||
%Comment{} = comment_fetched = Discussions.get_comment!(comment.id)
|
||||
assert comment = comment_fetched
|
||||
assert comment.text == comment_fetched.text
|
||||
assert comment.url == comment_fetched.url
|
||||
end
|
||||
|
||||
test "delete_comment/1 deletes the comment" do
|
||||
|
||||
@@ -292,7 +292,7 @@ defmodule Mobilizon.EventsTest do
|
||||
tag1: %Tag{id: tag1_id} = tag1,
|
||||
tag2: %Tag{id: tag2_id} = tag2
|
||||
} do
|
||||
assert {:ok, %TagRelation{} = tag_relation} =
|
||||
assert {:ok, %TagRelation{}} =
|
||||
Events.create_tag_relation(%{tag_id: tag1_id, link_id: tag2_id})
|
||||
|
||||
assert Events.are_tags_linked(tag1, tag2)
|
||||
|
||||
@@ -3,13 +3,13 @@ defmodule Mobilizon.MediaTest do
|
||||
|
||||
import Mobilizon.Factory
|
||||
|
||||
alias Mobilizon.{Config, Media}
|
||||
alias Mobilizon.{Config, Medias}
|
||||
|
||||
alias Mobilizon.Web.Upload.Uploader
|
||||
|
||||
describe "media" do
|
||||
setup [:ensure_local_uploader]
|
||||
alias Mobilizon.Media.Picture
|
||||
alias Mobilizon.Medias.Media
|
||||
|
||||
@valid_attrs %{
|
||||
file: %{
|
||||
@@ -24,39 +24,42 @@ defmodule Mobilizon.MediaTest do
|
||||
}
|
||||
}
|
||||
|
||||
test "get_picture!/1 returns the picture with given id" do
|
||||
picture = insert(:picture)
|
||||
assert Media.get_picture!(picture.id).id == picture.id
|
||||
test "get_media!/1 returns the media with given id" do
|
||||
media = insert(:media)
|
||||
assert Medias.get_media!(media.id).id == media.id
|
||||
end
|
||||
|
||||
test "create_picture/1 with valid data creates a picture" do
|
||||
assert {:ok, %Picture{} = picture} =
|
||||
Media.create_picture(Map.put(@valid_attrs, :actor_id, insert(:actor).id))
|
||||
test "create_media/1 with valid data creates a media" do
|
||||
assert {:ok, %Media{} = media} =
|
||||
Medias.create_media(Map.put(@valid_attrs, :actor_id, insert(:actor).id))
|
||||
|
||||
assert picture.file.name == "something old"
|
||||
assert media.file.name == "something old"
|
||||
end
|
||||
|
||||
test "update_picture/2 with valid data updates the picture" do
|
||||
picture = insert(:picture)
|
||||
test "update_media/2 with valid data updates the media" do
|
||||
media = insert(:media)
|
||||
|
||||
assert {:ok, %Picture{} = picture} =
|
||||
Media.update_picture(picture, Map.put(@update_attrs, :actor_id, insert(:actor).id))
|
||||
assert {:ok, %Media{} = media} =
|
||||
Medias.update_media(
|
||||
media,
|
||||
Map.put(@update_attrs, :actor_id, insert(:actor).id)
|
||||
)
|
||||
|
||||
assert picture.file.name == "something new"
|
||||
assert media.file.name == "something new"
|
||||
end
|
||||
|
||||
test "delete_picture/1 deletes the picture" do
|
||||
picture = insert(:picture)
|
||||
test "delete_media/1 deletes the media" do
|
||||
media = insert(:media)
|
||||
|
||||
%URI{path: "/media/" <> path} = URI.parse(picture.file.url)
|
||||
%URI{path: "/media/" <> path} = URI.parse(media.file.url)
|
||||
|
||||
assert File.exists?(
|
||||
Config.get!([Uploader.Local, :uploads]) <>
|
||||
"/" <> path
|
||||
)
|
||||
|
||||
assert {:ok, %Picture{}} = Media.delete_picture(picture)
|
||||
assert_raise Ecto.NoResultsError, fn -> Media.get_picture!(picture.id) end
|
||||
assert {:ok, %Media{}} = Medias.delete_media(media)
|
||||
assert_raise Ecto.NoResultsError, fn -> Medias.get_media!(media.id) end
|
||||
|
||||
refute File.exists?(
|
||||
Config.get!([Uploader.Local, :uploads]) <>
|
||||
|
||||
@@ -70,7 +70,7 @@ defmodule Mobilizon.PostsTest do
|
||||
%Post{} = post = insert(:post)
|
||||
assert {:error, %Ecto.Changeset{}} = Posts.update_post(post, @invalid_attrs)
|
||||
%Post{} = post_fetched = Posts.get_post(post.id)
|
||||
assert post = post_fetched
|
||||
assert post.body == post_fetched.body
|
||||
end
|
||||
|
||||
test "delete_post/1 deletes the post" do
|
||||
|
||||
56
test/service/clean_orphan_media_test.exs
Normal file
56
test/service/clean_orphan_media_test.exs
Normal file
@@ -0,0 +1,56 @@
|
||||
defmodule Mix.Tasks.Mobilizon.Media.CleanOrphanTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
|
||||
alias Mobilizon.Medias
|
||||
alias Mobilizon.Medias.Media
|
||||
alias Mobilizon.Service.CleanOrphanMedia
|
||||
|
||||
describe "clean orphan media" do
|
||||
test "with default values" do
|
||||
{:ok, old, _} = DateTime.from_iso8601("2020-11-20T17:35:23+01:00")
|
||||
%Media{id: media_id} = insert(:media, inserted_at: old)
|
||||
%Media{id: media_2_id} = insert(:media)
|
||||
|
||||
refute is_nil(Medias.get_media(media_id))
|
||||
refute is_nil(Medias.get_media(media_2_id))
|
||||
|
||||
assert {:ok, [found_media]} = CleanOrphanMedia.clean()
|
||||
assert found_media.id == media_id
|
||||
|
||||
assert is_nil(Medias.get_media(media_id))
|
||||
refute is_nil(Medias.get_media(media_2_id))
|
||||
end
|
||||
|
||||
test "as dry-run" do
|
||||
{:ok, old, _} = DateTime.from_iso8601("2020-11-20T17:35:23+01:00")
|
||||
%Media{id: media_id} = insert(:media, inserted_at: old)
|
||||
%Media{id: media_2_id} = insert(:media)
|
||||
|
||||
refute is_nil(Medias.get_media(media_id))
|
||||
refute is_nil(Medias.get_media(media_2_id))
|
||||
|
||||
assert {:ok, [found_media]} = CleanOrphanMedia.clean(dry_run: true)
|
||||
assert found_media.id == media_id
|
||||
|
||||
refute is_nil(Medias.get_media(media_id))
|
||||
refute is_nil(Medias.get_media(media_2_id))
|
||||
end
|
||||
|
||||
test "with custom grace period" do
|
||||
date = DateTime.utc_now() |> DateTime.add(24 * -3600)
|
||||
%Media{id: media_id} = insert(:media, inserted_at: date)
|
||||
%Media{id: media_2_id} = insert(:media)
|
||||
|
||||
refute is_nil(Medias.get_media(media_id))
|
||||
refute is_nil(Medias.get_media(media_2_id))
|
||||
|
||||
assert {:ok, [found_media]} = CleanOrphanMedia.clean(grace_period: 12)
|
||||
assert found_media.id == media_id
|
||||
|
||||
assert is_nil(Medias.get_media(media_id))
|
||||
refute is_nil(Medias.get_media(media_2_id))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -148,6 +148,7 @@ defmodule Mobilizon.Factory do
|
||||
event: build(:event),
|
||||
uuid: uuid,
|
||||
mentions: [],
|
||||
media: [],
|
||||
attributed_to: nil,
|
||||
local: true,
|
||||
deleted_at: nil,
|
||||
@@ -179,13 +180,14 @@ defmodule Mobilizon.Factory do
|
||||
local: true,
|
||||
publish_at: DateTime.utc_now(),
|
||||
url: Routes.page_url(Endpoint, :event, uuid),
|
||||
picture: insert(:picture),
|
||||
picture: insert(:media),
|
||||
uuid: uuid,
|
||||
join_options: :free,
|
||||
options: %{},
|
||||
participant_stats: %{},
|
||||
status: :confirmed,
|
||||
contacts: []
|
||||
contacts: [],
|
||||
media: []
|
||||
}
|
||||
end
|
||||
|
||||
@@ -269,16 +271,16 @@ defmodule Mobilizon.Factory do
|
||||
size: 13_227
|
||||
} = data
|
||||
|
||||
%Mobilizon.Media.File{
|
||||
name: "My Picture",
|
||||
%Mobilizon.Medias.File{
|
||||
name: "My Media",
|
||||
url: url,
|
||||
content_type: "image/png",
|
||||
size: 13_120
|
||||
}
|
||||
end
|
||||
|
||||
def picture_factory do
|
||||
%Mobilizon.Media.Picture{
|
||||
def media_factory do
|
||||
%Mobilizon.Medias.Media{
|
||||
file: build(:file),
|
||||
actor: build(:actor)
|
||||
}
|
||||
@@ -372,6 +374,7 @@ defmodule Mobilizon.Factory do
|
||||
tags: build_list(3, :tag),
|
||||
visibility: :public,
|
||||
publish_at: DateTime.utc_now(),
|
||||
media: [],
|
||||
url: Routes.page_url(Endpoint, :post, uuid)
|
||||
}
|
||||
end
|
||||
|
||||
124
test/tasks/media/clean_orphan_test.exs
Normal file
124
test/tasks/media/clean_orphan_test.exs
Normal file
@@ -0,0 +1,124 @@
|
||||
defmodule Mix.Tasks.Mobilizon.Media.CleanOrphanTest do
|
||||
use Mobilizon.DataCase
|
||||
import Mock
|
||||
import Mobilizon.Factory
|
||||
|
||||
alias Mix.Tasks.Mobilizon.Media.CleanOrphan
|
||||
alias Mobilizon.Service.CleanOrphanMedia
|
||||
|
||||
Mix.shell(Mix.Shell.Process)
|
||||
|
||||
describe "with default options" do
|
||||
test "nothing returned" do
|
||||
with_mock CleanOrphanMedia, clean: fn [dry_run: false, grace_period: 48] -> {:ok, []} end do
|
||||
CleanOrphan.run([])
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "No files were deleted"
|
||||
end
|
||||
end
|
||||
|
||||
test "media returned" do
|
||||
media1 = insert(:media)
|
||||
media2 = insert(:media)
|
||||
|
||||
with_mock CleanOrphanMedia,
|
||||
clean: fn [dry_run: false, grace_period: 48] -> {:ok, [media1, media2]} end do
|
||||
CleanOrphan.run([])
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "2 files have been deleted"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "with dry-run option" do
|
||||
test "with nothing returned" do
|
||||
with_mock CleanOrphanMedia,
|
||||
clean: fn [dry_run: true, grace_period: 48] -> {:ok, []} end do
|
||||
CleanOrphan.run(["--dry-run"])
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "No files would have been deleted"
|
||||
end
|
||||
end
|
||||
|
||||
test "with media returned" do
|
||||
media1 = insert(:media)
|
||||
media2 = insert(:media)
|
||||
|
||||
with_mock CleanOrphanMedia,
|
||||
clean: fn [dry_run: true, grace_period: 48] -> {:ok, [media1, media2]} end do
|
||||
CleanOrphan.run(["--dry-run"])
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "List of files that would have been deleted"
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
|
||||
assert output_received ==
|
||||
"ID: #{media1.id}, Actor: #{media1.actor_id}, URL: #{media1.file.url}"
|
||||
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
|
||||
assert output_received ==
|
||||
"ID: #{media2.id}, Actor: #{media2.actor_id}, URL: #{media2.file.url}"
|
||||
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "2 files would have been deleted"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "with verbose option" do
|
||||
test "with nothing returned" do
|
||||
with_mock CleanOrphanMedia,
|
||||
clean: fn [dry_run: false, grace_period: 48] -> {:ok, []} end do
|
||||
CleanOrphan.run(["--verbose"])
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "No files were deleted"
|
||||
end
|
||||
end
|
||||
|
||||
test "with media returned" do
|
||||
media1 = insert(:media)
|
||||
media2 = insert(:media)
|
||||
|
||||
with_mock CleanOrphanMedia,
|
||||
clean: fn [dry_run: false, grace_period: 48] -> {:ok, [media1, media2]} end do
|
||||
CleanOrphan.run(["--verbose"])
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "List of files that have been deleted"
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
|
||||
assert output_received ==
|
||||
"ID: #{media1.id}, Actor: #{media1.actor_id}, URL: #{media1.file.url}"
|
||||
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
|
||||
assert output_received ==
|
||||
"ID: #{media2.id}, Actor: #{media2.actor_id}, URL: #{media2.file.url}"
|
||||
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "2 files have been deleted"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "with days option" do
|
||||
test "with nothing returned" do
|
||||
with_mock CleanOrphanMedia,
|
||||
clean: fn [dry_run: false, grace_period: 120] -> {:ok, []} end do
|
||||
CleanOrphan.run(["--days", "5"])
|
||||
assert_received {:mix_shell, :info, [output_received]}
|
||||
assert output_received == "No files were deleted"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "returns an error" do
|
||||
test "for some reason" do
|
||||
with_mock CleanOrphanMedia,
|
||||
clean: fn [dry_run: false, grace_period: 48] -> {:error, "Some error"} end do
|
||||
CleanOrphan.run([])
|
||||
assert_received {:mix_shell, :error, [output_received]}
|
||||
assert output_received == "Error while cleaning orphan media files"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user