2021-01-29 08:47:59 +13:00
|
|
|
defmodule Ash.Test.Changeset.AuthorizerTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
defmodule Post do
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
authorizers: [
|
|
|
|
Ash.Test.Authorizer
|
|
|
|
]
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private? true
|
|
|
|
end
|
|
|
|
|
2022-04-04 17:48:37 +12:00
|
|
|
actions do
|
|
|
|
defaults [:create, :read, :update, :destroy]
|
2023-12-28 04:27:22 +13:00
|
|
|
|
|
|
|
create :title_is_authorization do
|
|
|
|
accept []
|
|
|
|
|
|
|
|
change fn changeset, context ->
|
|
|
|
Ash.Changeset.change_attribute(changeset, :title, context.authorize?)
|
|
|
|
end
|
|
|
|
end
|
2022-04-04 17:48:37 +12:00
|
|
|
end
|
|
|
|
|
2021-01-29 08:47:59 +13:00
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
|
|
|
|
attribute :title, :string, allow_nil?: false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-07 19:41:02 +13:00
|
|
|
defmodule Registry do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Registry
|
|
|
|
|
|
|
|
entries do
|
|
|
|
entry Post
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 08:47:59 +13:00
|
|
|
defmodule Api do
|
2022-07-19 16:48:31 +12:00
|
|
|
use Ash.Api, otp_app: :ash
|
2021-01-29 08:47:59 +13:00
|
|
|
|
|
|
|
resources do
|
2021-10-07 19:41:02 +13:00
|
|
|
registry Registry
|
2021-01-29 08:47:59 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-19 16:48:31 +12:00
|
|
|
describe "authorization options" do
|
|
|
|
setup do
|
|
|
|
on_exit(fn ->
|
|
|
|
Application.delete_env(:ash, Api)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-07-22 14:23:18 +12:00
|
|
|
test "authorize :always authorizes automatically" do
|
2022-07-19 16:48:31 +12:00
|
|
|
Application.put_env(:ash, Api,
|
|
|
|
authorization: [
|
2022-07-22 14:23:18 +12:00
|
|
|
authorize: :by_default
|
2022-07-19 16:48:31 +12:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
start_supervised({Ash.Test.Authorizer, strict_check: :forbidden})
|
|
|
|
|
|
|
|
assert_raise Ash.Error.Forbidden, fn ->
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"})
|
|
|
|
|> Api.create!()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-28 11:28:52 +13:00
|
|
|
# TODO: this needs to be addressed in ash 3.0
|
|
|
|
# test "authorize :by_default authorizes if actor is set" do
|
|
|
|
# Application.put_env(:ash, Api,
|
|
|
|
# authorization: [
|
|
|
|
# authorize: :by_default
|
|
|
|
# ]
|
|
|
|
# )
|
2023-12-28 04:27:22 +13:00
|
|
|
|
2023-12-28 11:28:52 +13:00
|
|
|
# start_supervised({Ash.Test.Authorizer, strict_check: :authorized})
|
2023-12-28 04:27:22 +13:00
|
|
|
|
2023-12-28 11:28:52 +13:00
|
|
|
# post =
|
|
|
|
# Post
|
|
|
|
# |> Ash.Changeset.for_create(:title_is_authorization, %{}, actor: :an_actor)
|
|
|
|
# |> Api.create!()
|
2023-12-28 04:27:22 +13:00
|
|
|
|
2023-12-28 11:28:52 +13:00
|
|
|
# assert post.title == "true"
|
|
|
|
# end
|
2023-12-28 04:27:22 +13:00
|
|
|
|
2022-07-19 16:48:31 +12:00
|
|
|
test "require_actor? requires an actor for all requests" do
|
|
|
|
Application.put_env(:ash, Api,
|
|
|
|
authorization: [
|
|
|
|
require_actor?: true,
|
2022-07-22 14:23:18 +12:00
|
|
|
authorize: :by_default
|
2022-07-19 16:48:31 +12:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
start_supervised({Ash.Test.Authorizer, strict_check: :forbidden})
|
|
|
|
|
2023-12-16 09:33:10 +13:00
|
|
|
assert_raise Ash.Error.Forbidden, fn ->
|
2022-07-19 16:48:31 +12:00
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"})
|
|
|
|
|> Api.create!()
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise Ash.Error.Forbidden, fn ->
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"})
|
|
|
|
|> Api.create!(actor: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise Ash.Error.Forbidden, fn ->
|
|
|
|
Ash.set_actor(nil)
|
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"})
|
|
|
|
|> Api.create!()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 08:47:59 +13:00
|
|
|
describe "strict check can filter results" do
|
|
|
|
test "a simple filter is applied" do
|
|
|
|
start_supervised(
|
|
|
|
{Ash.Test.Authorizer,
|
|
|
|
strict_check: {:filter, [title: "foo"]}, strict_check_context: [:query]}
|
|
|
|
)
|
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foo"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert [%Post{title: "foo"}] = Api.read!(Post, authorize?: true)
|
|
|
|
end
|
|
|
|
|
2022-10-06 13:08:36 +13:00
|
|
|
test "a filter cannot be applied to creates" do
|
2021-01-29 08:47:59 +13:00
|
|
|
start_supervised(
|
|
|
|
{Ash.Test.Authorizer,
|
|
|
|
strict_check: {:filter, [title: "foo"]}, strict_check_context: [:query, :changeset]}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Filter always fails on creates
|
|
|
|
assert_raise Ash.Error.Forbidden, fn ->
|
|
|
|
Post
|
2022-08-10 12:25:43 +12:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"}, authorize?: true)
|
|
|
|
|> Api.create!()
|
2021-01-29 08:47:59 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
good_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foo"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
bad_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
# Filters apply to the base data
|
|
|
|
assert_raise Ash.Error.Forbidden, fn ->
|
|
|
|
bad_post
|
2022-08-10 12:25:43 +12:00
|
|
|
|> Ash.Changeset.for_update(:update, %{title: "next"}, authorize?: true)
|
|
|
|
|> Api.update!()
|
2021-01-29 08:47:59 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
good_post
|
2022-08-10 12:25:43 +12:00
|
|
|
|> Ash.Changeset.for_update(:update, %{title: "next"}, authorize?: true)
|
|
|
|
|> Api.update!()
|
2021-01-29 08:47:59 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|