diff options
| author | Ivan Tashkinov <ivantashkinov@gmail.com> | 2019-09-14 16:11:44 +0300 | 
|---|---|---|
| committer | Ivan Tashkinov <ivantashkinov@gmail.com> | 2019-09-14 16:11:44 +0300 | 
| commit | 6bcedb693c47fcef44ff3b4412629d1c50c2a1c7 (patch) | |
| tree | 0420581ba993b70cb957c261cb83ef90597ddeb4 /test/web | |
| parent | c3f00447afc67b460e63b531e4f2432bfaa37bdb (diff) | |
| parent | a9b78f55e3561eec3cd125f030d2dd6ec338d406 (diff) | |
| download | pleroma-6bcedb693c47fcef44ff3b4412629d1c50c2a1c7.tar.gz pleroma-6bcedb693c47fcef44ff3b4412629d1c50c2a1c7.zip | |
[#1149] Merge remote-tracking branch 'remotes/upstream/develop' into 1149-oban-job-queue
# Conflicts:
#	docs/config.md
#	mix.lock
Diffstat (limited to 'test/web')
| -rw-r--r-- | test/web/activity_pub/activity_pub_controller_test.exs | 99 | ||||
| -rw-r--r-- | test/web/activity_pub/publisher_test.exs | 24 | ||||
| -rw-r--r-- | test/web/activity_pub/relay_test.exs | 13 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier_test.exs | 2 | ||||
| -rw-r--r-- | test/web/admin_api/admin_api_controller_test.exs | 12 | ||||
| -rw-r--r-- | test/web/admin_api/config_test.exs | 24 | ||||
| -rw-r--r-- | test/web/mastodon_api/mastodon_api_controller_test.exs | 24 | ||||
| -rw-r--r-- | test/web/twitter_api/util_controller_test.exs | 120 | ||||
| -rw-r--r-- | test/web/web_finger/web_finger_controller_test.exs | 13 | 
9 files changed, 300 insertions, 31 deletions
| diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index f1c1bb503..9b78fb72d 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -179,6 +179,49 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        assert json_response(conn, 404)      end + +    test "it caches a response", %{conn: conn} do +      note = insert(:note) +      uuid = String.split(note.data["id"], "/") |> List.last() + +      conn1 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/objects/#{uuid}") + +      assert json_response(conn1, :ok) +      assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"})) + +      conn2 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/objects/#{uuid}") + +      assert json_response(conn1, :ok) == json_response(conn2, :ok) +      assert Enum.any?(conn2.resp_headers, &(&1 == {"x-cache", "HIT from Pleroma"})) +    end + +    test "cached purged after object deletion", %{conn: conn} do +      note = insert(:note) +      uuid = String.split(note.data["id"], "/") |> List.last() + +      conn1 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/objects/#{uuid}") + +      assert json_response(conn1, :ok) +      assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"})) + +      Object.delete(note) + +      conn2 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/objects/#{uuid}") + +      assert "Not found" == json_response(conn2, :not_found) +    end    end    describe "/object/:uuid/likes" do @@ -268,6 +311,51 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        assert json_response(conn, 404)      end + +    test "it caches a response", %{conn: conn} do +      activity = insert(:note_activity) +      uuid = String.split(activity.data["id"], "/") |> List.last() + +      conn1 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/activities/#{uuid}") + +      assert json_response(conn1, :ok) +      assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"})) + +      conn2 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/activities/#{uuid}") + +      assert json_response(conn1, :ok) == json_response(conn2, :ok) +      assert Enum.any?(conn2.resp_headers, &(&1 == {"x-cache", "HIT from Pleroma"})) +    end + +    test "cached purged after activity deletion", %{conn: conn} do +      user = insert(:user) +      {:ok, activity} = CommonAPI.post(user, %{"status" => "cofe"}) + +      uuid = String.split(activity.data["id"], "/") |> List.last() + +      conn1 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/activities/#{uuid}") + +      assert json_response(conn1, :ok) +      assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"})) + +      Activity.delete_by_ap_id(activity.object.data["id"]) + +      conn2 = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/activities/#{uuid}") + +      assert "Not found" == json_response(conn2, :not_found) +    end    end    describe "/inbox" do @@ -370,6 +458,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        assert json_response(conn, 403)      end +    test "it doesn't crash without an authenticated user", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> put_req_header("accept", "application/activity+json") +        |> get("/users/#{user.nickname}/inbox") + +      assert json_response(conn, 403) +    end +      test "it returns a note activity in a collection", %{conn: conn} do        note_activity = insert(:direct_note_activity)        note_object = Object.normalize(note_activity) diff --git a/test/web/activity_pub/publisher_test.exs b/test/web/activity_pub/publisher_test.exs index 26d019878..c7d0dc3a5 100644 --- a/test/web/activity_pub/publisher_test.exs +++ b/test/web/activity_pub/publisher_test.exs @@ -5,6 +5,7 @@  defmodule Pleroma.Web.ActivityPub.PublisherTest do    use Pleroma.DataCase +  import ExUnit.CaptureLog    import Pleroma.Factory    import Tesla.Mock    import Mock @@ -188,7 +189,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 +216,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 diff --git a/test/web/activity_pub/relay_test.exs b/test/web/activity_pub/relay_test.exs index a64011ff0..7315dce26 100644 --- a/test/web/activity_pub/relay_test.exs +++ b/test/web/activity_pub/relay_test.exs @@ -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", diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index af47745b6..6c296eb0d 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -103,7 +103,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert capture_log(fn ->                 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data) -             end) =~ "[error] Couldn't fetch \"\"https://404.site/whatever\"\", error: nil" +             end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"      end      test "it works for incoming notices" do diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 578996f70..516de5d0c 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1779,7 +1779,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 +1800,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"]}                     ]                   }                 ] 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/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 9269e1e09..22da3e605 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -745,6 +745,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      assert id == to_string(activity.id)    end +  test "get statuses by IDs", %{conn: conn} do +    %{id: id1} = insert(:note_activity) +    %{id: id2} = insert(:note_activity) + +    query_string = "ids[]=#{id1}&ids[]=#{id2}" +    conn = get(conn, "/api/v1/statuses/?#{query_string}") + +    assert [%{"id" => ^id1}, %{"id" => ^id2}] = json_response(conn, :ok) +  end +    describe "deleting a status" do      test "when you created it", %{conn: conn} do        activity = insert(:note_activity) @@ -3957,13 +3967,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/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index 5a3ae53aa..0a2a48fb7 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -10,6 +10,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do    alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.CommonAPI +  import ExUnit.CaptureLog    import Pleroma.Factory    import Mock @@ -340,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 @@ -665,4 +668,111 @@ 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  end diff --git a/test/web/web_finger/web_finger_controller_test.exs b/test/web/web_finger/web_finger_controller_test.exs index e23086b2a..bd3ccaaf7 100644 --- a/test/web/web_finger/web_finger_controller_test.exs +++ b/test/web/web_finger/web_finger_controller_test.exs @@ -5,6 +5,7 @@  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 | 
