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-04-19 11:41:49 +12:00
|
|
|
import Ash.Changeset
|
2021-09-09 18:18:26 +12:00
|
|
|
require Ash.Query
|
2021-04-19 11:41:49 +12:00
|
|
|
|
2019-12-22 22:06:33 +13:00
|
|
|
defmodule Profile do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
|
|
|
|
actions do
|
2022-04-29 10:07:06 +12:00
|
|
|
defaults [:create, :read, :update, :destroy]
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2019-12-22 22:06:33 +13:00
|
|
|
attribute :bio, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2020-06-29 15:36:38 +12:00
|
|
|
belongs_to :author, Ash.Test.Actions.DestroyTest.Author
|
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 ->
|
|
|
|
Ash.Test.Actions.DestroyTest.Api.destroy!(data)
|
|
|
|
|
|
|
|
{: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
|
2020-06-22 15:26:47 +12:00
|
|
|
use Ash.Resource, 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
|
2022-04-29 10:07:06 +12:00
|
|
|
defaults [:create, :read, :update, :destroy]
|
2021-05-10 08:56:44 +12:00
|
|
|
|
2022-10-31 02:49:49 +13:00
|
|
|
destroy :manual do
|
|
|
|
accept []
|
|
|
|
|
|
|
|
manual fn changeset, _ ->
|
|
|
|
Ash.Test.Actions.DestroyTest.Api.destroy(changeset.data, return_destroyed?: true)
|
|
|
|
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
|
2019-12-22 22:06:33 +13:00
|
|
|
attribute :name, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2022-08-16 06:00:02 +12:00
|
|
|
has_one :profile, Profile, destination_attribute: :author_id
|
2019-12-22 22:06:33 +13:00
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
has_many :posts, Ash.Test.Actions.DestroyTest.Post, destination_attribute: :author_id
|
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
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-22 22:06:33 +13:00
|
|
|
|
|
|
|
actions do
|
2022-04-29 10:07:06 +12:00
|
|
|
defaults [:create, :read, :update, :destroy]
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2019-12-22 22:06:33 +13:00
|
|
|
attribute :title, :string
|
|
|
|
attribute :contents, :string
|
2020-08-28 10:36:28 +12:00
|
|
|
attribute :tag, :string, default: "garbage"
|
2019-12-22 22:06:33 +13:00
|
|
|
attribute :tag2, :string, default: &PostDefaults.garbage2/0
|
2020-06-01 17:14:23 +12:00
|
|
|
attribute :tag3, :string, default: {PostDefaults, :garbage3, []}
|
2019-12-22 22:06:33 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to :author, Author
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-07 19:41:02 +13:00
|
|
|
defmodule Registry do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Registry
|
|
|
|
|
|
|
|
entries do
|
|
|
|
entry(Author)
|
|
|
|
entry(Post)
|
|
|
|
entry(Profile)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-22 22:06:33 +13:00
|
|
|
defmodule Api do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-22 22:06:33 +13:00
|
|
|
use Ash.Api
|
|
|
|
|
2020-06-14 18:39:11 +12:00
|
|
|
resources do
|
2021-10-07 19:41:02 +13:00
|
|
|
registry Registry
|
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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "foo", contents: "bar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-22 22:06:33 +13:00
|
|
|
|
2020-04-20 15:15:52 +12:00
|
|
|
assert Api.destroy!(post) == :ok
|
2019-12-22 22:06:33 +13:00
|
|
|
|
2021-09-09 18:18:26 +12:00
|
|
|
refute Api.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
|
|
|
|
2022-04-06 04:42:50 +12:00
|
|
|
test "returns the record if requested" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> new(%{title: "foo", contents: "bar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post_id = post.id
|
|
|
|
|
|
|
|
assert {:ok, %{id: ^post_id}} = Api.destroy(post, return_destroyed?: true)
|
|
|
|
|
|
|
|
refute Api.read_one!(Ash.Query.filter(Post, id == ^post.id))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns the record and notifications if requested" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> new(%{title: "foo", contents: "bar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post_id = post.id
|
|
|
|
|
2022-06-10 05:52:32 +12:00
|
|
|
assert {:ok, %{id: ^post_id}, [_]} =
|
2022-04-06 04:42:50 +12:00
|
|
|
Api.destroy(post, return_destroyed?: true, return_notifications?: true)
|
|
|
|
|
|
|
|
refute Api.read_one!(Ash.Query.filter(Post, id == ^post.id))
|
|
|
|
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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "foobar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
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 ->
|
|
|
|
Api.destroy!(author, authorize?: true)
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert Api.get!(Author, author.id)
|
|
|
|
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
|
|
|
|
|> new(%{name: "foo"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert Api.destroy!(author, action: :manual) == :ok
|
|
|
|
|
2021-09-09 18:18:26 +12:00
|
|
|
refute Api.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
|