@@ -565,10 +565,9 @@ defmodule Realtime.Integration.RtChannelTest do
565
565
:authenticated_read_broadcast_and_presence ,
566
566
:authenticated_write_broadcast_and_presence
567
567
]
568
- test "on new access_token and channel is private policies are reevaluated" ,
568
+ test "on new access_token and channel is private policies are reevaluated for read policy " ,
569
569
% { topic: topic } do
570
570
{ socket , access_token } = get_connection ( "authenticated" )
571
- { :ok , new_token } = token_valid ( "anon" )
572
571
573
572
realtime_topic = "realtime:#{ topic } "
574
573
@@ -580,6 +579,8 @@ defmodule Realtime.Integration.RtChannelTest do
580
579
assert_receive % Message { event: "phx_reply" } , 500
581
580
assert_receive % Message { event: "presence_state" } , 500
582
581
582
+ { :ok , new_token } = token_valid ( "anon" )
583
+
583
584
WebsocketClient . send_event ( socket , realtime_topic , "access_token" , % {
584
585
"access_token" => new_token
585
586
} )
@@ -601,6 +602,65 @@ defmodule Realtime.Integration.RtChannelTest do
601
602
assert_receive % Message { event: "phx_close" , topic: ^ realtime_topic }
602
603
end
603
604
605
+ @ tag policies: [
606
+ :authenticated_read_broadcast_and_presence ,
607
+ :authenticated_write_broadcast_and_presence
608
+ ]
609
+ test "on new access_token and channel is private policies are reevaluated for write policy" ,
610
+ % { topic: topic , tenant: tenant } do
611
+ { socket , access_token } = get_connection ( "authenticated" )
612
+ realtime_topic = "realtime:#{ topic } "
613
+
614
+ WebsocketClient . join ( socket , realtime_topic , % {
615
+ config: % { broadcast: % { self: true } , private: true } ,
616
+ access_token: access_token
617
+ } )
618
+
619
+ assert_receive % Message { event: "phx_reply" } , 500
620
+ assert_receive % Message { event: "presence_state" } , 500
621
+ # Checks first send which will set write policy to true
622
+ payload = % { "event" => "TEST" , "payload" => % { "msg" => 1 } , "type" => "broadcast" }
623
+ WebsocketClient . send_event ( socket , realtime_topic , "broadcast" , payload )
624
+ Process . sleep ( 1000 )
625
+
626
+ assert_receive % Message {
627
+ event: "broadcast" ,
628
+ payload: ^ payload ,
629
+ topic: ^ realtime_topic
630
+ } ,
631
+ 500
632
+
633
+ # RLS policies changed to only allow read
634
+ { :ok , db_conn } = Database . connect ( tenant , "realtime_test" )
635
+ clean_table ( db_conn , "realtime" , "messages" )
636
+ create_rls_policies ( db_conn , [ :authenticated_read_broadcast_and_presence ] , % { topic: topic } )
637
+
638
+ # Set new token to recheck policies
639
+ { :ok , new_token } =
640
+ generate_token ( % {
641
+ exp: System . system_time ( :second ) + 1000 ,
642
+ role: "authenticated" ,
643
+ sub: random_string ( )
644
+ } )
645
+
646
+ WebsocketClient . send_event ( socket , realtime_topic , "access_token" , % {
647
+ "access_token" => new_token
648
+ } )
649
+
650
+ # Send message to be ignored
651
+ payload = % { "event" => "TEST" , "payload" => % { "msg" => 1 } , "type" => "broadcast" }
652
+ WebsocketClient . send_event ( socket , realtime_topic , "broadcast" , payload )
653
+
654
+ Process . sleep ( 1000 )
655
+
656
+ refute_receive % Message {
657
+ event: "broadcast" ,
658
+ payload: ^ payload ,
659
+ topic: ^ realtime_topic
660
+ } ,
661
+ 500
662
+ end
663
+
604
664
test "on new access_token and channel is public policies are not reevaluated" ,
605
665
% { topic: topic } do
606
666
{ socket , access_token } = get_connection ( "authenticated" )
0 commit comments