Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into auth-fetch-exception
This commit is contained in:
commit
687ac4a850
65 changed files with 1490 additions and 189 deletions
|
|
@ -31,8 +31,7 @@ defmodule Pleroma.ScheduledActivityTest do
|
|||
{:ok, sa1} = ScheduledActivity.create(user, attrs)
|
||||
{:ok, sa2} = ScheduledActivity.create(user, attrs)
|
||||
|
||||
jobs =
|
||||
Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
|
||||
jobs = Repo.all(from(j in Oban.Job, where: j.queue == "federator_outgoing", select: j.args))
|
||||
|
||||
assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
|
||||
end
|
||||
|
|
|
|||
199
test/pleroma/search/qdrant_search_test.exs
Normal file
199
test/pleroma/search/qdrant_search_test.exs
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Search.QdrantSearchTest do
|
||||
use Pleroma.DataCase, async: true
|
||||
use Oban.Testing, repo: Pleroma.Repo
|
||||
|
||||
import Pleroma.Factory
|
||||
import Mox
|
||||
|
||||
alias Pleroma.Search.QdrantSearch
|
||||
alias Pleroma.UnstubbedConfigMock, as: Config
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Workers.SearchIndexingWorker
|
||||
|
||||
describe "Qdrant search" do
|
||||
test "returns the correct healthcheck endpoints" do
|
||||
# No openai healthcheck URL
|
||||
Config
|
||||
|> expect(:get, 2, fn
|
||||
[Pleroma.Search.QdrantSearch, key], nil ->
|
||||
%{qdrant_url: "https://qdrant.url"}[key]
|
||||
end)
|
||||
|
||||
[health_endpoint] = QdrantSearch.healthcheck_endpoints()
|
||||
|
||||
assert "https://qdrant.url/healthz" == health_endpoint
|
||||
|
||||
# Set openai healthcheck URL
|
||||
Config
|
||||
|> expect(:get, 2, fn
|
||||
[Pleroma.Search.QdrantSearch, key], nil ->
|
||||
%{qdrant_url: "https://qdrant.url", openai_healthcheck_url: "https://openai.url/health"}[
|
||||
key
|
||||
]
|
||||
end)
|
||||
|
||||
[_, health_endpoint] = QdrantSearch.healthcheck_endpoints()
|
||||
|
||||
assert "https://openai.url/health" == health_endpoint
|
||||
end
|
||||
|
||||
test "searches for a term by encoding it and sending it to qdrant" do
|
||||
user = insert(:user)
|
||||
|
||||
{:ok, activity} =
|
||||
CommonAPI.post(user, %{
|
||||
status: "guys i just don't wanna leave the swamp",
|
||||
visibility: "public"
|
||||
})
|
||||
|
||||
Config
|
||||
|> expect(:get, 3, fn
|
||||
[Pleroma.Search, :module], nil ->
|
||||
QdrantSearch
|
||||
|
||||
[Pleroma.Search.QdrantSearch, key], nil ->
|
||||
%{
|
||||
openai_model: "a_model",
|
||||
openai_url: "https://openai.url",
|
||||
qdrant_url: "https://qdrant.url"
|
||||
}[key]
|
||||
end)
|
||||
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://openai.url/v1/embeddings", method: :post} ->
|
||||
Tesla.Mock.json(%{
|
||||
data: [%{embedding: [1, 2, 3]}]
|
||||
})
|
||||
|
||||
%{url: "https://qdrant.url/collections/posts/points/search", method: :post, body: body} ->
|
||||
data = Jason.decode!(body)
|
||||
refute data["filter"]
|
||||
|
||||
Tesla.Mock.json(%{
|
||||
result: [%{"id" => activity.id |> FlakeId.from_string() |> Ecto.UUID.cast!()}]
|
||||
})
|
||||
end)
|
||||
|
||||
results = QdrantSearch.search(nil, "guys i just don't wanna leave the swamp", %{})
|
||||
|
||||
assert results == [activity]
|
||||
end
|
||||
|
||||
test "for a given actor, ask for only relevant matches" do
|
||||
user = insert(:user)
|
||||
|
||||
{:ok, activity} =
|
||||
CommonAPI.post(user, %{
|
||||
status: "guys i just don't wanna leave the swamp",
|
||||
visibility: "public"
|
||||
})
|
||||
|
||||
Config
|
||||
|> expect(:get, 3, fn
|
||||
[Pleroma.Search, :module], nil ->
|
||||
QdrantSearch
|
||||
|
||||
[Pleroma.Search.QdrantSearch, key], nil ->
|
||||
%{
|
||||
openai_model: "a_model",
|
||||
openai_url: "https://openai.url",
|
||||
qdrant_url: "https://qdrant.url"
|
||||
}[key]
|
||||
end)
|
||||
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://openai.url/v1/embeddings", method: :post} ->
|
||||
Tesla.Mock.json(%{
|
||||
data: [%{embedding: [1, 2, 3]}]
|
||||
})
|
||||
|
||||
%{url: "https://qdrant.url/collections/posts/points/search", method: :post, body: body} ->
|
||||
data = Jason.decode!(body)
|
||||
|
||||
assert data["filter"] == %{
|
||||
"must" => [%{"key" => "actor", "match" => %{"value" => user.ap_id}}]
|
||||
}
|
||||
|
||||
Tesla.Mock.json(%{
|
||||
result: [%{"id" => activity.id |> FlakeId.from_string() |> Ecto.UUID.cast!()}]
|
||||
})
|
||||
end)
|
||||
|
||||
results =
|
||||
QdrantSearch.search(nil, "guys i just don't wanna leave the swamp", %{author: user})
|
||||
|
||||
assert results == [activity]
|
||||
end
|
||||
|
||||
test "indexes a public post on creation, deletes from the index on deletion" do
|
||||
user = insert(:user)
|
||||
|
||||
Tesla.Mock.mock(fn
|
||||
%{method: :post, url: "https://openai.url/v1/embeddings"} ->
|
||||
send(self(), "posted_to_openai")
|
||||
|
||||
Tesla.Mock.json(%{
|
||||
data: [%{embedding: [1, 2, 3]}]
|
||||
})
|
||||
|
||||
%{method: :put, url: "https://qdrant.url/collections/posts/points", body: body} ->
|
||||
send(self(), "posted_to_qdrant")
|
||||
|
||||
data = Jason.decode!(body)
|
||||
%{"points" => [%{"vector" => vector, "payload" => payload}]} = data
|
||||
|
||||
assert vector == [1, 2, 3]
|
||||
assert payload["actor"]
|
||||
assert payload["published_at"]
|
||||
|
||||
Tesla.Mock.json("ok")
|
||||
|
||||
%{method: :post, url: "https://qdrant.url/collections/posts/points/delete"} ->
|
||||
send(self(), "deleted_from_qdrant")
|
||||
Tesla.Mock.json("ok")
|
||||
end)
|
||||
|
||||
Config
|
||||
|> expect(:get, 6, fn
|
||||
[Pleroma.Search, :module], nil ->
|
||||
QdrantSearch
|
||||
|
||||
[Pleroma.Search.QdrantSearch, key], nil ->
|
||||
%{
|
||||
openai_model: "a_model",
|
||||
openai_url: "https://openai.url",
|
||||
qdrant_url: "https://qdrant.url"
|
||||
}[key]
|
||||
end)
|
||||
|
||||
{:ok, activity} =
|
||||
CommonAPI.post(user, %{
|
||||
status: "guys i just don't wanna leave the swamp",
|
||||
visibility: "public"
|
||||
})
|
||||
|
||||
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||
|
||||
assert_enqueued(
|
||||
worker: SearchIndexingWorker,
|
||||
args: args
|
||||
)
|
||||
|
||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||
assert_received("posted_to_openai")
|
||||
assert_received("posted_to_qdrant")
|
||||
|
||||
{:ok, _} = CommonAPI.delete(activity.id, user)
|
||||
|
||||
delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
|
||||
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
|
||||
assert :ok = perform_job(SearchIndexingWorker, delete_args)
|
||||
|
||||
assert_received("deleted_from_qdrant")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.ActivityPub.MRF.AntiMentionSpamPolicyTest do
|
||||
use Pleroma.DataCase
|
||||
import Pleroma.Factory
|
||||
alias Pleroma.Web.ActivityPub.MRF.AntiMentionSpamPolicy
|
||||
|
||||
test "it allows posts without mentions" do
|
||||
user = insert(:user, local: false)
|
||||
assert user.note_count == 0
|
||||
|
||||
message = %{
|
||||
"type" => "Create",
|
||||
"actor" => user.ap_id
|
||||
}
|
||||
|
||||
{:ok, _message} = AntiMentionSpamPolicy.filter(message)
|
||||
end
|
||||
|
||||
test "it allows posts from users with followers, posts, and age" do
|
||||
user =
|
||||
insert(:user,
|
||||
local: false,
|
||||
follower_count: 1,
|
||||
note_count: 1,
|
||||
inserted_at: ~N[1970-01-01 00:00:00]
|
||||
)
|
||||
|
||||
message = %{
|
||||
"type" => "Create",
|
||||
"actor" => user.ap_id
|
||||
}
|
||||
|
||||
{:ok, _message} = AntiMentionSpamPolicy.filter(message)
|
||||
end
|
||||
|
||||
test "it allows posts from local users" do
|
||||
user = insert(:user, local: true)
|
||||
|
||||
message = %{
|
||||
"type" => "Create",
|
||||
"actor" => user.ap_id
|
||||
}
|
||||
|
||||
{:ok, _message} = AntiMentionSpamPolicy.filter(message)
|
||||
end
|
||||
|
||||
test "it rejects posts with mentions from users without followers" do
|
||||
user = insert(:user, local: false, follower_count: 0)
|
||||
|
||||
message = %{
|
||||
"type" => "Create",
|
||||
"actor" => user.ap_id,
|
||||
"object" => %{
|
||||
"to" => ["https://pleroma.soykaf.com/users/1"],
|
||||
"cc" => ["https://pleroma.soykaf.com/users/1"],
|
||||
"actor" => user.ap_id
|
||||
}
|
||||
}
|
||||
|
||||
{:reject, _message} = AntiMentionSpamPolicy.filter(message)
|
||||
end
|
||||
end
|
||||
|
|
@ -27,19 +27,22 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do
|
|||
end
|
||||
|
||||
test "works with honkerific attachments" do
|
||||
attachment = %{
|
||||
honk = %{
|
||||
"mediaType" => "",
|
||||
"name" => "",
|
||||
"summary" => "298p3RG7j27tfsZ9RQ.jpg",
|
||||
"summary" => "Select your spirit chonk",
|
||||
"name" => "298p3RG7j27tfsZ9RQ.jpg",
|
||||
"type" => "Document",
|
||||
"url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
|
||||
}
|
||||
|
||||
assert {:ok, attachment} =
|
||||
AttachmentValidator.cast_and_validate(attachment)
|
||||
honk
|
||||
|> AttachmentValidator.cast_and_validate()
|
||||
|> Ecto.Changeset.apply_action(:insert)
|
||||
|
||||
assert attachment.mediaType == "application/octet-stream"
|
||||
assert attachment.summary == "Select your spirit chonk"
|
||||
assert attachment.name == "298p3RG7j27tfsZ9RQ.jpg"
|
||||
end
|
||||
|
||||
test "works with an unknown but valid mime type" do
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
|
||||
use Oban.Testing, repo: Pleroma.Repo
|
||||
use Pleroma.Web.ConnCase, async: true
|
||||
|
||||
alias Pleroma.Repo
|
||||
|
|
@ -78,7 +79,7 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
|
|||
}
|
||||
)
|
||||
|
||||
job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
|
||||
job = Repo.one(from(j in Oban.Job, where: j.queue == "federator_outgoing"))
|
||||
|
||||
assert job.args == %{"activity_id" => scheduled_activity.id}
|
||||
assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(scheduled_at)
|
||||
|
|
@ -124,9 +125,11 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
|
|||
}
|
||||
)
|
||||
|
||||
job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
|
||||
|
||||
assert job.args == %{"activity_id" => scheduled_activity.id}
|
||||
assert_enqueued(
|
||||
worker: Pleroma.Workers.ScheduledActivityWorker,
|
||||
args: %{"activity_id" => scheduled_activity.id},
|
||||
queue: :federator_outgoing
|
||||
)
|
||||
|
||||
res_conn =
|
||||
conn
|
||||
|
|
@ -135,7 +138,11 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
|
|||
|
||||
assert %{} = json_response_and_validate_schema(res_conn, 200)
|
||||
refute Repo.get(ScheduledActivity, scheduled_activity.id)
|
||||
refute Repo.get(Oban.Job, job.id)
|
||||
|
||||
refute_enqueued(
|
||||
worker: Pleroma.Workers.ScheduledActivityWorker,
|
||||
args: %{"activity_id" => scheduled_activity.id}
|
||||
)
|
||||
|
||||
res_conn =
|
||||
conn
|
||||
|
|
|
|||
|
|
@ -591,45 +591,78 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
|
|||
assert mention.url == recipient.ap_id
|
||||
end
|
||||
|
||||
test "attachments" do
|
||||
object = %{
|
||||
"type" => "Image",
|
||||
"url" => [
|
||||
%{
|
||||
"mediaType" => "image/png",
|
||||
"href" => "someurl",
|
||||
"width" => 200,
|
||||
"height" => 100
|
||||
}
|
||||
],
|
||||
"blurhash" => "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn",
|
||||
"uuid" => 6
|
||||
}
|
||||
describe "attachments" do
|
||||
test "Complete Mastodon style" do
|
||||
object = %{
|
||||
"type" => "Image",
|
||||
"url" => [
|
||||
%{
|
||||
"mediaType" => "image/png",
|
||||
"href" => "someurl",
|
||||
"width" => 200,
|
||||
"height" => 100
|
||||
}
|
||||
],
|
||||
"blurhash" => "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn",
|
||||
"uuid" => 6
|
||||
}
|
||||
|
||||
expected = %{
|
||||
id: "1638338801",
|
||||
type: "image",
|
||||
url: "someurl",
|
||||
remote_url: "someurl",
|
||||
preview_url: "someurl",
|
||||
text_url: "someurl",
|
||||
description: nil,
|
||||
pleroma: %{mime_type: "image/png"},
|
||||
meta: %{original: %{width: 200, height: 100, aspect: 2}},
|
||||
blurhash: "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn"
|
||||
}
|
||||
expected = %{
|
||||
id: "1638338801",
|
||||
type: "image",
|
||||
url: "someurl",
|
||||
remote_url: "someurl",
|
||||
preview_url: "someurl",
|
||||
text_url: "someurl",
|
||||
description: nil,
|
||||
pleroma: %{mime_type: "image/png"},
|
||||
meta: %{original: %{width: 200, height: 100, aspect: 2}},
|
||||
blurhash: "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn"
|
||||
}
|
||||
|
||||
api_spec = Pleroma.Web.ApiSpec.spec()
|
||||
api_spec = Pleroma.Web.ApiSpec.spec()
|
||||
|
||||
assert expected == StatusView.render("attachment.json", %{attachment: object})
|
||||
assert_schema(expected, "Attachment", api_spec)
|
||||
assert expected == StatusView.render("attachment.json", %{attachment: object})
|
||||
assert_schema(expected, "Attachment", api_spec)
|
||||
|
||||
# If theres a "id", use that instead of the generated one
|
||||
object = Map.put(object, "id", 2)
|
||||
result = StatusView.render("attachment.json", %{attachment: object})
|
||||
# If theres a "id", use that instead of the generated one
|
||||
object = Map.put(object, "id", 2)
|
||||
result = StatusView.render("attachment.json", %{attachment: object})
|
||||
|
||||
assert %{id: "2"} = result
|
||||
assert_schema(result, "Attachment", api_spec)
|
||||
assert %{id: "2"} = result
|
||||
assert_schema(result, "Attachment", api_spec)
|
||||
end
|
||||
|
||||
test "Honkerific" do
|
||||
object = %{
|
||||
"type" => "Image",
|
||||
"url" => [
|
||||
%{
|
||||
"mediaType" => "image/png",
|
||||
"href" => "someurl"
|
||||
}
|
||||
],
|
||||
"name" => "fool.jpeg",
|
||||
"summary" => "they have played us for absolute fools."
|
||||
}
|
||||
|
||||
expected = %{
|
||||
blurhash: nil,
|
||||
description: "they have played us for absolute fools.",
|
||||
id: "1638338801",
|
||||
pleroma: %{mime_type: "image/png", name: "fool.jpeg"},
|
||||
preview_url: "someurl",
|
||||
remote_url: "someurl",
|
||||
text_url: "someurl",
|
||||
type: "image",
|
||||
url: "someurl"
|
||||
}
|
||||
|
||||
api_spec = Pleroma.Web.ApiSpec.spec()
|
||||
|
||||
assert expected == StatusView.render("attachment.json", %{attachment: object})
|
||||
assert_schema(expected, "Attachment", api_spec)
|
||||
end
|
||||
end
|
||||
|
||||
test "put the url advertised in the Activity in to the url attribute" do
|
||||
|
|
|
|||
|
|
@ -3,14 +3,52 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
||||
use Pleroma.Web.ConnCase
|
||||
use Pleroma.Web.ConnCase, async: true
|
||||
|
||||
alias Plug.Conn
|
||||
|
||||
describe "http security enabled" do
|
||||
setup do: clear_config([:http_security, :enabled], true)
|
||||
import Mox
|
||||
|
||||
test "it sends CSP headers when enabled", %{conn: conn} do
|
||||
setup do
|
||||
base_config = Pleroma.Config.get([:http_security])
|
||||
%{base_config: base_config}
|
||||
end
|
||||
|
||||
defp mock_config(config, additional \\ %{}) do
|
||||
Pleroma.StaticStubbedConfigMock
|
||||
|> stub(:get, fn
|
||||
[:http_security, key] -> config[key]
|
||||
key -> additional[key]
|
||||
end)
|
||||
end
|
||||
|
||||
describe "http security enabled" do
|
||||
setup %{base_config: base_config} do
|
||||
%{base_config: Keyword.put(base_config, :enabled, true)}
|
||||
end
|
||||
|
||||
test "it does not contain unsafe-eval", %{conn: conn, base_config: base_config} do
|
||||
mock_config(base_config)
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
[header] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
refute header =~ ~r/unsafe-eval/
|
||||
end
|
||||
|
||||
test "with allow_unsafe_eval set, it does contain it", %{conn: conn, base_config: base_config} do
|
||||
base_config =
|
||||
base_config
|
||||
|> Keyword.put(:allow_unsafe_eval, true)
|
||||
|
||||
mock_config(base_config)
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
[header] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
assert header =~ ~r/unsafe-eval/
|
||||
end
|
||||
|
||||
test "it sends CSP headers when enabled", %{conn: conn, base_config: base_config} do
|
||||
mock_config(base_config)
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
refute Conn.get_resp_header(conn, "x-xss-protection") == []
|
||||
|
|
@ -22,8 +60,10 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
refute Conn.get_resp_header(conn, "content-security-policy") == []
|
||||
end
|
||||
|
||||
test "it sends STS headers when enabled", %{conn: conn} do
|
||||
clear_config([:http_security, :sts], true)
|
||||
test "it sends STS headers when enabled", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:sts, true)
|
||||
|> mock_config()
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
|
|
@ -31,8 +71,10 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
refute Conn.get_resp_header(conn, "expect-ct") == []
|
||||
end
|
||||
|
||||
test "it does not send STS headers when disabled", %{conn: conn} do
|
||||
clear_config([:http_security, :sts], false)
|
||||
test "it does not send STS headers when disabled", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:sts, false)
|
||||
|> mock_config()
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
|
|
@ -40,19 +82,30 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
assert Conn.get_resp_header(conn, "expect-ct") == []
|
||||
end
|
||||
|
||||
test "referrer-policy header reflects configured value", %{conn: conn} do
|
||||
resp = get(conn, "/api/v1/instance")
|
||||
test "referrer-policy header reflects configured value", %{
|
||||
conn: conn,
|
||||
base_config: base_config
|
||||
} do
|
||||
mock_config(base_config)
|
||||
|
||||
resp = get(conn, "/api/v1/instance")
|
||||
assert Conn.get_resp_header(resp, "referrer-policy") == ["same-origin"]
|
||||
|
||||
clear_config([:http_security, :referrer_policy], "no-referrer")
|
||||
base_config
|
||||
|> Keyword.put(:referrer_policy, "no-referrer")
|
||||
|> mock_config
|
||||
|
||||
resp = get(conn, "/api/v1/instance")
|
||||
|
||||
assert Conn.get_resp_header(resp, "referrer-policy") == ["no-referrer"]
|
||||
end
|
||||
|
||||
test "it sends `report-to` & `report-uri` CSP response headers", %{conn: conn} do
|
||||
test "it sends `report-to` & `report-uri` CSP response headers", %{
|
||||
conn: conn,
|
||||
base_config: base_config
|
||||
} do
|
||||
mock_config(base_config)
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
|
|
@ -65,7 +118,11 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
"{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
|
||||
end
|
||||
|
||||
test "default values for img-src and media-src with disabled media proxy", %{conn: conn} do
|
||||
test "default values for img-src and media-src with disabled media proxy", %{
|
||||
conn: conn,
|
||||
base_config: base_config
|
||||
} do
|
||||
mock_config(base_config)
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
|
|
@ -73,60 +130,129 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
assert csp =~ "img-src 'self' data: blob: https:;"
|
||||
end
|
||||
|
||||
test "it sets the Service-Worker-Allowed header", %{conn: conn} do
|
||||
clear_config([:http_security, :enabled], true)
|
||||
clear_config([:frontends, :primary], %{"name" => "fedi-fe", "ref" => "develop"})
|
||||
test "it sets the Service-Worker-Allowed header", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:enabled, true)
|
||||
|
||||
clear_config([:frontends, :available], %{
|
||||
"fedi-fe" => %{
|
||||
"name" => "fedi-fe",
|
||||
"custom-http-headers" => [{"service-worker-allowed", "/"}]
|
||||
}
|
||||
})
|
||||
additional_config =
|
||||
%{}
|
||||
|> Map.put([:frontends, :primary], %{"name" => "fedi-fe", "ref" => "develop"})
|
||||
|> Map.put(
|
||||
[:frontends, :available],
|
||||
%{
|
||||
"fedi-fe" => %{
|
||||
"name" => "fedi-fe",
|
||||
"custom-http-headers" => [{"service-worker-allowed", "/"}]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
assert Conn.get_resp_header(conn, "service-worker-allowed") == ["/"]
|
||||
end
|
||||
end
|
||||
|
||||
describe "img-src and media-src" do
|
||||
setup do
|
||||
clear_config([:http_security, :enabled], true)
|
||||
clear_config([:media_proxy, :enabled], true)
|
||||
clear_config([:media_proxy, :proxy_opts, :redirect_on_failure], false)
|
||||
setup %{base_config: base_config} do
|
||||
base_config =
|
||||
base_config
|
||||
|> Keyword.put(:enabled, true)
|
||||
|
||||
additional_config =
|
||||
%{}
|
||||
|> Map.put([:media_proxy, :enabled], true)
|
||||
|> Map.put([:media_proxy, :proxy_opts, :redirect_on_failure], false)
|
||||
|> Map.put([:media_proxy, :whitelist], [])
|
||||
|
||||
%{base_config: base_config, additional_config: additional_config}
|
||||
end
|
||||
|
||||
test "media_proxy with base_url", %{conn: conn} do
|
||||
test "media_proxy with base_url", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
url = "https://example.com"
|
||||
clear_config([:media_proxy, :base_url], url)
|
||||
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([:media_proxy, :base_url], url)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
|
||||
assert_media_img_src(conn, url)
|
||||
end
|
||||
|
||||
test "upload with base url", %{conn: conn} do
|
||||
test "upload with base url", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
url = "https://example2.com"
|
||||
clear_config([Pleroma.Upload, :base_url], url)
|
||||
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([Pleroma.Upload, :base_url], url)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
|
||||
assert_media_img_src(conn, url)
|
||||
end
|
||||
|
||||
test "with S3 public endpoint", %{conn: conn} do
|
||||
test "with S3 public endpoint", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
url = "https://example3.com"
|
||||
clear_config([Pleroma.Uploaders.S3, :public_endpoint], url)
|
||||
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([Pleroma.Uploaders.S3, :public_endpoint], url)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, url)
|
||||
end
|
||||
|
||||
test "with captcha endpoint", %{conn: conn} do
|
||||
clear_config([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
|
||||
test "with captcha endpoint", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
|
||||
|> Map.put([Pleroma.Captcha, :method], Pleroma.Captcha.Mock)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, "https://captcha.com")
|
||||
end
|
||||
|
||||
test "with media_proxy whitelist", %{conn: conn} do
|
||||
clear_config([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
|
||||
test "with media_proxy whitelist", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, "https://example7.com https://example6.com")
|
||||
end
|
||||
|
||||
# TODO: delete after removing support bare domains for media proxy whitelist
|
||||
test "with media_proxy bare domains whitelist (deprecated)", %{conn: conn} do
|
||||
clear_config([:media_proxy, :whitelist], ["example4.com", "example5.com"])
|
||||
test "with media_proxy bare domains whitelist (deprecated)", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([:media_proxy, :whitelist], ["example4.com", "example5.com"])
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, "example5.com example4.com")
|
||||
end
|
||||
end
|
||||
|
|
@ -138,8 +264,10 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
assert csp =~ "img-src 'self' data: blob: #{url};"
|
||||
end
|
||||
|
||||
test "it does not send CSP headers when disabled", %{conn: conn} do
|
||||
clear_config([:http_security, :enabled], false)
|
||||
test "it does not send CSP headers when disabled", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:enabled, false)
|
||||
|> mock_config
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue