Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
diff --git a/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs b/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs
index 0cd00e753..0e3b5311e 100644
--- a/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs
+++ b/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs
@@ -1,98 +1,98 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.WebhookControllerTest do
use Pleroma.Web.ConnCase, async: true
import Pleroma.Factory
alias Pleroma.Webhook
setup do
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/webhook" do
test "lists existing webhooks", %{conn: conn} do
Webhook.create(%{url: "https://example.com/webhook1", events: [:"report.created"]})
Webhook.create(%{url: "https://example.com/webhook2", events: [:"account.created"]})
response =
conn
|> get("/api/pleroma/admin/webhooks")
|> json_response_and_validate_schema(:ok)
assert length(response) == 2
end
end
describe "POST /api/pleroma/admin/webhooks" do
test "creates a webhook", %{conn: conn} do
%{"id" => id} =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/pleroma/admin/webhooks", %{
url: "http://example.com/webhook",
events: ["account.created"]
})
|> json_response_and_validate_schema(:ok)
assert %{url: "http://example.com/webhook", events: [:"account.created"]} = Webhook.get(id)
end
end
describe "PATCH /api/pleroma/admin/webhooks" do
test "edits a webhook", %{conn: conn} do
- %{id: id} =
+ {:ok, %{id: id}} =
Webhook.create(%{url: "https://example.com/webhook1", events: [:"report.created"]})
conn
|> put_req_header("content-type", "application/json")
|> patch("/api/pleroma/admin/webhooks/#{id}", %{
events: ["report.created", "account.created"]
})
|> json_response_and_validate_schema(:ok)
assert %{events: [:"report.created", :"account.created"]} = Webhook.get(id)
end
test "can't edit an internal webhook", %{conn: conn} do
- %{id: id} =
+ {:ok, %{id: id}} =
Webhook.create(%{url: "https://example.com/webhook1", events: [], internal: true})
conn
|> put_req_header("content-type", "application/json")
|> patch("/api/pleroma/admin/webhooks/#{id}", %{
events: ["report.created", "account.created"]
})
|> json_response_and_validate_schema(:forbidden)
assert %{events: []} = Webhook.get(id)
end
end
describe "DELETE /api/pleroma/admin/webhooks" do
test "deletes a webhook", %{conn: conn} do
- %{id: id} =
+ {:ok, %{id: id}} =
Webhook.create(%{url: "https://example.com/webhook1", events: [:"report.created"]})
conn
|> put_req_header("content-type", "application/json")
|> delete("/api/pleroma/admin/webhooks/#{id}")
|> json_response_and_validate_schema(:ok)
assert [] =
Webhook
|> Pleroma.Repo.all()
end
end
end
diff --git a/test/pleroma/webhook/notify_test.exs b/test/pleroma/webhook/notify_test.exs
index c99489940..568ed0d14 100644
--- a/test/pleroma/webhook/notify_test.exs
+++ b/test/pleroma/webhook/notify_test.exs
@@ -1,75 +1,75 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Webhook.NotifyTest do
use Pleroma.DataCase, async: true
alias Pleroma.Webhook
alias Pleroma.Webhook.Notify
import Pleroma.Factory
test "triggers a webhook for a report" do
%{id: activity_id} = activity = insert(:report_activity)
url = "https://example.com/webhook"
Webhook.create(%{url: url, events: [:"report.created"]})
Tesla.Mock.mock(fn %{url: ^url, body: body} = _ ->
report =
body
|> Jason.decode!()
|> Map.get("object")
assert %{"id" => ^activity_id} = report
%Tesla.Env{status: 200, body: ""}
end)
[job] = Notify.trigger_webhooks(activity, :"report.created")
Pleroma.Workers.WebhookWorker.perform(job)
end
test "triggers a webhook for an account" do
%{id: account_id} = user = insert(:user)
url = "https://example.com/webhook"
Webhook.create(%{url: url, events: [:"account.created"]})
Tesla.Mock.mock(fn %{url: ^url, body: body} = _ ->
report =
body
|> Jason.decode!()
|> Map.get("object")
assert %{"id" => ^account_id} = report
%Tesla.Env{status: 200, body: ""}
end)
[job] = Notify.trigger_webhooks(user, :"account.created")
Pleroma.Workers.WebhookWorker.perform(job)
end
test "notifies have a valid signature" do
activity = insert(:report_activity)
- %{secret: secret} =
- webhook = Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
+ {:ok, %{secret: secret} = webhook} =
+ Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
Tesla.Mock.mock(fn %{url: "https://example.com/webhook", body: body, headers: headers} = _ ->
{"X-Hub-Signature", "sha256=" <> signature} =
Enum.find(headers, fn {key, _} -> key == "X-Hub-Signature" end)
assert signature == :crypto.mac(:hmac, :sha256, secret, body) |> Base.encode16(case: :lower)
%Tesla.Env{status: 200, body: ""}
end)
Notify.report_created(webhook, activity)
end
end
diff --git a/test/pleroma/webhook_test.exs b/test/pleroma/webhook_test.exs
index ea1378730..ea1408708 100644
--- a/test/pleroma/webhook_test.exs
+++ b/test/pleroma/webhook_test.exs
@@ -1,56 +1,57 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.WebhookTest do
use Pleroma.DataCase, async: true
alias Pleroma.Webhook
test "creating a webhook" do
- %{id: id} = Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
+ {:ok, %{id: id}} =
+ Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
assert %{url: "https://example.com/webhook"} = Webhook.get(id)
end
test "editing a webhook" do
- %{id: id} =
- webhook = Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
+ {:ok, %{id: id} = webhook} =
+ Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
Webhook.update(webhook, %{events: [:"account.created"]})
assert %{events: [:"account.created"]} = Webhook.get(id)
end
test "filter webhooks by type" do
- %{id: id1} =
+ {:ok, %{id: id1}} =
Webhook.create(%{url: "https://example.com/webhook1", events: [:"report.created"]})
- %{id: id2} =
+ {:ok, %{id: id2}} =
Webhook.create(%{
url: "https://example.com/webhook2",
events: [:"account.created", :"report.created"]
})
Webhook.create(%{url: "https://example.com/webhook3", events: [:"account.created"]})
assert [%{id: ^id1}, %{id: ^id2}] = Webhook.get_by_type(:"report.created")
end
test "change webhook state" do
- %{id: id, enabled: true} =
- webhook = Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
+ {:ok, %{id: id, enabled: true} = webhook} =
+ Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
Webhook.set_enabled(webhook, false)
assert %{enabled: false} = Webhook.get(id)
end
test "rotate webhook secrets" do
- %{id: id, secret: secret} =
- webhook = Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
+ {:ok, %{id: id, secret: secret} = webhook} =
+ Webhook.create(%{url: "https://example.com/webhook", events: [:"report.created"]})
Webhook.rotate_secret(webhook)
%{secret: new_secret} = Webhook.get(id)
assert secret != new_secret
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Oct 31, 6:34 AM (7 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1801130
Default Alt Text
(8 KB)

Event Timeline