From 5a30fefb3203c7f9afeff35ae8af734568b4f409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Miko=C5=82ajczak?= Date: Fri, 10 Nov 2023 19:00:45 +0100 Subject: [PATCH] Add tests for domain controller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Mikołajczak --- lib/pleroma/domain.ex | 25 +-- .../controllers/domain_controller.ex | 18 +- .../operations/admin/domain_operation.ex | 2 +- .../controllers/domain_controller_test.exs | 166 ++++++++++++++++++ 4 files changed, 191 insertions(+), 20 deletions(-) create mode 100644 test/pleroma/web/admin_api/controllers/domain_controller_test.exs diff --git a/lib/pleroma/domain.ex b/lib/pleroma/domain.ex index cf7efc350a..a7e390e62d 100644 --- a/lib/pleroma/domain.ex +++ b/lib/pleroma/domain.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Domain do use Ecto.Schema import Ecto.Changeset + import Ecto.Query alias Pleroma.Repo @@ -28,24 +29,24 @@ def update_changeset(%__MODULE__{} = domain, params \\ %{}) do |> cast(params, [:public]) end + def list do + __MODULE__ + |> order_by(asc: :id) + |> Repo.all() + end + def get(id), do: Repo.get(__MODULE__, id) def create(params) do - {:ok, domain} = - %__MODULE__{} - |> changeset(params) - |> Repo.insert() - - domain + %__MODULE__{} + |> changeset(params) + |> Repo.insert() end def update(params, id) do - {:ok, domain} = - get(id) - |> update_changeset(params) - |> Repo.update() - - domain + get(id) + |> update_changeset(params) + |> Repo.update() end def delete(id) do diff --git a/lib/pleroma/web/admin_api/controllers/domain_controller.ex b/lib/pleroma/web/admin_api/controllers/domain_controller.ex index cb2b7dd3f6..d913db8a11 100644 --- a/lib/pleroma/web/admin_api/controllers/domain_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/domain_controller.ex @@ -5,8 +5,8 @@ defmodule Pleroma.Web.AdminAPI.DomainController do use Pleroma.Web, :controller - alias Pleroma.Repo alias Pleroma.Domain + alias Pleroma.Web.AdminAPI alias Pleroma.Web.Plugs.OAuthScopesPlug import Pleroma.Web.ControllerHelper, @@ -29,9 +29,7 @@ defmodule Pleroma.Web.AdminAPI.DomainController do defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.DomainOperation def index(conn, _) do - domains = - Domain - |> Repo.all() + domains = Domain.list() render(conn, "index.json", domains: domains) end @@ -39,15 +37,21 @@ def index(conn, _) do def create(%{body_params: params} = conn, _) do with {:domain_not_used, true} <- {:domain_not_used, params[:domain] !== Pleroma.Web.WebFinger.domain()}, - {:domain, domain} <- Domain.create(params) do + {:ok, domain} <- Domain.create(params) do render(conn, "show.json", domain: domain) else - {:domain_not_used, false} -> {:error, :invalid_domain} + {:domain_not_used, false} -> + {:error, :invalid_domain} + + {:error, changeset} -> + errors = Map.new(changeset.errors, fn {key, {error, _}} -> {key, error} end) + + {:errors, errors} end end def update(%{body_params: params} = conn, %{id: id}) do - domain = + {:ok, domain} = params |> Domain.update(id) diff --git a/lib/pleroma/web/api_spec/operations/admin/domain_operation.ex b/lib/pleroma/web/api_spec/operations/admin/domain_operation.ex index d73c67b3d5..37c663b649 100644 --- a/lib/pleroma/web/api_spec/operations/admin/domain_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/domain_operation.ex @@ -42,7 +42,7 @@ def create_operation do responses: %{ 200 => Operation.response("Response", "application/json", domain()), 400 => Operation.response("Bad Request", "application/json", ApiError), - 403 => Operation.response("Forbidden", "application/json", ApiError) + 403 => Operation.response("Forbidden", "application/json", ApiError), } } end diff --git a/test/pleroma/web/admin_api/controllers/domain_controller_test.exs b/test/pleroma/web/admin_api/controllers/domain_controller_test.exs new file mode 100644 index 0000000000..fc86a09258 --- /dev/null +++ b/test/pleroma/web/admin_api/controllers/domain_controller_test.exs @@ -0,0 +1,166 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.AdminAPI.DomainControllerTest do + use Pleroma.Web.ConnCase + + import Pleroma.Factory + + alias Pleroma.Domain + + setup do + clear_config([Pleroma.Web.WebFinger, :domain], "example.com") + + admin = insert(:user, is_admin: true) + token = insert(:oauth_admin_token, user: admin) + + conn = + build_conn() + |> assign(:user, admin) + |> assign(:token, token) + + {:ok, %{admin: admin, token: token, conn: conn}} + end + + describe "GET /api/pleroma/admin/domains" do + test "list created domains", %{conn: conn} do + _domain = + Domain.create(%{ + domain: "pleroma.mkljczk.pl", + public: true + }) + + _domain = + Domain.create(%{ + domain: "pleroma2.mkljczk.pl" + }) + + conn = get(conn, "/api/pleroma/admin/domains") + + [ + %{ + "id" => _id, + "domain" => "pleroma.mkljczk.pl", + "public" => true + }, + %{ + "id" => _id2, + "domain" => "pleroma2.mkljczk.pl", + "public" => false + } + ] = json_response_and_validate_schema(conn, 200) + end + end + + describe "POST /api/pleroma/admin/domains" do + test "create a valid domain", %{conn: conn} do + conn = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/domains", %{ + domain: "pleroma.mkljczk.pl", + public: true + }) + + %{ + "id" => _id, + "domain" => "pleroma.mkljczk.pl", + "public" => true + } = json_response_and_validate_schema(conn, 200) + end + + test "create a domain the same as host", %{conn: conn} do + conn = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/domains", %{ + domain: "example.com", + public: false + }) + + %{"error" => "invalid_domain"} = json_response_and_validate_schema(conn, 400) + end + + test "create duplicate domains", %{conn: conn} do + Domain.create(%{ + domain: "pleroma.mkljczk.pl", + public: true + }) + + conn = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/domains", %{ + domain: "pleroma.mkljczk.pl", + public: false + }) + + assert json_response_and_validate_schema(conn, 400) + end + end + + describe "PATCH /api/pleroma/admin/domains/:id" do + test "update domain privacy", %{conn: conn} do + {:ok, %{id: domain_id}} = + Domain.create(%{ + domain: "pleroma.mkljczk.pl", + public: true + }) + + conn = + conn + |> put_req_header("content-type", "application/json") + |> patch("/api/pleroma/admin/domains/#{domain_id}", %{ + public: false + }) + + %{ + "id" => _id, + "domain" => "pleroma.mkljczk.pl", + "public" => false + } = json_response_and_validate_schema(conn, 200) + end + + test "doesn't update domain name", %{conn: conn} do + {:ok, %{id: domain_id}} = + Domain.create(%{ + domain: "plemora.mkljczk.pl", + public: true + }) + + conn = + conn + |> put_req_header("content-type", "application/json") + |> patch("/api/pleroma/admin/domains/#{domain_id}", %{ + domain: "pleroma.mkljczk.pl" + }) + + %{ + "id" => _id, + "domain" => "plemora.mkljczk.pl", + "public" => true + } = json_response_and_validate_schema(conn, 200) + end + end + + describe "DELETE /api/pleroma/admin/domains/:id" do + test "delete a domain", %{conn: conn} do + {:ok, %{id: domain_id}} = + Domain.create(%{ + domain: "pleroma.mkljczk.pl", + public: true + }) + + conn = + conn + |> delete("/api/pleroma/admin/domains/#{domain_id}") + + %{} = json_response_and_validate_schema(conn, 200) + + domains = Domain.list() + + assert length(domains) == 0 + end + end +end