diff options
Diffstat (limited to 'test/support')
| -rw-r--r-- | test/support/builders/user_builder.ex | 3 | ||||
| -rw-r--r-- | test/support/captcha_mock.ex | 2 | ||||
| -rw-r--r-- | test/support/conn_case.ex | 1 | ||||
| -rw-r--r-- | test/support/data_case.ex | 1 | ||||
| -rw-r--r-- | test/support/factory.ex | 91 | ||||
| -rw-r--r-- | test/support/http_request_mock.ex | 124 | ||||
| -rw-r--r-- | test/support/web_push_http_client_mock.ex | 23 | 
7 files changed, 223 insertions, 22 deletions
diff --git a/test/support/builders/user_builder.ex b/test/support/builders/user_builder.ex index 7a1ca79b5..f58e1b0ad 100644 --- a/test/support/builders/user_builder.ex +++ b/test/support/builders/user_builder.ex @@ -1,5 +1,6 @@  defmodule Pleroma.Builders.UserBuilder do -  alias Pleroma.{User, Repo} +  alias Pleroma.Repo +  alias Pleroma.User    def build(data \\ %{}) do      user = %User{ diff --git a/test/support/captcha_mock.ex b/test/support/captcha_mock.ex index 9061f2b45..ef4e68bc5 100644 --- a/test/support/captcha_mock.ex +++ b/test/support/captcha_mock.ex @@ -7,7 +7,7 @@ defmodule Pleroma.Captcha.Mock do    @behaviour Service    @impl Service -  def new(), do: %{type: :mock} +  def new, do: %{type: :mock}    @impl Service    def validate(_token, _captcha, _data), do: :ok diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index c201d9a9b..ec5892ff5 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -33,6 +33,7 @@ defmodule Pleroma.Web.ConnCase do    setup tags do      Cachex.clear(:user_cache) +    Cachex.clear(:object_cache)      :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)      unless tags[:async] do diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 56d5896ad..df260bd3f 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -32,6 +32,7 @@ defmodule Pleroma.DataCase do    setup tags do      Cachex.clear(:user_cache) +    Cachex.clear(:object_cache)      :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)      unless tags[:async] do diff --git a/test/support/factory.ex b/test/support/factory.ex index 4ac77981a..ea59912cf 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -23,7 +23,7 @@ defmodule Pleroma.Factory do      }    end -  def note_factory do +  def note_factory(attrs \\ %{}) do      text = sequence(:text, &"This is :moominmamma: note #{&1}")      user = insert(:user) @@ -46,7 +46,7 @@ defmodule Pleroma.Factory do      }      %Pleroma.Object{ -      data: data +      data: merge_attributes(data, Map.get(attrs, :data, %{}))      }    end @@ -95,8 +95,8 @@ defmodule Pleroma.Factory do      }    end -  def note_activity_factory do -    note = insert(:note) +  def note_activity_factory(attrs \\ %{}) do +    note = attrs[:note] || insert(:note)      data = %{        "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(), @@ -135,9 +135,9 @@ defmodule Pleroma.Factory do      }    end -  def announce_activity_factory do -    note_activity = insert(:note_activity) -    user = insert(:user) +  def announce_activity_factory(attrs \\ %{}) do +    note_activity = attrs[:note_activity] || insert(:note_activity) +    user = attrs[:user] || insert(:user)      data = %{        "type" => "Announce", @@ -193,7 +193,7 @@ defmodule Pleroma.Factory do    def websub_subscription_factory do      %Pleroma.Web.Websub.WebsubServerSubscription{        topic: "http://example.org", -      callback: "http://example/org/callback", +      callback: "http://example.org/callback",        secret: "here's a secret",        valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),        state: "requested" @@ -214,10 +214,81 @@ defmodule Pleroma.Factory do      %Pleroma.Web.OAuth.App{        client_name: "Some client",        redirect_uris: "https://example.com/callback", -      scopes: "read", +      scopes: ["read", "write", "follow", "push"],        website: "https://example.com", -      client_id: "aaabbb==", +      client_id: Ecto.UUID.generate(),        client_secret: "aaa;/&bbb"      }    end + +  def instance_factory do +    %Pleroma.Instances.Instance{ +      host: "domain.com", +      unreachable_since: nil +    } +  end + +  def oauth_token_factory do +    oauth_app = insert(:oauth_app) + +    %Pleroma.Web.OAuth.Token{ +      token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(), +      refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(), +      user: build(:user), +      app_id: oauth_app.id, +      valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10) +    } +  end + +  def oauth_authorization_factory do +    %Pleroma.Web.OAuth.Authorization{ +      token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false), +      scopes: ["read", "write", "follow", "push"], +      valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10), +      user: build(:user), +      app: build(:oauth_app) +    } +  end + +  def push_subscription_factory do +    %Pleroma.Web.Push.Subscription{ +      user: build(:user), +      token: build(:oauth_token), +      endpoint: "https://example.com/example/1234", +      key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==", +      key_p256dh: +        "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=", +      data: %{} +    } +  end + +  def notification_factory do +    %Pleroma.Notification{ +      user: build(:user) +    } +  end + +  def scheduled_activity_factory do +    %Pleroma.ScheduledActivity{ +      user: build(:user), +      scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond), +      params: build(:note) |> Map.from_struct() |> Map.get(:data) +    } +  end + +  def registration_factory do +    user = insert(:user) + +    %Pleroma.Registration{ +      user: user, +      provider: "twitter", +      uid: "171799000", +      info: %{ +        "name" => "John Doe", +        "email" => "john@doe.com", +        "nickname" => "johndoe", +        "description" => "My bio" +      } +    } +  end  end diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex index 3043d2be6..5b355bfe6 100644 --- a/test/support/http_request_mock.ex +++ b/test/support/http_request_mock.ex @@ -36,6 +36,43 @@ defmodule HttpRequestMock do       }}    end +  def get("https://mastodon.social/users/emelie/statuses/101849165031453009", _, _, _) do +    {:ok, +     %Tesla.Env{ +       status: 200, +       body: File.read!("test/fixtures/httpoison_mock/status.emelie.json") +     }} +  end + +  def get("https://mastodon.social/users/emelie", _, _, _) do +    {:ok, +     %Tesla.Env{ +       status: 200, +       body: File.read!("test/fixtures/httpoison_mock/emelie.json") +     }} +  end + +  def get( +        "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/emelie", +        _, +        _, +        _ +      ) do +    {:ok, +     %Tesla.Env{ +       status: 200, +       body: File.read!("test/fixtures/httpoison_mock/webfinger_emelie.json") +     }} +  end + +  def get("https://mastodon.social/users/emelie.atom", _, _, _) do +    {:ok, +     %Tesla.Env{ +       status: 200, +       body: File.read!("test/fixtures/httpoison_mock/emelie.atom") +     }} +  end +    def get(          "https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com",          _, @@ -143,7 +180,10 @@ defmodule HttpRequestMock do       }}    end -  def get("https://squeet.me/xrd/?uri=lain@squeet.me", _, _, +  def get( +        "https://squeet.me/xrd/?uri=lain@squeet.me", +        _, +        _,          Accept: "application/xrd+xml,application/jrd+json"        ) do      {:ok, @@ -153,7 +193,10 @@ defmodule HttpRequestMock do       }}    end -  def get("https://mst3k.interlinked.me/users/luciferMysticus", _, _, +  def get( +        "https://mst3k.interlinked.me/users/luciferMysticus", +        _, +        _,          Accept: "application/activity+json"        ) do      {:ok, @@ -171,7 +214,10 @@ defmodule HttpRequestMock do       }}    end -  def get("https://hubzilla.example.org/channel/kaniini", _, _, +  def get( +        "https://hubzilla.example.org/channel/kaniini", +        _, +        _,          Accept: "application/activity+json"        ) do      {:ok, @@ -248,7 +294,10 @@ defmodule HttpRequestMock do       }}    end -  def get("http://mastodon.example.org/@admin/99541947525187367", _, _, +  def get( +        "http://mastodon.example.org/@admin/99541947525187367", +        _, +        _,          Accept: "application/activity+json"        ) do      {:ok, @@ -274,7 +323,10 @@ defmodule HttpRequestMock do       }}    end -  def get("https://mstdn.io/users/mayuutann/statuses/99568293732299394", _, _, +  def get( +        "https://mstdn.io/users/mayuutann/statuses/99568293732299394", +        _, +        _,          Accept: "application/activity+json"        ) do      {:ok, @@ -429,7 +481,10 @@ defmodule HttpRequestMock do       }}    end -  def get("https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056", _, _, +  def get( +        "https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056", +        _, +        _,          Accept: "application/atom+xml"        ) do      {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/sakamoto.atom")}} @@ -510,7 +565,10 @@ defmodule HttpRequestMock do       %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/squeet.me_host_meta")}}    end -  def get("https://squeet.me/xrd?uri=lain@squeet.me", _, _, +  def get( +        "https://squeet.me/xrd?uri=lain@squeet.me", +        _, +        _,          Accept: "application/xrd+xml,application/jrd+json"        ) do      {:ok, @@ -541,7 +599,10 @@ defmodule HttpRequestMock do       }}    end -  def get("http://framatube.org/main/xrd?uri=framasoft@framatube.org", _, _, +  def get( +        "http://framatube.org/main/xrd?uri=framasoft@framatube.org", +        _, +        _,          Accept: "application/xrd+xml,application/jrd+json"        ) do      {:ok, @@ -560,7 +621,10 @@ defmodule HttpRequestMock do       }}    end -  def get("http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de", _, _, +  def get( +        "http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de", +        _, +        _,          Accept: "application/xrd+xml,application/jrd+json"        ) do      {:ok, @@ -594,7 +658,10 @@ defmodule HttpRequestMock do       }}    end -  def get("https://gerzilla.de/xrd/?uri=kaniini@gerzilla.de", _, _, +  def get( +        "https://gerzilla.de/xrd/?uri=kaniini@gerzilla.de", +        _, +        _,          Accept: "application/xrd+xml,application/jrd+json"        ) do      {:ok, @@ -649,6 +716,10 @@ defmodule HttpRequestMock do      {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}}    end +  def get("https://mastodon.social/users/lambadalambda", _, _, _) do +    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.json")}} +  end +    def get("https://social.heldscal.la/user/23211", _, _, Accept: "application/activity+json") do      {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}    end @@ -657,10 +728,23 @@ defmodule HttpRequestMock do      {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}    end +  def get("http://example.com/malformed", _, _, _) do +    {:ok, +     %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/malformed-data.html")}} +  end +    def get("http://example.com/empty", _, _, _) do      {:ok, %Tesla.Env{status: 200, body: "hello"}}    end +  def get("http://404.site" <> _, _, _, _) do +    {:ok, +     %Tesla.Env{ +       status: 404, +       body: "" +     }} +  end +    def get(url, query, body, headers) do      {:error,       "Not implemented the mock response for get #{inspect(url)}, #{query}, #{inspect(body)}, #{ @@ -681,6 +765,26 @@ defmodule HttpRequestMock do       }}    end +  def post("http://200.site" <> _, _, _, _) do +    {:ok, +     %Tesla.Env{ +       status: 200, +       body: "" +     }} +  end + +  def post("http://connrefused.site" <> _, _, _, _) do +    {:error, :connrefused} +  end + +  def post("http://404.site" <> _, _, _, _) do +    {:ok, +     %Tesla.Env{ +       status: 404, +       body: "" +     }} +  end +    def post(url, _query, _body, _headers) do      {:error, "Not implemented the mock response for post #{inspect(url)}"}    end diff --git a/test/support/web_push_http_client_mock.ex b/test/support/web_push_http_client_mock.ex new file mode 100644 index 000000000..d8accd21c --- /dev/null +++ b/test/support/web_push_http_client_mock.ex @@ -0,0 +1,23 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.WebPushHttpClientMock do +  def get(url, headers \\ [], options \\ []) do +    { +      res, +      %Tesla.Env{status: status} +    } = Pleroma.HTTP.request(:get, url, "", headers, options) + +    {res, %{status_code: status}} +  end + +  def post(url, body, headers \\ [], options \\ []) do +    { +      res, +      %Tesla.Env{status: status} +    } = Pleroma.HTTP.request(:post, url, body, headers, options) + +    {res, %{status_code: status}} +  end +end  | 
