diff options
Diffstat (limited to 'test/web/websub')
| -rw-r--r-- | test/web/websub/websub_controller_test.exs | 57 | ||||
| -rw-r--r-- | test/web/websub/websub_test.exs | 44 | 
2 files changed, 56 insertions, 45 deletions
| diff --git a/test/web/websub/websub_controller_test.exs b/test/web/websub/websub_controller_test.exs index d861c241f..1e69ed01a 100644 --- a/test/web/websub/websub_controller_test.exs +++ b/test/web/websub/websub_controller_test.exs @@ -1,9 +1,14 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only +  defmodule Pleroma.Web.Websub.WebsubControllerTest do    use Pleroma.Web.ConnCase    import Pleroma.Factory -  alias Pleroma.Web.Websub.WebsubClientSubscription -  alias Pleroma.{Repo, Activity} +  alias Pleroma.Activity +  alias Pleroma.Repo    alias Pleroma.Web.Websub +  alias Pleroma.Web.Websub.WebsubClientSubscription    test "websub subscription request", %{conn: conn} do      user = insert(:user) @@ -46,35 +51,37 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do      assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5    end -  test "handles incoming feed updates", %{conn: conn} do -    websub = insert(:websub_client_subscription) -    doc = "some stuff" -    signature = Websub.sign(websub.secret, doc) +  describe "websub_incoming" do +    test "handles incoming feed updates", %{conn: conn} do +      websub = insert(:websub_client_subscription) +      doc = "some stuff" +      signature = Websub.sign(websub.secret, doc) -    conn = -      conn -      |> put_req_header("x-hub-signature", "sha1=" <> signature) -      |> put_req_header("content-type", "application/atom+xml") -      |> post("/push/subscriptions/#{websub.id}", doc) +      conn = +        conn +        |> put_req_header("x-hub-signature", "sha1=" <> signature) +        |> put_req_header("content-type", "application/atom+xml") +        |> post("/push/subscriptions/#{websub.id}", doc) -    assert response(conn, 200) == "OK" +      assert response(conn, 200) == "OK" -    assert length(Repo.all(Activity)) == 1 -  end +      assert length(Repo.all(Activity)) == 1 +    end -  test "rejects incoming feed updates with the wrong signature", %{conn: conn} do -    websub = insert(:websub_client_subscription) -    doc = "some stuff" -    signature = Websub.sign("wrong secret", doc) +    test "rejects incoming feed updates with the wrong signature", %{conn: conn} do +      websub = insert(:websub_client_subscription) +      doc = "some stuff" +      signature = Websub.sign("wrong secret", doc) -    conn = -      conn -      |> put_req_header("x-hub-signature", "sha1=" <> signature) -      |> put_req_header("content-type", "application/atom+xml") -      |> post("/push/subscriptions/#{websub.id}", doc) +      conn = +        conn +        |> put_req_header("x-hub-signature", "sha1=" <> signature) +        |> put_req_header("content-type", "application/atom+xml") +        |> post("/push/subscriptions/#{websub.id}", doc) -    assert response(conn, 500) == "Error" +      assert response(conn, 500) == "Error" -    assert length(Repo.all(Activity)) == 0 +      assert Enum.empty?(Repo.all(Activity)) +    end    end  end diff --git a/test/web/websub/websub_test.exs b/test/web/websub/websub_test.exs index da7bc9112..74386d7db 100644 --- a/test/web/websub/websub_test.exs +++ b/test/web/websub/websub_test.exs @@ -1,15 +1,22 @@ -defmodule Pleroma.Web.WebsubMock do -  def verify(sub) do -    {:ok, sub} -  end -end +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Web.WebsubTest do    use Pleroma.DataCase + +  alias Pleroma.Web.Router.Helpers    alias Pleroma.Web.Websub -  alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription} +  alias Pleroma.Web.Websub.WebsubClientSubscription +  alias Pleroma.Web.Websub.WebsubServerSubscription +    import Pleroma.Factory -  alias Pleroma.Web.Router.Helpers +  import Tesla.Mock + +  setup do +    mock(fn env -> apply(HttpRequestMock, :request, [env]) end) +    :ok +  end    test "a verification of a request that is accepted" do      sub = insert(:websub_subscription) @@ -26,8 +33,8 @@ defmodule Pleroma.Web.WebsubTest do        assert String.to_integer(seconds) > 0        {:ok, -       %HTTPoison.Response{ -         status_code: 200, +       %Tesla.Env{ +         status: 200,           body: challenge         }}      end @@ -41,8 +48,8 @@ defmodule Pleroma.Web.WebsubTest do      getter = fn _path, _headers, _options ->        {:ok, -       %HTTPoison.Response{ -         status_code: 500, +       %Tesla.Env{ +         status: 500,           body: ""         }}      end @@ -113,12 +120,7 @@ defmodule Pleroma.Web.WebsubTest do    test "discovers the hub and canonical url" do      topic = "https://mastodon.social/users/lambadalambda.atom" -    getter = fn ^topic -> -      doc = File.read!("test/fixtures/lambadalambda.atom") -      {:ok, %{status_code: 200, body: doc}} -    end - -    {:ok, discovered} = Websub.gather_feed_data(topic, getter) +    {:ok, discovered} = Websub.gather_feed_data(topic)      expected = %{        "hub" => "https://mastodon.social/api/push", @@ -158,7 +160,7 @@ defmodule Pleroma.Web.WebsubTest do                   websub.id                 ) -      {:ok, %{status_code: 202}} +      {:ok, %{status: 202}}      end      task = Task.async(fn -> Websub.request_subscription(websub, poster) end) @@ -177,7 +179,7 @@ defmodule Pleroma.Web.WebsubTest do      websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})      poster = fn ^hub, {:form, _data}, _headers -> -      {:ok, %{status_code: 202}} +      {:ok, %{status: 202}}      end      {:error, websub} = Websub.request_subscription(websub, poster, 1000) @@ -186,7 +188,7 @@ defmodule Pleroma.Web.WebsubTest do      websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})      poster = fn ^hub, {:form, _data}, _headers -> -      {:ok, %{status_code: 400}} +      {:ok, %{status: 400}}      end      {:error, websub} = Websub.request_subscription(websub, poster, 1000) @@ -209,6 +211,7 @@ defmodule Pleroma.Web.WebsubTest do          insert(:websub_client_subscription, %{            valid_until: NaiveDateTime.add(now, 2 * day),            topic: "http://example.org/still_good", +          hub: "http://example.org/still_good",            state: "accepted"          }) @@ -216,6 +219,7 @@ defmodule Pleroma.Web.WebsubTest do          insert(:websub_client_subscription, %{            valid_until: NaiveDateTime.add(now, day - 100),            topic: "http://example.org/needs_refresh", +          hub: "http://example.org/needs_refresh",            state: "accepted"          }) | 
