diff options
Diffstat (limited to 'test')
100 files changed, 1673 insertions, 411 deletions
| diff --git a/test/activity/ir/topics_test.exs b/test/activity/ir/topics_test.exs new file mode 100644 index 000000000..e75f83586 --- /dev/null +++ b/test/activity/ir/topics_test.exs @@ -0,0 +1,141 @@ +defmodule Pleroma.Activity.Ir.TopicsTest do +  use Pleroma.DataCase + +  alias Pleroma.Activity +  alias Pleroma.Activity.Ir.Topics +  alias Pleroma.Object + +  require Pleroma.Constants + +  describe "poll answer" do +    test "produce no topics" do +      activity = %Activity{object: %Object{data: %{"type" => "Answer"}}} + +      assert [] == Topics.get_activity_topics(activity) +    end +  end + +  describe "non poll answer" do +    test "always add user and list topics" do +      activity = %Activity{object: %Object{data: %{"type" => "FooBar"}}} +      topics = Topics.get_activity_topics(activity) + +      assert Enum.member?(topics, "user") +      assert Enum.member?(topics, "list") +    end +  end + +  describe "public visibility" do +    setup do +      activity = %Activity{ +        object: %Object{data: %{"type" => "Note"}}, +        data: %{"to" => [Pleroma.Constants.as_public()]} +      } + +      {:ok, activity: activity} +    end + +    test "produces public topic", %{activity: activity} do +      topics = Topics.get_activity_topics(activity) + +      assert Enum.member?(topics, "public") +    end + +    test "local action produces public:local topic", %{activity: activity} do +      activity = %{activity | local: true} +      topics = Topics.get_activity_topics(activity) + +      assert Enum.member?(topics, "public:local") +    end + +    test "non-local action does not produce public:local topic", %{activity: activity} do +      activity = %{activity | local: false} +      topics = Topics.get_activity_topics(activity) + +      refute Enum.member?(topics, "public:local") +    end +  end + +  describe "public visibility create events" do +    setup do +      activity = %Activity{ +        object: %Object{data: %{"type" => "Create", "attachment" => []}}, +        data: %{"to" => [Pleroma.Constants.as_public()]} +      } + +      {:ok, activity: activity} +    end + +    test "with no attachments doesn't produce public:media topics", %{activity: activity} do +      topics = Topics.get_activity_topics(activity) + +      refute Enum.member?(topics, "public:media") +      refute Enum.member?(topics, "public:local:media") +    end + +    test "converts tags to hash tags", %{activity: %{object: %{data: data} = object} = activity} do +      tagged_data = Map.put(data, "tag", ["foo", "bar"]) +      activity = %{activity | object: %{object | data: tagged_data}} + +      topics = Topics.get_activity_topics(activity) + +      assert Enum.member?(topics, "hashtag:foo") +      assert Enum.member?(topics, "hashtag:bar") +    end + +    test "only converts strinngs to hash tags", %{ +      activity: %{object: %{data: data} = object} = activity +    } do +      tagged_data = Map.put(data, "tag", [2]) +      activity = %{activity | object: %{object | data: tagged_data}} + +      topics = Topics.get_activity_topics(activity) + +      refute Enum.member?(topics, "hashtag:2") +    end +  end + +  describe "public visibility create events with attachments" do +    setup do +      activity = %Activity{ +        object: %Object{data: %{"type" => "Create", "attachment" => ["foo"]}}, +        data: %{"to" => [Pleroma.Constants.as_public()]} +      } + +      {:ok, activity: activity} +    end + +    test "produce public:media topics", %{activity: activity} do +      topics = Topics.get_activity_topics(activity) + +      assert Enum.member?(topics, "public:media") +    end + +    test "local produces public:local:media topics", %{activity: activity} do +      topics = Topics.get_activity_topics(activity) + +      assert Enum.member?(topics, "public:local:media") +    end + +    test "non-local doesn't produce public:local:media topics", %{activity: activity} do +      activity = %{activity | local: false} + +      topics = Topics.get_activity_topics(activity) + +      refute Enum.member?(topics, "public:local:media") +    end +  end + +  describe "non-public visibility" do +    test "produces direct topic" do +      activity = %Activity{object: %Object{data: %{"type" => "Note"}}, data: %{"to" => []}} +      topics = Topics.get_activity_topics(activity) + +      assert Enum.member?(topics, "direct") +      refute Enum.member?(topics, "public") +      refute Enum.member?(topics, "public:local") +      refute Enum.member?(topics, "public:media") +      refute Enum.member?(topics, "public:local:media") +    end +  end +end diff --git a/test/activity_test.exs b/test/activity_test.exs index 4152aaa7e..95d9341c4 100644 --- a/test/activity_test.exs +++ b/test/activity_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.ActivityTest do @@ -7,6 +7,7 @@ defmodule Pleroma.ActivityTest do    alias Pleroma.Activity    alias Pleroma.Bookmark    alias Pleroma.Object +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.ThreadMute    import Pleroma.Factory @@ -125,7 +126,8 @@ defmodule Pleroma.ActivityTest do        }        {:ok, local_activity} = Pleroma.Web.CommonAPI.post(user, %{"status" => "find me!"}) -      {:ok, remote_activity} = Pleroma.Web.Federator.incoming_ap_doc(params) +      {:ok, job} = Pleroma.Web.Federator.incoming_ap_doc(params) +      {:ok, remote_activity} = ObanHelpers.perform(job)        %{local_activity: local_activity, remote_activity: remote_activity, user: user}      end @@ -185,4 +187,39 @@ defmodule Pleroma.ActivityTest do      assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities    end + +  test "get_by_id_with_object/1" do +    %{id: id} = insert(:note_activity) + +    assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id) +  end + +  test "get_by_ap_id_with_object/1" do +    %{data: %{"id" => ap_id}} = insert(:note_activity) + +    assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} = +             Activity.get_by_ap_id_with_object(ap_id) +  end + +  test "get_by_id/1" do +    %{id: id} = insert(:note_activity) + +    assert %Activity{id: ^id} = Activity.get_by_id(id) +  end + +  test "all_by_actor_and_id/2" do +    user = insert(:user) + +    {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofe"}) +    {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofefe"}) + +    assert [] == Activity.all_by_actor_and_id(user, []) + +    activities = +      user.ap_id +      |> Activity.all_by_actor_and_id([id1, id2]) +      |> Enum.sort(&(&1.id < &2.id)) + +    assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities +  end  end diff --git a/test/captcha_test.exs b/test/captcha_test.exs index 7ca9a4607..9f395d6b4 100644 --- a/test/captcha_test.exs +++ b/test/captcha_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.CaptchaTest do diff --git a/test/config_test.exs b/test/config_test.exs index 73f3fcb0a..438fe62ee 100644 --- a/test/config_test.exs +++ b/test/config_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.ConfigTest do diff --git a/test/conversation_test.exs b/test/conversation_test.exs index 4e36494f8..693427d80 100644 --- a/test/conversation_test.exs +++ b/test/conversation_test.exs @@ -22,6 +22,8 @@ defmodule Pleroma.ConversationTest do      {:ok, _activity} =        CommonAPI.post(user, %{"visibility" => "direct", "status" => "hey @#{other_user.nickname}"}) +    Pleroma.Tests.ObanHelpers.perform_all() +      Repo.delete_all(Conversation)      Repo.delete_all(Conversation.Participation) diff --git a/test/activity_expiration_worker_test.exs b/test/daemons/activity_expiration_daemon_test.exs index 939d912f1..b51132fb0 100644 --- a/test/activity_expiration_worker_test.exs +++ b/test/daemons/activity_expiration_daemon_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.ActivityExpirationWorkerTest do @@ -10,7 +10,7 @@ defmodule Pleroma.ActivityExpirationWorkerTest do    test "deletes an activity" do      activity = insert(:note_activity)      expiration = insert(:expiration_in_the_past, %{activity_id: activity.id}) -    Pleroma.ActivityExpirationWorker.perform(:execute, expiration.id) +    Pleroma.Daemons.ActivityExpirationDaemon.perform(:execute, expiration.id)      refute Repo.get(Activity, activity.id)    end diff --git a/test/web/digest_email_worker_test.exs b/test/daemons/digest_email_daemon_test.exs index 15002330f..3168f3b9a 100644 --- a/test/web/digest_email_worker_test.exs +++ b/test/daemons/digest_email_daemon_test.exs @@ -2,11 +2,12 @@  # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only -defmodule Pleroma.DigestEmailWorkerTest do +defmodule Pleroma.DigestEmailDaemonTest do    use Pleroma.DataCase    import Pleroma.Factory -  alias Pleroma.DigestEmailWorker +  alias Pleroma.Daemons.DigestEmailDaemon +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.CommonAPI @@ -22,7 +23,10 @@ defmodule Pleroma.DigestEmailWorkerTest do      User.switch_email_notifications(user2, "digest", true)      CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"}) -    DigestEmailWorker.perform() +    DigestEmailDaemon.perform() +    ObanHelpers.perform_all() +    # Performing job(s) enqueued at previous step +    ObanHelpers.perform_all()      assert_received {:email, email}      assert email.to == [{user2.name, user2.email}] diff --git a/test/scheduled_activity_worker_test.exs b/test/daemons/scheduled_activity_daemon_test.exs index e3ad1244e..c8e464491 100644 --- a/test/scheduled_activity_worker_test.exs +++ b/test/daemons/scheduled_activity_daemon_test.exs @@ -1,8 +1,8 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only -defmodule Pleroma.ScheduledActivityWorkerTest do +defmodule Pleroma.ScheduledActivityDaemonTest do    use Pleroma.DataCase    alias Pleroma.ScheduledActivity    import Pleroma.Factory @@ -10,7 +10,7 @@ defmodule Pleroma.ScheduledActivityWorkerTest do    test "creates a status from the scheduled activity" do      user = insert(:user)      scheduled_activity = insert(:scheduled_activity, user: user, params: %{status: "hi"}) -    Pleroma.ScheduledActivityWorker.perform(:execute, scheduled_activity.id) +    Pleroma.Daemons.ScheduledActivityDaemon.perform(:execute, scheduled_activity.id)      refute Repo.get(ScheduledActivity, scheduled_activity.id)      activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id)) diff --git a/test/emails/admin_email_test.exs b/test/emails/admin_email_test.exs index 9e83c73c6..31eac5f12 100644 --- a/test/emails/admin_email_test.exs +++ b/test/emails/admin_email_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Emails.AdminEmailTest do diff --git a/test/emails/mailer_test.exs b/test/emails/mailer_test.exs index ae5effb7a..2425c85dd 100644 --- a/test/emails/mailer_test.exs +++ b/test/emails/mailer_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Emails.MailerTest do diff --git a/test/emails/user_email_test.exs b/test/emails/user_email_test.exs index 7d8df6abc..963565f7c 100644 --- a/test/emails/user_email_test.exs +++ b/test/emails/user_email_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Emails.UserEmailTest do diff --git a/test/fixtures/tesla_mock/poll_modified.json b/test/fixtures/tesla_mock/poll_modified.json new file mode 100644 index 000000000..1d026b592 --- /dev/null +++ b/test/fixtures/tesla_mock/poll_modified.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://patch.cx/schemas/litepub-0.1.jsonld",{"@language":"und"}],"actor":"https://patch.cx/users/rin","attachment":[],"attributedTo":"https://patch.cx/users/rin","cc":["https://patch.cx/users/rin/followers"],"closed":"2019-09-19T00:32:36.785333","content":"can you vote on this poll?","context":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","conversation":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","id":"https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d","oneOf":[{"name":"yes","replies":{"totalItems":8,"type":"Collection"},"type":"Note"},{"name":"no","replies":{"totalItems":3,"type":"Collection"},"type":"Note"}],"published":"2019-09-18T14:32:36.802152Z","sensitive":false,"summary":"","tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"type":"Question"}
\ No newline at end of file diff --git a/test/fixtures/tesla_mock/poll_original.json b/test/fixtures/tesla_mock/poll_original.json new file mode 100644 index 000000000..267876b3c --- /dev/null +++ b/test/fixtures/tesla_mock/poll_original.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://patch.cx/schemas/litepub-0.1.jsonld",{"@language":"und"}],"actor":"https://patch.cx/users/rin","attachment":[],"attributedTo":"https://patch.cx/users/rin","cc":["https://patch.cx/users/rin/followers"],"closed":"2019-09-19T00:32:36.785333","content":"can you vote on this poll?","context":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","conversation":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","id":"https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d","oneOf":[{"name":"yes","replies":{"totalItems":4,"type":"Collection"},"type":"Note"},{"name":"no","replies":{"totalItems":0,"type":"Collection"},"type":"Note"}],"published":"2019-09-18T14:32:36.802152Z","sensitive":false,"summary":"","tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"type":"Question"}
\ No newline at end of file diff --git a/test/fixtures/tesla_mock/rin.json b/test/fixtures/tesla_mock/rin.json new file mode 100644 index 000000000..2cf623764 --- /dev/null +++ b/test/fixtures/tesla_mock/rin.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://patch.cx/schemas/litepub-0.1.jsonld",{"@language":"und"}],"attachment":[],"endpoints":{"oauthAuthorizationEndpoint":"https://patch.cx/oauth/authorize","oauthRegistrationEndpoint":"https://patch.cx/api/v1/apps","oauthTokenEndpoint":"https://patch.cx/oauth/token","sharedInbox":"https://patch.cx/inbox"},"followers":"https://patch.cx/users/rin/followers","following":"https://patch.cx/users/rin/following","icon":{"type":"Image","url":"https://patch.cx/media/4e914f5b84e4a259a3f6c2d2edc9ab642f2ab05f3e3d9c52c81fc2d984b3d51e.jpg"},"id":"https://patch.cx/users/rin","image":{"type":"Image","url":"https://patch.cx/media/f739efddefeee49c6e67e947c4811fdc911785c16ae43da4c3684051fbf8da6a.jpg?name=f739efddefeee49c6e67e947c4811fdc911785c16ae43da4c3684051fbf8da6a.jpg"},"inbox":"https://patch.cx/users/rin/inbox","manuallyApprovesFollowers":false,"name":"rinpatch","outbox":"https://patch.cx/users/rin/outbox","preferredUsername":"rin","publicKey":{"id":"https://patch.cx/users/rin#main-key","owner":"https://patch.cx/users/rin","publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5DLtwGXNZElJyxFGfcVc\nXANhaMadj/iYYQwZjOJTV9QsbtiNBeIK54PJrYuU0/0YIdrvS1iqheX5IwXRhcwa\nhm3ZyLz7XeN9st7FBni4BmZMBtMpxAuYuu5p/jbWy13qAiYOhPreCx0wrWgm/lBD\n9mkgaxIxPooBE0S4ZWEJIDIV1Vft3AWcRUyWW1vIBK0uZzs6GYshbQZB952S0yo4\nFzI1hABGHncH8UvuFauh4EZ8tY7/X5I0pGRnDOcRN1dAht5w5yTA+6r5kebiFQjP\nIzN/eCO/a9Flrj9YGW7HDNtjSOH0A31PLRGlJtJO3yK57dnf5ppyCZGfL4emShQo\ncQIDAQAB\n-----END PUBLIC KEY-----\n\n"},"summary":"your friendly neighborhood pleroma developer<br>I like cute things and distributed systems, and really hate delete and redrafts","tag":[],"type":"Person","url":"https://patch.cx/users/rin"}
\ No newline at end of file diff --git a/test/formatter_test.exs b/test/formatter_test.exs index bfa673049..c443dfe7c 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.FormatterTest do diff --git a/test/html_test.exs b/test/html_test.exs index b8906c46a..306ad3b3b 100644 --- a/test/html_test.exs +++ b/test/html_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.HTMLTest do diff --git a/test/integration/mastodon_websocket_test.exs b/test/integration/mastodon_websocket_test.exs index 3975cdcd6..ed7ce8fe0 100644 --- a/test/integration/mastodon_websocket_test.exs +++ b/test/integration/mastodon_websocket_test.exs @@ -1,16 +1,16 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Integration.MastodonWebsocketTest do    use Pleroma.DataCase +  import ExUnit.CaptureLog    import Pleroma.Factory    alias Pleroma.Integration.WebsocketClient    alias Pleroma.Web.CommonAPI    alias Pleroma.Web.OAuth -  alias Pleroma.Web.Streamer    @path Pleroma.Web.Endpoint.url()          |> URI.parse() @@ -18,14 +18,9 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do          |> Map.put(:path, "/api/v1/streaming")          |> URI.to_string() -  setup do -    GenServer.start(Streamer, %{}, name: Streamer) - -    on_exit(fn -> -      if pid = Process.whereis(Streamer) do -        Process.exit(pid, :kill) -      end -    end) +  setup_all do +    start_supervised(Pleroma.Web.Streamer.supervisor()) +    :ok    end    def start_socket(qs \\ nil, headers \\ []) do @@ -39,13 +34,19 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do    end    test "refuses invalid requests" do -    assert {:error, {400, _}} = start_socket() -    assert {:error, {404, _}} = start_socket("?stream=ncjdk") +    capture_log(fn -> +      assert {:error, {400, _}} = start_socket() +      assert {:error, {404, _}} = start_socket("?stream=ncjdk") +      Process.sleep(30) +    end)    end    test "requires authentication and a valid token for protected streams" do -    assert {:error, {403, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa") -    assert {:error, {403, _}} = start_socket("?stream=user") +    capture_log(fn -> +      assert {:error, {403, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa") +      assert {:error, {403, _}} = start_socket("?stream=user") +      Process.sleep(30) +    end)    end    test "allows public streams without authentication" do @@ -100,19 +101,31 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do      test "accepts the 'user' stream", %{token: token} = _state do        assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}") -      assert {:error, {403, "Forbidden"}} = start_socket("?stream=user") + +      assert capture_log(fn -> +               assert {:error, {403, "Forbidden"}} = start_socket("?stream=user") +               Process.sleep(30) +             end) =~ ":badarg"      end      test "accepts the 'user:notification' stream", %{token: token} = _state do        assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}") -      assert {:error, {403, "Forbidden"}} = start_socket("?stream=user:notification") + +      assert capture_log(fn -> +               assert {:error, {403, "Forbidden"}} = start_socket("?stream=user:notification") +               Process.sleep(30) +             end) =~ ":badarg"      end      test "accepts valid token on Sec-WebSocket-Protocol header", %{token: token} do        assert {:ok, _} = start_socket("?stream=user", [{"Sec-WebSocket-Protocol", token.token}]) -      assert {:error, {403, "Forbidden"}} = -               start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}]) +      assert capture_log(fn -> +               assert {:error, {403, "Forbidden"}} = +                        start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}]) + +               Process.sleep(30) +             end) =~ ":badarg"      end    end  end diff --git a/test/list_test.exs b/test/list_test.exs index 8efba75ea..ba79251da 100644 --- a/test/list_test.exs +++ b/test/list_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.ListTest do diff --git a/test/notification_test.exs b/test/notification_test.exs index 2a52dad8d..54c0f9877 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.NotificationTest do @@ -8,6 +8,7 @@ defmodule Pleroma.NotificationTest do    import Pleroma.Factory    alias Pleroma.Notification +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.ActivityPub.Transmogrifier    alias Pleroma.Web.CommonAPI @@ -68,16 +69,7 @@ defmodule Pleroma.NotificationTest do    end    describe "create_notification" do -    setup do -      GenServer.start(Streamer, %{}, name: Streamer) - -      on_exit(fn -> -        if pid = Process.whereis(Streamer) do -          Process.exit(pid, :kill) -        end -      end) -    end - +    @tag needs_streamer: true      test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do        user = insert(:user)        task = Task.async(fn -> assert_receive {:text, _}, 4_000 end) @@ -588,7 +580,8 @@ defmodule Pleroma.NotificationTest do        refute Enum.empty?(Notification.for_user(other_user)) -      User.delete(user) +      {:ok, job} = User.delete(user) +      ObanHelpers.perform(job)        assert Enum.empty?(Notification.for_user(other_user))      end @@ -633,6 +626,7 @@ defmodule Pleroma.NotificationTest do        }        {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message) +      ObanHelpers.perform_all()        assert Enum.empty?(Notification.for_user(local_user))      end diff --git a/test/object_test.exs b/test/object_test.exs index ba96aeea4..dd228c32f 100644 --- a/test/object_test.exs +++ b/test/object_test.exs @@ -1,13 +1,16 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.ObjectTest do    use Pleroma.DataCase +  import ExUnit.CaptureLog    import Pleroma.Factory    import Tesla.Mock +  alias Pleroma.Activity    alias Pleroma.Object    alias Pleroma.Repo +  alias Pleroma.Web.CommonAPI    setup do      mock(fn env -> apply(HttpRequestMock, :request, [env]) end) @@ -89,4 +92,110 @@ defmodule Pleroma.ObjectTest do               )      end    end + +  describe "get_by_id_and_maybe_refetch" do +    setup do +      mock(fn +        %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} -> +          %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_original.json")} + +        env -> +          apply(HttpRequestMock, :request, [env]) +      end) + +      mock_modified = fn resp -> +        mock(fn +          %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} -> +            resp + +          env -> +            apply(HttpRequestMock, :request, [env]) +        end) +      end + +      on_exit(fn -> mock(fn env -> apply(HttpRequestMock, :request, [env]) end) end) + +      [mock_modified: mock_modified] +    end + +    test "refetches if the time since the last refetch is greater than the interval", %{ +      mock_modified: mock_modified +    } do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      mock_modified.(%Tesla.Env{ +        status: 200, +        body: File.read!("test/fixtures/tesla_mock/poll_modified.json") +      }) + +      updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1) +      assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8 +      assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3 +    end + +    test "returns the old object if refetch fails", %{mock_modified: mock_modified} do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      assert capture_log(fn -> +               mock_modified.(%Tesla.Env{status: 404, body: ""}) + +               updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1) +               assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +               assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0 +             end) =~ +               "[error] Couldn't refresh https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d" +    end + +    test "does not refetch if the time since the last refetch is greater than the interval", %{ +      mock_modified: mock_modified +    } do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      mock_modified.(%Tesla.Env{ +        status: 200, +        body: File.read!("test/fixtures/tesla_mock/poll_modified.json") +      }) + +      updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: 100) +      assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0 +    end + +    test "preserves internal fields on refetch", %{mock_modified: mock_modified} do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      user = insert(:user) +      activity = Activity.get_create_by_object_ap_id(object.data["id"]) +      {:ok, _activity, object} = CommonAPI.favorite(activity.id, user) + +      assert object.data["like_count"] == 1 + +      mock_modified.(%Tesla.Env{ +        status: 200, +        body: File.read!("test/fixtures/tesla_mock/poll_modified.json") +      }) + +      updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1) +      assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8 +      assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3 + +      assert updated_object.data["like_count"] == 1 +    end +  end  end diff --git a/test/plugs/authentication_plug_test.exs b/test/plugs/authentication_plug_test.exs index f7f8fd9f3..9ae4c506f 100644 --- a/test/plugs/authentication_plug_test.exs +++ b/test/plugs/authentication_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.AuthenticationPlugTest do diff --git a/test/plugs/cache_control_test.exs b/test/plugs/cache_control_test.exs index 45151b289..69ce6cc7d 100644 --- a/test/plugs/cache_control_test.exs +++ b/test/plugs/cache_control_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.CacheControlTest do diff --git a/test/plugs/ensure_public_or_authenticated_plug_test.exs b/test/plugs/ensure_public_or_authenticated_plug_test.exs index d45662a2a..bae95e150 100644 --- a/test/plugs/ensure_public_or_authenticated_plug_test.exs +++ b/test/plugs/ensure_public_or_authenticated_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.EnsurePublicOrAuthenticatedPlugTest do diff --git a/test/plugs/http_security_plug_test.exs b/test/plugs/http_security_plug_test.exs index 7a2835e3d..9c1c20541 100644 --- a/test/plugs/http_security_plug_test.exs +++ b/test/plugs/http_security_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do diff --git a/test/plugs/http_signature_plug_test.exs b/test/plugs/http_signature_plug_test.exs index d6fd9ea81..d8ace36da 100644 --- a/test/plugs/http_signature_plug_test.exs +++ b/test/plugs/http_signature_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.Plugs.HTTPSignaturePlugTest do diff --git a/test/plugs/instance_static_test.exs b/test/plugs/instance_static_test.exs index 6aabc45a4..9b27246fa 100644 --- a/test/plugs/instance_static_test.exs +++ b/test/plugs/instance_static_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.RuntimeStaticPlugTest do diff --git a/test/plugs/legacy_authentication_plug_test.exs b/test/plugs/legacy_authentication_plug_test.exs index 9804e073b..568ef5abd 100644 --- a/test/plugs/legacy_authentication_plug_test.exs +++ b/test/plugs/legacy_authentication_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.LegacyAuthenticationPlugTest do diff --git a/test/plugs/mapped_identity_to_signature_plug_test.exs b/test/plugs/mapped_identity_to_signature_plug_test.exs index bb45d9edf..6b9d3649d 100644 --- a/test/plugs/mapped_identity_to_signature_plug_test.exs +++ b/test/plugs/mapped_identity_to_signature_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.Plugs.MappedSignatureToIdentityPlugTest do diff --git a/test/plugs/oauth_plug_test.exs b/test/plugs/oauth_plug_test.exs index 5a2ed11cc..dea11cdb0 100644 --- a/test/plugs/oauth_plug_test.exs +++ b/test/plugs/oauth_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.OAuthPlugTest do diff --git a/test/plugs/oauth_scopes_plug_test.exs b/test/plugs/oauth_scopes_plug_test.exs index f328026df..6a13ea811 100644 --- a/test/plugs/oauth_scopes_plug_test.exs +++ b/test/plugs/oauth_scopes_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.OAuthScopesPlugTest do diff --git a/test/plugs/set_format_plug_test.exs b/test/plugs/set_format_plug_test.exs index bb21956bb..27c026fdd 100644 --- a/test/plugs/set_format_plug_test.exs +++ b/test/plugs/set_format_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.SetFormatPlugTest do diff --git a/test/plugs/set_locale_plug_test.exs b/test/plugs/set_locale_plug_test.exs index b6c4c1cea..0aaeedc1e 100644 --- a/test/plugs/set_locale_plug_test.exs +++ b/test/plugs/set_locale_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.SetLocalePlugTest do diff --git a/test/plugs/uploaded_media_plug_test.exs b/test/plugs/uploaded_media_plug_test.exs index 49cf5396a..5ba963139 100644 --- a/test/plugs/uploaded_media_plug_test.exs +++ b/test/plugs/uploaded_media_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.UploadedMediaPlugTest do diff --git a/test/scheduled_activity_test.exs b/test/scheduled_activity_test.exs index edc7cc3f9..dcf12fb49 100644 --- a/test/scheduled_activity_test.exs +++ b/test/scheduled_activity_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.ScheduledActivityTest do diff --git a/test/support/captcha_mock.ex b/test/support/captcha_mock.ex index ef4e68bc5..65ca6b3bd 100644 --- a/test/support/captcha_mock.ex +++ b/test/support/captcha_mock.ex @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Captcha.Mock do diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index ec5892ff5..9897f72ce 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.ConnCase do @@ -40,6 +40,10 @@ defmodule Pleroma.Web.ConnCase do        Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})      end +    if tags[:needs_streamer] do +      start_supervised(Pleroma.Web.Streamer.supervisor()) +    end +      {:ok, conn: Phoenix.ConnTest.build_conn()}    end  end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index f3d98e7e3..4ffcbac9e 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.DataCase do @@ -39,6 +39,10 @@ defmodule Pleroma.DataCase do        Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})      end +    if tags[:needs_streamer] do +      start_supervised(Pleroma.Web.Streamer.supervisor()) +    end +      :ok    end diff --git a/test/support/helpers.ex b/test/support/helpers.ex index a601b3ec8..ce39dd9d8 100644 --- a/test/support/helpers.ex +++ b/test/support/helpers.ex @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Tests.Helpers do diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex index 231e7c498..5506c0626 100644 --- a/test/support/http_request_mock.ex +++ b/test/support/http_request_mock.ex @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule HttpRequestMock do @@ -1004,6 +1004,10 @@ defmodule HttpRequestMock do      {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/sjw.json")}}    end +  def get("https://patch.cx/users/rin", _, _, _) do +    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/rin.json")}} +  end +    def get(url, query, body, headers) do      {:error,       "Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{ diff --git a/test/support/mrf_module_mock.ex b/test/support/mrf_module_mock.ex index 12c7e22bc..632c7ff1d 100644 --- a/test/support/mrf_module_mock.ex +++ b/test/support/mrf_module_mock.ex @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule MRFModuleMock do diff --git a/test/support/oban_helpers.ex b/test/support/oban_helpers.ex new file mode 100644 index 000000000..72792c064 --- /dev/null +++ b/test/support/oban_helpers.ex @@ -0,0 +1,42 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Tests.ObanHelpers do +  @moduledoc """ +  Oban test helpers. +  """ + +  alias Pleroma.Repo + +  def perform_all do +    Oban.Job +    |> Repo.all() +    |> perform() +  end + +  def perform(%Oban.Job{} = job) do +    res = apply(String.to_existing_atom("Elixir." <> job.worker), :perform, [job.args, job]) +    Repo.delete(job) +    res +  end + +  def perform(jobs) when is_list(jobs) do +    for job <- jobs, do: perform(job) +  end + +  def member?(%{} = job_args, jobs) when is_list(jobs) do +    Enum.any?(jobs, fn job -> +      member?(job_args, job.args) +    end) +  end + +  def member?(%{} = test_attrs, %{} = attrs) do +    Enum.all?( +      test_attrs, +      fn {k, _v} -> member?(test_attrs[k], attrs[k]) end +    ) +  end + +  def member?(x, y), do: x == y +end diff --git a/test/support/web_push_http_client_mock.ex b/test/support/web_push_http_client_mock.ex index d8accd21c..1d6ccff7e 100644 --- a/test/support/web_push_http_client_mock.ex +++ b/test/support/web_push_http_client_mock.ex @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.WebPushHttpClientMock do diff --git a/test/tasks/digest_test.exs b/test/tasks/digest_test.exs index 4bfa1fb93..96d762685 100644 --- a/test/tasks/digest_test.exs +++ b/test/tasks/digest_test.exs @@ -4,6 +4,7 @@ defmodule Mix.Tasks.Pleroma.DigestTest do    import Pleroma.Factory    import Swoosh.TestAssertions +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.Web.CommonAPI    setup_all do @@ -39,6 +40,8 @@ defmodule Mix.Tasks.Pleroma.DigestTest do        :ok = Mix.Tasks.Pleroma.Digest.run(["test", user2.nickname, yesterday_date]) +      ObanHelpers.perform_all() +        assert_receive {:mix_shell, :info, [message]}        assert message =~ "Digest email have been sent" diff --git a/test/tasks/ecto/migrate_test.exs b/test/tasks/ecto/migrate_test.exs index 0538a7b40..42f6cbf47 100644 --- a/test/tasks/ecto/migrate_test.exs +++ b/test/tasks/ecto/migrate_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-onl  defmodule Mix.Tasks.Pleroma.Ecto.MigrateTest do diff --git a/test/tasks/relay_test.exs b/test/tasks/relay_test.exs index 7bde56606..c866608ab 100644 --- a/test/tasks/relay_test.exs +++ b/test/tasks/relay_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Mix.Tasks.Pleroma.RelayTest do diff --git a/test/tasks/user_test.exs b/test/tasks/user_test.exs index 2b9453042..cf12d9ed6 100644 --- a/test/tasks/user_test.exs +++ b/test/tasks/user_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Mix.Tasks.Pleroma.UserTest do diff --git a/test/test_helper.exs b/test/test_helper.exs index a927b2c3d..c8dbee010 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  os_exclude = if :os.type() == {:unix, :darwin}, do: [skip_on_mac: true], else: [] @@ -7,3 +7,8 @@ ExUnit.start(exclude: os_exclude)  Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, :manual)  Mox.defmock(Pleroma.ReverseProxy.ClientMock, for: Pleroma.ReverseProxy.Client)  {:ok, _} = Application.ensure_all_started(:ex_machina) + +ExUnit.after_suite(fn _results -> +  uploads = Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads], "test/uploads") +  File.rm_rf!(uploads) +end) diff --git a/test/upload_test.exs b/test/upload_test.exs index 6721fe82e..0ca5ebced 100644 --- a/test/upload_test.exs +++ b/test/upload_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.UploadTest do diff --git a/test/user_search_test.exs b/test/user_search_test.exs index 48ce973ad..f7ab31287 100644 --- a/test/user_search_test.exs +++ b/test/user_search_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.UserSearchTest do diff --git a/test/user_test.exs b/test/user_test.exs index a25b72f4e..39ba69668 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.UserTest do @@ -7,14 +7,16 @@ defmodule Pleroma.UserTest do    alias Pleroma.Builders.UserBuilder    alias Pleroma.Object    alias Pleroma.Repo +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.ActivityPub.ActivityPub    alias Pleroma.Web.CommonAPI    use Pleroma.DataCase +  use Oban.Testing, repo: Pleroma.Repo -  import Pleroma.Factory    import Mock +  import Pleroma.Factory    setup_all do      Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) @@ -570,22 +572,6 @@ defmodule Pleroma.UserTest do          refute cs.valid?        end)      end - -    test "it restricts some sizes" do -      bio_limit = Pleroma.Config.get([:instance, :user_bio_length], 5000) -      name_limit = Pleroma.Config.get([:instance, :user_name_length], 100) - -      [bio: bio_limit, name: name_limit] -      |> Enum.each(fn {field, size} -> -        string = String.pad_leading(".", size) -        cs = User.remote_user_creation(Map.put(@valid_remote, field, string)) -        assert cs.valid? - -        string = String.pad_leading(".", size + 1) -        cs = User.remote_user_creation(Map.put(@valid_remote, field, string)) -        refute cs.valid? -      end) -    end    end    describe "followers and friends" do @@ -725,7 +711,9 @@ defmodule Pleroma.UserTest do          user3.nickname        ] -      result = User.follow_import(user1, identifiers) +      {:ok, job} = User.follow_import(user1, identifiers) +      result = ObanHelpers.perform(job) +        assert is_list(result)        assert result == [user2, user3]      end @@ -936,7 +924,9 @@ defmodule Pleroma.UserTest do          user3.nickname        ] -      result = User.blocks_import(user1, identifiers) +      {:ok, job} = User.blocks_import(user1, identifiers) +      result = ObanHelpers.perform(job) +        assert is_list(result)        assert result == [user2, user3]      end @@ -1053,7 +1043,9 @@ defmodule Pleroma.UserTest do      test "it deletes deactivated user" do        {:ok, user} = insert(:user, info: %{deactivated: true}) |> User.set_cache() -      assert {:ok, _} = User.delete(user) +      {:ok, job} = User.delete(user) +      {:ok, _user} = ObanHelpers.perform(job) +        refute User.get_by_id(user.id)      end @@ -1071,7 +1063,8 @@ defmodule Pleroma.UserTest do        {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)        {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user) -      {:ok, _} = User.delete(user) +      {:ok, job} = User.delete(user) +      {:ok, _user} = ObanHelpers.perform(job)        follower = User.get_cached_by_id(follower.id) @@ -1081,7 +1074,7 @@ defmodule Pleroma.UserTest do        user_activities =          user.ap_id -        |> Activity.query_by_actor() +        |> Activity.Queries.by_actor()          |> Repo.all()          |> Enum.map(fn act -> act.data["type"] end) @@ -1103,12 +1096,18 @@ defmodule Pleroma.UserTest do        {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")        {:ok, _} = User.follow(follower, user) -      {:ok, _user} = User.delete(user) - -      assert called( -               Pleroma.Web.ActivityPub.Publisher.publish_one(%{ -                 inbox: "http://mastodon.example.org/inbox" -               }) +      {:ok, job} = User.delete(user) +      {:ok, _user} = ObanHelpers.perform(job) + +      assert ObanHelpers.member?( +               %{ +                 "op" => "publish_one", +                 "params" => %{ +                   "inbox" => "http://mastodon.example.org/inbox", +                   "id" => "pleroma:fakeid" +                 } +               }, +               all_enqueued(worker: Pleroma.Workers.PublisherWorker)               )      end    end @@ -1117,11 +1116,60 @@ defmodule Pleroma.UserTest do      assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")    end -  test "insert or update a user from given data" do -    user = insert(:user, %{nickname: "nick@name.de"}) -    data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname} +  describe "insert or update a user from given data" do +    test "with normal data" do +      user = insert(:user, %{nickname: "nick@name.de"}) +      data = %{ap_id: user.ap_id <> "xxx", name: user.name, nickname: user.nickname} + +      assert {:ok, %User{}} = User.insert_or_update_user(data) +    end + +    test "with overly long fields" do +      current_max_length = Pleroma.Config.get([:instance, :account_field_value_length], 255) +      user = insert(:user, nickname: "nickname@supergood.domain") + +      data = %{ +        ap_id: user.ap_id, +        name: user.name, +        nickname: user.nickname, +        info: %{ +          fields: [ +            %{"name" => "myfield", "value" => String.duplicate("h", current_max_length + 1)} +          ] +        } +      } + +      assert {:ok, %User{}} = User.insert_or_update_user(data) +    end + +    test "with an overly long bio" do +      current_max_length = Pleroma.Config.get([:instance, :user_bio_length], 5000) +      user = insert(:user, nickname: "nickname@supergood.domain") -    assert {:ok, %User{}} = User.insert_or_update_user(data) +      data = %{ +        ap_id: user.ap_id, +        name: user.name, +        nickname: user.nickname, +        bio: String.duplicate("h", current_max_length + 1), +        info: %{} +      } + +      assert {:ok, %User{}} = User.insert_or_update_user(data) +    end + +    test "with an overly long display name" do +      current_max_length = Pleroma.Config.get([:instance, :user_name_length], 100) +      user = insert(:user, nickname: "nickname@supergood.domain") + +      data = %{ +        ap_id: user.ap_id, +        name: String.duplicate("h", current_max_length + 1), +        nickname: user.nickname, +        info: %{} +      } + +      assert {:ok, %User{}} = User.insert_or_update_user(data) +    end    end    describe "per-user rich-text filtering" do @@ -1153,7 +1201,8 @@ defmodule Pleroma.UserTest do      test "User.delete() plugs any possible zombie objects" do        user = insert(:user) -      {:ok, _} = User.delete(user) +      {:ok, job} = User.delete(user) +      {:ok, _} = ObanHelpers.perform(job)        {:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}") @@ -1614,4 +1663,31 @@ defmodule Pleroma.UserTest do        assert User.user_info(other_user).following_count == 152      end    end + +  describe "change_email/2" do +    setup do +      [user: insert(:user)] +    end + +    test "blank email returns error", %{user: user} do +      assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "") +      assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil) +    end + +    test "non unique email returns error", %{user: user} do +      %{email: email} = insert(:user) + +      assert {:error, %{errors: [email: {"has already been taken", _}]}} = +               User.change_email(user, email) +    end + +    test "invalid email returns error", %{user: user} do +      assert {:error, %{errors: [email: {"has invalid format", _}]}} = +               User.change_email(user, "cofe") +    end + +    test "changes email", %{user: user} do +      assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party") +    end +  end  end diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index 9698c7099..9e8e420ec 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -1,19 +1,24 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do    use Pleroma.Web.ConnCase +  use Oban.Testing, repo: Pleroma.Repo +    import Pleroma.Factory    alias Pleroma.Activity +  alias Pleroma.Delivery    alias Pleroma.Instances    alias Pleroma.Object +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.ActivityPub.ObjectView    alias Pleroma.Web.ActivityPub.Relay    alias Pleroma.Web.ActivityPub.UserView    alias Pleroma.Web.ActivityPub.Utils    alias Pleroma.Web.CommonAPI +  alias Pleroma.Workers.ReceiverWorker    setup_all do      Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) @@ -365,7 +370,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do          |> post("/inbox", data)        assert "ok" == json_response(conn, 200) -      :timer.sleep(500) + +      ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))        assert Activity.get_by_ap_id(data["id"])      end @@ -407,7 +413,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do          |> post("/users/#{user.nickname}/inbox", data)        assert "ok" == json_response(conn, 200) -      :timer.sleep(500) +      ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))        assert Activity.get_by_ap_id(data["id"])      end @@ -436,7 +442,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do          |> post("/users/#{recipient.nickname}/inbox", data)        assert "ok" == json_response(conn, 200) -      :timer.sleep(500) +      ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))        assert Activity.get_by_ap_id(data["id"])      end @@ -526,6 +532,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        |> post("/users/#{recipient.nickname}/inbox", data)        |> json_response(200) +      ObanHelpers.perform(all_enqueued(worker: ReceiverWorker)) +        activity = Activity.get_by_ap_id(data["id"])        assert activity.id @@ -601,6 +609,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do          |> post("/users/#{user.nickname}/outbox", data)        result = json_response(conn, 201) +        assert Activity.get_by_ap_id(result["id"])      end @@ -885,4 +894,86 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        assert result["totalItems"] == 15      end    end + +  describe "delivery tracking" do +    test "it tracks a signed object fetch", %{conn: conn} do +      user = insert(:user, local: false) +      activity = insert(:note_activity) +      object = Object.normalize(activity) + +      object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url()) + +      conn +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, user) +      |> get(object_path) +      |> json_response(200) + +      assert Delivery.get(object.id, user.id) +    end + +    test "it tracks a signed activity fetch", %{conn: conn} do +      user = insert(:user, local: false) +      activity = insert(:note_activity) +      object = Object.normalize(activity) + +      activity_path = String.trim_leading(activity.data["id"], Pleroma.Web.Endpoint.url()) + +      conn +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, user) +      |> get(activity_path) +      |> json_response(200) + +      assert Delivery.get(object.id, user.id) +    end + +    test "it tracks a signed object fetch when the json is cached", %{conn: conn} do +      user = insert(:user, local: false) +      other_user = insert(:user, local: false) +      activity = insert(:note_activity) +      object = Object.normalize(activity) + +      object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url()) + +      conn +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, user) +      |> get(object_path) +      |> json_response(200) + +      build_conn() +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, other_user) +      |> get(object_path) +      |> json_response(200) + +      assert Delivery.get(object.id, user.id) +      assert Delivery.get(object.id, other_user.id) +    end + +    test "it tracks a signed activity fetch when the json is cached", %{conn: conn} do +      user = insert(:user, local: false) +      other_user = insert(:user, local: false) +      activity = insert(:note_activity) +      object = Object.normalize(activity) + +      activity_path = String.trim_leading(activity.data["id"], Pleroma.Web.Endpoint.url()) + +      conn +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, user) +      |> get(activity_path) +      |> json_response(200) + +      build_conn() +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, other_user) +      |> get(activity_path) +      |> json_response(200) + +      assert Delivery.get(object.id, user.id) +      assert Delivery.get(object.id, other_user.id) +    end +  end  end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index f72b44aed..4100108a5 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -38,9 +38,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do          stream: fn _, _ -> nil end do          ActivityPub.stream_out_participations(conversation.participations) -        Enum.each(participations, fn participation -> -          assert called(Pleroma.Web.Streamer.stream("participation", participation)) -        end) +        assert called(Pleroma.Web.Streamer.stream("participation", participations))        end      end    end @@ -686,7 +684,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        user = insert(:user)        {:ok, like_activity, _object} = ActivityPub.like(user, object_activity) -      assert called(Pleroma.Web.Federator.publish(like_activity, 5)) +      assert called(Pleroma.Web.Federator.publish(like_activity))      end      test "returns exist activity if object already liked" do @@ -747,7 +745,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)        assert object.data["like_count"] == 0 -      assert called(Pleroma.Web.Federator.publish(unlike_activity, 5)) +      assert called(Pleroma.Web.Federator.publish(unlike_activity))      end      test "unliking a previously liked object" do diff --git a/test/web/activity_pub/mrf/mediaproxy_warming_policy_test.exs b/test/web/activity_pub/mrf/mediaproxy_warming_policy_test.exs index 372e789be..95a809d25 100644 --- a/test/web/activity_pub/mrf/mediaproxy_warming_policy_test.exs +++ b/test/web/activity_pub/mrf/mediaproxy_warming_policy_test.exs @@ -6,6 +6,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicyTest do    use Pleroma.DataCase    alias Pleroma.HTTP +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy    import Mock @@ -24,6 +25,11 @@ defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicyTest do    test "it prefetches media proxy URIs" do      with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do        MediaProxyWarmingPolicy.filter(@message) + +      ObanHelpers.perform_all() +      # Performing jobs which has been just enqueued +      ObanHelpers.perform_all() +        assert called(HTTP.get(:_, :_, :_))      end    end diff --git a/test/web/activity_pub/publisher_test.exs b/test/web/activity_pub/publisher_test.exs index 36a39c84c..df03b4008 100644 --- a/test/web/activity_pub/publisher_test.exs +++ b/test/web/activity_pub/publisher_test.exs @@ -3,15 +3,18 @@  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.ActivityPub.PublisherTest do -  use Pleroma.DataCase +  use Pleroma.Web.ConnCase +  import ExUnit.CaptureLog    import Pleroma.Factory    import Tesla.Mock    import Mock    alias Pleroma.Activity    alias Pleroma.Instances +  alias Pleroma.Object    alias Pleroma.Web.ActivityPub.Publisher +  alias Pleroma.Web.CommonAPI    @as_public "https://www.w3.org/ns/activitystreams#Public" @@ -188,7 +191,10 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        actor = insert(:user)        inbox = "http://connrefused.site/users/nick1/inbox" -      assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) +      assert capture_log(fn -> +               assert {:error, _} = +                        Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) +             end) =~ "connrefused"        assert called(Instances.set_unreachable(inbox))      end @@ -212,14 +218,16 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        actor = insert(:user)        inbox = "http://connrefused.site/users/nick1/inbox" -      assert {:error, _} = -               Publisher.publish_one(%{ -                 inbox: inbox, -                 json: "{}", -                 actor: actor, -                 id: 1, -                 unreachable_since: NaiveDateTime.utc_now() -               }) +      assert capture_log(fn -> +               assert {:error, _} = +                        Publisher.publish_one(%{ +                          inbox: inbox, +                          json: "{}", +                          actor: actor, +                          id: 1, +                          unreachable_since: NaiveDateTime.utc_now() +                        }) +             end) =~ "connrefused"        refute called(Instances.set_unreachable(inbox))      end @@ -257,10 +265,74 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        assert called(                 Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{                   inbox: "https://domain.com/users/nick1/inbox", -                 actor: actor, +                 actor_id: actor.id,                   id: note_activity.data["id"]                 })               )      end + +    test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.", +                   Pleroma.Web.Federator.Publisher, +                   [:passthrough], +                   [] do +      fetcher = +        insert(:user, +          local: false, +          info: %{ +            ap_enabled: true, +            source_data: %{"inbox" => "https://domain.com/users/nick1/inbox"} +          } +        ) + +      another_fetcher = +        insert(:user, +          local: false, +          info: %{ +            ap_enabled: true, +            source_data: %{"inbox" => "https://domain2.com/users/nick1/inbox"} +          } +        ) + +      actor = insert(:user) + +      note_activity = insert(:note_activity, user: actor) +      object = Object.normalize(note_activity) + +      activity_path = String.trim_leading(note_activity.data["id"], Pleroma.Web.Endpoint.url()) +      object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url()) + +      build_conn() +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, fetcher) +      |> get(object_path) +      |> json_response(200) + +      build_conn() +      |> put_req_header("accept", "application/activity+json") +      |> assign(:user, another_fetcher) +      |> get(activity_path) +      |> json_response(200) + +      {:ok, delete} = CommonAPI.delete(note_activity.id, actor) + +      res = Publisher.publish(actor, delete) +      assert res == :ok + +      assert called( +               Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ +                 inbox: "https://domain.com/users/nick1/inbox", +                 actor_id: actor.id, +                 id: delete.data["id"] +               }) +             ) + +      assert called( +               Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ +                 inbox: "https://domain2.com/users/nick1/inbox", +                 actor_id: actor.id, +                 id: delete.data["id"] +               }) +             ) +    end    end  end diff --git a/test/web/activity_pub/relay_test.exs b/test/web/activity_pub/relay_test.exs index 4f7d592a6..0f7556538 100644 --- a/test/web/activity_pub/relay_test.exs +++ b/test/web/activity_pub/relay_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.ActivityPub.RelayTest do @@ -10,6 +10,7 @@ defmodule Pleroma.Web.ActivityPub.RelayTest do    alias Pleroma.Web.ActivityPub.ActivityPub    alias Pleroma.Web.ActivityPub.Relay +  import ExUnit.CaptureLog    import Pleroma.Factory    import Mock @@ -20,7 +21,9 @@ defmodule Pleroma.Web.ActivityPub.RelayTest do    describe "follow/1" do      test "returns errors when user not found" do -      assert Relay.follow("test-ap-id") == {:error, "Could not fetch by AP id"} +      assert capture_log(fn -> +               assert Relay.follow("test-ap-id") == {:error, "Could not fetch by AP id"} +             end) =~ "Could not fetch by AP id"      end      test "returns activity" do @@ -37,7 +40,9 @@ defmodule Pleroma.Web.ActivityPub.RelayTest do    describe "unfollow/1" do      test "returns errors when user not found" do -      assert Relay.unfollow("test-ap-id") == {:error, "Could not fetch by AP id"} +      assert capture_log(fn -> +               assert Relay.unfollow("test-ap-id") == {:error, "Could not fetch by AP id"} +             end) =~ "Could not fetch by AP id"      end      test "returns activity" do @@ -78,7 +83,9 @@ defmodule Pleroma.Web.ActivityPub.RelayTest do            }          ) -      assert Relay.publish(activity) == {:error, nil} +      assert capture_log(fn -> +               assert Relay.publish(activity) == {:error, nil} +             end) =~ "[error] error: nil"      end      test_with_mock "returns announce activity and publish to federate", @@ -92,7 +99,7 @@ defmodule Pleroma.Web.ActivityPub.RelayTest do        assert activity.data["type"] == "Announce"        assert activity.data["actor"] == service_actor.ap_id        assert activity.data["object"] == obj.data["id"] -      assert called(Pleroma.Web.Federator.publish(activity, 5)) +      assert called(Pleroma.Web.Federator.publish(activity))      end      test_with_mock "returns announce activity and not publish to federate", @@ -106,7 +113,7 @@ defmodule Pleroma.Web.ActivityPub.RelayTest do        assert activity.data["type"] == "Announce"        assert activity.data["actor"] == service_actor.ap_id        assert activity.data["object"] == obj.data["id"] -      refute called(Pleroma.Web.Federator.publish(activity, 5)) +      refute called(Pleroma.Web.Federator.publish(activity))      end    end  end diff --git a/test/web/activity_pub/transmogrifier/follow_handling_test.exs b/test/web/activity_pub/transmogrifier/follow_handling_test.exs index fe89f7cb0..99ab573c5 100644 --- a/test/web/activity_pub/transmogrifier/follow_handling_test.exs +++ b/test/web/activity_pub/transmogrifier/follow_handling_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.ActivityPub.Transmogrifier.FollowHandlingTest do diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 67f972472..a35db71dc 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do @@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do    alias Pleroma.Object    alias Pleroma.Object.Fetcher    alias Pleroma.Repo +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.ActivityPub.ActivityPub    alias Pleroma.Web.ActivityPub.Transmogrifier @@ -648,6 +649,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do          |> Poison.decode!()        {:ok, _} = Transmogrifier.handle_incoming(data) +      ObanHelpers.perform_all()        refute User.get_cached_by_ap_id(ap_id)      end @@ -1210,6 +1212,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert user.info.note_count == 1        {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye") +      ObanHelpers.perform_all() +        assert user.info.ap_enabled        assert user.info.note_count == 1        assert user.follower_address == "https://niu.moe/users/rye/followers" diff --git a/test/web/activity_pub/utils_test.exs b/test/web/activity_pub/utils_test.exs index eb429b2c4..b1c1d6f71 100644 --- a/test/web/activity_pub/utils_test.exs +++ b/test/web/activity_pub/utils_test.exs @@ -87,6 +87,18 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do        assert Utils.determine_explicit_mentions(object) == []      end + +    test "works with an object has tags as map" do +      object = %{ +        "tag" => %{ +          "type" => "Mention", +          "href" => "https://example.com/~alyssa", +          "name" => "Alyssa P. Hacker" +        } +      } + +      assert Utils.determine_explicit_mentions(object) == ["https://example.com/~alyssa"] +    end    end    describe "make_unlike_data/3" do @@ -300,8 +312,8 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do        {:ok, follow_activity_two} =          Utils.update_follow_state_for_all(follow_activity_two, "accept") -      assert Repo.get(Activity, follow_activity.id).data["state"] == "accept" -      assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept" +      assert refresh_record(follow_activity).data["state"] == "accept" +      assert refresh_record(follow_activity_two).data["state"] == "accept"      end    end @@ -323,8 +335,8 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do        {:ok, follow_activity_two} = Utils.update_follow_state(follow_activity_two, "reject") -      assert Repo.get(Activity, follow_activity.id).data["state"] == "pending" -      assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject" +      assert refresh_record(follow_activity).data["state"] == "pending" +      assert refresh_record(follow_activity_two).data["state"] == "reject"      end    end @@ -401,4 +413,216 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do        assert ^like_activity = Utils.get_existing_like(user.ap_id, object)      end    end + +  describe "get_get_existing_announce/2" do +    test "returns nil if announce not found" do +      actor = insert(:user) +      refute Utils.get_existing_announce(actor.ap_id, %{data: %{"id" => "test"}}) +    end + +    test "fetches existing announce" do +      note_activity = insert(:note_activity) +      assert object = Object.normalize(note_activity) +      actor = insert(:user) + +      {:ok, announce, _object} = ActivityPub.announce(actor, object) +      assert Utils.get_existing_announce(actor.ap_id, object) == announce +    end +  end + +  describe "fetch_latest_block/2" do +    test "fetches last block activities" do +      user1 = insert(:user) +      user2 = insert(:user) + +      assert {:ok, %Activity{} = _} = ActivityPub.block(user1, user2) +      assert {:ok, %Activity{} = _} = ActivityPub.block(user1, user2) +      assert {:ok, %Activity{} = activity} = ActivityPub.block(user1, user2) + +      assert Utils.fetch_latest_block(user1, user2) == activity +    end +  end + +  describe "recipient_in_message/3" do +    test "returns true when recipient in `to`" do +      recipient = insert(:user) +      actor = insert(:user) +      assert Utils.recipient_in_message(recipient, actor, %{"to" => recipient.ap_id}) + +      assert Utils.recipient_in_message( +               recipient, +               actor, +               %{"to" => [recipient.ap_id], "cc" => ""} +             ) +    end + +    test "returns true when recipient in `cc`" do +      recipient = insert(:user) +      actor = insert(:user) +      assert Utils.recipient_in_message(recipient, actor, %{"cc" => recipient.ap_id}) + +      assert Utils.recipient_in_message( +               recipient, +               actor, +               %{"cc" => [recipient.ap_id], "to" => ""} +             ) +    end + +    test "returns true when recipient in `bto`" do +      recipient = insert(:user) +      actor = insert(:user) +      assert Utils.recipient_in_message(recipient, actor, %{"bto" => recipient.ap_id}) + +      assert Utils.recipient_in_message( +               recipient, +               actor, +               %{"bcc" => "", "bto" => [recipient.ap_id]} +             ) +    end + +    test "returns true when recipient in `bcc`" do +      recipient = insert(:user) +      actor = insert(:user) +      assert Utils.recipient_in_message(recipient, actor, %{"bcc" => recipient.ap_id}) + +      assert Utils.recipient_in_message( +               recipient, +               actor, +               %{"bto" => "", "bcc" => [recipient.ap_id]} +             ) +    end + +    test "returns true when message without addresses fields" do +      recipient = insert(:user) +      actor = insert(:user) +      assert Utils.recipient_in_message(recipient, actor, %{"bccc" => recipient.ap_id}) + +      assert Utils.recipient_in_message( +               recipient, +               actor, +               %{"btod" => "", "bccc" => [recipient.ap_id]} +             ) +    end + +    test "returns false" do +      recipient = insert(:user) +      actor = insert(:user) +      refute Utils.recipient_in_message(recipient, actor, %{"to" => "ap_id"}) +    end +  end + +  describe "lazy_put_activity_defaults/2" do +    test "returns map with id and published data" do +      note_activity = insert(:note_activity) +      object = Object.normalize(note_activity) +      res = Utils.lazy_put_activity_defaults(%{"context" => object.data["id"]}) +      assert res["context"] == object.data["id"] +      assert res["context_id"] == object.id +      assert res["id"] +      assert res["published"] +    end + +    test "returns map with fake id and published data" do +      assert %{ +               "context" => "pleroma:fakecontext", +               "context_id" => -1, +               "id" => "pleroma:fakeid", +               "published" => _ +             } = Utils.lazy_put_activity_defaults(%{}, true) +    end + +    test "returns activity data with object" do +      note_activity = insert(:note_activity) +      object = Object.normalize(note_activity) + +      res = +        Utils.lazy_put_activity_defaults(%{ +          "context" => object.data["id"], +          "object" => %{} +        }) + +      assert res["context"] == object.data["id"] +      assert res["context_id"] == object.id +      assert res["id"] +      assert res["published"] +      assert res["object"]["id"] +      assert res["object"]["published"] +      assert res["object"]["context"] == object.data["id"] +      assert res["object"]["context_id"] == object.id +    end +  end + +  describe "make_flag_data" do +    test "returns empty map when params is invalid" do +      assert Utils.make_flag_data(%{}, %{}) == %{} +    end + +    test "returns map with Flag object" do +      reporter = insert(:user) +      target_account = insert(:user) +      {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"}) +      context = Utils.generate_context_id() +      content = "foobar" + +      target_ap_id = target_account.ap_id +      activity_ap_id = activity.data["id"] + +      res = +        Utils.make_flag_data( +          %{ +            actor: reporter, +            context: context, +            account: target_account, +            statuses: [%{"id" => activity.data["id"]}], +            content: content +          }, +          %{} +        ) + +      assert %{ +               "type" => "Flag", +               "content" => ^content, +               "context" => ^context, +               "object" => [^target_ap_id, ^activity_ap_id], +               "state" => "open" +             } = res +    end +  end + +  describe "add_announce_to_object/2" do +    test "adds actor to announcement" do +      user = insert(:user) +      object = insert(:note) + +      activity = +        insert(:note_activity, +          data: %{ +            "actor" => user.ap_id, +            "cc" => [Pleroma.Constants.as_public()] +          } +        ) + +      assert {:ok, updated_object} = Utils.add_announce_to_object(activity, object) +      assert updated_object.data["announcements"] == [user.ap_id] +      assert updated_object.data["announcement_count"] == 1 +    end +  end + +  describe "remove_announce_from_object/2" do +    test "removes actor from announcements" do +      user = insert(:user) +      user2 = insert(:user) + +      object = +        insert(:note, +          data: %{"announcements" => [user.ap_id, user2.ap_id], "announcement_count" => 2} +        ) + +      activity = insert(:note_activity, data: %{"actor" => user.ap_id}) + +      assert {:ok, updated_object} = Utils.remove_announce_from_object(activity, object) +      assert updated_object.data["announcements"] == [user2.ap_id] +      assert updated_object.data["announcement_count"] == 1 +    end +  end  end diff --git a/test/web/activity_pub/views/user_view_test.exs b/test/web/activity_pub/views/user_view_test.exs index 4390f9272..8878b8ea4 100644 --- a/test/web/activity_pub/views/user_view_test.exs +++ b/test/web/activity_pub/views/user_view_test.exs @@ -121,10 +121,20 @@ defmodule Pleroma.Web.ActivityPub.UserViewTest do        other_user = insert(:user)        {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)        assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user}) -      info = Map.put(user.info, :hide_followers, true) +      info = Map.merge(user.info, %{hide_followers_count: true, hide_followers: true})        user = Map.put(user, :info, info)        assert %{"totalItems" => 0} = UserView.render("followers.json", %{user: user})      end + +    test "sets correct totalItems when followers are hidden but the follower counter is not" do +      user = insert(:user) +      other_user = insert(:user) +      {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user) +      assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user}) +      info = Map.merge(user.info, %{hide_followers_count: false, hide_followers: true}) +      user = Map.put(user, :info, info) +      assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user}) +    end    end    describe "following" do @@ -133,9 +143,19 @@ defmodule Pleroma.Web.ActivityPub.UserViewTest do        other_user = insert(:user)        {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)        assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user}) -      info = Map.put(user.info, :hide_follows, true) +      info = Map.merge(user.info, %{hide_follows_count: true, hide_follows: true})        user = Map.put(user, :info, info)        assert %{"totalItems" => 0} = UserView.render("following.json", %{user: user})      end + +    test "sets correct totalItems when follows are hidden but the follow counter is not" do +      user = insert(:user) +      other_user = insert(:user) +      {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user) +      assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user}) +      info = Map.merge(user.info, %{hide_follows_count: false, hide_follows: true}) +      user = Map.put(user, :info, info) +      assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user}) +    end    end  end diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 4e2c27431..108143f6a 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do @@ -574,18 +574,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end    end -  test "/api/pleroma/admin/users/invite_token" do -    admin = insert(:user, info: %{is_admin: true}) - -    conn = -      build_conn() -      |> assign(:user, admin) -      |> put_req_header("accept", "application/json") -      |> get("/api/pleroma/admin/users/invite_token") - -    assert conn.status == 200 -  end -    test "/api/pleroma/admin/users/:nickname/password_reset" do      admin = insert(:user, info: %{is_admin: true})      user = insert(:user) @@ -1064,7 +1052,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               "@#{admin.nickname} deactivated user @#{user.nickname}"    end -  describe "GET /api/pleroma/admin/users/invite_token" do +  describe "POST /api/pleroma/admin/users/invite_token" do      setup do        admin = insert(:user, info: %{is_admin: true}) @@ -1076,10 +1064,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end      test "without options", %{conn: conn} do -      conn = get(conn, "/api/pleroma/admin/users/invite_token") +      conn = post(conn, "/api/pleroma/admin/users/invite_token") -      token = json_response(conn, 200) -      invite = UserInviteToken.find_by_token!(token) +      invite_json = json_response(conn, 200) +      invite = UserInviteToken.find_by_token!(invite_json["token"])        refute invite.used        refute invite.expires_at        refute invite.max_use @@ -1088,12 +1076,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      test "with expires_at", %{conn: conn} do        conn = -        get(conn, "/api/pleroma/admin/users/invite_token", %{ -          "invite" => %{"expires_at" => Date.to_string(Date.utc_today())} +        post(conn, "/api/pleroma/admin/users/invite_token", %{ +          "expires_at" => Date.to_string(Date.utc_today())          }) -      token = json_response(conn, 200) -      invite = UserInviteToken.find_by_token!(token) +      invite_json = json_response(conn, 200) +      invite = UserInviteToken.find_by_token!(invite_json["token"])        refute invite.used        assert invite.expires_at == Date.utc_today() @@ -1102,13 +1090,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end      test "with max_use", %{conn: conn} do -      conn = -        get(conn, "/api/pleroma/admin/users/invite_token", %{ -          "invite" => %{"max_use" => 150} -        }) +      conn = post(conn, "/api/pleroma/admin/users/invite_token", %{"max_use" => 150}) -      token = json_response(conn, 200) -      invite = UserInviteToken.find_by_token!(token) +      invite_json = json_response(conn, 200) +      invite = UserInviteToken.find_by_token!(invite_json["token"])        refute invite.used        refute invite.expires_at        assert invite.max_use == 150 @@ -1117,12 +1102,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      test "with max use and expires_at", %{conn: conn} do        conn = -        get(conn, "/api/pleroma/admin/users/invite_token", %{ -          "invite" => %{"max_use" => 150, "expires_at" => Date.to_string(Date.utc_today())} +        post(conn, "/api/pleroma/admin/users/invite_token", %{ +          "max_use" => 150, +          "expires_at" => Date.to_string(Date.utc_today())          }) -      token = json_response(conn, 200) -      invite = UserInviteToken.find_by_token!(token) +      invite_json = json_response(conn, 200) +      invite = UserInviteToken.find_by_token!(invite_json["token"])        refute invite.used        assert invite.expires_at == Date.utc_today()        assert invite.max_use == 150 @@ -1309,6 +1295,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          |> json_response(:ok)        assert Enum.empty?(response["reports"]) +      assert response["total"] == 0      end      test "returns reports", %{conn: conn} do @@ -1331,6 +1318,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert length(response["reports"]) == 1        assert report["id"] == report_id + +      assert response["total"] == 1      end      test "returns reports with specified state", %{conn: conn} do @@ -1364,6 +1353,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert length(response["reports"]) == 1        assert open_report["id"] == first_report_id +      assert response["total"] == 1 +        response =          conn          |> get("/api/pleroma/admin/reports", %{ @@ -1376,6 +1367,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert length(response["reports"]) == 1        assert closed_report["id"] == second_report_id +      assert response["total"] == 1 +        response =          conn          |> get("/api/pleroma/admin/reports", %{ @@ -1384,6 +1377,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          |> json_response(:ok)        assert Enum.empty?(response["reports"]) +      assert response["total"] == 0      end      test "returns 403 when requested by a non-admin" do @@ -1779,7 +1773,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                  %{"tuple" => [":seconds_valid", 60]},                  %{"tuple" => [":path", ""]},                  %{"tuple" => [":key1", nil]}, -                %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]} +                %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}, +                %{"tuple" => [":regex1", "~r/https:\/\/example.com/"]}, +                %{"tuple" => [":regex2", "~r/https:\/\/example.com/u"]}, +                %{"tuple" => [":regex3", "~r/https:\/\/example.com/i"]}, +                %{"tuple" => [":regex4", "~r/https:\/\/example.com/s"]}                ]              }            ] @@ -1796,7 +1794,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                       %{"tuple" => [":seconds_valid", 60]},                       %{"tuple" => [":path", ""]},                       %{"tuple" => [":key1", nil]}, -                     %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]} +                     %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}, +                     %{"tuple" => [":regex1", "~r/https:\\/\\/example.com/"]}, +                     %{"tuple" => [":regex2", "~r/https:\\/\\/example.com/u"]}, +                     %{"tuple" => [":regex3", "~r/https:\\/\\/example.com/i"]}, +                     %{"tuple" => [":regex4", "~r/https:\\/\\/example.com/s"]}                     ]                   }                 ] @@ -2088,7 +2090,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          post(conn, "/api/pleroma/admin/config", %{            configs: [              %{ -              "group" => "pleroma_job_queue", +              "group" => "oban",                "key" => ":queues",                "value" => [                  %{"tuple" => [":federator_incoming", 50]}, @@ -2106,7 +2108,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert json_response(conn, 200) == %{                 "configs" => [                   %{ -                   "group" => "pleroma_job_queue", +                   "group" => "oban",                     "key" => ":queues",                     "value" => [                       %{"tuple" => [":federator_incoming", 50]}, diff --git a/test/web/admin_api/config_test.exs b/test/web/admin_api/config_test.exs index 3190dc1c8..204446b79 100644 --- a/test/web/admin_api/config_test.exs +++ b/test/web/admin_api/config_test.exs @@ -103,6 +103,30 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do        assert Config.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/      end +    test "link sigil" do +      binary = Config.transform("~r/https:\/\/example.com/") +      assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/) +      assert Config.from_binary(binary) == ~r/https:\/\/example.com/ +    end + +    test "link sigil with u modifier" do +      binary = Config.transform("~r/https:\/\/example.com/u") +      assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/u) +      assert Config.from_binary(binary) == ~r/https:\/\/example.com/u +    end + +    test "link sigil with i modifier" do +      binary = Config.transform("~r/https:\/\/example.com/i") +      assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i) +      assert Config.from_binary(binary) == ~r/https:\/\/example.com/i +    end + +    test "link sigil with s modifier" do +      binary = Config.transform("~r/https:\/\/example.com/s") +      assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s) +      assert Config.from_binary(binary) == ~r/https:\/\/example.com/s +    end +      test "2 child tuple" do        binary = Config.transform(%{"tuple" => ["v1", ":v2"]})        assert binary == :erlang.term_to_binary({"v1", :v2}) diff --git a/test/web/admin_api/search_test.exs b/test/web/admin_api/search_test.exs index 501a8d007..9df4cd539 100644 --- a/test/web/admin_api/search_test.exs +++ b/test/web/admin_api/search_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.AdminAPI.SearchTest do diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index c281dd1f1..230146451 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.CommonAPI.UtilsTest do diff --git a/test/web/federator_test.exs b/test/web/federator_test.exs index 09e54533f..43a715706 100644 --- a/test/web/federator_test.exs +++ b/test/web/federator_test.exs @@ -1,12 +1,17 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.FederatorTest do    alias Pleroma.Instances +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.Web.CommonAPI    alias Pleroma.Web.Federator +  alias Pleroma.Workers.PublisherWorker +    use Pleroma.DataCase +  use Oban.Testing, repo: Pleroma.Repo +    import Pleroma.Factory    import Mock @@ -24,15 +29,6 @@ defmodule Pleroma.Web.FederatorTest do    clear_config([:instance, :rewrite_policy])    clear_config([:mrf_keyword]) -  describe "Publisher.perform" do -    test "call `perform` with unknown task" do -      assert { -               :error, -               "Don't know what to do with this" -             } = Pleroma.Web.Federator.Publisher.perform("test", :ok, :ok) -    end -  end -    describe "Publish an activity" do      setup do        user = insert(:user) @@ -53,6 +49,7 @@ defmodule Pleroma.Web.FederatorTest do      } do        with_mocks([relay_mock]) do          Federator.publish(activity) +        ObanHelpers.perform(all_enqueued(worker: PublisherWorker))        end        assert_received :relay_publish @@ -66,6 +63,7 @@ defmodule Pleroma.Web.FederatorTest do        with_mocks([relay_mock]) do          Federator.publish(activity) +        ObanHelpers.perform(all_enqueued(worker: PublisherWorker))        end        refute_received :relay_publish @@ -73,10 +71,7 @@ defmodule Pleroma.Web.FederatorTest do    end    describe "Targets reachability filtering in `publish`" do -    test_with_mock "it federates only to reachable instances via AP", -                   Pleroma.Web.ActivityPub.Publisher, -                   [:passthrough], -                   [] do +    test "it federates only to reachable instances via AP" do        user = insert(:user)        {inbox1, inbox2} = @@ -104,20 +99,20 @@ defmodule Pleroma.Web.FederatorTest do        {:ok, _activity} =          CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"}) -      assert called( -               Pleroma.Web.ActivityPub.Publisher.publish_one(%{ -                 inbox: inbox1, -                 unreachable_since: dt -               }) -             ) +      expected_dt = NaiveDateTime.to_iso8601(dt) -      refute called(Pleroma.Web.ActivityPub.Publisher.publish_one(%{inbox: inbox2})) +      ObanHelpers.perform(all_enqueued(worker: PublisherWorker)) + +      assert ObanHelpers.member?( +               %{ +                 "op" => "publish_one", +                 "params" => %{"inbox" => inbox1, "unreachable_since" => expected_dt} +               }, +               all_enqueued(worker: PublisherWorker) +             )      end -    test_with_mock "it federates only to reachable instances via Websub", -                   Pleroma.Web.Websub, -                   [:passthrough], -                   [] do +    test "it federates only to reachable instances via Websub" do        user = insert(:user)        websub_topic = Pleroma.Web.OStatus.feed_path(user) @@ -142,23 +137,27 @@ defmodule Pleroma.Web.FederatorTest do        {:ok, _activity} = CommonAPI.post(user, %{"status" => "HI"}) -      assert called( -               Pleroma.Web.Websub.publish_one(%{ -                 callback: sub2.callback, -                 unreachable_since: dt -               }) -             ) +      expected_callback = sub2.callback +      expected_dt = NaiveDateTime.to_iso8601(dt) + +      ObanHelpers.perform(all_enqueued(worker: PublisherWorker)) -      refute called(Pleroma.Web.Websub.publish_one(%{callback: sub1.callback})) +      assert ObanHelpers.member?( +               %{ +                 "op" => "publish_one", +                 "params" => %{ +                   "callback" => expected_callback, +                   "unreachable_since" => expected_dt +                 } +               }, +               all_enqueued(worker: PublisherWorker) +             )      end -    test_with_mock "it federates only to reachable instances via Salmon", -                   Pleroma.Web.Salmon, -                   [:passthrough], -                   [] do +    test "it federates only to reachable instances via Salmon" do        user = insert(:user) -      remote_user1 = +      _remote_user1 =          insert(:user, %{            local: false,            nickname: "nick1@domain.com", @@ -174,6 +173,8 @@ defmodule Pleroma.Web.FederatorTest do            info: %{salmon: "https://domain2.com/salmon"}          }) +      remote_user2_id = remote_user2.id +        dt = NaiveDateTime.utc_now()        Instances.set_unreachable(remote_user2.ap_id, dt) @@ -182,14 +183,20 @@ defmodule Pleroma.Web.FederatorTest do        {:ok, _activity} =          CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"}) -      assert called( -               Pleroma.Web.Salmon.publish_one(%{ -                 recipient: remote_user2, -                 unreachable_since: dt -               }) -             ) +      expected_dt = NaiveDateTime.to_iso8601(dt) + +      ObanHelpers.perform(all_enqueued(worker: PublisherWorker)) -      refute called(Pleroma.Web.Salmon.publish_one(%{recipient: remote_user1})) +      assert ObanHelpers.member?( +               %{ +                 "op" => "publish_one", +                 "params" => %{ +                   "recipient_id" => remote_user2_id, +                   "unreachable_since" => expected_dt +                 } +               }, +               all_enqueued(worker: PublisherWorker) +             )      end    end @@ -209,7 +216,8 @@ defmodule Pleroma.Web.FederatorTest do          "to" => ["https://www.w3.org/ns/activitystreams#Public"]        } -      {:ok, _activity} = Federator.incoming_ap_doc(params) +      assert {:ok, job} = Federator.incoming_ap_doc(params) +      assert {:ok, _activity} = ObanHelpers.perform(job)      end      test "rejects incoming AP docs with incorrect origin" do @@ -227,7 +235,8 @@ defmodule Pleroma.Web.FederatorTest do          "to" => ["https://www.w3.org/ns/activitystreams#Public"]        } -      :error = Federator.incoming_ap_doc(params) +      assert {:ok, job} = Federator.incoming_ap_doc(params) +      assert :error = ObanHelpers.perform(job)      end      test "it does not crash if MRF rejects the post" do @@ -242,7 +251,8 @@ defmodule Pleroma.Web.FederatorTest do          File.read!("test/fixtures/mastodon-post-activity.json")          |> Poison.decode!() -      assert Federator.incoming_ap_doc(params) == :error +      assert {:ok, job} = Federator.incoming_ap_doc(params) +      assert :error = ObanHelpers.perform(job)      end    end  end diff --git a/test/web/instances/instance_test.exs b/test/web/instances/instance_test.exs index 3fd011fd3..e54d708ad 100644 --- a/test/web/instances/instance_test.exs +++ b/test/web/instances/instance_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Instances.InstanceTest do @@ -16,7 +16,8 @@ defmodule Pleroma.Instances.InstanceTest do    describe "set_reachable/1" do      test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do -      instance = insert(:instance, unreachable_since: NaiveDateTime.utc_now()) +      unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now()) +      instance = insert(:instance, unreachable_since: unreachable_since)        assert {:ok, instance} = Instance.set_reachable(instance.host)        refute instance.unreachable_since diff --git a/test/web/instances/instances_test.exs b/test/web/instances/instances_test.exs index dea8e2aea..65b03b155 100644 --- a/test/web/instances/instances_test.exs +++ b/test/web/instances/instances_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.InstancesTest do diff --git a/test/web/mastodon_api/controllers/mastodon_api_controller/update_credentials_test.exs b/test/web/mastodon_api/controllers/mastodon_api_controller/update_credentials_test.exs index 87ee82050..89d4ca37e 100644 --- a/test/web/mastodon_api/controllers/mastodon_api_controller/update_credentials_test.exs +++ b/test/web/mastodon_api/controllers/mastodon_api_controller/update_credentials_test.exs @@ -128,6 +128,22 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do        assert user["pleroma"]["hide_followers"] == true      end +    test "updates the user's hide_followers_count and hide_follows_count", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{ +          hide_followers_count: "true", +          hide_follows_count: "true" +        }) + +      assert user = json_response(conn, 200) +      assert user["pleroma"]["hide_followers_count"] == true +      assert user["pleroma"]["hide_follows_count"] == true +    end +      test "updates the user's skip_thread_containment option", %{conn: conn} do        user = insert(:user) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index f4902d043..fb04748bb 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -13,6 +13,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do    alias Pleroma.Object    alias Pleroma.Repo    alias Pleroma.ScheduledActivity +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.ActivityPub.ActivityPub    alias Pleroma.Web.CommonAPI @@ -751,7 +752,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      query_string = "ids[]=#{id1}&ids[]=#{id2}"      conn = get(conn, "/api/v1/statuses/?#{query_string}") -    assert [%{"id" => ^id1}, %{"id" => ^id2}] = json_response(conn, :ok) +    assert [%{"id" => ^id1}, %{"id" => ^id2}] = Enum.sort_by(json_response(conn, :ok), & &1["id"])    end    describe "deleting a status" do @@ -3698,7 +3699,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do          build_conn()          |> assign(:user, user) -      [conn: conn, activity: activity] +      [conn: conn, activity: activity, user: user]      end      test "returns users who have favorited the status", %{conn: conn, activity: activity} do @@ -3758,6 +3759,32 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        [%{"id" => id}] = response        assert id == other_user.id      end + +    test "requires authentification for private posts", %{conn: conn, user: user} do +      other_user = insert(:user) + +      {:ok, activity} = +        CommonAPI.post(user, %{ +          "status" => "@#{other_user.nickname} wanna get some #cofe together?", +          "visibility" => "direct" +        }) + +      {:ok, _, _} = CommonAPI.favorite(activity.id, other_user) + +      conn +      |> assign(:user, nil) +      |> get("/api/v1/statuses/#{activity.id}/favourited_by") +      |> json_response(404) + +      response = +        build_conn() +        |> assign(:user, other_user) +        |> get("/api/v1/statuses/#{activity.id}/favourited_by") +        |> json_response(200) + +      [%{"id" => id}] = response +      assert id == other_user.id +    end    end    describe "GET /api/v1/statuses/:id/reblogged_by" do @@ -3769,7 +3796,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do          build_conn()          |> assign(:user, user) -      [conn: conn, activity: activity] +      [conn: conn, activity: activity, user: user]      end      test "returns users who have reblogged the status", %{conn: conn, activity: activity} do @@ -3829,6 +3856,29 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        [%{"id" => id}] = response        assert id == other_user.id      end + +    test "requires authentification for private posts", %{conn: conn, user: user} do +      other_user = insert(:user) + +      {:ok, activity} = +        CommonAPI.post(user, %{ +          "status" => "@#{other_user.nickname} wanna get some #cofe together?", +          "visibility" => "direct" +        }) + +      conn +      |> assign(:user, nil) +      |> get("/api/v1/statuses/#{activity.id}/reblogged_by") +      |> json_response(404) + +      response = +        build_conn() +        |> assign(:user, other_user) +        |> get("/api/v1/statuses/#{activity.id}/reblogged_by") +        |> json_response(200) + +      assert [] == response +    end    end    describe "POST /auth/password, with valid parameters" do @@ -3848,6 +3898,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      end      test "it sends an email to user", %{user: user} do +      ObanHelpers.perform_all()        token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)        email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token) @@ -3908,6 +3959,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")        |> json_response(:no_content) +      ObanHelpers.perform_all() +        email = Pleroma.Emails.UserEmail.account_confirmation_email(user)        notify_email = Config.get([:instance, :notify_email])        instance_name = Config.get([:instance, :name]) @@ -3963,13 +4016,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        Config.put([:suggestions, :enabled], true)        Config.put([:suggestions, :third_party_engine], "http://test500?{{host}}&{{user}}") -      res = -        conn -        |> assign(:user, user) -        |> get("/api/v1/suggestions") -        |> json_response(500) +      assert capture_log(fn -> +               res = +                 conn +                 |> assign(:user, user) +                 |> get("/api/v1/suggestions") +                 |> json_response(500) -      assert res == "Something went wrong" +               assert res == "Something went wrong" +             end) =~ "Could not retrieve suggestions"      end      test "returns suggestions", %{conn: conn, user: user, other_user: other_user} do diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index 1d8b28339..6206107f7 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MastodonAPI.AccountViewTest do @@ -79,6 +79,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          hide_favorites: true,          hide_followers: false,          hide_follows: false, +        hide_followers_count: false, +        hide_follows_count: false,          relationship: %{},          skip_thread_containment: false        } @@ -147,6 +149,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          hide_favorites: true,          hide_followers: false,          hide_follows: false, +        hide_followers_count: false, +        hide_follows_count: false,          relationship: %{},          skip_thread_containment: false        } @@ -318,6 +322,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          hide_favorites: true,          hide_followers: false,          hide_follows: false, +        hide_followers_count: false, +        hide_follows_count: false,          relationship: %{            id: to_string(user.id),            following: false, @@ -361,8 +367,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do    end    describe "hiding follows/following" do -    test "shows when follows/following are hidden and sets follower/following count to 0" do -      user = insert(:user, info: %{hide_followers: true, hide_follows: true}) +    test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do +      info = %{ +        hide_followers: true, +        hide_followers_count: true, +        hide_follows: true, +        hide_follows_count: true +      } + +      user = insert(:user, info: info) +        other_user = insert(:user)        {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)        {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user) @@ -370,6 +384,19 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        assert %{                 followers_count: 0,                 following_count: 0, +               pleroma: %{hide_follows_count: true, hide_followers_count: true} +             } = AccountView.render("account.json", %{user: user}) +    end + +    test "shows when follows/followers are hidden" do +      user = insert(:user, info: %{hide_followers: true, hide_follows: true}) +      other_user = insert(:user) +      {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user) +      {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user) + +      assert %{ +               followers_count: 1, +               following_count: 1,                 pleroma: %{hide_follows: true, hide_followers: true}               } = AccountView.render("account.json", %{user: user})      end diff --git a/test/web/mastodon_api/views/list_view_test.exs b/test/web/mastodon_api/views/list_view_test.exs index fb00310b9..59e896a7c 100644 --- a/test/web/mastodon_api/views/list_view_test.exs +++ b/test/web/mastodon_api/views/list_view_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MastodonAPI.ListViewTest do diff --git a/test/web/mastodon_api/views/notification_view_test.exs b/test/web/mastodon_api/views/notification_view_test.exs index 977ea1e87..9231aaec8 100644 --- a/test/web/mastodon_api/views/notification_view_test.exs +++ b/test/web/mastodon_api/views/notification_view_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do diff --git a/test/web/mastodon_api/views/push_subscription_view_test.exs b/test/web/mastodon_api/views/push_subscription_view_test.exs index dc935fc82..4e4f5b7e6 100644 --- a/test/web/mastodon_api/views/push_subscription_view_test.exs +++ b/test/web/mastodon_api/views/push_subscription_view_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MastodonAPI.PushSubscriptionViewTest do diff --git a/test/web/mastodon_api/views/scheduled_activity_view_test.exs b/test/web/mastodon_api/views/scheduled_activity_view_test.exs index ecbb855d4..6387e4555 100644 --- a/test/web/mastodon_api/views/scheduled_activity_view_test.exs +++ b/test/web/mastodon_api/views/scheduled_activity_view_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MastodonAPI.ScheduledActivityViewTest do diff --git a/test/web/mastodon_api/views/status_view_test.exs b/test/web/mastodon_api/views/status_view_test.exs index fcdd7fbcb..51f8434fa 100644 --- a/test/web/mastodon_api/views/status_view_test.exs +++ b/test/web/mastodon_api/views/status_view_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MastodonAPI.StatusViewTest do diff --git a/test/web/media_proxy/media_proxy_controller_test.exs b/test/web/media_proxy/media_proxy_controller_test.exs index 53b8f556b..fdfdb5ec6 100644 --- a/test/web/media_proxy/media_proxy_controller_test.exs +++ b/test/web/media_proxy/media_proxy_controller_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do diff --git a/test/web/media_proxy/media_proxy_test.exs b/test/web/media_proxy/media_proxy_test.exs index 79699cac5..96bdde219 100644 --- a/test/web/media_proxy/media_proxy_test.exs +++ b/test/web/media_proxy/media_proxy_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.MediaProxyTest do diff --git a/test/web/node_info_test.exs b/test/web/node_info_test.exs index f6147c286..e15a0bfff 100644 --- a/test/web/node_info_test.exs +++ b/test/web/node_info_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.NodeInfoTest do diff --git a/test/web/oauth/authorization_test.exs b/test/web/oauth/authorization_test.exs index d8b008437..2e82a7b79 100644 --- a/test/web/oauth/authorization_test.exs +++ b/test/web/oauth/authorization_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OAuth.AuthorizationTest do diff --git a/test/web/oauth/oauth_controller_test.exs b/test/web/oauth/oauth_controller_test.exs index b492c7794..2780e1746 100644 --- a/test/web/oauth/oauth_controller_test.exs +++ b/test/web/oauth/oauth_controller_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OAuth.OAuthControllerTest do diff --git a/test/web/oauth/token/utils_test.exs b/test/web/oauth/token/utils_test.exs index 20e338cab..dc1f9a986 100644 --- a/test/web/oauth/token/utils_test.exs +++ b/test/web/oauth/token/utils_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OAuth.Token.UtilsTest do diff --git a/test/web/oauth/token_test.exs b/test/web/oauth/token_test.exs index 3c07309b7..5359940f8 100644 --- a/test/web/oauth/token_test.exs +++ b/test/web/oauth/token_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OAuth.TokenTest do diff --git a/test/web/ostatus/activity_representer_test.exs b/test/web/ostatus/activity_representer_test.exs index a3a92ce5b..a8d500890 100644 --- a/test/web/ostatus/activity_representer_test.exs +++ b/test/web/ostatus/activity_representer_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do diff --git a/test/web/ostatus/feed_representer_test.exs b/test/web/ostatus/feed_representer_test.exs index 3c7b126e7..d1cadf1e4 100644 --- a/test/web/ostatus/feed_representer_test.exs +++ b/test/web/ostatus/feed_representer_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OStatus.FeedRepresenterTest do diff --git a/test/web/ostatus/ostatus_controller_test.exs b/test/web/ostatus/ostatus_controller_test.exs index 095ae7041..ec96f0012 100644 --- a/test/web/ostatus/ostatus_controller_test.exs +++ b/test/web/ostatus/ostatus_controller_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OStatus.OStatusControllerTest do diff --git a/test/web/ostatus/ostatus_test.exs b/test/web/ostatus/ostatus_test.exs index 803a97695..70a0e4473 100644 --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.OStatusTest do @@ -628,4 +628,18 @@ defmodule Pleroma.Web.OStatusTest do        refute OStatus.is_representable?(note_activity)      end    end + +  describe "make_user/2" do +    test "creates new user" do +      {:ok, user} = OStatus.make_user("https://social.heldscal.la/user/23211") + +      created_user = +        User +        |> Repo.get_by(ap_id: "https://social.heldscal.la/user/23211") +        |> Map.put(:last_digest_emailed_at, nil) + +      assert user.info +      assert user == created_user +    end +  end  end diff --git a/test/web/plugs/federating_plug_test.exs b/test/web/plugs/federating_plug_test.exs index bb2e1687a..9dcab93da 100644 --- a/test/web/plugs/federating_plug_test.exs +++ b/test/web/plugs/federating_plug_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.FederatingPlugTest do diff --git a/test/web/push/impl_test.exs b/test/web/push/impl_test.exs index e2f89f40a..2f6ce4bd2 100644 --- a/test/web/push/impl_test.exs +++ b/test/web/push/impl_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.Push.ImplTest do diff --git a/test/web/retry_queue_test.exs b/test/web/retry_queue_test.exs deleted file mode 100644 index ecb3ce5d0..000000000 --- a/test/web/retry_queue_test.exs +++ /dev/null @@ -1,48 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule MockActivityPub do -  def publish_one({ret, waiter}) do -    send(waiter, :complete) -    {ret, "success"} -  end -end - -defmodule Pleroma.Web.Federator.RetryQueueTest do -  use Pleroma.DataCase -  alias Pleroma.Web.Federator.RetryQueue - -  @small_retry_count 0 -  @hopeless_retry_count 10 - -  setup do -    RetryQueue.reset_stats() -  end - -  test "RetryQueue responds to stats request" do -    assert %{delivered: 0, dropped: 0} == RetryQueue.get_stats() -  end - -  test "failed posts are retried" do -    {:retry, _timeout} = RetryQueue.get_retry_params(@small_retry_count) - -    wait_task = -      Task.async(fn -> -        receive do -          :complete -> :ok -        end -      end) - -    RetryQueue.enqueue({:ok, wait_task.pid}, MockActivityPub, @small_retry_count) -    Task.await(wait_task) -    assert %{delivered: 1, dropped: 0} == RetryQueue.get_stats() -  end - -  test "posts that have been tried too many times are dropped" do -    {:drop, _timeout} = RetryQueue.get_retry_params(@hopeless_retry_count) - -    RetryQueue.enqueue({:ok, nil}, MockActivityPub, @hopeless_retry_count) -    assert %{delivered: 0, dropped: 1} == RetryQueue.get_stats() -  end -end diff --git a/test/web/salmon/salmon_test.exs b/test/web/salmon/salmon_test.exs index e86e76fe9..153ec41ac 100644 --- a/test/web/salmon/salmon_test.exs +++ b/test/web/salmon/salmon_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.Salmon.SalmonTest do @@ -96,6 +96,6 @@ defmodule Pleroma.Web.Salmon.SalmonTest do      Salmon.publish(user, activity) -    assert called(Publisher.enqueue_one(Salmon, %{recipient: mentioned_user})) +    assert called(Publisher.enqueue_one(Salmon, %{recipient_id: mentioned_user.id}))    end  end diff --git a/test/web/streamer/ping_test.exs b/test/web/streamer/ping_test.exs new file mode 100644 index 000000000..3d52c00e4 --- /dev/null +++ b/test/web/streamer/ping_test.exs @@ -0,0 +1,36 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.PingTest do +  use Pleroma.DataCase + +  import Pleroma.Factory +  alias Pleroma.Web.Streamer + +  setup do +    start_supervised({Streamer.supervisor(), [ping_interval: 30]}) + +    :ok +  end + +  describe "sockets" do +    setup do +      user = insert(:user) +      {:ok, %{user: user}} +    end + +    test "it sends pings", %{user: user} do +      task = +        Task.async(fn -> +          assert_receive {:text, received_event}, 40 +          assert_receive {:text, received_event}, 40 +          assert_receive {:text, received_event}, 40 +        end) + +      Streamer.add_socket("public", %{transport_pid: task.pid, assigns: %{user: user}}) + +      Task.await(task) +    end +  end +end diff --git a/test/web/streamer/state_test.exs b/test/web/streamer/state_test.exs new file mode 100644 index 000000000..d1aeac541 --- /dev/null +++ b/test/web/streamer/state_test.exs @@ -0,0 +1,54 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.StateTest do +  use Pleroma.DataCase + +  import Pleroma.Factory +  alias Pleroma.Web.Streamer +  alias Pleroma.Web.Streamer.StreamerSocket + +  @moduletag needs_streamer: true + +  describe "sockets" do +    setup do +      user = insert(:user) +      user2 = insert(:user) +      {:ok, %{user: user, user2: user2}} +    end + +    test "it can add a socket", %{user: user} do +      Streamer.add_socket("public", %{transport_pid: 1, assigns: %{user: user}}) + +      assert(%{"public" => [%StreamerSocket{transport_pid: 1}]} = Streamer.get_sockets()) +    end + +    test "it can add multiple sockets per user", %{user: user} do +      Streamer.add_socket("public", %{transport_pid: 1, assigns: %{user: user}}) +      Streamer.add_socket("public", %{transport_pid: 2, assigns: %{user: user}}) + +      assert( +        %{ +          "public" => [ +            %StreamerSocket{transport_pid: 2}, +            %StreamerSocket{transport_pid: 1} +          ] +        } = Streamer.get_sockets() +      ) +    end + +    test "it will not add a duplicate socket", %{user: user} do +      Streamer.add_socket("activity", %{transport_pid: 1, assigns: %{user: user}}) +      Streamer.add_socket("activity", %{transport_pid: 1, assigns: %{user: user}}) + +      assert( +        %{ +          "activity" => [ +            %StreamerSocket{transport_pid: 1} +          ] +        } = Streamer.get_sockets() +      ) +    end +  end +end diff --git a/test/web/streamer_test.exs b/test/web/streamer/streamer_test.exs index 96fa7645f..b8fcd41fa 100644 --- a/test/web/streamer_test.exs +++ b/test/web/streamer/streamer_test.exs @@ -1,28 +1,24 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.StreamerTest do    use Pleroma.DataCase +  import Pleroma.Factory +    alias Pleroma.List    alias Pleroma.User    alias Pleroma.Web.CommonAPI    alias Pleroma.Web.Streamer -  import Pleroma.Factory +  alias Pleroma.Web.Streamer.StreamerSocket +  alias Pleroma.Web.Streamer.Worker +  @moduletag needs_streamer: true    clear_config_all([:instance, :skip_thread_containment])    describe "user streams" do      setup do -      GenServer.start(Streamer, %{}, name: Streamer) - -      on_exit(fn -> -        if pid = Process.whereis(Streamer) do -          Process.exit(pid, :kill) -        end -      end) -        user = insert(:user)        notify = insert(:notification, user: user, activity: build(:note_activity))        {:ok, %{user: user, notify: notify}} @@ -125,11 +121,9 @@ defmodule Pleroma.Web.StreamerTest do          assert_receive {:text, _}, 4_000        end) -    fake_socket = %{ +    fake_socket = %StreamerSocket{        transport_pid: task.pid, -      assigns: %{ -        user: user -      } +      user: user      }      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"}) @@ -138,7 +132,7 @@ defmodule Pleroma.Web.StreamerTest do        "public" => [fake_socket]      } -    Streamer.push_to_socket(topics, "public", activity) +    Worker.push_to_socket(topics, "public", activity)      Task.await(task) @@ -155,11 +149,9 @@ defmodule Pleroma.Web.StreamerTest do          assert received_event == expected_event        end) -    fake_socket = %{ +    fake_socket = %StreamerSocket{        transport_pid: task.pid, -      assigns: %{ -        user: user -      } +      user: user      }      {:ok, activity} = CommonAPI.delete(activity.id, other_user) @@ -168,7 +160,7 @@ defmodule Pleroma.Web.StreamerTest do        "public" => [fake_socket]      } -    Streamer.push_to_socket(topics, "public", activity) +    Worker.push_to_socket(topics, "public", activity)      Task.await(task)    end @@ -189,9 +181,9 @@ defmodule Pleroma.Web.StreamerTest do          )        task = Task.async(fn -> refute_receive {:text, _}, 1_000 end) -      fake_socket = %{transport_pid: task.pid, assigns: %{user: user}} +      fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}        topics = %{"public" => [fake_socket]} -      Streamer.push_to_socket(topics, "public", activity) +      Worker.push_to_socket(topics, "public", activity)        Task.await(task)      end @@ -211,9 +203,9 @@ defmodule Pleroma.Web.StreamerTest do          )        task = Task.async(fn -> assert_receive {:text, _}, 1_000 end) -      fake_socket = %{transport_pid: task.pid, assigns: %{user: user}} +      fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}        topics = %{"public" => [fake_socket]} -      Streamer.push_to_socket(topics, "public", activity) +      Worker.push_to_socket(topics, "public", activity)        Task.await(task)      end @@ -233,9 +225,9 @@ defmodule Pleroma.Web.StreamerTest do          )        task = Task.async(fn -> assert_receive {:text, _}, 1_000 end) -      fake_socket = %{transport_pid: task.pid, assigns: %{user: user}} +      fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}        topics = %{"public" => [fake_socket]} -      Streamer.push_to_socket(topics, "public", activity) +      Worker.push_to_socket(topics, "public", activity)        Task.await(task)      end @@ -251,11 +243,9 @@ defmodule Pleroma.Web.StreamerTest do          refute_receive {:text, _}, 1_000        end) -    fake_socket = %{ +    fake_socket = %StreamerSocket{        transport_pid: task.pid, -      assigns: %{ -        user: user -      } +      user: user      }      {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"}) @@ -264,7 +254,7 @@ defmodule Pleroma.Web.StreamerTest do        "public" => [fake_socket]      } -    Streamer.push_to_socket(topics, "public", activity) +    Worker.push_to_socket(topics, "public", activity)      Task.await(task)    end @@ -284,11 +274,9 @@ defmodule Pleroma.Web.StreamerTest do          refute_receive {:text, _}, 1_000        end) -    fake_socket = %{ +    fake_socket = %StreamerSocket{        transport_pid: task.pid, -      assigns: %{ -        user: user_a -      } +      user: user_a      }      {:ok, activity} = @@ -301,7 +289,7 @@ defmodule Pleroma.Web.StreamerTest do        "list:#{list.id}" => [fake_socket]      } -    Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics) +    Worker.handle_call({:stream, "list", activity}, self(), topics)      Task.await(task)    end @@ -318,11 +306,9 @@ defmodule Pleroma.Web.StreamerTest do          refute_receive {:text, _}, 1_000        end) -    fake_socket = %{ +    fake_socket = %StreamerSocket{        transport_pid: task.pid, -      assigns: %{ -        user: user_a -      } +      user: user_a      }      {:ok, activity} = @@ -335,12 +321,12 @@ defmodule Pleroma.Web.StreamerTest do        "list:#{list.id}" => [fake_socket]      } -    Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics) +    Worker.handle_call({:stream, "list", activity}, self(), topics)      Task.await(task)    end -  test "it send wanted private posts to list" do +  test "it sends wanted private posts to list" do      user_a = insert(:user)      user_b = insert(:user) @@ -354,11 +340,9 @@ defmodule Pleroma.Web.StreamerTest do          assert_receive {:text, _}, 1_000        end) -    fake_socket = %{ +    fake_socket = %StreamerSocket{        transport_pid: task.pid, -      assigns: %{ -        user: user_a -      } +      user: user_a      }      {:ok, activity} = @@ -367,11 +351,12 @@ defmodule Pleroma.Web.StreamerTest do          "visibility" => "private"        }) -    topics = %{ -      "list:#{list.id}" => [fake_socket] -    } +    Streamer.add_socket( +      "list:#{list.id}", +      fake_socket +    ) -    Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics) +    Worker.handle_call({:stream, "list", activity}, self(), %{})      Task.await(task)    end @@ -387,11 +372,9 @@ defmodule Pleroma.Web.StreamerTest do          refute_receive {:text, _}, 1_000        end) -    fake_socket = %{ +    fake_socket = %StreamerSocket{        transport_pid: task.pid, -      assigns: %{ -        user: user1 -      } +      user: user1      }      {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"}) @@ -401,7 +384,7 @@ defmodule Pleroma.Web.StreamerTest do        "public" => [fake_socket]      } -    Streamer.push_to_socket(topics, "public", announce_activity) +    Worker.push_to_socket(topics, "public", announce_activity)      Task.await(task)    end @@ -417,6 +400,8 @@ defmodule Pleroma.Web.StreamerTest do      task = Task.async(fn -> refute_receive {:text, _}, 4_000 end) +    Process.sleep(4000) +      Streamer.add_socket(        "user",        %{transport_pid: task.pid, assigns: %{user: user2}} @@ -428,14 +413,6 @@ defmodule Pleroma.Web.StreamerTest do    describe "direct streams" do      setup do -      GenServer.start(Streamer, %{}, name: Streamer) - -      on_exit(fn -> -        if pid = Process.whereis(Streamer) do -          Process.exit(pid, :kill) -        end -      end) -        :ok      end @@ -480,6 +457,8 @@ defmodule Pleroma.Web.StreamerTest do            refute_receive {:text, _}, 4_000          end) +      Process.sleep(1000) +        Streamer.add_socket(          "direct",          %{transport_pid: task.pid, assigns: %{user: user}} @@ -521,6 +500,8 @@ defmodule Pleroma.Web.StreamerTest do            assert last_status["id"] == to_string(create_activity.id)          end) +      Process.sleep(1000) +        Streamer.add_socket(          "direct",          %{transport_pid: task.pid, assigns: %{user: user}} diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index c5b18234e..08f264431 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -1,10 +1,11 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do    use Pleroma.DataCase    alias Pleroma.Repo +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.UserInviteToken    alias Pleroma.Web.MastodonAPI.AccountView @@ -68,6 +69,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do      }      {:ok, user} = TwitterAPI.register_user(data) +    ObanHelpers.perform_all()      assert user.info.confirmation_pending diff --git a/test/web/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index cf8e69d2b..56e318182 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -4,10 +4,13 @@  defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do    use Pleroma.Web.ConnCase +  use Oban.Testing, repo: Pleroma.Repo    alias Pleroma.Repo +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.CommonAPI +  import ExUnit.CaptureLog    import Pleroma.Factory    import Mock @@ -42,8 +45,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do          {File, [],           read!: fn "follow_list.txt" ->             "Account address,Show boosts\n#{user2.ap_id},true" -         end}, -        {PleromaJobQueue, [:passthrough], []} +         end}        ]) do          response =            conn @@ -51,15 +53,16 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do            |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})            |> json_response(:ok) -        assert called( -                 PleromaJobQueue.enqueue( -                   :background, -                   User, -                   [:follow_import, user1, [user2.ap_id]] -                 ) -               ) -          assert response == "job started" + +        assert ObanHelpers.member?( +                 %{ +                   "op" => "follow_import", +                   "follower_id" => user1.id, +                   "followed_identifiers" => [user2.ap_id] +                 }, +                 all_enqueued(worker: Pleroma.Workers.BackgroundWorker) +               )        end      end @@ -118,8 +121,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do        user3 = insert(:user)        with_mocks([ -        {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}, -        {PleromaJobQueue, [:passthrough], []} +        {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}        ]) do          response =            conn @@ -127,15 +129,16 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do            |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})            |> json_response(:ok) -        assert called( -                 PleromaJobQueue.enqueue( -                   :background, -                   User, -                   [:blocks_import, user1, [user2.ap_id, user3.ap_id]] -                 ) -               ) -          assert response == "job started" + +        assert ObanHelpers.member?( +                 %{ +                   "op" => "blocks_import", +                   "blocker_id" => user1.id, +                   "blocked_identifiers" => [user2.ap_id, user3.ap_id] +                 }, +                 all_enqueued(worker: Pleroma.Workers.BackgroundWorker) +               )        end      end    end @@ -338,12 +341,14 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do      test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do        user = insert(:user) -      response = -        conn -        |> assign(:user, user) -        |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found") +      assert capture_log(fn -> +               response = +                 conn +                 |> assign(:user, user) +                 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found") -      assert html_response(response, 200) =~ "Error fetching user" +               assert html_response(response, 200) =~ "Error fetching user" +             end) =~ "Object has been deleted"      end    end @@ -557,6 +562,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do          |> json_response(:ok)        assert response == %{"status" => "success"} +      ObanHelpers.perform_all()        user = User.get_cached_by_id(user.id) @@ -662,4 +668,216 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do        assert called(Pleroma.Captcha.new())      end    end + +  defp with_credentials(conn, username, password) do +    header_content = "Basic " <> Base.encode64("#{username}:#{password}") +    put_req_header(conn, "authorization", header_content) +  end + +  defp valid_user(_context) do +    user = insert(:user) +    [user: user] +  end + +  describe "POST /api/pleroma/change_email" do +    setup [:valid_user] + +    test "without credentials", %{conn: conn} do +      conn = post(conn, "/api/pleroma/change_email") +      assert json_response(conn, 403) == %{"error" => "Invalid credentials."} +    end + +    test "with credentials and invalid password", %{conn: conn, user: current_user} do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_email", %{ +          "password" => "hi", +          "email" => "test@test.com" +        }) + +      assert json_response(conn, 200) == %{"error" => "Invalid password."} +    end + +    test "with credentials, valid password and invalid email", %{ +      conn: conn, +      user: current_user +    } do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_email", %{ +          "password" => "test", +          "email" => "foobar" +        }) + +      assert json_response(conn, 200) == %{"error" => "Email has invalid format."} +    end + +    test "with credentials, valid password and no email", %{ +      conn: conn, +      user: current_user +    } do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_email", %{ +          "password" => "test" +        }) + +      assert json_response(conn, 200) == %{"error" => "Email can't be blank."} +    end + +    test "with credentials, valid password and blank email", %{ +      conn: conn, +      user: current_user +    } do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_email", %{ +          "password" => "test", +          "email" => "" +        }) + +      assert json_response(conn, 200) == %{"error" => "Email can't be blank."} +    end + +    test "with credentials, valid password and non unique email", %{ +      conn: conn, +      user: current_user +    } do +      user = insert(:user) + +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_email", %{ +          "password" => "test", +          "email" => user.email +        }) + +      assert json_response(conn, 200) == %{"error" => "Email has already been taken."} +    end + +    test "with credentials, valid password and valid email", %{ +      conn: conn, +      user: current_user +    } do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_email", %{ +          "password" => "test", +          "email" => "cofe@foobar.com" +        }) + +      assert json_response(conn, 200) == %{"status" => "success"} +    end +  end + +  describe "POST /api/pleroma/change_password" do +    setup [:valid_user] + +    test "without credentials", %{conn: conn} do +      conn = post(conn, "/api/pleroma/change_password") +      assert json_response(conn, 403) == %{"error" => "Invalid credentials."} +    end + +    test "with credentials and invalid password", %{conn: conn, user: current_user} do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_password", %{ +          "password" => "hi", +          "new_password" => "newpass", +          "new_password_confirmation" => "newpass" +        }) + +      assert json_response(conn, 200) == %{"error" => "Invalid password."} +    end + +    test "with credentials, valid password and new password and confirmation not matching", %{ +      conn: conn, +      user: current_user +    } do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_password", %{ +          "password" => "test", +          "new_password" => "newpass", +          "new_password_confirmation" => "notnewpass" +        }) + +      assert json_response(conn, 200) == %{ +               "error" => "New password does not match confirmation." +             } +    end + +    test "with credentials, valid password and invalid new password", %{ +      conn: conn, +      user: current_user +    } do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_password", %{ +          "password" => "test", +          "new_password" => "", +          "new_password_confirmation" => "" +        }) + +      assert json_response(conn, 200) == %{ +               "error" => "New password can't be blank." +             } +    end + +    test "with credentials, valid password and matching new password and confirmation", %{ +      conn: conn, +      user: current_user +    } do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/change_password", %{ +          "password" => "test", +          "new_password" => "newpass", +          "new_password_confirmation" => "newpass" +        }) + +      assert json_response(conn, 200) == %{"status" => "success"} +      fetched_user = User.get_cached_by_id(current_user.id) +      assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true +    end +  end + +  describe "POST /api/pleroma/delete_account" do +    setup [:valid_user] + +    test "without credentials", %{conn: conn} do +      conn = post(conn, "/api/pleroma/delete_account") +      assert json_response(conn, 403) == %{"error" => "Invalid credentials."} +    end + +    test "with credentials and invalid password", %{conn: conn, user: current_user} do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/delete_account", %{"password" => "hi"}) + +      assert json_response(conn, 200) == %{"error" => "Invalid password."} +    end + +    test "with credentials and valid password", %{conn: conn, user: current_user} do +      conn = +        conn +        |> with_credentials(current_user.nickname, "test") +        |> post("/api/pleroma/delete_account", %{"password" => "test"}) + +      assert json_response(conn, 200) == %{"status" => "success"} +      # Wait a second for the started task to end +      :timer.sleep(1000) +    end +  end  end diff --git a/test/web/uploader_controller_test.exs b/test/web/uploader_controller_test.exs index 70028df1c..7c7f9a6ea 100644 --- a/test/web/uploader_controller_test.exs +++ b/test/web/uploader_controller_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.UploaderControllerTest do diff --git a/test/web/views/error_view_test.exs b/test/web/views/error_view_test.exs index 3857d585f..4e5398c83 100644 --- a/test/web/views/error_view_test.exs +++ b/test/web/views/error_view_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.ErrorViewTest do diff --git a/test/web/web_finger/web_finger_controller_test.exs b/test/web/web_finger/web_finger_controller_test.exs index e23086b2a..49cd1460b 100644 --- a/test/web/web_finger/web_finger_controller_test.exs +++ b/test/web/web_finger/web_finger_controller_test.exs @@ -1,10 +1,11 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do    use Pleroma.Web.ConnCase +  import ExUnit.CaptureLog    import Pleroma.Factory    import Tesla.Mock @@ -75,11 +76,13 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do    test "Sends a 404 when invalid format" do      user = insert(:user) -    assert_raise Phoenix.NotAcceptableError, fn -> -      build_conn() -      |> put_req_header("accept", "text/html") -      |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost") -    end +    assert capture_log(fn -> +             assert_raise Phoenix.NotAcceptableError, fn -> +               build_conn() +               |> put_req_header("accept", "text/html") +               |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost") +             end +           end) =~ "no supported media type in accept header"    end    test "Sends a 400 when resource param is missing" do diff --git a/test/web/web_finger/web_finger_test.exs b/test/web/web_finger/web_finger_test.exs index 8fdb9adea..696c1bd70 100644 --- a/test/web/web_finger/web_finger_test.exs +++ b/test/web/web_finger/web_finger_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.WebFingerTest do diff --git a/test/web/websub/websub_controller_test.exs b/test/web/websub/websub_controller_test.exs index 59cacbe68..f6d002b3b 100644 --- a/test/web/websub/websub_controller_test.exs +++ b/test/web/websub/websub_controller_test.exs @@ -1,5 +1,5 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.Websub.WebsubControllerTest do diff --git a/test/web/websub/websub_test.exs b/test/web/websub/websub_test.exs index 74386d7db..46ca545de 100644 --- a/test/web/websub/websub_test.exs +++ b/test/web/websub/websub_test.exs @@ -1,14 +1,17 @@  # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.WebsubTest do    use Pleroma.DataCase +  use Oban.Testing, repo: Pleroma.Repo +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.Web.Router.Helpers    alias Pleroma.Web.Websub    alias Pleroma.Web.Websub.WebsubClientSubscription    alias Pleroma.Web.Websub.WebsubServerSubscription +  alias Pleroma.Workers.SubscriberWorker    import Pleroma.Factory    import Tesla.Mock @@ -224,6 +227,7 @@ defmodule Pleroma.Web.WebsubTest do          })        _refresh = Websub.refresh_subscriptions() +      ObanHelpers.perform(all_enqueued(worker: SubscriberWorker))        assert still_good == Repo.get(WebsubClientSubscription, still_good.id)        refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id) | 
