test: Reproducing a bug where the flow does not honor tenant (#900)

This commit is contained in:
Jechol Lee 2024-02-16 13:26:44 +09:00 committed by GitHub
parent e214043218
commit aa7278fe79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

101
test/flow/tenant_test.exs Normal file
View file

@ -0,0 +1,101 @@
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")
assert %Ash.Flow.Result{valid?: false} = DestroyPost.run(foo_post.id)
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!()
assert %Ash.Flow.Result{valid?: false} = DestroyPost.run(foo_post.id, tenant: "bar")
assert %Ash.Flow.Result{valid?: true} = DestroyPost.run(foo_post.id, tenant: "foo")
end
end