From 6908f29e0a170131d2f328d68d36f054b592a0a3 Mon Sep 17 00:00:00 2001 From: Wim Vanderbauwhede Date: Sat, 28 Apr 2018 12:01:43 +0100 Subject: Added a test (written by @andrewzah) for the MR --- test/web/twitter_api/twitter_api_controller_test.exs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test') diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 406dace1c..c239239d3 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -784,4 +784,20 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert status["id"] == activity.id end end + + test "Convert newlines to
in bio", %{conn: conn} do + user = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "description" => "Hello,\r\nWorld! I\n am a test." + }) + + user = Repo.get!(User, user.id) + assert user.bio == "Hello,
World! I
am a test." + end + + end -- cgit v1.2.3 From bc215f568473bb3801b6eff238fee48f5de2bab8 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 29 Apr 2018 11:28:26 +0200 Subject: Fix format. --- test/web/twitter_api/twitter_api_controller_test.exs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index c239239d3..d7113979a 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -786,18 +786,16 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end test "Convert newlines to
in bio", %{conn: conn} do - user = insert(:user) + user = insert(:user) - conn = - conn - |> assign(:user, user) - |> post("/api/account/update_profile.json", %{ - "description" => "Hello,\r\nWorld! I\n am a test." - }) + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "description" => "Hello,\r\nWorld! I\n am a test." + }) - user = Repo.get!(User, user.id) - assert user.bio == "Hello,
World! I
am a test." + user = Repo.get!(User, user.id) + assert user.bio == "Hello,
World! I
am a test." end - - end -- cgit v1.2.3 From fcd3eca167508c1dde290f431353ace9d513de86 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 4 May 2018 20:30:29 +0200 Subject: MastodonAPI: Support idempotency key. --- .../mastodon_api/mastodon_api_controller_test.exs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 5293b9364..432dca28a 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -63,9 +63,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "posting a status", %{conn: conn} do user = insert(:user) - conn = + idempotency_key = "Pikachu rocks!" + + conn_one = conn |> assign(:user, user) + |> put_req_header("idempotency-key", idempotency_key) |> post("/api/v1/statuses", %{ "status" => "cofe", "spoiler_text" => "2hu", @@ -73,9 +76,23 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do }) assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = - json_response(conn, 200) + json_response(conn_one, 200) assert Repo.get(Activity, id) + + conn_two = + conn + |> assign(:user, user) + |> put_req_header("idempotency-key", idempotency_key) + |> post("/api/v1/statuses", %{ + "status" => "cofe", + "spoiler_text" => "2hu", + "sensitive" => "false" + }) + + assert %{"id" => second_id} = json_response(conn_two, 200) + + assert id == second_id end test "posting a sensitive status", %{conn: conn} do -- cgit v1.2.3 From 90c4bed0af40911e0f8a28743814e4c07e91e9ae Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 4 May 2018 21:15:39 +0200 Subject: Don't expire idempotency cache for five minutes. --- test/web/mastodon_api/mastodon_api_controller_test.exs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 432dca28a..69a0299ac 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -75,6 +75,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do "sensitive" => "false" }) + {:ok, ttl} = Cachex.ttl(:user_cache, "idem:#{idempotency_key}") + # 5 Minutes + assert ttl > :timer.seconds(5 * 60 - 1) + assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = json_response(conn_one, 200) -- cgit v1.2.3 From ab4aa5720aeac8541fbf99e6c98e7260cb19d41e Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 4 May 2018 20:59:01 +0000 Subject: Fix a bunch of unused variable warnings --- test/support/builders/activity_builder.ex | 2 +- test/support/httpoison_mock.ex | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/support/builders/activity_builder.ex b/test/support/builders/activity_builder.ex index d9c188955..eb72d5ba9 100644 --- a/test/support/builders/activity_builder.ex +++ b/test/support/builders/activity_builder.ex @@ -26,7 +26,7 @@ defmodule Pleroma.Builders.ActivityBuilder do end def insert_list(times, data \\ %{}, opts \\ %{}) do - Enum.map(1..times, fn n -> + Enum.map(1..times, fn _n -> {:ok, activity} = insert(data, opts) activity end) diff --git a/test/support/httpoison_mock.ex b/test/support/httpoison_mock.ex index ba35c4460..4a5a9ea85 100644 --- a/test/support/httpoison_mock.ex +++ b/test/support/httpoison_mock.ex @@ -367,7 +367,7 @@ defmodule HTTPoisonMock do def post( "https://social.heldscal.la/main/push/hub", - {:form, data}, + {:form, _data}, "Content-type": "application/x-www-form-urlencoded" ) do {:ok, @@ -711,11 +711,11 @@ defmodule HTTPoisonMock do }"} end - def post(url, body, headers) do + def post(url, _body, _headers) do {:error, "Not implemented the mock response for post #{inspect(url)}"} end - def post(url, body, headers, options) do + def post(url, _body, _headers, _options) do {:error, "Not implemented the mock response for post #{inspect(url)}"} end end -- cgit v1.2.3 From c464355d1ac7f9558aa50f7038035b9a47614822 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 5 May 2018 11:15:57 +0200 Subject: Idempotency: Use special cache, keep for 6 hours. --- .../web/mastodon_api/mastodon_api_controller_test.exs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 69a0299ac..883ebc61e 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -75,9 +75,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do "sensitive" => "false" }) - {:ok, ttl} = Cachex.ttl(:user_cache, "idem:#{idempotency_key}") - # 5 Minutes - assert ttl > :timer.seconds(5 * 60 - 1) + {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key) + # Six hours + assert ttl > :timer.seconds(6 * 60 * 60 - 1) assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = json_response(conn_one, 200) @@ -97,6 +97,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert %{"id" => second_id} = json_response(conn_two, 200) assert id == second_id + + conn_three = + conn + |> assign(:user, user) + |> post("/api/v1/statuses", %{ + "status" => "cofe", + "spoiler_text" => "2hu", + "sensitive" => "false" + }) + + assert %{"id" => third_id} = json_response(conn_three, 200) + + refute id == third_id end test "posting a sensitive status", %{conn: conn} do -- cgit v1.2.3 From 9810153aebeb7dfd9b65774dd8775e382736bbf1 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 5 May 2018 13:40:47 +0200 Subject: Streamer: Don't send out stream events for blocked users. --- test/web/streamer_test.exs | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 test/web/streamer_test.exs (limited to 'test') diff --git a/test/web/streamer_test.exs b/test/web/streamer_test.exs new file mode 100644 index 000000000..47d491d1b --- /dev/null +++ b/test/web/streamer_test.exs @@ -0,0 +1,63 @@ +defmodule Pleroma.Web.StreamerTest do + use Pleroma.DataCase + + alias Pleroma.Web.Streamer + alias Pleroma.User + alias Pleroma.Web.CommonAPI + import Pleroma.Factory + + test "it sends to public" do + user = insert(:user) + other_user = insert(:user) + + task = + Task.async(fn -> + assert_receive {:text, _}, 4_000 + end) + + fake_socket = %{ + transport_pid: task.pid, + assigns: %{ + user: user + } + } + + {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"}) + + topics = %{ + "public" => [fake_socket] + } + + Streamer.push_to_socket(topics, "public", activity) + + Task.await(task) + end + + test "it doesn't send to blocked users" do + user = insert(:user) + blocked_user = insert(:user) + {:ok, user} = User.block(user, blocked_user) + + task = + Task.async(fn -> + refute_receive {:text, _}, 1_000 + end) + + fake_socket = %{ + transport_pid: task.pid, + assigns: %{ + user: user + } + } + + {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"}) + + topics = %{ + "public" => [fake_socket] + } + + Streamer.push_to_socket(topics, "public", activity) + + Task.await(task) + end +end -- cgit v1.2.3 From 89603eda9ea51c5bd3c681fce958a4a6b446debe Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Mon, 7 May 2018 20:51:14 +0200 Subject: do not create notification for yourself --- test/notification_test.exs | 7 +++++++ test/web/twitter_api/twitter_api_controller_test.exs | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/notification_test.exs b/test/notification_test.exs index 568ad642c..2ca1ac13d 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -33,6 +33,13 @@ defmodule Pleroma.NotificationTest do assert nil == Notification.create_notification(activity, user) end + + test "it doesn't create a notification for user if he is the activity author" do + activity = insert(:note_activity) + author = User.get_by_ap_id(activity.data["actor"]) + + assert nil == Notification.create_notification(activity, author) + end end describe "get notification" do diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index d7113979a..896fe246d 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -257,8 +257,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end test "with credentials", %{conn: conn, user: current_user} do + other_user = insert(:user) + {:ok, activity} = - ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user}) + ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user}) conn = conn -- cgit v1.2.3