AttachmentValidator: Actually require url

This commit is contained in:
Lain Soykaf 2022-12-15 17:46:20 -05:00
parent 63d00f8123
commit 9838790a7d
2 changed files with 16 additions and 3 deletions

View file

@ -45,9 +45,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
struct struct
|> cast(data, [:id, :type, :mediaType, :name, :blurhash]) |> cast(data, [:id, :type, :mediaType, :name, :blurhash])
|> cast_embed(:url, with: &url_changeset/2) |> cast_embed(:url, with: &url_changeset/2, required: true)
|> validate_inclusion(:type, ~w[Link Document Audio Image Video]) |> validate_inclusion(:type, ~w[Link Document Audio Image Video])
|> validate_required([:type, :mediaType, :url]) |> validate_required([:type, :mediaType])
end end
def url_changeset(struct, data) do def url_changeset(struct, data) do
@ -91,6 +91,6 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
defp validate_data(cng) do defp validate_data(cng) do
cng cng
|> validate_inclusion(:type, ~w[Document Audio Image Video]) |> validate_inclusion(:type, ~w[Document Audio Image Video])
|> validate_required([:mediaType, :url, :type]) |> validate_required([:mediaType, :type])
end end
end end

View file

@ -11,6 +11,19 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do
import Pleroma.Factory import Pleroma.Factory
describe "attachments" do describe "attachments" do
test "fails without url" do
attachment = %{
"mediaType" => "",
"name" => "",
"summary" => "298p3RG7j27tfsZ9RQ.jpg",
"type" => "Document"
}
assert {:error, _cng} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
end
test "works with honkerific attachments" do test "works with honkerific attachments" do
attachment = %{ attachment = %{
"mediaType" => "", "mediaType" => "",