--- /dev/null
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ApiSpec.Admin.ConfigOperation do
+ alias OpenApiSpex.Operation
+ alias OpenApiSpex.Schema
+ alias Pleroma.Web.ApiSpec.Schemas.ApiError
+
+ import Pleroma.Web.ApiSpec.Helpers
+
+ def open_api_operation(action) do
+ operation = String.to_existing_atom("#{action}_operation")
+ apply(__MODULE__, operation, [])
+ end
+
+ def show_operation do
+ %Operation{
+ tags: ["Admin", "Config"],
+ summary: "Get list of merged default settings with saved in database",
+ operationId: "AdminAPI.ConfigController.show",
+ parameters: [
+ Operation.parameter(
+ :only_db,
+ :query,
+ %Schema{type: :boolean, default: false},
+ "Get only saved in database settings"
+ )
+ ],
+ security: [%{"oAuth" => ["read"]}],
+ responses: %{
+ 200 => Operation.response("Config", "application/json", config_response()),
+ 400 => Operation.response("Bad Request", "application/json", ApiError)
+ }
+ }
+ end
+
+ def update_operation do
+ %Operation{
+ tags: ["Admin", "Config"],
+ summary: "Update config settings",
+ operationId: "AdminAPI.ConfigController.update",
+ security: [%{"oAuth" => ["write"]}],
+ requestBody:
+ request_body("Parameters", %Schema{
+ type: :object,
+ properties: %{
+ configs: %Schema{
+ type: :array,
+ items: %Schema{
+ type: :object,
+ properties: %{
+ group: %Schema{type: :string},
+ key: %Schema{type: :string},
+ value: any(),
+ delete: %Schema{type: :boolean},
+ subkeys: %Schema{type: :array, items: %Schema{type: :string}}
+ }
+ }
+ }
+ }
+ }),
+ responses: %{
+ 200 => Operation.response("Config", "application/json", config_response()),
+ 400 => Operation.response("Bad Request", "application/json", ApiError)
+ }
+ }
+ end
+
+ def descriptions_operation do
+ %Operation{
+ tags: ["Admin", "Config"],
+ summary: "Get JSON with config descriptions.",
+ operationId: "AdminAPI.ConfigController.descriptions",
+ security: [%{"oAuth" => ["read"]}],
+ responses: %{
+ 200 =>
+ Operation.response("Config Descriptions", "application/json", %Schema{
+ type: :array,
+ items: %Schema{
+ type: :object,
+ properties: %{
+ group: %Schema{type: :string},
+ key: %Schema{type: :string},
+ type: %Schema{oneOf: [%Schema{type: :string}, %Schema{type: :array}]},
+ description: %Schema{type: :string},
+ children: %Schema{
+ type: :array,
+ items: %Schema{
+ type: :object,
+ properties: %{
+ key: %Schema{type: :string},
+ type: %Schema{oneOf: [%Schema{type: :string}, %Schema{type: :array}]},
+ description: %Schema{type: :string},
+ suggestions: %Schema{type: :array}
+ }
+ }
+ }
+ }
+ }
+ }),
+ 400 => Operation.response("Bad Request", "application/json", ApiError)
+ }
+ }
+ end
+
+ defp any do
+ %Schema{
+ oneOf: [
+ %Schema{type: :array},
+ %Schema{type: :object},
+ %Schema{type: :string},
+ %Schema{type: :integer},
+ %Schema{type: :boolean}
+ ]
+ }
+ end
+
+ defp config_response do
+ %Schema{
+ type: :object,
+ properties: %{
+ configs: %Schema{
+ type: :array,
+ items: %Schema{
+ type: :object,
+ properties: %{
+ group: %Schema{type: :string},
+ key: %Schema{type: :string},
+ value: any()
+ }
+ }
+ },
+ need_reboot: %Schema{
+ type: :boolean,
+ description:
+ "If `need_reboot` is `true`, instance must be restarted, so reboot time settings can take effect"
+ }
+ }
+ }
+ end
+end
Config.put(:configurable_from_database, false)
conn = get(conn, "/api/pleroma/admin/config")
- assert json_response(conn, 400) ==
+ assert json_response_and_validate_schema(conn, 400) ==
%{
"error" => "To use this endpoint you need to enable configuration from database."
}
config1 = insert(:config)
config2 = insert(:config)
- conn = get(conn, "/api/pleroma/admin/config", %{"only_db" => true})
+ conn = get(conn, "/api/pleroma/admin/config?only_db=true")
%{
"configs" => [
"value" => _
}
]
- } = json_response(conn, 200)
+ } = json_response_and_validate_schema(conn, 200)
assert key1 == config1.key
assert key2 == config2.key
%{"configs" => configs} =
conn
|> get("/api/pleroma/admin/config")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
[instance_config] =
Enum.filter(configs, fn %{"group" => group, "key" => key} ->
%{"configs" => configs} =
conn
|> get("/api/pleroma/admin/config")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
assert length(configs) > 3
%{"configs" => configs} =
conn
|> get("/api/pleroma/admin/config")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
vals =
Enum.filter(configs, fn %{"group" => group, "key" => key} ->
end
test "POST /api/pleroma/admin/config error", %{conn: conn} do
- conn = post(conn, "/api/pleroma/admin/config", %{"configs" => []})
+ conn =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{"configs" => []})
- assert json_response(conn, 400) ==
+ assert json_response_and_validate_schema(conn, 400) ==
%{"error" => "To use this endpoint you need to enable configuration from database."}
end
on_exit(fn -> Application.put_env(:ueberauth, Ueberauth, ueberauth) end)
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{group: ":pleroma", key: ":key1", value: "value1"},
%{
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
end)
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
group: ":quack",
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":quack",
config = insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: 2))
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{group: config.group, key: config.key, value: [%{"tuple" => [":key3", 3]}]}
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
chat = Config.get(:chat)
on_exit(fn -> Config.put(:chat, chat) end)
- assert post(
- conn,
+ assert conn
+ |> put_req_header("content-type", "application/json")
+ |> post(
"/api/pleroma/admin/config",
%{
configs: [
]
}
)
- |> json_response(200) == %{
+ |> json_response_and_validate_schema(200) == %{
"configs" => [
%{
"db" => [":enabled"],
configs =
conn
|> get("/api/pleroma/admin/config")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
assert configs["need_reboot"]
capture_log(fn ->
- assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{}
+ assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) ==
+ %{}
end) =~ "pleroma restarted"
configs =
conn
|> get("/api/pleroma/admin/config")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
assert configs["need_reboot"] == false
end
chat = Config.get(:chat)
on_exit(fn -> Config.put(:chat, chat) end)
- assert post(
- conn,
+ assert conn
+ |> put_req_header("content-type", "application/json")
+ |> post(
"/api/pleroma/admin/config",
%{
configs: [
]
}
)
- |> json_response(200) == %{
+ |> json_response_and_validate_schema(200) == %{
"configs" => [
%{
"db" => [":enabled"],
"need_reboot" => true
}
- assert post(conn, "/api/pleroma/admin/config", %{
+ assert conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{group: ":pleroma", key: ":key1", value: [%{"tuple" => [":key3", 3]}]}
]
})
- |> json_response(200) == %{
+ |> json_response_and_validate_schema(200) == %{
"configs" => [
%{
"group" => ":pleroma",
}
capture_log(fn ->
- assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{}
+ assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) ==
+ %{}
end) =~ "pleroma restarted"
configs =
conn
|> get("/api/pleroma/admin/config")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
assert configs["need_reboot"] == false
end
insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: [k1: 1, k2: 2]))
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
group: config.group,
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
test "saving special atoms", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
"configs" => [
%{
"group" => ":pleroma",
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
assert Application.get_env(:logger, :backends) == []
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
group: config.group,
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":logger",
)
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{group: config.group, key: config.key, value: "Tesla.Adapter.Httpc"}
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":tesla",
)
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{group: config1.group, key: config1.key, value: "another_value"},
%{group: config2.group, key: config2.key, value: "another_value"}
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
build_conn()
|> assign(:user, admin)
|> assign(:token, token)
+ |> put_req_header("content-type", "application/json")
|> post("/api/pleroma/admin/config", %{
configs: [
%{group: config2.group, key: config2.key, delete: true},
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => []
}
test "common config example", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
"group" => ":pleroma",
assert Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma"
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
test "tuples with more than two values", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
"group" => ":pleroma",
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
test "settings with nesting map", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
"group" => ":pleroma",
]
})
- assert json_response(conn, 200) ==
+ assert json_response_and_validate_schema(conn, 200) ==
%{
"configs" => [
%{
test "value as map", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
"group" => ":pleroma",
]
})
- assert json_response(conn, 200) ==
+ assert json_response_and_validate_schema(conn, 200) ==
%{
"configs" => [
%{
test "queues key as atom", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
"group" => ":oban",
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":oban",
)
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
group: config.group,
]
})
- assert json_response(conn, 200) == %{
+ assert json_response_and_validate_schema(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
test "proxy tuple localhost", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
group: ":pleroma",
"db" => db
}
]
- } = json_response(conn, 200)
+ } = json_response_and_validate_schema(conn, 200)
assert %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]} in value
assert ":proxy_url" in db
test "proxy tuple domain", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
group: ":pleroma",
"db" => db
}
]
- } = json_response(conn, 200)
+ } = json_response_and_validate_schema(conn, 200)
assert %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]} in value
assert ":proxy_url" in db
test "proxy tuple ip", %{conn: conn} do
conn =
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{
group: ":pleroma",
"db" => db
}
]
- } = json_response(conn, 200)
+ } = json_response_and_validate_schema(conn, 200)
assert %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]} in value
assert ":proxy_url" in db
{:not_real}
])
- post(conn, "/api/pleroma/admin/config", %{
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/pleroma/admin/config", %{
configs: [
%{group: ":pleroma", key: ":key1", value: "value1"},
%{group: ":pleroma", key: ":key2", value: "value2"},
assign(conn, :user, admin)
|> get("/api/pleroma/admin/config/descriptions")
- assert [child | _others] = json_response(conn, 200)
+ assert [child | _others] = json_response_and_validate_schema(conn, 200)
assert child["children"]
assert child["key"]
assign(conn, :user, admin)
|> get("/api/pleroma/admin/config/descriptions")
- children = json_response(conn, 200)
+ children = json_response_and_validate_schema(conn, 200)
assert length(children) == 4