From 3782f1cfe2421450f5b47efac28255c2e24853de Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Tue, 10 Oct 2023 11:10:58 -0400 Subject: [PATCH] fix: honor `authorize?` option for calls to aggregate --- lib/ash/api/interface.ex | 10 +++++++++- test/actions/aggregate_test.exs | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/ash/api/interface.ex b/lib/ash/api/interface.ex index d00288a1..b3cb951d 100644 --- a/lib/ash/api/interface.ex +++ b/lib/ash/api/interface.ex @@ -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 diff --git a/test/actions/aggregate_test.exs b/test/actions/aggregate_test.exs index c25d313c..e3119127 100644 --- a/test/actions/aggregate_test.exs +++ b/test/actions/aggregate_test.exs @@ -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