adding gun adapter
[akkoma] / test / web / admin_api / admin_api_controller_test.exs
index cfa6207c24061e3ed79b374057b2cc366bd20be7..02ffbfa0b056c3653a893f8bc2799846bf76bbda 100644 (file)
@@ -1364,6 +1364,30 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       }
     end
 
+    test "requires admin:write:reports scope", %{conn: conn, id: id, admin: admin} do
+      read_token = insert(:oauth_token, user: admin, scopes: ["admin:read"])
+      write_token = insert(:oauth_token, user: admin, scopes: ["admin:write:reports"])
+
+      response =
+        conn
+        |> assign(:token, read_token)
+        |> patch("/api/pleroma/admin/reports", %{
+          "reports" => [%{"state" => "resolved", "id" => id}]
+        })
+        |> json_response(403)
+
+      assert response == %{
+               "error" => "Insufficient permissions: admin:write:reports."
+             }
+
+      conn
+      |> assign(:token, write_token)
+      |> patch("/api/pleroma/admin/reports", %{
+        "reports" => [%{"state" => "resolved", "id" => id}]
+      })
+      |> json_response(:no_content)
+    end
+
     test "mark report as resolved", %{conn: conn, id: id, admin: admin} do
       conn
       |> patch("/api/pleroma/admin/reports", %{
@@ -1875,13 +1899,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                "To use this endpoint you need to enable configuration from database."
     end
 
-    test "without any settings in db", %{conn: conn} do
-      conn = get(conn, "/api/pleroma/admin/config")
-
-      assert json_response(conn, 400) ==
-               "To use configuration from database migrate your settings to database."
-    end
-
     test "with settings only in db", %{conn: conn} do
       config1 = insert(:config)
       config2 = insert(:config)
@@ -1907,6 +1924,22 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       assert key2 == config2.key
     end
 
+    test "db is added to settings that are in db", %{conn: conn} do
+      _config = insert(:config, key: ":instance", value: ConfigDB.to_binary(name: "Some name"))
+
+      %{"configs" => configs} =
+        conn
+        |> get("/api/pleroma/admin/config")
+        |> json_response(200)
+
+      [instance_config] =
+        Enum.filter(configs, fn %{"group" => group, "key" => key} ->
+          group == ":pleroma" and key == ":instance"
+        end)
+
+      assert instance_config["db"] == [":name"]
+    end
+
     test "merged default setting with db settings", %{conn: conn} do
       config1 = insert(:config)
       config2 = insert(:config)
@@ -1965,13 +1998,16 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
         |> get("/api/pleroma/admin/config")
         |> json_response(200)
 
-      [%{"key" => ":emoji", "value" => emoji_val}, %{"key" => ":assets", "value" => assets_val}] =
+      vals =
         Enum.filter(configs, fn %{"group" => group, "key" => key} ->
           group == ":pleroma" and key in [config1.key, config2.key]
         end)
 
-      emoji_val = ConfigDB.transform_with_out_binary(emoji_val)
-      assets_val = ConfigDB.transform_with_out_binary(assets_val)
+      emoji = Enum.find(vals, fn %{"key" => key} -> key == ":emoji" end)
+      assets = Enum.find(vals, fn %{"key" => key} -> key == ":assets" end)
+
+      emoji_val = ConfigDB.transform_with_out_binary(emoji["value"])
+      assets_val = ConfigDB.transform_with_out_binary(assets["value"])
 
       assert emoji_val[:groups] == [a: 1, b: 2]
       assert assets_val[:mascots] == [a: 1, b: 2]
@@ -2000,7 +2036,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
         Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)
         Application.put_env(:pleroma, :http, http)
         Application.put_env(:tesla, :adapter, Tesla.Mock)
-        :ok = File.rm("config/test.exported_from_db.secret.exs")
       end)
     end
 
@@ -2010,6 +2045,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
     @tag capture_log: true
     test "create new config setting in db", %{conn: conn} do
+      ueberauth = Application.get_env(:ueberauth, Ueberauth)
+      on_exit(fn -> Application.put_env(:ueberauth, Ueberauth, ueberauth) end)
+
       conn =
         post(conn, "/api/pleroma/admin/config", %{
           configs: [
@@ -2124,7 +2162,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []}
     end
 
-    test "save config setting without key", %{conn: conn} do
+    test "save configs setting without explicit key", %{conn: conn} do
       level = Application.get_env(:quack, :level)
       meta = Application.get_env(:quack, :meta)
       webhook_url = Application.get_env(:quack, :webhook_url)
@@ -2210,6 +2248,34 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
              }
     end
 
+    test "saving config which need pleroma reboot", %{conn: conn} do
+      chat = Pleroma.Config.get(:chat)
+      on_exit(fn -> Pleroma.Config.put(:chat, chat) end)
+
+      conn =
+        post(
+          conn,
+          "/api/pleroma/admin/config",
+          %{
+            configs: [
+              %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]}
+            ]
+          }
+        )
+
+      assert json_response(conn, 200) == %{
+               "configs" => [
+                 %{
+                   "db" => [":enabled"],
+                   "group" => ":pleroma",
+                   "key" => ":chat",
+                   "value" => [%{"tuple" => [":enabled", true]}]
+                 }
+               ],
+               "need_reboot" => true
+             }
+    end
+
     test "saving config with nested merge", %{conn: conn} do
       config =
         insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: [k1: 1, k2: 2]))
@@ -2373,29 +2439,31 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "value" => "Tesla.Adapter.Httpc",
                    "db" => [":adapter"]
                  }
-               ]
+               ],
+               "need_reboot" => true
              }
     end
 
-    test "update config setting & delete", %{conn: conn} do
+    test "update config setting & delete with fallback to default value", %{
+      conn: conn,
+      admin: admin,
+      token: token
+    } do
+      ueberauth = Application.get_env(:ueberauth, Ueberauth)
       config1 = insert(:config, key: ":keyaa1")
       config2 = insert(:config, key: ":keyaa2")
 
-      insert(:config,
-        group: "ueberauth",
-        key: "Ueberauth.Strategy.Microsoft.OAuth"
-      )
+      config3 =
+        insert(:config,
+          group: ":ueberauth",
+          key: "Ueberauth"
+        )
 
       conn =
         post(conn, "/api/pleroma/admin/config", %{
           configs: [
             %{group: config1.group, key: config1.key, value: "another_value"},
-            %{group: config2.group, key: config2.key, delete: true},
-            %{
-              group: "ueberauth",
-              key: "Ueberauth.Strategy.Microsoft.OAuth",
-              delete: true
-            }
+            %{group: config2.group, key: config2.key, value: "another_value"}
           ]
         })
 
@@ -2406,12 +2474,41 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "key" => config1.key,
                    "value" => "another_value",
                    "db" => [":keyaa1"]
+                 },
+                 %{
+                   "group" => ":pleroma",
+                   "key" => config2.key,
+                   "value" => "another_value",
+                   "db" => [":keyaa2"]
                  }
                ]
              }
 
       assert Application.get_env(:pleroma, :keyaa1) == "another_value"
-      refute Application.get_env(:pleroma, :keyaa2)
+      assert Application.get_env(:pleroma, :keyaa2) == "another_value"
+      assert Application.get_env(:ueberauth, Ueberauth) == ConfigDB.from_binary(config3.value)
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> assign(:token, token)
+        |> post("/api/pleroma/admin/config", %{
+          configs: [
+            %{group: config2.group, key: config2.key, delete: true},
+            %{
+              group: ":ueberauth",
+              key: "Ueberauth",
+              delete: true
+            }
+          ]
+        })
+
+      assert json_response(conn, 200) == %{
+               "configs" => []
+             }
+
+      assert Application.get_env(:ueberauth, Ueberauth) == ueberauth
+      refute Keyword.has_key?(Application.get_all_env(:pleroma), :keyaa2)
     end
 
     test "common config example", %{conn: conn} do
@@ -2430,7 +2527,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                 %{"tuple" => [":seconds_valid", 60]},
                 %{"tuple" => [":path", ""]},
                 %{"tuple" => [":key1", nil]},
-                %{"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"]},
@@ -2460,7 +2556,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                      %{"tuple" => [":seconds_valid", 60]},
                      %{"tuple" => [":path", ""]},
                      %{"tuple" => [":key1", nil]},
-                     %{"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"]},
@@ -2473,7 +2568,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                      ":seconds_valid",
                      ":path",
                      ":key1",
-                     ":partial_chain",
                      ":regex1",
                      ":regex2",
                      ":regex3",
@@ -2487,7 +2581,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "value" => "Tesla.Adapter.Httpc",
                    "db" => [":adapter"]
                  }
-               ]
+               ],
+               "need_reboot" => true
              }
     end
 
@@ -2881,47 +2976,15 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     end
   end
 
-  describe "config mix tasks run" do
-    setup do
-      Mix.shell(Mix.Shell.Quiet)
-
-      on_exit(fn ->
-        Mix.shell(Mix.Shell.IO)
-      end)
-
-      :ok
-    end
-
+  describe "GET /api/pleroma/admin/restart" do
     clear_config(:configurable_from_database) do
       Pleroma.Config.put(:configurable_from_database, true)
     end
 
-    clear_config([:feed, :post_title]) do
-      Pleroma.Config.put([:feed, :post_title], %{max_length: 100, omission: "…"})
-    end
-
-    test "transfer settings to DB and to file", %{conn: conn} do
-      assert Repo.all(Pleroma.ConfigDB) == []
-      Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
-      assert Repo.aggregate(Pleroma.ConfigDB, :count, :id) > 0
-
-      conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
-
-      assert json_response(conn, 200) == %{}
-      assert Repo.all(Pleroma.ConfigDB) == []
-    end
-
-    test "returns error if configuration from database is off", %{conn: conn} do
-      initial = Pleroma.Config.get(:configurable_from_database)
-      on_exit(fn -> Pleroma.Config.put(:configurable_from_database, initial) end)
-      Pleroma.Config.put(:configurable_from_database, false)
-
-      conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
-
-      assert json_response(conn, 400) ==
-               "To use this endpoint you need to enable configuration from database."
-
-      assert Repo.all(Pleroma.ConfigDB) == []
+    test "pleroma restarts", %{conn: conn} do
+      ExUnit.CaptureLog.capture_log(fn ->
+        assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{}
+      end) =~ "pleroma restarted"
     end
   end
 
@@ -3275,7 +3338,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
       response = json_response(ret_conn, 200)
 
-      assert length(response) == 0
+      assert Enum.empty?(response)
     end
   end