aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/rel_me_anchor_nofollow.html14
-rw-r--r--test/fixtures/rel_me_null.html1
-rw-r--r--test/support/factory.ex35
-rw-r--r--test/support/web_push_http_client_mock.ex23
-rw-r--r--test/upload_test.exs7
-rw-r--r--test/web/mastodon_api/push_subscription_view_test.exs23
-rw-r--r--test/web/mastodon_api/subscription_controller_test.exs192
-rw-r--r--test/web/node_info_test.exs6
-rw-r--r--test/web/push/impl_test.exs145
-rw-r--r--test/web/rel_me_test.exs12
-rw-r--r--test/web/rich_media/helpers_test.exs62
11 files changed, 506 insertions, 14 deletions
diff --git a/test/fixtures/rel_me_anchor_nofollow.html b/test/fixtures/rel_me_anchor_nofollow.html
new file mode 100644
index 000000000..c856f0091
--- /dev/null
+++ b/test/fixtures/rel_me_anchor_nofollow.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8"/>
+ <title>Blog</title>
+ </head>
+ <body>
+ <article>
+ <h1>Lorem ipsum</h1>
+ <p>Lorem ipsum dolor sit ameph, …</p>
+ <a rel="me nofollow" href="https://social.example.org/users/lain">lain’s account</a>
+ </article>
+ </body>
+</html>
diff --git a/test/fixtures/rel_me_null.html b/test/fixtures/rel_me_null.html
index 57d424b80..5ab5f10c1 100644
--- a/test/fixtures/rel_me_null.html
+++ b/test/fixtures/rel_me_null.html
@@ -8,6 +8,7 @@
<article>
<h1>Lorem ipsum</h1>
<p>Lorem ipsum dolor sit ameph, …</p>
+ <a rel="nofollow" href="https://social.example.org/users/lain">lain’s account</a>
</article>
</body>
</html>
diff --git a/test/support/factory.ex b/test/support/factory.ex
index d1956d1cd..18f77f01a 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -23,7 +23,7 @@ defmodule Pleroma.Factory do
}
end
- def note_factory do
+ def note_factory(attrs \\ %{}) do
text = sequence(:text, &"This is :moominmamma: note #{&1}")
user = insert(:user)
@@ -46,7 +46,7 @@ defmodule Pleroma.Factory do
}
%Pleroma.Object{
- data: data
+ data: merge_attributes(data, Map.get(attrs, :data, %{}))
}
end
@@ -95,8 +95,8 @@ defmodule Pleroma.Factory do
}
end
- def note_activity_factory do
- note = insert(:note)
+ def note_activity_factory(attrs \\ %{}) do
+ note = attrs[:note] || insert(:note)
data = %{
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
@@ -135,9 +135,9 @@ defmodule Pleroma.Factory do
}
end
- def announce_activity_factory do
- note_activity = insert(:note_activity)
- user = insert(:user)
+ def announce_activity_factory(attrs \\ %{}) do
+ note_activity = attrs[:note_activity] || insert(:note_activity)
+ user = attrs[:user] || insert(:user)
data = %{
"type" => "Announce",
@@ -229,15 +229,32 @@ defmodule Pleroma.Factory do
end
def oauth_token_factory do
- user = insert(:user)
oauth_app = insert(:oauth_app)
%Pleroma.Web.OAuth.Token{
token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
- user_id: user.id,
+ user: build(:user),
app_id: oauth_app.id,
valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
}
end
+
+ def push_subscription_factory do
+ %Pleroma.Web.Push.Subscription{
+ user: build(:user),
+ token: build(:oauth_token),
+ endpoint: "https://example.com/example/1234",
+ key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
+ key_p256dh:
+ "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
+ data: %{}
+ }
+ end
+
+ def notification_factory do
+ %Pleroma.Notification{
+ user: build(:user)
+ }
+ end
end
diff --git a/test/support/web_push_http_client_mock.ex b/test/support/web_push_http_client_mock.ex
new file mode 100644
index 000000000..d8accd21c
--- /dev/null
+++ b/test/support/web_push_http_client_mock.ex
@@ -0,0 +1,23 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.WebPushHttpClientMock do
+ def get(url, headers \\ [], options \\ []) do
+ {
+ res,
+ %Tesla.Env{status: status}
+ } = Pleroma.HTTP.request(:get, url, "", headers, options)
+
+ {res, %{status_code: status}}
+ end
+
+ def post(url, body, headers \\ [], options \\ []) do
+ {
+ res,
+ %Tesla.Env{status: status}
+ } = Pleroma.HTTP.request(:post, url, body, headers, options)
+
+ {res, %{status_code: status}}
+ end
+end
diff --git a/test/upload_test.exs b/test/upload_test.exs
index b2d9eca38..bdda01b3f 100644
--- a/test/upload_test.exs
+++ b/test/upload_test.exs
@@ -153,19 +153,20 @@ defmodule Pleroma.UploadTest do
assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
end
- test "replaces : (colon) and ? (question-mark) to %3A and %3F (respectively)" do
+ test "escapes reserved uri characters" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
- filename: "is:an?image.jpg"
+ filename: ":?#[]@!$&\\'()*+,;=.jpg"
}
{:ok, data} = Upload.store(file)
[attachment_url | _] = data["url"]
- assert Path.basename(attachment_url["href"]) == "is%3Aan%3Fimage.jpg"
+ assert Path.basename(attachment_url["href"]) ==
+ "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
end
end
end
diff --git a/test/web/mastodon_api/push_subscription_view_test.exs b/test/web/mastodon_api/push_subscription_view_test.exs
new file mode 100644
index 000000000..dc935fc82
--- /dev/null
+++ b/test/web/mastodon_api/push_subscription_view_test.exs
@@ -0,0 +1,23 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.MastodonAPI.PushSubscriptionViewTest do
+ use Pleroma.DataCase
+ import Pleroma.Factory
+ alias Pleroma.Web.MastodonAPI.PushSubscriptionView, as: View
+ alias Pleroma.Web.Push
+
+ test "Represent a subscription" do
+ subscription = insert(:push_subscription, data: %{"alerts" => %{"mention" => true}})
+
+ expected = %{
+ alerts: %{"mention" => true},
+ endpoint: subscription.endpoint,
+ id: to_string(subscription.id),
+ server_key: Keyword.get(Push.vapid_config(), :public_key)
+ }
+
+ assert expected == View.render("push_subscription.json", %{subscription: subscription})
+ end
+end
diff --git a/test/web/mastodon_api/subscription_controller_test.exs b/test/web/mastodon_api/subscription_controller_test.exs
new file mode 100644
index 000000000..7dfb02f63
--- /dev/null
+++ b/test/web/mastodon_api/subscription_controller_test.exs
@@ -0,0 +1,192 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
+ use Pleroma.Web.ConnCase
+
+ import Pleroma.Factory
+ alias Pleroma.Web.Push
+ alias Pleroma.Web.Push.Subscription
+
+ @sub %{
+ "endpoint" => "https://example.com/example/1234",
+ "keys" => %{
+ "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
+ "p256dh" =>
+ "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
+ }
+ }
+ @server_key Keyword.get(Push.vapid_config(), :public_key)
+
+ setup do
+ user = insert(:user)
+ token = insert(:oauth_token, user: user, scopes: ["push"])
+
+ conn =
+ build_conn()
+ |> assign(:user, user)
+ |> assign(:token, token)
+
+ %{conn: conn, user: user, token: token}
+ end
+
+ defmacro assert_error_when_disable_push(do: yield) do
+ quote do
+ vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
+ Application.put_env(:web_push_encryption, :vapid_details, [])
+ assert "Something went wrong" == unquote(yield)
+ Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
+ end
+ end
+
+ describe "creates push subscription" do
+ test "returns error when push disabled ", %{conn: conn} do
+ assert_error_when_disable_push do
+ conn
+ |> post("/api/v1/push/subscription", %{})
+ |> json_response(500)
+ end
+ end
+
+ test "successful creation", %{conn: conn} do
+ result =
+ conn
+ |> post("/api/v1/push/subscription", %{
+ "data" => %{"alerts" => %{"mention" => true, "test" => true}},
+ "subscription" => @sub
+ })
+ |> json_response(200)
+
+ [subscription] = Pleroma.Repo.all(Subscription)
+
+ assert %{
+ "alerts" => %{"mention" => true},
+ "endpoint" => subscription.endpoint,
+ "id" => to_string(subscription.id),
+ "server_key" => @server_key
+ } == result
+ end
+ 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(500)
+ end
+ end
+
+ test "returns error when user hasn't subscription", %{conn: conn} do
+ res =
+ conn
+ |> get("/api/v1/push/subscription", %{})
+ |> json_response(404)
+
+ assert "Not found" == res
+ end
+
+ test "returns a user subsciption", %{conn: conn, user: user, token: token} do
+ subscription =
+ insert(:push_subscription,
+ user: user,
+ token: token,
+ data: %{"alerts" => %{"mention" => true}}
+ )
+
+ res =
+ conn
+ |> get("/api/v1/push/subscription", %{})
+ |> json_response(200)
+
+ expect = %{
+ "alerts" => %{"mention" => true},
+ "endpoint" => "https://example.com/example/1234",
+ "id" => to_string(subscription.id),
+ "server_key" => @server_key
+ }
+
+ assert expect == res
+ end
+ end
+
+ describe "updates a user subsciption" do
+ setup %{conn: conn, user: user, token: token} do
+ subscription =
+ insert(:push_subscription,
+ user: user,
+ token: token,
+ data: %{"alerts" => %{"mention" => 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(500)
+ end
+ end
+
+ test "returns updated subsciption", %{conn: conn, subscription: subscription} do
+ res =
+ conn
+ |> put("/api/v1/push/subscription", %{
+ data: %{"alerts" => %{"mention" => false, "follow" => true}}
+ })
+ |> json_response(200)
+
+ expect = %{
+ "alerts" => %{"follow" => true, "mention" => false},
+ "endpoint" => "https://example.com/example/1234",
+ "id" => to_string(subscription.id),
+ "server_key" => @server_key
+ }
+
+ assert expect == res
+ end
+ 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(500)
+ end
+ end
+
+ test "returns error when user hasn't subscription", %{conn: conn} do
+ res =
+ conn
+ |> delete("/api/v1/push/subscription", %{})
+ |> json_response(404)
+
+ assert "Not found" == res
+ end
+
+ test "returns empty result and delete user subsciption", %{
+ conn: conn,
+ user: user,
+ token: token
+ } do
+ subscription =
+ insert(:push_subscription,
+ user: user,
+ token: token,
+ data: %{"alerts" => %{"mention" => true}}
+ )
+
+ res =
+ conn
+ |> delete("/api/v1/push/subscription", %{})
+ |> json_response(200)
+
+ assert %{} == res
+ refute Pleroma.Repo.get(Subscription, subscription.id)
+ end
+ end
+end
diff --git a/test/web/node_info_test.exs b/test/web/node_info_test.exs
index 763549bd1..038feecc1 100644
--- a/test/web/node_info_test.exs
+++ b/test/web/node_info_test.exs
@@ -8,7 +8,8 @@ defmodule Pleroma.Web.NodeInfoTest do
import Pleroma.Factory
test "nodeinfo shows staff accounts", %{conn: conn} do
- user = insert(:user, %{local: true, info: %{is_moderator: true}})
+ moderator = insert(:user, %{local: true, info: %{is_moderator: true}})
+ admin = insert(:user, %{local: true, info: %{is_admin: true}})
conn =
conn
@@ -16,7 +17,8 @@ defmodule Pleroma.Web.NodeInfoTest do
assert result = json_response(conn, 200)
- assert user.ap_id in result["metadata"]["staffAccounts"]
+ assert moderator.ap_id in result["metadata"]["staffAccounts"]
+ assert admin.ap_id in result["metadata"]["staffAccounts"]
end
test "nodeinfo shows restricted nicknames", %{conn: conn} do
diff --git a/test/web/push/impl_test.exs b/test/web/push/impl_test.exs
new file mode 100644
index 000000000..3f9f3d809
--- /dev/null
+++ b/test/web/push/impl_test.exs
@@ -0,0 +1,145 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Push.ImplTest do
+ use Pleroma.DataCase
+
+ alias Pleroma.Web.Push.Impl
+ alias Pleroma.Web.Push.Subscription
+
+ import Pleroma.Factory
+
+ setup_all do
+ Tesla.Mock.mock_global(fn
+ %{method: :post, url: "https://example.com/example/1234"} ->
+ %Tesla.Env{status: 200}
+
+ %{method: :post, url: "https://example.com/example/not_found"} ->
+ %Tesla.Env{status: 400}
+
+ %{method: :post, url: "https://example.com/example/bad"} ->
+ %Tesla.Env{status: 100}
+ end)
+
+ :ok
+ end
+
+ @sub %{
+ endpoint: "https://example.com/example/1234",
+ keys: %{
+ auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
+ p256dh:
+ "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
+ }
+ }
+ @api_key "BASgACIHpN1GYgzSRp"
+ @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
+
+ test "performs sending notifications" do
+ user = insert(:user)
+ user2 = insert(:user)
+ insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
+ insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
+
+ insert(:push_subscription,
+ user: user,
+ data: %{alerts: %{"follow" => true, "mention" => true}}
+ )
+
+ insert(:push_subscription,
+ user: user,
+ data: %{alerts: %{"follow" => true, "mention" => false}}
+ )
+
+ notif =
+ insert(:notification,
+ user: user,
+ activity: %Pleroma.Activity{
+ data: %{
+ "type" => "Create",
+ "actor" => user.ap_id,
+ "object" => %{"content" => "<Lorem ipsum dolor sit amet."}
+ }
+ }
+ )
+
+ assert Impl.perform_send(notif) == [:ok, :ok]
+ end
+
+ test "returns error if notif does not match " do
+ assert Impl.perform_send(%{}) == :error
+ end
+
+ test "successful message sending" do
+ assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
+ end
+
+ test "fail message sending" do
+ assert Impl.push_message(
+ @message,
+ Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
+ @api_key,
+ %Subscription{}
+ ) == :error
+ end
+
+ test "delete subsciption if restult send message between 400..500" do
+ subscription = insert(:push_subscription)
+
+ assert Impl.push_message(
+ @message,
+ Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
+ @api_key,
+ subscription
+ ) == :ok
+
+ refute Pleroma.Repo.get(Subscription, subscription.id)
+ end
+
+ test "renders body for create activity" do
+ assert Impl.format_body(
+ %{
+ activity: %{
+ data: %{
+ "type" => "Create",
+ "object" => %{
+ "content" =>
+ "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
+ }
+ }
+ }
+ },
+ %{nickname: "Bob"}
+ ) ==
+ "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
+ end
+
+ test "renders body for follow activity" do
+ assert Impl.format_body(%{activity: %{data: %{"type" => "Follow"}}}, %{nickname: "Bob"}) ==
+ "@Bob has followed you"
+ end
+
+ test "renders body for announce activity" do
+ user = insert(:user)
+
+ note =
+ insert(:note, %{
+ data: %{
+ "content" =>
+ "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
+ }
+ })
+
+ note_activity = insert(:note_activity, %{note: note})
+ announce_activity = insert(:announce_activity, %{user: user, note_activity: note_activity})
+
+ assert Impl.format_body(%{activity: announce_activity}, user) ==
+ "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
+ end
+
+ test "renders body for like activity" do
+ assert Impl.format_body(%{activity: %{data: %{"type" => "Like"}}}, %{nickname: "Bob"}) ==
+ "@Bob has favorited your post"
+ end
+end
diff --git a/test/web/rel_me_test.exs b/test/web/rel_me_test.exs
index ba8038e69..5188f4de1 100644
--- a/test/web/rel_me_test.exs
+++ b/test/web/rel_me_test.exs
@@ -11,6 +11,12 @@ defmodule Pleroma.Web.RelMeTest do
%{
method: :get,
+ url: "http://example.com/rel_me/anchor_nofollow"
+ } ->
+ %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_anchor_nofollow.html")}
+
+ %{
+ method: :get,
url: "http://example.com/rel_me/link"
} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_link.html")}
@@ -33,6 +39,7 @@ defmodule Pleroma.Web.RelMeTest do
assert Pleroma.Web.RelMe.parse("http://example.com/rel_me/link") == {:ok, hrefs}
assert Pleroma.Web.RelMe.parse("http://example.com/rel_me/anchor") == {:ok, hrefs}
+ assert Pleroma.Web.RelMe.parse("http://example.com/rel_me/anchor_nofollow") == {:ok, hrefs}
end
test "maybe_put_rel_me/2" do
@@ -49,6 +56,11 @@ defmodule Pleroma.Web.RelMeTest do
assert Pleroma.Web.RelMe.maybe_put_rel_me("http://example.com/rel_me/anchor", profile_urls) ==
attr
+ assert Pleroma.Web.RelMe.maybe_put_rel_me(
+ "http://example.com/rel_me/anchor_nofollow",
+ profile_urls
+ ) == attr
+
assert Pleroma.Web.RelMe.maybe_put_rel_me("http://example.com/rel_me/link", profile_urls) ==
attr
end
diff --git a/test/web/rich_media/helpers_test.exs b/test/web/rich_media/helpers_test.exs
new file mode 100644
index 000000000..60d93768f
--- /dev/null
+++ b/test/web/rich_media/helpers_test.exs
@@ -0,0 +1,62 @@
+defmodule Pleroma.Web.RichMedia.HelpersTest do
+ use Pleroma.DataCase
+
+ alias Pleroma.Web.CommonAPI
+
+ import Pleroma.Factory
+ import Tesla.Mock
+
+ setup do
+ mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
+ :ok
+ end
+
+ test "refuses to crawl incomplete URLs" do
+ user = insert(:user)
+
+ {:ok, activity} =
+ CommonAPI.post(user, %{
+ "status" => "[test](example.com/ogp)",
+ "content_type" => "text/markdown"
+ })
+
+ Pleroma.Config.put([:rich_media, :enabled], true)
+
+ assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
+
+ Pleroma.Config.put([:rich_media, :enabled], false)
+ end
+
+ test "refuses to crawl malformed URLs" do
+ user = insert(:user)
+
+ {:ok, activity} =
+ CommonAPI.post(user, %{
+ "status" => "[test](example.com[]/ogp)",
+ "content_type" => "text/markdown"
+ })
+
+ Pleroma.Config.put([:rich_media, :enabled], true)
+
+ assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
+
+ Pleroma.Config.put([:rich_media, :enabled], false)
+ end
+
+ test "crawls valid, complete URLs" do
+ user = insert(:user)
+
+ {:ok, activity} =
+ CommonAPI.post(user, %{
+ "status" => "[test](http://example.com/ogp)",
+ "content_type" => "text/markdown"
+ })
+
+ Pleroma.Config.put([:rich_media, :enabled], true)
+
+ assert %{page_url: "http://example.com/ogp", rich_media: _} =
+ Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
+
+ Pleroma.Config.put([:rich_media, :enabled], false)
+ end
+end