2024-02-14 22:36:34 +13:00
|
|
|
defmodule FilterFieldPolicyTest do
|
|
|
|
use AshPostgres.RepoCase, async: false
|
|
|
|
|
2024-02-15 02:17:05 +13:00
|
|
|
alias AshPostgres.Test.{Api, Post, Organization, User}
|
2024-02-14 22:36:34 +13:00
|
|
|
|
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
setup do
|
|
|
|
# I needed this to make the query actually fail
|
|
|
|
# this triggers an exeption in the reporting of
|
|
|
|
# the policy breakdowns because the policies are
|
|
|
|
# nil
|
|
|
|
|
|
|
|
# Without this it would just run the query which
|
|
|
|
# might be even worse as the policies are not applied
|
|
|
|
# correctly and I guess `nil` is a special case for
|
|
|
|
# field policies because you can have no polcies
|
|
|
|
# and that is still valid?
|
|
|
|
current_level = Logger.level()
|
|
|
|
current_setting = Application.get_env(:ash, :policies)
|
|
|
|
|
|
|
|
Application.put_env(
|
|
|
|
:ash,
|
|
|
|
:policies,
|
|
|
|
Keyword.merge(current_setting |> List.wrap(), log_policy_breakdowns: current_level)
|
|
|
|
)
|
|
|
|
|
|
|
|
on_exit(fn ->
|
|
|
|
Application.put_env(:ash, :policies, current_setting)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2024-02-14 22:41:58 +13:00
|
|
|
test "filter uses the correct field policies when exanding refs" do
|
2024-02-14 22:36:34 +13:00
|
|
|
organization =
|
|
|
|
Organization
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "test_org"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
2024-02-15 02:17:05 +13:00
|
|
|
User
|
|
|
|
|> Ash.Changeset.for_create(:create, %{organization_id: organization.id, name: "foo bar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
2024-02-14 22:36:34 +13:00
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{organization_id: organization.id})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
filter = Ash.Filter.parse_input!(Post, %{organization: %{name: %{ilike: "%org"}}})
|
|
|
|
|
|
|
|
assert [_] =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.do_filter(filter)
|
|
|
|
|> Ash.Query.for_read(:allow_any)
|
2024-02-15 02:17:05 +13:00
|
|
|
|> Api.read!(actor: %{id: "test"})
|
|
|
|
|
|
|
|
filter = Ash.Filter.parse_input!(Post, %{organization: %{users: %{name: %{ilike: "%bar"}}}})
|
|
|
|
|
|
|
|
assert [_] =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.do_filter(filter)
|
|
|
|
|> Ash.Query.for_read(:allow_any)
|
|
|
|
|> Api.read!(actor: %{id: "test"})
|
2024-02-14 22:36:34 +13:00
|
|
|
end
|
|
|
|
end
|