From 4a7a34ae8c2ad12b2b9903c1d70bfe85d10af49e Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 26 Jun 2020 14:47:38 +0200 Subject: [PATCH 1/6] Preloading: Return correct data for statusnet stuff. --- lib/pleroma/web/preload/status_net.ex | 9 +++++---- test/web/preload/status_net_test.exs | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/preload/status_net.ex b/lib/pleroma/web/preload/status_net.ex index 367442d5c..810ad512b 100644 --- a/lib/pleroma/web/preload/status_net.ex +++ b/lib/pleroma/web/preload/status_net.ex @@ -4,7 +4,7 @@ defmodule Pleroma.Web.Preload.Providers.StatusNet do alias Pleroma.Web.Preload.Providers.Provider - alias Pleroma.Web.TwitterAPI.UtilView + alias Pleroma.Web.TwitterAPI.UtilController @behaviour Provider @config_url :"/api/statusnet/config.json" @@ -16,9 +16,10 @@ def generate_terms(_params) do end defp build_config_tag(acc) do - instance = Pleroma.Config.get(:instance) - info_data = UtilView.status_net_config(instance) + resp = + Plug.Test.conn(:get, @config_url |> to_string()) + |> UtilController.config(nil) - Map.put(acc, @config_url, info_data) + Map.put(acc, @config_url, resp.resp_body) end end diff --git a/test/web/preload/status_net_test.exs b/test/web/preload/status_net_test.exs index ab6823a7e..2cdc82930 100644 --- a/test/web/preload/status_net_test.exs +++ b/test/web/preload/status_net_test.exs @@ -9,6 +9,7 @@ defmodule Pleroma.Web.Preload.Providers.StatusNetTest do setup do: {:ok, StatusNet.generate_terms(nil)} test "it renders the info", %{"/api/statusnet/config.json": info} do - assert info =~ "Pleroma" + assert {:ok, res} = Jason.decode(info) + assert res["site"] end end From a2002ebb6393d53030d5fc565bae90f3fedd48a8 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 26 Jun 2020 14:48:23 +0200 Subject: [PATCH 2/6] Preloading: Fix nodeinfo url. --- lib/pleroma/web/preload/instance.ex | 2 +- test/web/preload/instance_test.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/preload/instance.ex b/lib/pleroma/web/preload/instance.ex index 0b6fd3313..3b95fe403 100644 --- a/lib/pleroma/web/preload/instance.ex +++ b/lib/pleroma/web/preload/instance.ex @@ -10,7 +10,7 @@ defmodule Pleroma.Web.Preload.Providers.Instance do @behaviour Provider @instance_url :"/api/v1/instance" @panel_url :"/instance/panel.html" - @nodeinfo_url :"/nodeinfo/2.0" + @nodeinfo_url :"/nodeinfo/2.0.json" @impl Provider def generate_terms(_params) do diff --git a/test/web/preload/instance_test.exs b/test/web/preload/instance_test.exs index 42a0d87bc..51b9dc549 100644 --- a/test/web/preload/instance_test.exs +++ b/test/web/preload/instance_test.exs @@ -25,7 +25,7 @@ test "it renders the panel", %{"/instance/panel.html": panel} do ) end - test "it renders the node_info", %{"/nodeinfo/2.0": nodeinfo} do + test "it renders the node_info", %{"/nodeinfo/2.0.json": nodeinfo} do %{ metadata: metadata, version: "2.0" From a19f8778afddb7f504b08cedde752e37da52dc96 Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 29 Jun 2020 11:06:20 +0200 Subject: [PATCH 3/6] User preloader: Put user info at correct key --- lib/pleroma/web/preload/user.ex | 11 ++++++----- test/web/preload/user_test.exs | 14 +++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/pleroma/web/preload/user.ex b/lib/pleroma/web/preload/user.ex index 3a244845b..7fef0a4ac 100644 --- a/lib/pleroma/web/preload/user.ex +++ b/lib/pleroma/web/preload/user.ex @@ -3,11 +3,12 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Preload.Providers.User do + alias Pleroma.User alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.Preload.Providers.Provider @behaviour Provider - @account_url :"/api/v1/accounts" + @account_url_base :"/api/v1/accounts" @impl Provider def generate_terms(%{user: user}) do @@ -16,10 +17,10 @@ def generate_terms(%{user: user}) do def generate_terms(_params), do: %{} - def build_accounts_tag(acc, nil), do: acc - - def build_accounts_tag(acc, user) do + def build_accounts_tag(acc, %User{} = user) do account_data = AccountView.render("show.json", %{user: user, for: user}) - Map.put(acc, @account_url, account_data) + Map.put(acc, :"#{@account_url_base}/#{user.id}", account_data) end + + def build_accounts_tag(acc, _), do: acc end diff --git a/test/web/preload/user_test.exs b/test/web/preload/user_test.exs index 99232cdfa..68d69d977 100644 --- a/test/web/preload/user_test.exs +++ b/test/web/preload/user_test.exs @@ -9,13 +9,11 @@ defmodule Pleroma.Web.Preload.Providers.UserTest do describe "returns empty when user doesn't exist" do test "nil user specified" do - refute User.generate_terms(%{user: nil}) - |> Map.has_key?("/api/v1/accounts") + assert User.generate_terms(%{user: nil}) == %{} end test "missing user specified" do - refute User.generate_terms(%{user: :not_a_user}) - |> Map.has_key?("/api/v1/accounts") + assert User.generate_terms(%{user: :not_a_user}) == %{} end end @@ -23,11 +21,13 @@ test "missing user specified" do setup do user = insert(:user) - {:ok, User.generate_terms(%{user: user})} + terms = User.generate_terms(%{user: user}) + %{terms: terms, user: user} end - test "account is rendered", %{"/api/v1/accounts": accounts} do - assert %{acct: user, username: user} = accounts + test "account is rendered", %{terms: terms, user: user} do + account = terms[:"/api/v1/accounts/#{user.id}"] + assert %{acct: user, username: user} = account end end end From 8630a6c7f52a68ab32025b1c80a6398599908c68 Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 29 Jun 2020 11:41:00 +0200 Subject: [PATCH 4/6] Preloaders: Use strings as keys. --- lib/pleroma/web/preload/instance.ex | 6 +++--- lib/pleroma/web/preload/status_net.ex | 2 +- lib/pleroma/web/preload/timelines.ex | 2 +- lib/pleroma/web/preload/user.ex | 4 ++-- test/web/preload/instance_test.exs | 6 +++--- test/web/preload/status_net_test.exs | 2 +- test/web/preload/timeline_test.exs | 2 +- test/web/preload/user_test.exs | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/pleroma/web/preload/instance.ex b/lib/pleroma/web/preload/instance.ex index 3b95fe403..b34d7cf37 100644 --- a/lib/pleroma/web/preload/instance.ex +++ b/lib/pleroma/web/preload/instance.ex @@ -8,9 +8,9 @@ defmodule Pleroma.Web.Preload.Providers.Instance do alias Pleroma.Web.Preload.Providers.Provider @behaviour Provider - @instance_url :"/api/v1/instance" - @panel_url :"/instance/panel.html" - @nodeinfo_url :"/nodeinfo/2.0.json" + @instance_url "/api/v1/instance" + @panel_url "/instance/panel.html" + @nodeinfo_url "/nodeinfo/2.0.json" @impl Provider def generate_terms(_params) do diff --git a/lib/pleroma/web/preload/status_net.ex b/lib/pleroma/web/preload/status_net.ex index 810ad512b..9b62f87a2 100644 --- a/lib/pleroma/web/preload/status_net.ex +++ b/lib/pleroma/web/preload/status_net.ex @@ -7,7 +7,7 @@ defmodule Pleroma.Web.Preload.Providers.StatusNet do alias Pleroma.Web.TwitterAPI.UtilController @behaviour Provider - @config_url :"/api/statusnet/config.json" + @config_url "/api/statusnet/config.json" @impl Provider def generate_terms(_params) do diff --git a/lib/pleroma/web/preload/timelines.ex b/lib/pleroma/web/preload/timelines.ex index e531b8960..57de04051 100644 --- a/lib/pleroma/web/preload/timelines.ex +++ b/lib/pleroma/web/preload/timelines.ex @@ -8,7 +8,7 @@ defmodule Pleroma.Web.Preload.Providers.Timelines do alias Pleroma.Web.Preload.Providers.Provider @behaviour Provider - @public_url :"/api/v1/timelines/public" + @public_url "/api/v1/timelines/public" @impl Provider def generate_terms(params) do diff --git a/lib/pleroma/web/preload/user.ex b/lib/pleroma/web/preload/user.ex index 7fef0a4ac..b3d2e9b8d 100644 --- a/lib/pleroma/web/preload/user.ex +++ b/lib/pleroma/web/preload/user.ex @@ -8,7 +8,7 @@ defmodule Pleroma.Web.Preload.Providers.User do alias Pleroma.Web.Preload.Providers.Provider @behaviour Provider - @account_url_base :"/api/v1/accounts" + @account_url_base "/api/v1/accounts" @impl Provider def generate_terms(%{user: user}) do @@ -19,7 +19,7 @@ def generate_terms(_params), do: %{} def build_accounts_tag(acc, %User{} = user) do account_data = AccountView.render("show.json", %{user: user, for: user}) - Map.put(acc, :"#{@account_url_base}/#{user.id}", account_data) + Map.put(acc, "#{@account_url_base}/#{user.id}", account_data) end def build_accounts_tag(acc, _), do: acc diff --git a/test/web/preload/instance_test.exs b/test/web/preload/instance_test.exs index 51b9dc549..5bb6c5981 100644 --- a/test/web/preload/instance_test.exs +++ b/test/web/preload/instance_test.exs @@ -8,7 +8,7 @@ defmodule Pleroma.Web.Preload.Providers.InstanceTest do setup do: {:ok, Instance.generate_terms(nil)} - test "it renders the info", %{"/api/v1/instance": info} do + test "it renders the info", %{"/api/v1/instance" => info} do assert %{ description: description, email: "admin@example.com", @@ -18,14 +18,14 @@ test "it renders the info", %{"/api/v1/instance": info} do assert String.equivalent?(description, "Pleroma: An efficient and flexible fediverse server") end - test "it renders the panel", %{"/instance/panel.html": panel} do + test "it renders the panel", %{"/instance/panel.html" => panel} do assert String.contains?( panel, "

Welcome to Pleroma!

" ) end - test "it renders the node_info", %{"/nodeinfo/2.0.json": nodeinfo} do + test "it renders the node_info", %{"/nodeinfo/2.0.json" => nodeinfo} do %{ metadata: metadata, version: "2.0" diff --git a/test/web/preload/status_net_test.exs b/test/web/preload/status_net_test.exs index 2cdc82930..df7acdb11 100644 --- a/test/web/preload/status_net_test.exs +++ b/test/web/preload/status_net_test.exs @@ -8,7 +8,7 @@ defmodule Pleroma.Web.Preload.Providers.StatusNetTest do setup do: {:ok, StatusNet.generate_terms(nil)} - test "it renders the info", %{"/api/statusnet/config.json": info} do + test "it renders the info", %{"/api/statusnet/config.json" => info} do assert {:ok, res} = Jason.decode(info) assert res["site"] end diff --git a/test/web/preload/timeline_test.exs b/test/web/preload/timeline_test.exs index da6a3aded..fea95a6a4 100644 --- a/test/web/preload/timeline_test.exs +++ b/test/web/preload/timeline_test.exs @@ -9,7 +9,7 @@ defmodule Pleroma.Web.Preload.Providers.TimelineTest do alias Pleroma.Web.CommonAPI alias Pleroma.Web.Preload.Providers.Timelines - @public_url :"/api/v1/timelines/public" + @public_url "/api/v1/timelines/public" describe "unauthenticated timeliness when restricted" do setup do diff --git a/test/web/preload/user_test.exs b/test/web/preload/user_test.exs index 68d69d977..83f065e27 100644 --- a/test/web/preload/user_test.exs +++ b/test/web/preload/user_test.exs @@ -26,7 +26,7 @@ test "missing user specified" do end test "account is rendered", %{terms: terms, user: user} do - account = terms[:"/api/v1/accounts/#{user.id}"] + account = terms["/api/v1/accounts/#{user.id}"] assert %{acct: user, username: user} = account end end From 2382a2a1511e1042d960946aacfde7a49fac9dd0 Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 30 Jun 2020 11:35:54 +0200 Subject: [PATCH 5/6] Preload: Load the correct instance panel --- lib/pleroma/web/preload/instance.ex | 3 ++- test/fixtures/preload_static/instance/panel.html | 1 + test/web/preload/instance_test.exs | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/preload_static/instance/panel.html diff --git a/lib/pleroma/web/preload/instance.ex b/lib/pleroma/web/preload/instance.ex index 0b6fd3313..5c6e33e47 100644 --- a/lib/pleroma/web/preload/instance.ex +++ b/lib/pleroma/web/preload/instance.ex @@ -6,6 +6,7 @@ defmodule Pleroma.Web.Preload.Providers.Instance do alias Pleroma.Web.MastodonAPI.InstanceView alias Pleroma.Web.Nodeinfo.Nodeinfo alias Pleroma.Web.Preload.Providers.Provider + alias Pleroma.Plugs.InstanceStatic @behaviour Provider @instance_url :"/api/v1/instance" @@ -27,7 +28,7 @@ defp build_info_tag(acc) do end defp build_panel_tag(acc) do - instance_path = Path.join(:code.priv_dir(:pleroma), "static/instance/panel.html") + instance_path = InstanceStatic.file_path(@panel_url |> to_string()) if File.exists?(instance_path) do panel_data = File.read!(instance_path) diff --git a/test/fixtures/preload_static/instance/panel.html b/test/fixtures/preload_static/instance/panel.html new file mode 100644 index 000000000..fc58e4e93 --- /dev/null +++ b/test/fixtures/preload_static/instance/panel.html @@ -0,0 +1 @@ +HEY! diff --git a/test/web/preload/instance_test.exs b/test/web/preload/instance_test.exs index 42a0d87bc..df150d7be 100644 --- a/test/web/preload/instance_test.exs +++ b/test/web/preload/instance_test.exs @@ -25,6 +25,17 @@ test "it renders the panel", %{"/instance/panel.html": panel} do ) end + test "it works with overrides" do + clear_config([:instance, :static_dir], "test/fixtures/preload_static") + + %{"/instance/panel.html": panel} = Instance.generate_terms(nil) + + assert String.contains?( + panel, + "HEY!" + ) + end + test "it renders the node_info", %{"/nodeinfo/2.0": nodeinfo} do %{ metadata: metadata, From 8b7055e25e76565cd3376c0b5dda5e54d24881f0 Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 30 Jun 2020 11:55:58 +0200 Subject: [PATCH 6/6] Credo fixes --- lib/pleroma/web/preload/instance.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/preload/instance.ex b/lib/pleroma/web/preload/instance.ex index 3d16f290b..50d1f3382 100644 --- a/lib/pleroma/web/preload/instance.ex +++ b/lib/pleroma/web/preload/instance.ex @@ -3,10 +3,10 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Preload.Providers.Instance do + alias Pleroma.Plugs.InstanceStatic alias Pleroma.Web.MastodonAPI.InstanceView alias Pleroma.Web.Nodeinfo.Nodeinfo alias Pleroma.Web.Preload.Providers.Provider - alias Pleroma.Plugs.InstanceStatic @behaviour Provider @instance_url "/api/v1/instance"