2024-02-14 14:20:49 +13:00
|
|
|
defmodule ArchivalWithPolicyTest do
|
|
|
|
use ExUnit.Case
|
|
|
|
|
|
|
|
defmodule Author do
|
|
|
|
use Ash.Resource,
|
2024-03-30 03:19:24 +13:00
|
|
|
domain: ArchivalWithPolicyTest.Domain,
|
2024-02-14 14:20:49 +13:00
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
extensions: [AshArchival.Resource],
|
|
|
|
authorizers: [Ash.Policy.Authorizer]
|
|
|
|
|
|
|
|
ets do
|
|
|
|
table(:authors)
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
archive do
|
|
|
|
archive_related([:posts])
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-30 03:19:24 +13:00
|
|
|
default_accept(:*)
|
2024-02-14 14:20:49 +13:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-30 03:19:24 +13:00
|
|
|
has_many(:posts, ArchivalWithPolicyTest.Post) do
|
|
|
|
public?(true)
|
|
|
|
end
|
2024-02-14 14:20:49 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
policies do
|
|
|
|
policy always() do
|
|
|
|
authorize_if(action_type(:create))
|
|
|
|
authorize_if(action_type(:read))
|
|
|
|
authorize_if(actor_attribute_equals(:admin, true))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule AuthorWithArchive do
|
|
|
|
use Ash.Resource,
|
2024-03-30 03:19:24 +13:00
|
|
|
domain: ArchivalWithPolicyTest.Domain,
|
2024-02-14 14:20:49 +13:00
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
table(:authors)
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-30 03:19:24 +13:00
|
|
|
default_accept(:*)
|
2024-02-14 14:20:49 +13:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
2024-03-30 03:19:24 +13:00
|
|
|
attribute(:archived_at, :utc_datetime_usec, public?: true)
|
2024-02-14 14:20:49 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Post do
|
|
|
|
use Ash.Resource,
|
2024-03-30 03:19:24 +13:00
|
|
|
domain: ArchivalWithPolicyTest.Domain,
|
2024-02-14 14:20:49 +13:00
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
extensions: [AshArchival.Resource],
|
|
|
|
authorizers: [Ash.Policy.Authorizer]
|
|
|
|
|
|
|
|
ets do
|
|
|
|
table(:posts)
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
archive do
|
|
|
|
archive_related([:comments])
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-30 03:19:24 +13:00
|
|
|
default_accept(:*)
|
2024-02-14 14:20:49 +13:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to :author, Author do
|
2024-03-30 03:19:24 +13:00
|
|
|
public?(true)
|
2024-02-14 14:20:49 +13:00
|
|
|
attribute_writable?(true)
|
|
|
|
end
|
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
has_many(:comments, ArchivalWithPolicyTest.Comment) do
|
|
|
|
public?(true)
|
|
|
|
end
|
2024-02-14 14:20:49 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
policies do
|
|
|
|
policy always() do
|
|
|
|
authorize_if(action_type(:create))
|
|
|
|
authorize_if(action_type(:read))
|
|
|
|
authorize_if(actor_attribute_equals(:admin, true))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule PostWithArchive do
|
|
|
|
use Ash.Resource,
|
2024-03-30 03:19:24 +13:00
|
|
|
domain: ArchivalWithPolicyTest.Domain,
|
2024-02-14 14:20:49 +13:00
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
table(:posts)
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-30 03:19:24 +13:00
|
|
|
default_accept(:*)
|
2024-02-14 14:20:49 +13:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
2024-03-30 03:19:24 +13:00
|
|
|
attribute(:archived_at, :utc_datetime_usec, public?: true)
|
2024-02-14 14:20:49 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Comment do
|
|
|
|
use Ash.Resource,
|
2024-03-30 03:19:24 +13:00
|
|
|
domain: ArchivalWithPolicyTest.Domain,
|
2024-02-14 14:20:49 +13:00
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
extensions: [AshArchival.Resource],
|
|
|
|
authorizers: [Ash.Policy.Authorizer]
|
|
|
|
|
|
|
|
ets do
|
|
|
|
table(:comments)
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-30 03:19:24 +13:00
|
|
|
default_accept(:*)
|
2024-02-14 14:20:49 +13:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to :post, Post do
|
2024-03-30 03:19:24 +13:00
|
|
|
public?(true)
|
2024-02-14 14:20:49 +13:00
|
|
|
attribute_writable?(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
policies do
|
|
|
|
policy always() do
|
|
|
|
authorize_if(action_type(:create))
|
|
|
|
authorize_if(action_type(:read))
|
|
|
|
authorize_if(actor_attribute_equals(:admin, true))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule CommentWithArchive do
|
|
|
|
use Ash.Resource,
|
2024-03-30 03:19:24 +13:00
|
|
|
domain: ArchivalWithPolicyTest.Domain,
|
2024-02-14 14:20:49 +13:00
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
table(:comments)
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-30 03:19:24 +13:00
|
|
|
default_accept(:*)
|
2024-02-14 14:20:49 +13:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
2024-03-30 03:19:24 +13:00
|
|
|
attribute(:archived_at, :utc_datetime_usec, public?: true)
|
2024-02-14 14:20:49 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
defmodule Domain do
|
|
|
|
use Ash.Domain
|
2024-02-14 14:20:49 +13:00
|
|
|
|
|
|
|
authorization do
|
2024-02-21 05:16:33 +13:00
|
|
|
authorize(:always)
|
2024-02-14 14:20:49 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
resources do
|
2024-03-30 03:19:24 +13:00
|
|
|
resource(Author)
|
|
|
|
resource(AuthorWithArchive)
|
|
|
|
resource(Post)
|
|
|
|
resource(PostWithArchive)
|
|
|
|
resource(Comment)
|
|
|
|
resource(CommentWithArchive)
|
2024-02-14 14:20:49 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "destroying a record archives it" do
|
|
|
|
comment =
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.for_create(:create)
|
2024-03-30 03:19:24 +13:00
|
|
|
|> Ash.create!()
|
2024-02-14 14:20:49 +13:00
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
assert :ok = comment |> Ash.destroy!(actor: %{admin: true})
|
2024-02-14 14:20:49 +13:00
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
[archived] = Ash.read!(CommentWithArchive)
|
2024-02-14 14:20:49 +13:00
|
|
|
assert archived.id == comment.id
|
|
|
|
assert archived.archived_at
|
|
|
|
end
|
|
|
|
|
|
|
|
test "destroying a record archives any `archive_related` it has configured" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create)
|
2024-03-30 03:19:24 +13:00
|
|
|
|> Ash.create!()
|
2024-02-14 14:20:49 +13:00
|
|
|
|
|
|
|
comment =
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.for_create(:create, %{post_id: post.id})
|
2024-03-30 03:19:24 +13:00
|
|
|
|> Ash.create!()
|
2024-02-14 14:20:49 +13:00
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
assert :ok = post |> Ash.destroy!(actor: %{admin: true})
|
2024-02-14 14:20:49 +13:00
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
[archived] = Ash.read!(CommentWithArchive)
|
2024-02-14 14:20:49 +13:00
|
|
|
assert archived.id == comment.id
|
|
|
|
assert archived.archived_at
|
|
|
|
end
|
|
|
|
|
|
|
|
test "destroying a record triggers a cascading archive." do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create)
|
2024-03-30 03:19:24 +13:00
|
|
|
|> Ash.create!()
|
2024-02-14 14:20:49 +13:00
|
|
|
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{author_id: author.id})
|
2024-03-30 03:19:24 +13:00
|
|
|
|> Ash.create!()
|
2024-02-14 14:20:49 +13:00
|
|
|
|
|
|
|
comment =
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.for_create(:create, %{post_id: post.id})
|
2024-03-30 03:19:24 +13:00
|
|
|
|> Ash.create!()
|
2024-02-14 14:20:49 +13:00
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
assert :ok = author |> Ash.destroy!(actor: %{admin: true})
|
2024-02-14 14:20:49 +13:00
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
[archived_post] = Ash.read!(PostWithArchive)
|
2024-02-14 14:20:49 +13:00
|
|
|
assert archived_post.id == post.id
|
|
|
|
assert archived_post.archived_at
|
|
|
|
|
2024-03-30 03:19:24 +13:00
|
|
|
[archived_comment] = Ash.read!(CommentWithArchive)
|
2024-02-14 14:20:49 +13:00
|
|
|
assert archived_comment.id == comment.id
|
|
|
|
assert archived_comment.archived_at
|
|
|
|
end
|
|
|
|
end
|