2024-02-16 17:26:44 +13:00
|
|
|
defmodule Ash.Flow.TenantTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
defmodule Post do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
|
|
|
defaults [:create, :read, :update, :destroy]
|
|
|
|
|
|
|
|
read :get_by_id do
|
|
|
|
get_by(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
attribute :tenant, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
multitenancy do
|
|
|
|
global? true
|
|
|
|
strategy :attribute
|
|
|
|
attribute :tenant
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Registry do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Registry
|
|
|
|
|
|
|
|
entries do
|
|
|
|
entry(Post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Api do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Api
|
|
|
|
|
|
|
|
resources do
|
|
|
|
registry Registry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule DestroyPost do
|
|
|
|
use Ash.Flow
|
|
|
|
|
|
|
|
flow do
|
|
|
|
api Api
|
|
|
|
|
|
|
|
argument :post_id, :uuid do
|
|
|
|
allow_nil? false
|
|
|
|
end
|
|
|
|
|
|
|
|
returns [:destroy_post]
|
|
|
|
end
|
|
|
|
|
|
|
|
steps do
|
|
|
|
read :get_post, Post, :get_by_id do
|
|
|
|
get? true
|
|
|
|
input %{id: arg(:post_id)}
|
|
|
|
end
|
|
|
|
|
|
|
|
destroy :destroy_post, Post, :destroy do
|
|
|
|
record result(:get_post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "honors Ash.set_tenant" do
|
|
|
|
foo_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{tenant: "foo"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Ash.set_tenant("bar")
|
2024-02-16 17:30:09 +13:00
|
|
|
|
|
|
|
assert %Ash.Flow.Result{valid?: true, result: %{destroy_post: nil}} =
|
|
|
|
DestroyPost.run(foo_post.id)
|
2024-02-16 17:26:44 +13:00
|
|
|
|
|
|
|
Ash.set_tenant("foo")
|
|
|
|
assert %Ash.Flow.Result{valid?: true} = DestroyPost.run(foo_post.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "honors tenant in opts" do
|
|
|
|
foo_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{tenant: "foo"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
2024-02-16 17:30:09 +13:00
|
|
|
assert %Ash.Flow.Result{valid?: true, result: %{destroy_post: nil}} =
|
|
|
|
DestroyPost.run(foo_post.id, tenant: "bar")
|
|
|
|
|
2024-02-16 17:26:44 +13:00
|
|
|
assert %Ash.Flow.Result{valid?: true} = DestroyPost.run(foo_post.id, tenant: "foo")
|
|
|
|
end
|
|
|
|
end
|