@@ -23,44 +23,57 @@ defmodule Realtime.Tenants.AuthorizationTest do
2323 ]
2424 test "authenticated user has expected policies" , context do
2525 { :ok , policies } =
26- Authorization . get_read_authorizations (
27- % Policies { } ,
28- context . db_conn ,
29- context . authorization_context
30- )
26+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
3127
3228 { :ok , policies } =
33- Authorization . get_write_authorizations (
34- policies ,
35- context . db_conn ,
36- context . authorization_context
37- )
29+ Authorization . get_write_authorizations ( policies , context . db_conn , context . authorization_context )
3830
3931 assert % Policies {
4032 broadcast: % BroadcastPolicies { read: true , write: true } ,
4133 presence: % PresencePolicies { read: true , write: true }
4234 } == policies
4335 end
4436
37+ @ tag role: "authenticated" ,
38+ policies: [ :authenticated_read_matching_user_sub ] ,
39+ sub: "ccbdfd51-c5aa-4d61-8c17-647664466a26"
40+ test "authenticated user sub is available" , context do
41+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: true , write: nil } } } =
42+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
43+
44+ authorization_context = % { context . authorization_context | sub: "135f6d25-5840-4266-a8ca-b9a45960e424" }
45+
46+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: false , write: nil } } } =
47+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , authorization_context )
48+ end
49+
50+ @ tag role: "authenticated" ,
51+ policies: [ :read_matching_user_role ]
52+ test "user role is exposed" , context do
53+ # policy role is checking for "authenticated"
54+ # set_config is setting request.jwt.claim.role to authenticated as well
55+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: true , write: nil } } } =
56+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
57+
58+ authorization_context = % { context . authorization_context | role: "anon" }
59+
60+ # policy role is checking for "authenticated"
61+ # set_config is setting request.jwt.claim.role to anon
62+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: false , write: nil } } } =
63+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , authorization_context )
64+ end
65+
4566 @ tag role: "anon" ,
4667 policies: [
4768 :authenticated_read_broadcast_and_presence ,
4869 :authenticated_write_broadcast_and_presence
4970 ]
5071 test "anon user has no policies" , context do
5172 { :ok , policies } =
52- Authorization . get_read_authorizations (
53- % Policies { } ,
54- context . db_conn ,
55- context . authorization_context
56- )
73+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
5774
5875 { :ok , policies } =
59- Authorization . get_write_authorizations (
60- policies ,
61- context . db_conn ,
62- context . authorization_context
63- )
76+ Authorization . get_write_authorizations ( policies , context . db_conn , context . authorization_context )
6477
6578 assert % Policies {
6679 broadcast: % BroadcastPolicies { read: false , write: false } ,
@@ -119,39 +132,19 @@ defmodule Realtime.Tenants.AuthorizationTest do
119132 policies: [ :broken_read_presence , :broken_write_presence ]
120133 test "broken RLS policy sets policies to false and shows error to user" , context do
121134 assert { :error , :rls_policy_error , % Postgrex.Error { } } =
122- Authorization . get_read_authorizations (
123- % Policies { } ,
124- context . db_conn ,
125- context . authorization_context
126- )
135+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
127136
128137 assert { :error , :rls_policy_error , % Postgrex.Error { } } =
129- Authorization . get_write_authorizations (
130- % Policies { } ,
131- context . db_conn ,
132- context . authorization_context
133- )
138+ Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
134139
135140 assert { :error , :rls_policy_error , % Postgrex.Error { } } =
136- Authorization . get_read_authorizations (
137- % Policies { } ,
138- context . db_conn ,
139- context . authorization_context
140- )
141+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
141142
142143 assert { :error , :rls_policy_error , % Postgrex.Error { } } =
143- Authorization . get_write_authorizations (
144- % Policies { } ,
145- context . db_conn ,
146- context . authorization_context
147- )
144+ Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
148145
149146 assert { :error , :rls_policy_error , % Postgrex.Error { } } =
150- Authorization . get_write_authorizations (
151- % Policies { } ,
152- context . db_conn ,
153- context . authorization_context
154- )
147+ Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
155148 end
156149 end
157150
@@ -162,19 +155,8 @@ defmodule Realtime.Tenants.AuthorizationTest do
162155 :authenticated_write_broadcast_and_presence
163156 ]
164157 test "authenticated user has expected policies" , context do
165- { :ok , _ } =
166- Authorization . get_read_authorizations (
167- % Policies { } ,
168- context . db_conn ,
169- context . authorization_context
170- )
171-
172- { :ok , _ } =
173- Authorization . get_write_authorizations (
174- % Policies { } ,
175- context . db_conn ,
176- context . authorization_context
177- )
158+ { :ok , _ } = Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
159+ { :ok , _ } = Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
178160
179161 { :ok , db_conn } = Database . connect ( context . tenant , "realtime_test" )
180162 assert { :ok , [ ] } = Repo . all ( db_conn , Message , Message )
@@ -205,19 +187,8 @@ defmodule Realtime.Tenants.AuthorizationTest do
205187 % { }
206188 )
207189
208- { :ok , _ } =
209- Authorization . get_read_authorizations (
210- % Policies { } ,
211- context . db_conn ,
212- context . authorization_context
213- )
214-
215- { :ok , _ } =
216- Authorization . get_write_authorizations (
217- % Policies { } ,
218- context . db_conn ,
219- context . authorization_context
220- )
190+ { :ok , _ } = Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
191+ { :ok , _ } = Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
221192
222193 external_id = context . authorization_context . tenant_id
223194
@@ -232,19 +203,20 @@ defmodule Realtime.Tenants.AuthorizationTest do
232203 def rls_context ( context ) do
233204 tenant = Containers . checkout_tenant ( run_migrations: true )
234205 { :ok , db_conn } = Database . connect ( tenant , "realtime_test" , :stop )
235- topic = random_string ( )
206+ topic = context [ :topic ] || random_string ( )
236207
237- create_rls_policies ( db_conn , context . policies , % { topic: topic } )
208+ create_rls_policies ( db_conn , context . policies , % { topic: topic , sub: context [ :sub ] , role: context . role } )
238209
239- claims = % { sub: random_string ( ) , role: context . role , exp: Joken . current_time ( ) + 1_000 }
210+ claims = % { " sub" => context [ :sub ] || random_string ( ) , " role" => context . role , " exp" => Joken . current_time ( ) + 1_000 }
240211
241212 authorization_context =
242213 Authorization . build_authorization_params ( % {
243214 tenant_id: tenant . external_id ,
244215 topic: topic ,
245216 claims: claims ,
246217 headers: [ { "header-1" , "value-1" } ] ,
247- role: claims . role
218+ role: claims [ "role" ] ,
219+ sub: claims [ "sub" ]
248220 } )
249221
250222 Realtime.Tenants.Migrations . create_partitions ( db_conn )
0 commit comments