Skip to content

Commit 0e989ac

Browse files
committed
test: write failing test to illustrate regression
1 parent ed3ff07 commit 0e989ac

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

test/aggregate_test.exs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule AshSql.AggregateTest do
22
use AshPostgres.RepoCase, async: false
33
import ExUnit.CaptureIO
4+
45
alias AshPostgres.Test.{Author, Comment, Organization, Post, Rating, User}
56

67
require Ash.Query
@@ -1683,4 +1684,52 @@ defmodule AshSql.AggregateTest do
16831684
|> Ash.read_one!()
16841685
end
16851686
end
1687+
1688+
@tag :regression
1689+
test "aggregates with authorization policies using relates_to_actor_via with existing resources" do
1690+
org =
1691+
AshPostgres.Test.Organization
1692+
|> Ash.Changeset.for_create(:create, %{name: "Test Org"})
1693+
|> Ash.create!()
1694+
1695+
user =
1696+
AshPostgres.Test.User
1697+
|> Ash.Changeset.for_create(:create, %{name: "test_user", organization_id: org.id})
1698+
|> Ash.create!()
1699+
1700+
AshPostgres.Test.User
1701+
|> Ash.Changeset.for_create(:create, %{name: "another_user", organization_id: org.id})
1702+
|> Ash.create!()
1703+
1704+
author =
1705+
AshPostgres.Test.Author
1706+
|> Ash.Changeset.for_create(:create, %{first_name: "Test", last_name: "Author"})
1707+
|> Ash.create!()
1708+
1709+
post =
1710+
AshPostgres.Test.Post
1711+
|> Ash.Changeset.for_create(:create, %{
1712+
title: "Test Post",
1713+
organization_id: org.id,
1714+
author_id: author.id
1715+
})
1716+
|> Ash.create!()
1717+
1718+
comment1 =
1719+
AshPostgres.Test.Comment
1720+
|> Ash.Changeset.for_create(:create, %{
1721+
title: "First comment",
1722+
post_id: post.id,
1723+
author_id: author.id
1724+
})
1725+
|> Ash.create!()
1726+
1727+
loaded_post =
1728+
AshPostgres.Test.Post
1729+
|> Ash.Query.filter(id == ^post.id)
1730+
|> Ash.Query.load(:comments_in_my_org)
1731+
|> Ash.read_one!(actor: user)
1732+
1733+
assert loaded_post.comments_in_my_org == 1
1734+
end
16861735
end

test/support/resources/post.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,10 @@ defmodule AshPostgres.Test.Post do
12181218
count :count_comments_with_modify_query, :comments do
12191219
read_action(:with_modify_query)
12201220
end
1221+
1222+
count :comments_in_my_org, :comments do
1223+
filter(expr(post.organization_id == ^actor(:organization_id)))
1224+
end
12211225
end
12221226
end
12231227

0 commit comments

Comments
 (0)