@@ -356,4 +356,90 @@ defmodule AshPostgres.Test.MultitenancyTest do
356356 end
357357 )
358358 end
359+
360+ describe "context multitenancy prefix inheritance" do
361+ test "loading aggregates on context multitenant resources with relationships works" , % {
362+ org1: org1
363+ } do
364+ user =
365+ User
366+ |> Ash.Changeset . for_create ( :create , % { name: "a" } , tenant: tenant ( org1 ) )
367+ |> Ash.Changeset . manage_relationship ( :org , org1 , type: :append_and_remove )
368+ |> Ash . create! ( )
369+
370+ _post =
371+ Post
372+ |> Ash.Changeset . for_create ( :create , % { name: "foobar" } ,
373+ authorize?: false ,
374+ tenant: tenant ( org1 )
375+ )
376+ |> Ash.Changeset . manage_relationship ( :user , user , type: :append_and_remove )
377+ |> Ash . create! ( )
378+
379+ # Load an aggregate that goes through a relationship path crossing multitenancy boundaries
380+ # This should properly inherit the prefix from the parent query
381+ assert [ % { count_visited: 1 } ] =
382+ User
383+ |> Ash.Query . filter ( id == ^ user . id )
384+ |> Ash.Query . load ( :count_visited )
385+ |> Ash.Query . set_tenant ( tenant ( org1 ) )
386+ |> Ash . read! ( )
387+ end
388+
389+ test "loading context multitenant relationship from attribute multitenant resource inherits prefix" ,
390+ % { org1: org1 } do
391+ user =
392+ User
393+ |> Ash.Changeset . for_create ( :create , % { name: "test_user" } , tenant: tenant ( org1 ) )
394+ |> Ash.Changeset . manage_relationship ( :org , org1 , type: :append_and_remove )
395+ |> Ash . create! ( )
396+
397+ post =
398+ Post
399+ |> Ash.Changeset . for_create ( :create , % { name: "test_post" } ,
400+ authorize?: false ,
401+ tenant: tenant ( org1 )
402+ )
403+ |> Ash.Changeset . manage_relationship ( :user , user , type: :append_and_remove )
404+ |> Ash . create! ( )
405+
406+ # Load posts from user - this crosses from attribute multitenancy to context multitenancy
407+ # The prefix should be properly set on the join query
408+ loaded_user = Ash . load! ( user , :posts , tenant: tenant ( org1 ) )
409+ assert length ( loaded_user . posts ) == 1
410+ assert hd ( loaded_user . posts ) . id == post . id
411+ end
412+
413+ test "querying context multitenant resource and loading many_to_many properly inherits prefix" ,
414+ % { org1: org1 } do
415+ post1 =
416+ Post
417+ |> Ash.Changeset . for_create ( :create , % { name: "post1" } ,
418+ authorize?: false ,
419+ tenant: tenant ( org1 )
420+ )
421+ |> Ash . create! ( )
422+
423+ post2 =
424+ Post
425+ |> Ash.Changeset . for_create ( :create , % { name: "post2" } ,
426+ authorize?: false ,
427+ tenant: tenant ( org1 )
428+ )
429+ |> Ash.Changeset . manage_relationship ( :linked_posts , [ post1 ] , type: :append_and_remove )
430+ |> Ash . create! ( )
431+
432+ # Query with tenant set, load many_to_many relationship
433+ # The join through PostLink should inherit the prefix
434+ result =
435+ Post
436+ |> Ash.Query . filter ( id == ^ post2 . id )
437+ |> Ash.Query . load ( :linked_posts )
438+ |> Ash.Query . set_tenant ( tenant ( org1 ) )
439+ |> Ash . read! ( )
440+
441+ assert [ % { linked_posts: [ % { id: post1_id } ] } ] = result
442+ assert post1_id == post1 . id
443+ end
444+ end
359445end
0 commit comments