From 4fb6da6eee416f401cbf4b4e63bfccd7f9758f6f Mon Sep 17 00:00:00 2001 From: Barnabas Jovanovics Date: Thu, 28 Sep 2023 14:36:37 +0200 Subject: [PATCH] test: update test to load forbidden field through union (#713) --- test/actions/load_test.exs | 76 +++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/test/actions/load_test.exs b/test/actions/load_test.exs index fcbc4104..62ecfa98 100644 --- a/test/actions/load_test.exs +++ b/test/actions/load_test.exs @@ -25,7 +25,9 @@ defmodule Ash.Test.Actions.LoadTest do end defmodule Bio do - use Ash.Resource, data_layer: :embedded + use Ash.Resource, + authorizers: [Ash.Policy.Authorizer], + data_layer: :embedded attributes do attribute(:first_name, :string) @@ -38,13 +40,32 @@ defmodule Ash.Test.Actions.LoadTest do end end + field_policies do + field_policy :* do + authorize_if always() + end + + field_policy :forbidden_name do + forbid_if always() + end + end + + policies do + policy always() do + authorize_if always() + end + end + calculations do calculate(:full_name, :string, expr(first_name <> " " <> last_name)) + calculate(:forbidden_name, :string, expr(first_name <> " " <> last_name)) end end defmodule OtherKindOfBio do - use Ash.Resource, data_layer: :embedded + use Ash.Resource, + authorizers: [Ash.Policy.Authorizer], + data_layer: :embedded attributes do attribute(:first_name, :string) @@ -57,8 +78,26 @@ defmodule Ash.Test.Actions.LoadTest do end end + field_policies do + field_policy :* do + authorize_if always() + end + + field_policy :forbidden_name do + forbid_if always() + end + end + + policies do + policy always() do + authorize_if always() + end + end + calculations do calculate(:full_name, :string, expr(first_name <> " " <> last_name)) + + calculate(:forbidden_name, :string, expr(first_name <> " " <> last_name)) end end @@ -842,22 +881,39 @@ defmodule Ash.Test.Actions.LoadTest do |> Api.create!() assert [ - %{bio_union_calc: %Ash.Union{value: %{full_name: "donald duck"}}}, - %{bio_union_calc: %Ash.Union{value: %{full_name: "donald duck"}}} + %{ + bio_union_calc: %Ash.Union{ + value: %{full_name: "donald duck", forbidden_name: %Ash.ForbiddenField{}} + } + }, + %{ + bio_union_calc: %Ash.Union{ + value: %{full_name: "donald duck", forbidden_name: %Ash.ForbiddenField{}} + } + } ] = Author - |> Ash.Query.load(bio_union_calc: {%{}, [*: :full_name]}) - |> Api.read!() + |> Ash.Query.load(bio_union_calc: {%{}, [*: [:full_name, :forbidden_name]]}) + |> Api.read!(actor: %{name: "zerg"}) assert [ - %{bio_union_calc: %Ash.Union{value: %{full_name: "donald duck"}}}, - %{bio_union_calc: %Ash.Union{value: %{full_name: "donald duck"}}} + %{ + bio_union_calc: %Ash.Union{ + value: %{full_name: "donald duck", forbidden_name: %Ash.ForbiddenField{}} + } + }, + %{ + bio_union_calc: %Ash.Union{ + value: %{full_name: "donald duck", forbidden_name: %Ash.ForbiddenField{}} + } + } ] = Author |> Ash.Query.load( - bio_union_calc: {%{}, [bio: :full_name, other_kind_of_bio: :full_name]} + bio_union_calc: + {%{}, [bio: :full_name, other_kind_of_bio: [:full_name, :forbidden_name]]} ) - |> Api.read!() + |> Api.read!(actor: %{name: "zerg"}) end end end