Add tests for domain controller

Signed-off-by: Marcin Mikołajczak <git@mkljczk.pl>
This commit is contained in:
Marcin Mikołajczak 2023-11-10 19:00:45 +01:00
parent 182e0b58da
commit 5a30fefb32
4 changed files with 191 additions and 20 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1,166 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
# 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