2019-05-22 14:12:11 +02:00
|
|
|
defmodule Mobilizon.MediaTest do
|
|
|
|
use Mobilizon.DataCase
|
|
|
|
|
|
|
|
import Mobilizon.Factory
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.{Config, Medias}
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.Upload.Uploader
|
2020-01-23 21:59:50 +01:00
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
describe "media" do
|
2019-06-05 18:29:39 +02:00
|
|
|
setup [:ensure_local_uploader]
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Medias.Media
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
|
|
@valid_attrs %{
|
|
|
|
file: %{
|
|
|
|
url: "https://something.tld/media/something",
|
|
|
|
name: "something old"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@update_attrs %{
|
|
|
|
file: %{
|
|
|
|
url: "https://something.tld/media/something_updated",
|
|
|
|
name: "something new"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
test "get_media!/1 returns the media with given id" do
|
|
|
|
media = insert(:media)
|
|
|
|
assert Medias.get_media!(media.id).id == media.id
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
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))
|
2019-05-31 17:58:03 +02:00
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
assert media.file.name == "something old"
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
test "update_media/2 with valid data updates the media" do
|
|
|
|
media = insert(:media)
|
2019-05-31 17:58:03 +02:00
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
assert {:ok, %Media{} = media} =
|
|
|
|
Medias.update_media(
|
|
|
|
media,
|
|
|
|
Map.put(@update_attrs, :actor_id, insert(:actor).id)
|
|
|
|
)
|
2019-05-31 17:58:03 +02:00
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
assert media.file.name == "something new"
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
test "delete_media/1 deletes the media" do
|
|
|
|
media = insert(:media)
|
2019-06-05 18:29:39 +02:00
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
%URI{path: "/media/" <> path} = URI.parse(media.file.url)
|
2019-06-05 18:29:39 +02:00
|
|
|
|
|
|
|
assert File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> path
|
|
|
|
)
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
assert {:ok, %Media{}} = Medias.delete_media(media)
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> Medias.get_media!(media.id) end
|
2019-06-05 18:29:39 +02:00
|
|
|
|
|
|
|
refute File.exists?(
|
2020-01-23 21:59:50 +01:00
|
|
|
Config.get!([Uploader.Local, :uploads]) <>
|
2019-06-05 18:29:39 +02:00
|
|
|
"/" <> path
|
|
|
|
)
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|