Add policies for Author resource.

This commit is contained in:
2025-04-14 21:59:31 +03:00
parent 73f2f8c261
commit d3233a7aab
2 changed files with 69 additions and 31 deletions
@@ -3,45 +3,50 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
alias DecentralisedBookIndex.Metadata
setup do
user = generate(user(role: :moderator))
%{user: user}
end
describe "authors and registries relationship" do
test "a new author get new registry by default" do
assert {:ok, author} = Metadata.create_author("Author", "An description")
test "a new author get new registry by default", %{user: user} do
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert author.author_alias_registry_id != nil
end
test "a new author belongs to a registry if specified" do
assert {:ok, registry} = Metadata.create_author_alias_registry()
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, registry.id)
test "a new author belongs to a registry if specified", %{user: user} do
assert {:ok, registry} = Metadata.create_author_alias_registry(actor: user)
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, registry.id, actor: user)
assert author.author_alias_registry_id == registry.id
end
test "a new author to a registry via a related author record" do
{:ok, related_author} = Metadata.create_author("Author", "An description")
test "a new author to a registry via a related author record", %{user: user} do
{:ok, related_author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, author} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id)
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id, nil, actor: user)
assert related_author.author_alias_registry_id == author.author_alias_registry_id
end
test "a new author to a registry via a empty related author record" do
test "a new author to a registry via a empty related author record", %{user: user} do
assert {:error, _} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil)
Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil, actor: user)
end
end
describe "authors alternatives names" do
test "new author has no alternatives names" do
{:ok, author} = Metadata.create_author("Author", "An description")
test "new author has no alternatives names", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author)
assert alternatives_names = []
end
test "author has related author so they has one alternative name" do
{:ok, related_author} = Metadata.create_author("Author", "An description")
test "author has related author so they has one alternative name", %{user: user} do
{:ok, related_author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, author} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id)
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id, nil, actor: user)
assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author)
assert alternatives_names = [related_author]
@@ -49,19 +54,19 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
end
describe "author's ids" do
test "the list has the author's id" do
{:ok, author} = Metadata.create_author("Author", "An description")
test "the list has the author's id", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, ids} = Metadata.get_author_ids(author)
assert {:ok, ids} = Metadata.get_author_ids(author, actor: user)
assert author.id in ids
end
test "the list has the aliases' ids" do
{:ok, author} = Metadata.create_author("Author", "An description")
test "the list has the aliases' ids", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, alias1} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", author.id)
Metadata.add_author_to_related_alias_registry("Author2", "An description2", author.id, nil, actor: user)
{:ok, alias2} =
Metadata.add_author_to_related_alias_registry("Author3", "An description3", author.id)
Metadata.add_author_to_related_alias_registry("Author3", "An description3", author.id, nil, actor: user)
assert {:ok, ids} = Metadata.get_author_ids(author)
@@ -72,15 +77,15 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
end
describe "authors and avatar_url" do
test "a new author has no avatar by default" do
assert {:ok, author} = Metadata.create_author("Author", "An description")
test "a new author has no avatar by default", %{user: user} do
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert author.avatar_url == nil
end
test "a new author has a avatar" do
test "a new author has a avatar", %{user: user} do
avatar_url = "/images/avatar.png"
assert {:ok, author} = Metadata.create_author("Author", "An description", avatar_url)
assert {:ok, author} = Metadata.create_author("Author", "An description", avatar_url, nil, actor: user)
assert author.avatar_url == avatar_url
end
end