Add policies for AuthorRole resource.
This commit is contained in:
@@ -3,10 +3,33 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do
|
|||||||
otp_app: :decentralised_book_index,
|
otp_app: :decentralised_book_index,
|
||||||
domain: DecentralisedBookIndex.Metadata,
|
domain: DecentralisedBookIndex.Metadata,
|
||||||
data_layer: AshPostgres.DataLayer,
|
data_layer: AshPostgres.DataLayer,
|
||||||
extensions: [AshJsonApi.Resource]
|
extensions: [AshJsonApi.Resource],
|
||||||
|
authorizers: [Ash.Policy.Authorizer]
|
||||||
|
|
||||||
alias DecentralisedBookIndex.Metadata
|
alias DecentralisedBookIndex.Metadata
|
||||||
|
|
||||||
|
policies do
|
||||||
|
bypass actor_attribute_equals(:role, :admin) do
|
||||||
|
authorize_if always()
|
||||||
|
end
|
||||||
|
|
||||||
|
policy action_type(:read) do
|
||||||
|
authorize_if always()
|
||||||
|
end
|
||||||
|
|
||||||
|
policy action_type(:create) do
|
||||||
|
authorize_if actor_attribute_equals(:role, :moderator)
|
||||||
|
end
|
||||||
|
|
||||||
|
policy action_type(:update) do
|
||||||
|
authorize_if actor_attribute_equals(:role, :moderator)
|
||||||
|
end
|
||||||
|
|
||||||
|
policy action_type(:destroy) do
|
||||||
|
authorize_if actor_attribute_equals(:role, :admin)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
json_api do
|
json_api do
|
||||||
type "author_role"
|
type "author_role"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,11 +3,16 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRolesTest do
|
|||||||
|
|
||||||
alias DecentralisedBookIndex.Metadata
|
alias DecentralisedBookIndex.Metadata
|
||||||
|
|
||||||
describe "create action" do
|
setup do
|
||||||
test "an author's role must belongs to an author" do
|
user = generate(user(role: :moderator))
|
||||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
%{user: user}
|
||||||
|
end
|
||||||
|
|
||||||
assert {:ok, author_role} = Metadata.create_author_role(author.id, 1, "role")
|
describe "create action" do
|
||||||
|
test "an author's role must belongs to an author", %{user: user} do
|
||||||
|
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
|
||||||
|
|
||||||
|
assert {:ok, author_role} = Metadata.create_author_role(author.id, 1, "role", actor: user)
|
||||||
|
|
||||||
{:ok, author_role} =
|
{:ok, author_role} =
|
||||||
author_role
|
author_role
|
||||||
@@ -16,22 +21,22 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRolesTest do
|
|||||||
assert author.id == author_role.author_id
|
assert author.id == author_role.author_id
|
||||||
end
|
end
|
||||||
|
|
||||||
test "an author's role can be blank" do
|
test "an author's role can be blank", %{user: user} do
|
||||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
|
||||||
|
|
||||||
{:ok, author_role} = Metadata.create_author_role(author.id, 1, "")
|
{:ok, author_role} = Metadata.create_author_role(author.id, 1, "", actor: user)
|
||||||
|
|
||||||
refute author_role.role
|
refute author_role.role
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "author's avatar image" do
|
describe "author's avatar image" do
|
||||||
test "update avatar image" do
|
test "update avatar image", %{user: user} do
|
||||||
avatar_url = "/images/avatar.avif"
|
avatar_url = "/images/avatar.avif"
|
||||||
|
|
||||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
|
||||||
|
|
||||||
assert {:ok, author} = Metadata.assign_author_avatar_image(author, avatar_url)
|
assert {:ok, author} = Metadata.assign_author_avatar_image(author, avatar_url, actor: user)
|
||||||
|
|
||||||
assert author.avatar_url == avatar_url
|
assert author.avatar_url == avatar_url
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user