|
| 1 | +defmodule Ash.Test.MultitenancyTest do |
| 2 | + @moduledoc false |
| 3 | + use ExUnit.Case, async: true |
| 4 | + |
| 5 | + alias Ash.Test.Domain, as: Domain |
| 6 | + |
| 7 | + defmodule MultiTenant do |
| 8 | + use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets |
| 9 | + |
| 10 | + multitenancy do |
| 11 | + strategy :attribute |
| 12 | + attribute :owner |
| 13 | + end |
| 14 | + |
| 15 | + attributes do |
| 16 | + attribute :id, :integer, primary_key?: true, allow_nil?: false, public?: true |
| 17 | + attribute :owner, :integer, primary_key?: true, allow_nil?: false, public?: true |
| 18 | + end |
| 19 | + |
| 20 | + actions do |
| 21 | + default_accept :* |
| 22 | + defaults [:read, :create] |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + test "reading an object doesn't require multitenancy attribute in the primary key" do |
| 27 | + MultiTenant |
| 28 | + |> Ash.Changeset.for_create(:create, %{id: 1000, owner: 1}) |
| 29 | + |> Ash.create!(tenant: 1) |
| 30 | + |
| 31 | + MultiTenant |
| 32 | + |> Ash.get!(1000, tenant: 1) |
| 33 | + end |
| 34 | + |
| 35 | + defmodule NonMultiTenant do |
| 36 | + use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets |
| 37 | + |
| 38 | + attributes do |
| 39 | + attribute :id, :integer, primary_key?: true, allow_nil?: false, public?: true |
| 40 | + attribute :owner, :integer, primary_key?: true, allow_nil?: false, public?: true |
| 41 | + end |
| 42 | + |
| 43 | + actions do |
| 44 | + default_accept :* |
| 45 | + defaults [:read, :create] |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + test "reading an object without multitenancy requires attribute in the primary key" do |
| 50 | + NonMultiTenant |
| 51 | + |> Ash.Changeset.for_create(:create, %{id: 1000, owner: 1}) |
| 52 | + |> Ash.create!() |
| 53 | + |
| 54 | + ExUnit.Assertions.assert_raise(Ash.Error.Invalid, fn -> |
| 55 | + NonMultiTenant |
| 56 | + |> Ash.get!(1000) |
| 57 | + end) |
| 58 | + |
| 59 | + NonMultiTenant |
| 60 | + |> Ash.get!(%{id: 1000, owner: 1}) |
| 61 | + end |
| 62 | +end |
0 commit comments