Merge branch 'deprecate-public_endpoint' into 'develop'
[akkoma] / test / support / data_case.ex
index ba8848952e11b0d7e7e7895f05b8099194a7177f..1f33450e63da80b466b6b3bfd7224e8753f1ae82 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.DataCase do
@@ -18,6 +18,8 @@ defmodule Pleroma.DataCase do
 
   use ExUnit.CaseTemplate
 
+  import Pleroma.Tests.Helpers, only: [clear_config: 2]
+
   using do
     quote do
       alias Pleroma.Repo
@@ -27,16 +29,52 @@ defmodule Pleroma.DataCase do
       import Ecto.Query
       import Pleroma.DataCase
       use Pleroma.Tests.Helpers
+
+      # Sets up OAuth access with specified scopes
+      defp oauth_access(scopes, opts \\ []) do
+        user =
+          Keyword.get_lazy(opts, :user, fn ->
+            Pleroma.Factory.insert(:user)
+          end)
+
+        token =
+          Keyword.get_lazy(opts, :oauth_token, fn ->
+            Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
+          end)
+
+        %{user: user, token: token}
+      end
     end
   end
 
+  def clear_cachex do
+    Pleroma.Supervisor
+    |> Supervisor.which_children()
+    |> Enum.each(fn
+      {name, _, _, [Cachex]} ->
+        name
+        |> to_string
+        |> String.trim_leading("cachex_")
+        |> Kernel.<>("_cache")
+        |> String.to_existing_atom()
+        |> Cachex.clear()
+
+      _ ->
+        nil
+    end)
+  end
+
   setup tags do
-    Cachex.clear(:user_cache)
-    Cachex.clear(:object_cache)
     :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
 
-    unless tags[:async] do
+    if tags[:async] do
+      Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache)
+      Mox.set_mox_private()
+    else
       Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
+      Mox.stub_with(Pleroma.CachexMock, Pleroma.CachexProxy)
+      Mox.set_mox_global()
+      clear_cachex()
     end
 
     if tags[:needs_streamer] do
@@ -47,21 +85,32 @@ defmodule Pleroma.DataCase do
       })
     end
 
+    stub_pipeline()
+
+    Mox.verify_on_exit!()
+
     :ok
   end
 
-  def ensure_local_uploader(context) do
-    test_uploader = Map.get(context, :uploader, Pleroma.Uploaders.Local)
-    uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
-    filters = Pleroma.Config.get([Pleroma.Upload, :filters])
+  def stub_pipeline do
+    Mox.stub_with(Pleroma.Web.ActivityPub.SideEffectsMock, Pleroma.Web.ActivityPub.SideEffects)
 
-    Pleroma.Config.put([Pleroma.Upload, :uploader], test_uploader)
-    Pleroma.Config.put([Pleroma.Upload, :filters], [])
+    Mox.stub_with(
+      Pleroma.Web.ActivityPub.ObjectValidatorMock,
+      Pleroma.Web.ActivityPub.ObjectValidator
+    )
 
-    on_exit(fn ->
-      Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
-      Pleroma.Config.put([Pleroma.Upload, :filters], filters)
-    end)
+    Mox.stub_with(Pleroma.Web.ActivityPub.MRFMock, Pleroma.Web.ActivityPub.MRF)
+    Mox.stub_with(Pleroma.Web.ActivityPub.ActivityPubMock, Pleroma.Web.ActivityPub.ActivityPub)
+    Mox.stub_with(Pleroma.Web.FederatorMock, Pleroma.Web.Federator)
+    Mox.stub_with(Pleroma.ConfigMock, Pleroma.Config)
+  end
+
+  def ensure_local_uploader(context) do
+    test_uploader = Map.get(context, :uploader) || Pleroma.Uploaders.Local
+
+    clear_config([Pleroma.Upload, :uploader], test_uploader)
+    clear_config([Pleroma.Upload, :filters], [])
 
     :ok
   end