2019-12-22 22:06:33 +13:00
|
|
|
defmodule Ash.Test.Actions.DestroyTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-22 22:06:33 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2021-09-09 18:18:26 +12:00
|
|
|
require Ash.Query
|
2021-04-19 11:41:49 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
|
|
|
|
2019-12-22 22:06:33 +13:00
|
|
|
defmodule Profile do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2020-06-14 18:39:11 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :bio, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :author, Ash.Test.Actions.DestroyTest.Author do
|
|
|
|
public?(true)
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-10 08:56:44 +12:00
|
|
|
defmodule ManualDestroyAuthor do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource.Change
|
|
|
|
|
|
|
|
def change(changeset, _, _) do
|
|
|
|
Ash.Changeset.after_action(changeset, fn _changeset, data ->
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.destroy!(data)
|
2021-05-10 08:56:44 +12:00
|
|
|
|
|
|
|
{:ok, data}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-22 22:06:33 +13:00
|
|
|
defmodule Author do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource,
|
|
|
|
domain: Domain,
|
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
authorizers: [Ash.Test.Authorizer]
|
2020-06-14 18:39:11 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2021-05-10 08:56:44 +12:00
|
|
|
|
2024-04-26 14:18:38 +12:00
|
|
|
destroy :soft_with_confirm do
|
|
|
|
accept []
|
|
|
|
soft? true
|
|
|
|
require_atomic? false
|
|
|
|
argument :confirm, :string, allow_nil?: false
|
|
|
|
|
|
|
|
change fn changeset, _ ->
|
|
|
|
Ash.Changeset.before_action(changeset, fn changeset ->
|
|
|
|
if changeset.arguments[:confirm] == "CONFIRM" do
|
|
|
|
changeset
|
|
|
|
else
|
|
|
|
Ash.Changeset.add_error(changeset,
|
|
|
|
field: :confirm,
|
|
|
|
message: "Type CONFIRM to confirm"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-31 02:49:49 +13:00
|
|
|
destroy :manual do
|
|
|
|
accept []
|
|
|
|
|
|
|
|
manual fn changeset, _ ->
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.destroy(changeset.data, return_destroyed?: true)
|
2022-10-31 02:49:49 +13:00
|
|
|
end
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :name, :string, public?: true
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one :profile, Profile, destination_attribute: :author_id, public?: true
|
2019-12-22 22:06:33 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
has_many :posts, Ash.Test.Actions.DestroyTest.Post,
|
|
|
|
destination_attribute: :author_id,
|
|
|
|
public?: true
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule PostDefaults do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
|
|
|
def garbage2, do: "garbage2"
|
|
|
|
def garbage3, do: "garbage3"
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Post do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2020-06-14 18:39:11 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :title, :string, public?: true
|
|
|
|
attribute :contents, :string, public?: true
|
|
|
|
attribute :tag, :string, default: "garbage", public?: true
|
|
|
|
attribute :tag2, :string, default: &PostDefaults.garbage2/0, public?: true
|
|
|
|
attribute :tag3, :string, default: {PostDefaults, :garbage3, []}, public?: true
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :author, Author do
|
|
|
|
public?(true)
|
|
|
|
end
|
2020-06-14 18:39:11 +12:00
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "simple destroy" do
|
|
|
|
test "allows destroying a record" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foo", contents: "bar"})
|
|
|
|
|> Ash.create!()
|
2019-12-22 22:06:33 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert Ash.destroy!(post) == :ok
|
2019-12-22 22:06:33 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
refute Ash.read_one!(Ash.Query.filter(Post, id == ^post.id))
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
2020-06-22 15:26:47 +12:00
|
|
|
|
2024-04-26 14:18:38 +12:00
|
|
|
test "before action hooks are run" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "foo"})
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
assert_raise Ash.Error.Invalid, ~r/Type CONFIRM to confirm/, fn ->
|
|
|
|
author
|
|
|
|
|> Ash.Changeset.for_destroy(:soft_with_confirm, %{confirm: "NOT CONFIRMED"})
|
|
|
|
|> Ash.destroy!()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-06 04:42:50 +12:00
|
|
|
test "returns the record if requested" do
|
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foo", contents: "bar"})
|
|
|
|
|> Ash.create!()
|
2022-04-06 04:42:50 +12:00
|
|
|
|
|
|
|
post_id = post.id
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert {:ok, %{id: ^post_id}} = Ash.destroy(post, return_destroyed?: true)
|
2022-04-06 04:42:50 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
refute Ash.read_one!(Ash.Query.filter(Post, id == ^post.id))
|
2022-04-06 04:42:50 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns the record and notifications if requested" do
|
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foo", contents: "bar"})
|
|
|
|
|> Ash.create!()
|
2022-04-06 04:42:50 +12:00
|
|
|
|
|
|
|
post_id = post.id
|
|
|
|
|
2022-06-10 05:52:32 +12:00
|
|
|
assert {:ok, %{id: ^post_id}, [_]} =
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.destroy(post, return_destroyed?: true, return_notifications?: true)
|
2022-04-06 04:42:50 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
refute Ash.read_one!(Ash.Query.filter(Post, id == ^post.id))
|
2022-04-06 04:42:50 +12:00
|
|
|
end
|
|
|
|
|
2020-06-22 15:26:47 +12:00
|
|
|
test "the destroy does not happen if it is unauthorized" do
|
2020-07-12 18:25:53 +12:00
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "foobar"})
|
|
|
|
|> Ash.create!(authorize?: false)
|
2020-06-22 15:26:47 +12:00
|
|
|
|
2021-01-29 08:47:59 +13:00
|
|
|
start_supervised({Ash.Test.Authorizer, strict_check: :continue, check: :forbidden})
|
|
|
|
|
2020-06-22 15:26:47 +12:00
|
|
|
assert_raise(Ash.Error.Forbidden, fn ->
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.destroy!(author, authorize?: true)
|
2020-06-22 15:26:47 +12:00
|
|
|
end)
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert Ash.get!(Author, author.id, authorize?: false)
|
2020-06-22 15:26:47 +12:00
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
2021-05-10 08:56:44 +12:00
|
|
|
|
|
|
|
describe "manual destroy" do
|
2023-09-26 02:40:29 +13:00
|
|
|
test "allows destroying a record" do
|
2021-05-10 08:56:44 +12:00
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "foo"})
|
|
|
|
|> Ash.create!()
|
2021-05-10 08:56:44 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert Ash.destroy!(author, action: :manual) == :ok
|
2021-05-10 08:56:44 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
refute Ash.read_one!(Ash.Query.filter(Author, id == ^author.id))
|
2021-05-10 08:56:44 +12:00
|
|
|
end
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|