Merge branch 'load-all-dms' into 'develop'
[akkoma] / test / web / rich_media / controllers / rich_media_controller_test.exs
1 defmodule Pleroma.Web.RichMedia.RichMediaControllerTest do
2 use Pleroma.Web.ConnCase
3 import Pleroma.Factory
4
5 setup do
6 Tesla.Mock.mock(fn
7 %{
8 method: :get,
9 url: "http://example.com/ogp"
10 } ->
11 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
12
13 %{method: :get, url: "http://example.com/empty"} ->
14 %Tesla.Env{status: 200, body: "hello"}
15 end)
16
17 :ok
18 end
19
20 describe "GET /api/rich_media/parse" do
21 setup do
22 user = insert(:user)
23
24 [user: user]
25 end
26
27 test "returns 404 if not metadata found", %{user: user} do
28 build_conn()
29 |> with_credentials(user.nickname, "test")
30 |> get("/api/rich_media/parse", %{"url" => "http://example.com/empty"})
31 |> json_response(404)
32 end
33
34 test "returns OGP metadata", %{user: user} do
35 response =
36 build_conn()
37 |> with_credentials(user.nickname, "test")
38 |> get("/api/rich_media/parse", %{"url" => "http://example.com/ogp"})
39 |> json_response(200)
40
41 assert response == %{
42 "image" => "http://ia.media-imdb.com/images/rock.jpg",
43 "title" => "The Rock",
44 "type" => "video.movie",
45 "url" => "http://www.imdb.com/title/tt0117500/"
46 }
47 end
48 end
49
50 defp with_credentials(conn, username, password) do
51 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
52 put_req_header(conn, "authorization", header_content)
53 end
54 end