Fix existing tests

This commit is contained in:
Alex Gleason 2021-05-04 18:21:43 -05:00
parent 0afa091e6c
commit 3a4ad366d5
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 33 additions and 25 deletions

View file

@ -24,8 +24,10 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do
embed_url: "", embed_url: "",
blurhash: nil blurhash: nil
def parse(%{url: url, oembed: %{"type" => type, "title" => title} = oembed} = embed) def parse(%Embed{url: url, oembed: %{"type" => type, "title" => title} = oembed} = embed)
when type in @types do when type in @types and is_binary(url) do
uri = URI.parse(url)
%Card{ %Card{
url: url, url: url,
title: title, title: title,
@ -33,8 +35,8 @@ def parse(%{url: url, oembed: %{"type" => type, "title" => title} = oembed} = em
type: oembed["type"], type: oembed["type"],
author_name: oembed["author_name"], author_name: oembed["author_name"],
author_url: oembed["author_url"], author_url: oembed["author_url"],
provider_name: oembed["provider_name"] || URI.parse(url).host, provider_name: oembed["provider_name"] || uri.host,
provider_url: oembed["provider_url"], provider_url: oembed["provider_url"] || "#{uri.scheme}://#{uri.host}",
html: oembed["html"], html: oembed["html"],
width: oembed["width"], width: oembed["width"],
height: oembed["height"], height: oembed["height"],
@ -44,13 +46,16 @@ def parse(%{url: url, oembed: %{"type" => type, "title" => title} = oembed} = em
|> validate() |> validate()
end end
def parse(%{url: url} = embed) do def parse(%Embed{url: url} = embed) when is_binary(url) do
uri = URI.parse(url)
%Card{ %Card{
url: url, url: url,
title: get_title(embed), title: get_title(embed),
description: get_description(embed), description: get_description(embed),
type: "link", type: "link",
provider_name: URI.parse(url).host, provider_name: uri.host,
provider_url: "#{uri.scheme}://#{uri.host}",
image: get_image(embed) |> proxy() image: get_image(embed) |> proxy()
} }
|> validate() |> validate()

View file

@ -1331,16 +1331,13 @@ test "returns rich-media card", %{conn: conn, user: user} do
"url" => "https://example.com/ogp", "url" => "https://example.com/ogp",
"description" => "description" =>
"Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.", "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
"pleroma" => %{ "author_name" => "",
"opengraph" => %{ "author_url" => "",
"image" => "http://ia.media-imdb.com/images/rock.jpg", "blurhash" => nil,
"title" => "The Rock", "embed_url" => "",
"type" => "video.movie", "height" => 0,
"url" => "https://example.com/ogp", "html" => "",
"description" => "width" => 0
"Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
}
}
} }
response = response =
@ -1380,13 +1377,13 @@ test "replaces missing description with an empty string", %{conn: conn, user: us
"provider_name" => "example.com", "provider_name" => "example.com",
"provider_url" => "https://example.com", "provider_url" => "https://example.com",
"url" => "https://example.com/ogp-missing-data", "url" => "https://example.com/ogp-missing-data",
"pleroma" => %{ "author_name" => "",
"opengraph" => %{ "author_url" => "",
"title" => "Pleroma", "blurhash" => nil,
"type" => "website", "embed_url" => "",
"url" => "https://example.com/ogp-missing-data" "height" => 0,
} "html" => "",
} "width" => 0
} }
end end
end end

View file

@ -10,10 +10,13 @@ defmodule Pleroma.Web.RichMedia.Parser.CardTest do
describe "parse/1" do describe "parse/1" do
test "converts an %Embed{} into a %Card{}" 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 = embed =
File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html") File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html")
|> Floki.parse_document!() |> Floki.parse_document!()
|> TwitterCard.parse(%Embed{}) |> TwitterCard.parse(%Embed{url: url})
expected = %Card{ expected = %Card{
description: description:
@ -21,7 +24,10 @@ test "converts an %Embed{} into a %Card{}" do
image: image:
"https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg", "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.", 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} assert Card.parse(embed) == {:ok, expected}