fix: honor authorize? option for calls to aggregate

This commit is contained in:
Zach Daniel 2023-10-10 11:10:58 -04:00
parent 0ded7366f9
commit 3782f1cfe2
2 changed files with 13 additions and 1 deletions

View file

@ -245,7 +245,15 @@ defmodule Ash.Api.Interface do
end
defp split_aggregate_opts(opts) do
Keyword.split(opts, Ash.Query.Aggregate.opt_keys())
{left, right} = Keyword.split(opts, Ash.Query.Aggregate.opt_keys())
case Keyword.fetch(left, :authorize?) do
{:ok, value} ->
{left, Keyword.put(right, :authorize?, value)}
:error ->
{left, right}
end
end
def aggregate(query, aggregate_or_aggregates, opts \\ []) do

View file

@ -115,17 +115,21 @@ defmodule Ash.Test.Actions.AggregateTest do
test "runs authorization" do
assert %{count: 0} = Api.aggregate!(Post, {:count, :count}, authorize?: true)
assert 0 = Api.count!(Post, authorize?: true)
Post
|> Ash.Changeset.for_create(:create, %{title: "title"})
|> Api.create!()
assert %{count: 0} = Api.aggregate!(Post, {:count, :count}, authorize?: true)
assert 0 = Api.count!(Post, authorize?: true)
Post
|> Ash.Changeset.for_create(:create, %{title: "title", public: true})
|> Api.create!()
assert %{count: 1} = Api.aggregate!(Post, {:count, :count}, authorize?: true)
assert 1 = Api.count!(Post, authorize?: true)
end
end