Merge branch 'develop' into 'hide-muted-reactions'

# Conflicts:
#   CHANGELOG.md
This commit is contained in:
minibikini 2020-11-18 20:22:40 +00:00
commit 6669ac5bf7
113 changed files with 790 additions and 1639 deletions

View file

@ -0,0 +1,72 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.FrontendTest do
use Pleroma.DataCase
alias Pleroma.Frontend
@dir "test/frontend_static_test"
setup do
File.mkdir_p!(@dir)
clear_config([:instance, :static_dir], @dir)
on_exit(fn ->
File.rm_rf(@dir)
end)
end
test "it downloads and unzips a known frontend" do
clear_config([:frontends, :available], %{
"pleroma" => %{
"ref" => "fantasy",
"name" => "pleroma",
"build_url" => "http://gensokyo.2hu/builds/${ref}"
}
})
Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")}
end)
Frontend.install("pleroma")
assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
end
test "it also works given a file" do
clear_config([:frontends, :available], %{
"pleroma" => %{
"ref" => "fantasy",
"name" => "pleroma",
"build_dir" => ""
}
})
folder = Path.join([@dir, "frontends", "pleroma", "fantasy"])
previously_existing = Path.join([folder, "temp"])
File.mkdir_p!(folder)
File.write!(previously_existing, "yey")
assert File.exists?(previously_existing)
Frontend.install("pleroma", file: "test/fixtures/tesla_mock/frontend.zip")
assert File.exists?(Path.join([folder, "test.txt"]))
refute File.exists?(previously_existing)
end
test "it downloads and unzips unknown frontends" do
Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")}
end)
Frontend.install("unknown",
ref: "baka",
build_url: "http://gensokyo.2hu/madeup.zip",
build_dir: ""
)
assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
end
end

View file

@ -0,0 +1,141 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do
use Pleroma.Web.ConnCase
import Pleroma.Factory
alias Pleroma.Config
@dir "test/frontend_static_test"
setup do
clear_config([:instance, :static_dir], @dir)
File.mkdir_p!(Pleroma.Frontend.dir())
on_exit(fn ->
File.rm_rf(@dir)
end)
admin = insert(:user, is_admin: true)
token = insert(:oauth_admin_token, user: admin)
conn =
build_conn()
|> assign(:user, admin)
|> assign(:token, token)
{:ok, %{admin: admin, token: token, conn: conn}}
end
describe "GET /api/pleroma/admin/frontends" do
test "it lists available frontends", %{conn: conn} do
response =
conn
|> get("/api/pleroma/admin/frontends")
|> json_response_and_validate_schema(:ok)
assert Enum.map(response, & &1["name"]) ==
Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
end
end
describe "POST /api/pleroma/admin/frontends/install" do
test "from available frontends", %{conn: conn} do
clear_config([:frontends, :available], %{
"pleroma" => %{
"ref" => "fantasy",
"name" => "pleroma",
"build_url" => "http://gensokyo.2hu/builds/${ref}"
}
})
Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")}
end)
conn
|> put_req_header("content-type", "application/json")
|> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"})
|> json_response_and_validate_schema(:ok)
assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
response =
conn
|> get("/api/pleroma/admin/frontends")
|> json_response_and_validate_schema(:ok)
assert response == [
%{
"build_url" => "http://gensokyo.2hu/builds/${ref}",
"git" => nil,
"installed" => true,
"name" => "pleroma",
"ref" => "fantasy"
}
]
end
test "from a file", %{conn: conn} do
clear_config([:frontends, :available], %{
"pleroma" => %{
"ref" => "fantasy",
"name" => "pleroma",
"build_dir" => ""
}
})
conn
|> put_req_header("content-type", "application/json")
|> post("/api/pleroma/admin/frontends/install", %{
name: "pleroma",
file: "test/fixtures/tesla_mock/frontend.zip"
})
|> json_response_and_validate_schema(:ok)
assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
end
test "from an URL", %{conn: conn} do
Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")}
end)
conn
|> put_req_header("content-type", "application/json")
|> post("/api/pleroma/admin/frontends/install", %{
name: "unknown",
ref: "baka",
build_url: "http://gensokyo.2hu/madeup.zip",
build_dir: ""
})
|> json_response_and_validate_schema(:ok)
assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
end
test "failing returns an error", %{conn: conn} do
Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
%Tesla.Env{status: 404, body: ""}
end)
result =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/pleroma/admin/frontends/install", %{
name: "unknown",
ref: "baka",
build_url: "http://gensokyo.2hu/madeup.zip",
build_dir: ""
})
|> json_response_and_validate_schema(400)
assert result == %{"error" => "Could not download or unzip the frontend"}
end
end
end

View file

@ -1,124 +0,0 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.FedSockets.FedRegistryTest do
use ExUnit.Case
alias Pleroma.Web.FedSockets
alias Pleroma.Web.FedSockets.FedRegistry
alias Pleroma.Web.FedSockets.SocketInfo
@good_domain "http://good.domain"
@good_domain_origin "good.domain:80"
setup do
start_supervised({Pleroma.Web.FedSockets.Supervisor, []})
build_test_socket(@good_domain)
Process.sleep(10)
:ok
end
describe "add_fed_socket/1 without conflicting sockets" do
test "can be added" do
Process.sleep(10)
assert {:ok, %SocketInfo{origin: origin}} = FedRegistry.get_fed_socket(@good_domain_origin)
assert origin == "good.domain:80"
end
test "multiple origins can be added" do
build_test_socket("http://anothergood.domain")
Process.sleep(10)
assert {:ok, %SocketInfo{origin: origin_1}} =
FedRegistry.get_fed_socket(@good_domain_origin)
assert {:ok, %SocketInfo{origin: origin_2}} =
FedRegistry.get_fed_socket("anothergood.domain:80")
assert origin_1 == "good.domain:80"
assert origin_2 == "anothergood.domain:80"
assert FedRegistry.list_all() |> Enum.count() == 2
end
end
describe "add_fed_socket/1 when duplicate sockets conflict" do
setup do
build_test_socket(@good_domain)
build_test_socket(@good_domain)
Process.sleep(10)
:ok
end
test "will be ignored" do
assert {:ok, %SocketInfo{origin: origin, pid: _pid_one}} =
FedRegistry.get_fed_socket(@good_domain_origin)
assert origin == "good.domain:80"
assert FedRegistry.list_all() |> Enum.count() == 1
end
test "the newer process will be closed" do
pid_two = build_test_socket(@good_domain)
assert {:ok, %SocketInfo{origin: origin, pid: _pid_one}} =
FedRegistry.get_fed_socket(@good_domain_origin)
assert origin == "good.domain:80"
Process.sleep(10)
refute Process.alive?(pid_two)
assert FedRegistry.list_all() |> Enum.count() == 1
end
end
describe "get_fed_socket/1" do
test "returns missing for unknown hosts" do
assert {:error, :missing} = FedRegistry.get_fed_socket("not_a_dmoain")
end
test "returns rejected for hosts previously rejected" do
"rejected.domain:80"
|> FedSockets.uri_for_origin()
|> FedRegistry.set_host_rejected()
assert {:error, :rejected} = FedRegistry.get_fed_socket("rejected.domain:80")
end
test "can retrieve a previously added SocketInfo" do
build_test_socket(@good_domain)
Process.sleep(10)
assert {:ok, %SocketInfo{origin: origin}} = FedRegistry.get_fed_socket(@good_domain_origin)
assert origin == "good.domain:80"
end
test "removes references to SocketInfos when the process crashes" do
assert {:ok, %SocketInfo{origin: origin, pid: pid}} =
FedRegistry.get_fed_socket(@good_domain_origin)
assert origin == "good.domain:80"
Process.exit(pid, :testing)
Process.sleep(100)
assert {:error, :missing} = FedRegistry.get_fed_socket(@good_domain_origin)
end
end
def build_test_socket(uri) do
Kernel.spawn(fn -> fed_socket_almost(uri) end)
end
def fed_socket_almost(origin) do
FedRegistry.add_fed_socket(origin)
receive do
:close ->
:ok
after
5_000 -> :timeout
end
end
end

View file

@ -1,67 +0,0 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.FedSockets.FetchRegistryTest do
use ExUnit.Case
alias Pleroma.Web.FedSockets.FetchRegistry
alias Pleroma.Web.FedSockets.FetchRegistry.FetchRegistryData
@json_message "hello"
@json_reply "hello back"
setup do
start_supervised(
{Pleroma.Web.FedSockets.Supervisor,
[
ping_interval: 8,
connection_duration: 15,
rejection_duration: 5,
fed_socket_fetches: [default: 10, interval: 10]
]}
)
:ok
end
test "fetches can be stored" do
uuid = FetchRegistry.register_fetch(@json_message)
assert {:error, :waiting} = FetchRegistry.check_fetch(uuid)
end
test "fetches can return" do
uuid = FetchRegistry.register_fetch(@json_message)
task = Task.async(fn -> FetchRegistry.register_fetch_received(uuid, @json_reply) end)
assert {:error, :waiting} = FetchRegistry.check_fetch(uuid)
Task.await(task)
assert {:ok, %FetchRegistryData{received_json: received_json}} =
FetchRegistry.check_fetch(uuid)
assert received_json == @json_reply
end
test "fetches are deleted once popped from stack" do
uuid = FetchRegistry.register_fetch(@json_message)
task = Task.async(fn -> FetchRegistry.register_fetch_received(uuid, @json_reply) end)
Task.await(task)
assert {:ok, %FetchRegistryData{received_json: received_json}} =
FetchRegistry.check_fetch(uuid)
assert received_json == @json_reply
assert {:ok, @json_reply} = FetchRegistry.pop_fetch(uuid)
assert {:error, :missing} = FetchRegistry.check_fetch(uuid)
end
test "fetches can time out" do
uuid = FetchRegistry.register_fetch(@json_message)
assert {:error, :waiting} = FetchRegistry.check_fetch(uuid)
Process.sleep(500)
assert {:error, :missing} = FetchRegistry.check_fetch(uuid)
end
end

View file

@ -1,118 +0,0 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.FedSockets.SocketInfoTest do
use ExUnit.Case
alias Pleroma.Web.FedSockets
alias Pleroma.Web.FedSockets.SocketInfo
describe "uri_for_origin" do
test "provides the fed_socket URL given the origin information" do
endpoint = "example.com:4000"
assert FedSockets.uri_for_origin(endpoint) =~ "ws://"
assert FedSockets.uri_for_origin(endpoint) =~ endpoint
end
end
describe "origin" do
test "will provide the origin field given a url" do
endpoint = "example.com:4000"
assert SocketInfo.origin("ws://#{endpoint}") == endpoint
assert SocketInfo.origin("http://#{endpoint}") == endpoint
assert SocketInfo.origin("https://#{endpoint}") == endpoint
end
test "will proide the origin field given a uri" do
endpoint = "example.com:4000"
uri = URI.parse("http://#{endpoint}")
assert SocketInfo.origin(uri) == endpoint
end
end
describe "touch" do
test "will update the TTL" do
endpoint = "example.com:4000"
socket = SocketInfo.build("ws://#{endpoint}")
Process.sleep(2)
touched_socket = SocketInfo.touch(socket)
assert socket.connected_until < touched_socket.connected_until
end
end
describe "expired?" do
setup do
start_supervised(
{Pleroma.Web.FedSockets.Supervisor,
[
ping_interval: 8,
connection_duration: 5,
rejection_duration: 5,
fed_socket_rejections: [lazy: true]
]}
)
:ok
end
test "tests if the TTL is exceeded" do
endpoint = "example.com:4000"
socket = SocketInfo.build("ws://#{endpoint}")
refute SocketInfo.expired?(socket)
Process.sleep(10)
assert SocketInfo.expired?(socket)
end
end
describe "creating outgoing connection records" do
test "can be passed a string" do
assert %{conn_pid: :pid, origin: _origin} = SocketInfo.build("example.com:4000", :pid)
end
test "can be passed a URI" do
uri = URI.parse("http://example.com:4000")
assert %{conn_pid: :pid, origin: origin} = SocketInfo.build(uri, :pid)
assert origin =~ "example.com:4000"
end
test "will include the port number" do
assert %{conn_pid: :pid, origin: origin} = SocketInfo.build("http://example.com:4000", :pid)
assert origin =~ ":4000"
end
test "will provide the port if missing" do
assert %{conn_pid: :pid, origin: "example.com:80"} =
SocketInfo.build("http://example.com", :pid)
assert %{conn_pid: :pid, origin: "example.com:443"} =
SocketInfo.build("https://example.com", :pid)
end
end
describe "creating incoming connection records" do
test "can be passed a string" do
assert %{pid: _, origin: _origin} = SocketInfo.build("example.com:4000")
end
test "can be passed a URI" do
uri = URI.parse("example.com:4000")
assert %{pid: _, origin: _origin} = SocketInfo.build(uri)
end
test "will include the port number" do
assert %{pid: _, origin: origin} = SocketInfo.build("http://example.com:4000")
assert origin =~ ":4000"
end
test "will provide the port if missing" do
assert %{pid: _, origin: "example.com:80"} = SocketInfo.build("http://example.com")
assert %{pid: _, origin: "example.com:443"} = SocketInfo.build("https://example.com")
end
end
end

View file

@ -45,21 +45,77 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
end
end
describe "creates push subscription" do
test "returns error when push disabled ", %{conn: conn} do
describe "when disabled" do
test "POST returns error", %{conn: conn} do
assert_error_when_disable_push do
conn
|> post("/api/v1/push/subscription", %{subscription: @sub})
|> post("/api/v1/push/subscription", %{
"data" => %{"alerts" => %{"mention" => true}},
"subscription" => @sub
})
|> json_response_and_validate_schema(403)
end
end
test "GET returns error", %{conn: conn} do
assert_error_when_disable_push do
conn
|> get("/api/v1/push/subscription", %{})
|> json_response_and_validate_schema(403)
end
end
test "PUT returns error", %{conn: conn} do
assert_error_when_disable_push do
conn
|> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
|> json_response_and_validate_schema(403)
end
end
test "DELETE returns error", %{conn: conn} do
assert_error_when_disable_push do
conn
|> delete("/api/v1/push/subscription", %{})
|> json_response_and_validate_schema(403)
end
end
end
describe "creates push subscription" do
test "ignores unsupported types", %{conn: conn} do
result =
conn
|> post("/api/v1/push/subscription", %{
"data" => %{
"alerts" => %{
"fake_unsupported_type" => true
}
},
"subscription" => @sub
})
|> json_response_and_validate_schema(200)
refute %{
"alerts" => %{
"fake_unsupported_type" => true
}
} == result
end
test "successful creation", %{conn: conn} do
result =
conn
|> post("/api/v1/push/subscription", %{
"data" => %{
"alerts" => %{"mention" => true, "test" => true, "pleroma:chat_mention" => true}
"alerts" => %{
"mention" => true,
"favourite" => true,
"follow" => true,
"reblog" => true,
"pleroma:chat_mention" => true,
"pleroma:emoji_reaction" => true
}
},
"subscription" => @sub
})
@ -68,7 +124,14 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
[subscription] = Pleroma.Repo.all(Subscription)
assert %{
"alerts" => %{"mention" => true, "pleroma:chat_mention" => true},
"alerts" => %{
"mention" => true,
"favourite" => true,
"follow" => true,
"reblog" => true,
"pleroma:chat_mention" => true,
"pleroma:emoji_reaction" => true
},
"endpoint" => subscription.endpoint,
"id" => to_string(subscription.id),
"server_key" => @server_key
@ -77,14 +140,6 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
end
describe "gets a user subscription" do
test "returns error when push disabled ", %{conn: conn} do
assert_error_when_disable_push do
conn
|> get("/api/v1/push/subscription", %{})
|> json_response_and_validate_schema(403)
end
end
test "returns error when user hasn't subscription", %{conn: conn} do
res =
conn
@ -124,30 +179,47 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
insert(:push_subscription,
user: user,
token: token,
data: %{"alerts" => %{"mention" => true}}
data: %{
"alerts" => %{
"mention" => true,
"favourite" => true,
"follow" => true,
"reblog" => true,
"pleroma:chat_mention" => true,
"pleroma:emoji_reaction" => true
}
}
)
%{conn: conn, user: user, token: token, subscription: subscription}
end
test "returns error when push disabled ", %{conn: conn} do
assert_error_when_disable_push do
conn
|> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
|> json_response_and_validate_schema(403)
end
end
test "returns updated subsciption", %{conn: conn, subscription: subscription} do
res =
conn
|> put("/api/v1/push/subscription", %{
data: %{"alerts" => %{"mention" => false, "follow" => true}}
data: %{
"alerts" => %{
"mention" => false,
"favourite" => false,
"follow" => false,
"reblog" => false,
"pleroma:chat_mention" => false,
"pleroma:emoji_reaction" => false
}
}
})
|> json_response_and_validate_schema(200)
expect = %{
"alerts" => %{"follow" => true, "mention" => false},
"alerts" => %{
"mention" => false,
"favourite" => false,
"follow" => false,
"reblog" => false,
"pleroma:chat_mention" => false,
"pleroma:emoji_reaction" => false
},
"endpoint" => "https://example.com/example/1234",
"id" => to_string(subscription.id),
"server_key" => @server_key
@ -158,14 +230,6 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
end
describe "deletes the user subscription" do
test "returns error when push disabled ", %{conn: conn} do
assert_error_when_disable_push do
conn
|> delete("/api/v1/push/subscription", %{})
|> json_response_and_validate_schema(403)
end
end
test "returns error when user hasn't subscription", %{conn: conn} do
res =
conn

View file

@ -184,6 +184,24 @@ defmodule Pleroma.Web.Push.ImplTest do
"New Favorite"
end
test "renders title and body for pleroma:emoji_reaction activity" do
user = insert(:user, nickname: "Bob")
{:ok, activity} =
CommonAPI.post(user, %{
status: "This post is a really good post!"
})
{:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍")
object = Object.normalize(activity)
assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) ==
"@Bob reacted with 👍"
assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) ==
"New Reaction"
end
test "renders title for create activity with direct visibility" do
user = insert(:user, nickname: "Bob")