Ash FrameworkAF
Ash Framework8mo ago
32 replies
Ege

Error in action when policy is added

I have this action on the DashboardGroup resource:
read :get_by_id do
  get? true
  argument :id, :uuid, allow_nil?: false
  argument :organization_id, :uuid, allow_nil?: false
  filter expr(id == ^arg(:id))
end

Attributes:
attributes do
  uuid_primary_key :id

  attribute :name, :string
  attribute :student_filter, :map
  attribute :educator_id, :uuid
  attribute :organization_id, :uuid

  create_timestamp :inserted_at
  update_timestamp :updated_at
end

Relationships:
belongs_to :educator, User do
  source_attribute :educator_id
  destination_attribute :id
end

Policy:
policy action(:get_by_id) do
  authorize_if relates_to_actor_via(:educator)
end

Struggling with tests. I have this:
test "get_by_id/3", ctx do
  group = generate(dashboard_group(organization_id: ctx.parent_org.id, educator_id: ctx.user.id))

  {:ok, group} = DashboardGroup.get_by_id(group.id, ctx.parent_org.id, actor: ctx.user)
  assert is_nil(group) == false

  user2 = generate(user(type: :educator, first_name: "Bob", last_name: "Marley"))
  generate(organization_member(user_id: user2.id, organization_id: ctx.parent_org.id, role: :base))

  group2 = generate(dashboard_group(organization_id: ctx.parent_org.id, educator_id: user2.id))

  {:ok, group2} = DashboardGroup.get_by_id(group2.id, ctx.parent_org.id, actor: ctx.user)
  assert is_nil(group2) == true
end

The second to last line is giving an error:
** (MatchError) no match of right hand side value: {:error, %Ash.Error.Invalid{errors: [%Ash.Error.Query.NotFound{primary_key: nil, resource: MyApp.Ash.Dashboards.DashboardGroup, splode: Ash.Error, bread_crumbs: [], vars: [], path: [], stacktrace: #Splode.Stacktrace<>, class: :invalid}]}}

But inside LV modules, trying to get a dashboard group by id when the actor does not have permission results in {:ok, nil}
Was this page helpful?