mirror of
https://github.com/ash-project/ash.git
synced 2024-09-19 21:13:10 +12:00
test: add test for aggregate field policy (#644)
This commit is contained in:
parent
1eebedd189
commit
c442d83534
3 changed files with 31 additions and 3 deletions
|
@ -531,7 +531,7 @@ defmodule Ash.Actions.Helpers do
|
|||
end
|
||||
|
||||
defp replace_dynamic_loads(record, field, type, %Ash.Changeset{} = changeset)
|
||||
when type in [:attribute, :calculation] do
|
||||
when type in [:attribute, :calculation, :aggregate] do
|
||||
query =
|
||||
changeset.resource
|
||||
|> Ash.Query.new()
|
||||
|
@ -541,7 +541,7 @@ defmodule Ash.Actions.Helpers do
|
|||
end
|
||||
|
||||
defp replace_dynamic_loads(record, field, type, query)
|
||||
when type in [:attribute, :calculation] do
|
||||
when type in [:attribute, :calculation, :aggregate] do
|
||||
query.calculations
|
||||
|> Enum.reduce(
|
||||
record,
|
||||
|
|
|
@ -25,7 +25,8 @@ defmodule Ash.Test.Policy.FieldPolicyTest do
|
|||
|
||||
describe "introspection" do
|
||||
test "introspection returns field policies" do
|
||||
assert [%Ash.Policy.FieldPolicy{}] = Ash.Policy.Info.field_policies(User)
|
||||
assert [%Ash.Policy.FieldPolicy{}, %Ash.Policy.FieldPolicy{}] =
|
||||
Ash.Policy.Info.field_policies(User)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -57,6 +58,18 @@ defmodule Ash.Test.Policy.FieldPolicyTest do
|
|||
|> Map.get(:role)
|
||||
end
|
||||
|
||||
test "can load a resource with a forbidden aggregate", %{
|
||||
representative: representative
|
||||
} do
|
||||
assert %Ash.ForbiddenField{field: :ticket_count, type: :aggregate} ==
|
||||
User
|
||||
|> Ash.Query.for_read(:read, authorize?: true, actor: representative)
|
||||
|> Ash.Query.filter(id == ^representative.id)
|
||||
|> Ash.Query.load([:ticket_count])
|
||||
|> Api.read_one!(authorize?: true, actor: representative)
|
||||
|> Map.get(:ticket_count)
|
||||
end
|
||||
|
||||
test "when reading as a user that cant see the field, its value is not displayed", %{
|
||||
representative: representative,
|
||||
user: user
|
||||
|
|
|
@ -20,6 +20,17 @@ defmodule Ash.Test.Support.PolicyField.User do
|
|||
end
|
||||
end
|
||||
|
||||
relationships do
|
||||
has_many :tickets, Ash.Test.Support.PolicyField.Ticket do
|
||||
source_attribute :id
|
||||
destination_attribute :reporter_id
|
||||
end
|
||||
end
|
||||
|
||||
aggregates do
|
||||
count :ticket_count, :tickets
|
||||
end
|
||||
|
||||
policies do
|
||||
policy always() do
|
||||
authorize_if always()
|
||||
|
@ -30,5 +41,9 @@ defmodule Ash.Test.Support.PolicyField.User do
|
|||
field_policy :role do
|
||||
authorize_if actor_attribute_equals(:role, :representative)
|
||||
end
|
||||
|
||||
field_policy :ticket_count do
|
||||
authorize_if actor_attribute_equals(:role, :reporter)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue