Fix existing tests
This commit is contained in:
parent
0afa091e6c
commit
3a4ad366d5
3 changed files with 33 additions and 25 deletions
|
@ -24,8 +24,10 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do
|
|||
embed_url: "",
|
||||
blurhash: nil
|
||||
|
||||
def parse(%{url: url, oembed: %{"type" => type, "title" => title} = oembed} = embed)
|
||||
when type in @types do
|
||||
def parse(%Embed{url: url, oembed: %{"type" => type, "title" => title} = oembed} = embed)
|
||||
when type in @types and is_binary(url) do
|
||||
uri = URI.parse(url)
|
||||
|
||||
%Card{
|
||||
url: url,
|
||||
title: title,
|
||||
|
@ -33,8 +35,8 @@ def parse(%{url: url, oembed: %{"type" => type, "title" => title} = oembed} = em
|
|||
type: oembed["type"],
|
||||
author_name: oembed["author_name"],
|
||||
author_url: oembed["author_url"],
|
||||
provider_name: oembed["provider_name"] || URI.parse(url).host,
|
||||
provider_url: oembed["provider_url"],
|
||||
provider_name: oembed["provider_name"] || uri.host,
|
||||
provider_url: oembed["provider_url"] || "#{uri.scheme}://#{uri.host}",
|
||||
html: oembed["html"],
|
||||
width: oembed["width"],
|
||||
height: oembed["height"],
|
||||
|
@ -44,13 +46,16 @@ def parse(%{url: url, oembed: %{"type" => type, "title" => title} = oembed} = em
|
|||
|> validate()
|
||||
end
|
||||
|
||||
def parse(%{url: url} = embed) do
|
||||
def parse(%Embed{url: url} = embed) when is_binary(url) do
|
||||
uri = URI.parse(url)
|
||||
|
||||
%Card{
|
||||
url: url,
|
||||
title: get_title(embed),
|
||||
description: get_description(embed),
|
||||
type: "link",
|
||||
provider_name: URI.parse(url).host,
|
||||
provider_name: uri.host,
|
||||
provider_url: "#{uri.scheme}://#{uri.host}",
|
||||
image: get_image(embed) |> proxy()
|
||||
}
|
||||
|> validate()
|
||||
|
|
|
@ -1331,16 +1331,13 @@ test "returns rich-media card", %{conn: conn, user: user} do
|
|||
"url" => "https://example.com/ogp",
|
||||
"description" =>
|
||||
"Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
|
||||
"pleroma" => %{
|
||||
"opengraph" => %{
|
||||
"image" => "http://ia.media-imdb.com/images/rock.jpg",
|
||||
"title" => "The Rock",
|
||||
"type" => "video.movie",
|
||||
"url" => "https://example.com/ogp",
|
||||
"description" =>
|
||||
"Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
|
||||
}
|
||||
}
|
||||
"author_name" => "",
|
||||
"author_url" => "",
|
||||
"blurhash" => nil,
|
||||
"embed_url" => "",
|
||||
"height" => 0,
|
||||
"html" => "",
|
||||
"width" => 0
|
||||
}
|
||||
|
||||
response =
|
||||
|
@ -1380,13 +1377,13 @@ test "replaces missing description with an empty string", %{conn: conn, user: us
|
|||
"provider_name" => "example.com",
|
||||
"provider_url" => "https://example.com",
|
||||
"url" => "https://example.com/ogp-missing-data",
|
||||
"pleroma" => %{
|
||||
"opengraph" => %{
|
||||
"title" => "Pleroma",
|
||||
"type" => "website",
|
||||
"url" => "https://example.com/ogp-missing-data"
|
||||
}
|
||||
}
|
||||
"author_name" => "",
|
||||
"author_url" => "",
|
||||
"blurhash" => nil,
|
||||
"embed_url" => "",
|
||||
"height" => 0,
|
||||
"html" => "",
|
||||
"width" => 0
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,10 +10,13 @@ defmodule Pleroma.Web.RichMedia.Parser.CardTest do
|
|||
|
||||
describe "parse/1" do
|
||||
test "converts an %Embed{} into a %Card{}" do
|
||||
url =
|
||||
"https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html"
|
||||
|
||||
embed =
|
||||
File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html")
|
||||
|> Floki.parse_document!()
|
||||
|> TwitterCard.parse(%Embed{})
|
||||
|> TwitterCard.parse(%Embed{url: url})
|
||||
|
||||
expected = %Card{
|
||||
description:
|
||||
|
@ -21,7 +24,10 @@ test "converts an %Embed{} into a %Card{}" do
|
|||
image:
|
||||
"https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg",
|
||||
title: "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
|
||||
type: "link"
|
||||
type: "link",
|
||||
provider_name: "www.nytimes.com",
|
||||
provider_url: "https://www.nytimes.com",
|
||||
url: url
|
||||
}
|
||||
|
||||
assert Card.parse(embed) == {:ok, expected}
|
||||
|
|
Loading…
Reference in a new issue