@@ -73,13 +73,11 @@ defmodule WraftDocWeb.Auth.CurrentOrganisationTest do
7373 |> CurrentOrganisation . call ( [ ] )
7474
7575 assert conn . assigns [ :current_user ] . current_org_id == user . current_org_id
76- # Permissions should be unique and aggregated
7776 assert length ( conn . assigns [ :current_user ] . permissions ) == 4
7877 assert "layout:index" in conn . assigns [ :current_user ] . permissions
7978 assert "layout:show" in conn . assigns [ :current_user ] . permissions
8079 assert "layout:create" in conn . assigns [ :current_user ] . permissions
8180 assert "layout:update" in conn . assigns [ :current_user ] . permissions
82- # Role names should include both roles
8381 assert length ( conn . assigns [ :current_user ] . role_names ) == 2
8482 assert role1 . name in conn . assigns [ :current_user ] . role_names
8583 assert role2 . name in conn . assigns [ :current_user ] . role_names
@@ -126,7 +124,6 @@ defmodule WraftDocWeb.Auth.CurrentOrganisationTest do
126124 { :ok , token , _claims } =
127125 Guardian . encode_and_sign ( user , % { organisation_id: user . current_org_id } )
128126
129- # Pre-assign user with org_id
130127 user_with_org = Map . put ( user , :current_org_id , organisation . id )
131128
132129 conn =
@@ -135,7 +132,6 @@ defmodule WraftDocWeb.Auth.CurrentOrganisationTest do
135132 |> assign ( :current_user , user_with_org )
136133 |> CurrentOrganisation . call ( [ ] )
137134
138- # Should keep the existing org_id and not process JWT
139135 assert conn . assigns [ :current_user ] . current_org_id == organisation . id
140136 refute conn . halted
141137 end
@@ -187,14 +183,60 @@ defmodule WraftDocWeb.Auth.CurrentOrganisationTest do
187183 |> conn_init ( )
188184 |> CurrentOrganisation . call ( [ ] )
189185
190- # Should only have role from organisation1
191186 assert conn . assigns [ :current_user ] . current_org_id == organisation1 . id
192187 assert conn . assigns [ :current_user ] . role_names == [ role1 . name ]
193188 assert conn . assigns [ :current_user ] . permissions == [ "layout:index" ]
194189 refute role2 . name in conn . assigns [ :current_user ] . role_names
195190 refute "layout:show" in conn . assigns [ :current_user ] . permissions
196191 refute conn . halted
197192 end
193+
194+ test "does not halt when no JWT token is provided" do
195+ conn =
196+ build_conn ( )
197+ |> put_req_header ( "content-type" , "application/json" )
198+ |> put_resp_content_type ( "application/json" )
199+ |> Map . put ( :params , % { } )
200+ |> CurrentOrganisation . call ( [ ] )
201+
202+ refute conn . halted
203+ end
204+
205+ test "does not halt when claims are nil" do
206+ conn =
207+ build_conn ( )
208+ |> put_req_header ( "content-type" , "application/json" )
209+ |> put_resp_content_type ( "application/json" )
210+ |> Map . put ( :params , % { } )
211+ |> Plug . put_current_claims ( nil )
212+ |> CurrentOrganisation . call ( [ ] )
213+
214+ refute conn . halted
215+ end
216+
217+ test "does not halt when claims don't have organisation_id" do
218+ user = insert ( :user_with_organisation )
219+
220+ { :ok , token , _claims } =
221+ Guardian . encode_and_sign ( user , % { some_other_key: "value" } )
222+
223+ conn =
224+ build_conn ( )
225+ |> put_req_header ( "authorization" , "Bearer " <> token )
226+ |> put_resp_content_type ( "application/json" )
227+ |> Map . put ( :params , % { } )
228+ |> then ( fn conn ->
229+ { :ok , claims } = Guardian . decode_and_verify ( token )
230+
231+ conn
232+ |> Plug . put_current_claims ( claims )
233+ |> Plug . put_current_resource ( claims [ "sub" ] )
234+ |> CurrentUser . call ( [ ] )
235+ end )
236+ |> CurrentOrganisation . call ( [ ] )
237+
238+ refute conn . halted
239+ end
198240 end
199241
200242 # Private
0 commit comments