Skip to content

Commit 5d78189

Browse files
authored
test: Include test coverage for actor in context when resolving mutations backed by generic actions (#418)
1 parent 8154766 commit 5d78189

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

test/plug_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ defmodule AshGraphql.PlugTest do
4949
assert resp["data"]["currentUser"]["name"] == user.name
5050
end
5151

52+
test "actor is set for preparations on Generic Actions" do
53+
query = ~s|
54+
mutation {
55+
preparation
56+
}
57+
|
58+
59+
user =
60+
AshGraphql.Test.User
61+
|> Ash.Changeset.for_create(:create, %{name: "My Name"})
62+
|> Ash.create!()
63+
64+
resp =
65+
query
66+
|> conn()
67+
|> Ash.PlugHelpers.set_actor(user)
68+
|> run()
69+
70+
assert resp["data"]["preparation"] == user.name
71+
end
72+
5273
test "when the actor is not set, the current user return nil" do
5374
resp =
5475
"""

test/support/resources/user.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ defmodule AshGraphql.Test.User do
3636
destroy :delete_current_user, :destroy_current_user do
3737
identity false
3838
end
39+
40+
action :preparation, :preparation
3941
end
4042
end
4143

@@ -99,6 +101,22 @@ defmodule AshGraphql.Test.User do
99101
destroy :destroy_current_user do
100102
filter(expr(id == ^actor(:id)))
101103
end
104+
105+
action :preparation, :string do
106+
prepare fn
107+
input, %{actor: %{name: name}} ->
108+
Ash.ActionInput.set_context(input, %{name: name})
109+
110+
input, _context ->
111+
input
112+
end
113+
114+
run fn _input, %{source_context: source_context} ->
115+
name = Map.get(source_context, :name)
116+
117+
{:ok, name}
118+
end
119+
end
102120
end
103121

104122
attributes do
@@ -133,6 +151,10 @@ defmodule AshGraphql.Test.User do
133151
policy action_type(:destroy) do
134152
authorize_if(expr(id == ^actor(:id)))
135153
end
154+
155+
policy action(:preparation) do
156+
authorize_if always()
157+
end
136158
end
137159

138160
field_policies do

0 commit comments

Comments
 (0)