2023-09-18 11:17:13 +12:00
|
|
|
defmodule AshGraphql.GenericActionsTest do
|
2023-09-16 05:45:12 +12:00
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
setup do
|
|
|
|
on_exit(fn ->
|
2024-04-02 07:03:06 +13:00
|
|
|
Application.delete_env(:ash_graphql, AshGraphql.Test.Domain)
|
2023-09-16 05:45:12 +12:00
|
|
|
|
|
|
|
AshGraphql.TestHelpers.stop_ets()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generic action queries can be run" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
query {
|
|
|
|
postCount
|
|
|
|
}
|
|
|
|
"""
|
2023-11-24 23:29:01 +13:00
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema, context: %{actor: %{id: "an-actor"}})
|
2023-09-16 05:45:12 +12:00
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{data: %{"postCount" => 0}} = result
|
|
|
|
end
|
|
|
|
|
2023-11-25 01:24:27 +13:00
|
|
|
test "generic action that have policies return forbidden if no actor is present" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
query {
|
|
|
|
postCount
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
assert Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: nil,
|
|
|
|
errors: [
|
|
|
|
%{
|
|
|
|
code: "forbidden",
|
|
|
|
message: "forbidden",
|
|
|
|
path: ["postCount"],
|
|
|
|
fields: [],
|
|
|
|
vars: %{},
|
|
|
|
locations: [%{line: 2, column: 3}],
|
|
|
|
short_message: "forbidden"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
} == result
|
|
|
|
end
|
|
|
|
|
2023-09-16 05:45:12 +12:00
|
|
|
test "generic action mutations can be run" do
|
2024-04-02 07:03:06 +13:00
|
|
|
post =
|
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar", best: true)
|
|
|
|
|> Ash.create!()
|
2023-09-16 05:45:12 +12:00
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation {
|
|
|
|
randomPost {
|
|
|
|
id
|
|
|
|
comments{
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
post_id = post.id
|
|
|
|
|
|
|
|
assert %{data: %{"randomPost" => %{"id" => ^post_id, "comments" => []}}} = result
|
|
|
|
end
|
2023-10-03 12:44:19 +13:00
|
|
|
|
|
|
|
test "generic action mutations can be run with input" do
|
2024-04-02 07:03:06 +13:00
|
|
|
post =
|
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar", best: true)
|
|
|
|
|> Ash.create!()
|
2023-10-03 12:44:19 +13:00
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation {
|
|
|
|
randomPost(input: {published: true}) {
|
|
|
|
id
|
|
|
|
comments{
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
post_id = post.id
|
|
|
|
|
|
|
|
assert %{data: %{"randomPost" => %{"id" => ^post_id, "comments" => []}}} = result
|
|
|
|
end
|
2023-09-16 05:45:12 +12:00
|
|
|
end
|