test: add test for requiring actor in atomic changes

This commit is contained in:
Zach Daniel 2024-05-03 12:36:20 -04:00
parent 2ab788fd35
commit a26efe03e1

View file

@ -81,6 +81,26 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
end end
end end
defmodule AtomicallyRequireActor do
use Ash.Resource.Change
def change(changeset, _, context) do
if context.actor do
changeset
else
Ash.Changeset.add_error(changeset, "actor is required")
end
end
def atomic(changeset, _, context) do
if context.actor do
{:atomic, changeset, %{}}
else
{:atomic, Ash.Changeset.add_error(changeset, "actor is required"), %{}}
end
end
end
defmodule Post do defmodule Post do
@moduledoc false @moduledoc false
use Ash.Resource, use Ash.Resource,
@ -167,6 +187,7 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
argument :authorize?, :boolean, allow_nil?: false argument :authorize?, :boolean, allow_nil?: false
change set_context(%{authorize?: arg(:authorize?)}) change set_context(%{authorize?: arg(:authorize?)})
change AtomicallyRequireActor
end end
update :update_with_match do update :update_with_match do
@ -584,6 +605,7 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
:update_with_policy, :update_with_policy,
%{title2: "updated value", authorize?: true}, %{title2: "updated value", authorize?: true},
authorize?: true, authorize?: true,
actor: %{foo: :bar},
resource: Post, resource: Post,
return_records?: true, return_records?: true,
return_errors?: true return_errors?: true
@ -616,6 +638,7 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
:update_with_policy, :update_with_policy,
%{title2: "updated value", authorize?: true}, %{title2: "updated value", authorize?: true},
authorize?: true, authorize?: true,
actor: %{foo: :bar},
resource: Post, resource: Post,
return_records?: true, return_records?: true,
return_errors?: true, return_errors?: true,
@ -638,6 +661,7 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
%{title2: "updated value", authorize?: false}, %{title2: "updated value", authorize?: false},
strategy: :atomic, strategy: :atomic,
authorize?: true, authorize?: true,
actor: %{foo: :bar},
resource: Post, resource: Post,
return_records?: true, return_records?: true,
return_errors?: true return_errors?: true
@ -659,6 +683,7 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
:update_with_policy, :update_with_policy,
%{title2: "updated value", authorize?: false}, %{title2: "updated value", authorize?: false},
strategy: :stream, strategy: :stream,
actor: %{foo: :bar},
authorize?: true, authorize?: true,
resource: Post, resource: Post,
return_records?: true, return_records?: true,