mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 21:43:02 +12:00
c9f20f1488
improvement: add `always_authorize?` and `require_actor?` to api config
154 lines
3.5 KiB
Elixir
154 lines
3.5 KiB
Elixir
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
|
|
|
|
actions do
|
|
defaults [:create, :read, :update, :destroy]
|
|
end
|
|
|
|
attributes do
|
|
uuid_primary_key :id
|
|
|
|
attribute :title, :string, allow_nil?: false
|
|
end
|
|
end
|
|
|
|
defmodule Registry do
|
|
@moduledoc false
|
|
use Ash.Registry
|
|
|
|
entries do
|
|
entry Post
|
|
end
|
|
end
|
|
|
|
defmodule Api do
|
|
use Ash.Api, otp_app: :ash
|
|
|
|
resources do
|
|
registry Registry
|
|
end
|
|
end
|
|
|
|
describe "authorization options" do
|
|
setup do
|
|
on_exit(fn ->
|
|
Application.delete_env(:ash, Api)
|
|
end)
|
|
end
|
|
|
|
test "always_authorize? authorizes automatically" do
|
|
Application.put_env(:ash, Api,
|
|
authorization: [
|
|
always_authorize?: true
|
|
]
|
|
)
|
|
|
|
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
|
|
|
|
test "require_actor? requires an actor for all requests" do
|
|
Application.put_env(:ash, Api,
|
|
authorization: [
|
|
require_actor?: true,
|
|
always_authorize?: true
|
|
]
|
|
)
|
|
|
|
start_supervised({Ash.Test.Authorizer, strict_check: :forbidden})
|
|
|
|
assert_raise Ash.Error.Forbidden.ApiRequiresActor, fn ->
|
|
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
|
|
|
|
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
|
|
|
|
test "a simple filter can also be applied to changesets" do
|
|
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
|
|
|> Ash.Changeset.for_create(:create, %{title: "test"})
|
|
|> Api.create!(authorize?: true)
|
|
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
|
|
|> Ash.Changeset.for_update(:update, %{title: "next"})
|
|
|> Api.update!(authorize?: true)
|
|
end
|
|
|
|
good_post
|
|
|> Ash.Changeset.for_update(:update, %{title: "next"})
|
|
|> Api.update!(authorize?: true)
|
|
end
|
|
end
|
|
end
|