1- import pytest
1+ import concurrent .futures
2+ from datetime import datetime , timezone
23from unittest .mock import AsyncMock , Mock , patch
4+
35import jwt
4- from datetime import datetime , timezone
5- import concurrent .futures
6+ import pytest
7+ from cryptography .hazmat .primitives import serialization
8+ from cryptography .hazmat .primitives .asymmetric import rsa
69
710from tests .conftest import with_jwks_mock
811from workos .session import AsyncSession , Session , _get_jwks_client
1619 RefreshWithSessionCookieSuccessResponse ,
1720)
1821
19- from cryptography .hazmat .primitives import serialization
20- from cryptography .hazmat .primitives .asymmetric import rsa
21-
2222
2323class SessionFixtures :
2424 @pytest .fixture (autouse = True )
@@ -48,6 +48,7 @@ def session_constants(self):
4848 "sid" : "session_123" ,
4949 "org_id" : "organization_123" ,
5050 "role" : "admin" ,
51+ "roles" : ["admin" ],
5152 "permissions" : ["read" ],
5253 "entitlements" : ["feature_1" ],
5354 "exp" : int (current_datetime .timestamp ()) + 3600 ,
@@ -215,6 +216,75 @@ def test_authenticate_success(self, session_constants, mock_user_management):
215216 "sid" : session_constants ["SESSION_ID" ],
216217 "org_id" : session_constants ["ORGANIZATION_ID" ],
217218 "role" : "admin" ,
219+ "roles" : ["admin" ],
220+ "permissions" : ["read" ],
221+ "entitlements" : ["feature_1" ],
222+ "exp" : int (datetime .now (timezone .utc ).timestamp ()) + 3600 ,
223+ "iat" : int (datetime .now (timezone .utc ).timestamp ()),
224+ },
225+ session_constants ["PRIVATE_KEY" ],
226+ algorithm = "RS256" ,
227+ ),
228+ "user" : {
229+ "object" : "user" ,
230+ "id" : session_constants ["USER_ID" ],
231+ 232+ "email_verified" : True ,
233+ "created_at" : session_constants ["CURRENT_TIMESTAMP" ],
234+ "updated_at" : session_constants ["CURRENT_TIMESTAMP" ],
235+ },
236+ "impersonator" : None ,
237+ }
238+
239+ # Mock the JWT payload that would be decoded
240+ mock_jwt_payload = {
241+ "sid" : session_constants ["SESSION_ID" ],
242+ "org_id" : session_constants ["ORGANIZATION_ID" ],
243+ "role" : "admin" ,
244+ "roles" : ["admin" ],
245+ "permissions" : ["read" ],
246+ "entitlements" : ["feature_1" ],
247+ }
248+
249+ with patch .object (Session , "unseal_data" , return_value = mock_session ), patch (
250+ "jwt.decode" , return_value = mock_jwt_payload
251+ ), patch .object (
252+ session .jwks ,
253+ "get_signing_key_from_jwt" ,
254+ return_value = Mock (key = session_constants ["PUBLIC_KEY" ]),
255+ ):
256+ response = session .authenticate ()
257+
258+ assert isinstance (response , AuthenticateWithSessionCookieSuccessResponse )
259+ assert response .authenticated is True
260+ assert response .session_id == session_constants ["SESSION_ID" ]
261+ assert response .organization_id == session_constants ["ORGANIZATION_ID" ]
262+ assert response .role == "admin"
263+ assert response .roles == ["admin" ]
264+ assert response .permissions == ["read" ]
265+ assert response .entitlements == ["feature_1" ]
266+ assert response .user .id == session_constants ["USER_ID" ]
267+ assert response .impersonator is None
268+
269+ @with_jwks_mock
270+ def test_authenticate_success_with_roles (
271+ self , session_constants , mock_user_management
272+ ):
273+ session = Session (
274+ user_management = mock_user_management ,
275+ client_id = session_constants ["CLIENT_ID" ],
276+ session_data = session_constants ["SESSION_DATA" ],
277+ cookie_password = session_constants ["COOKIE_PASSWORD" ],
278+ )
279+
280+ # Mock the session data that would be unsealed
281+ mock_session = {
282+ "access_token" : jwt .encode (
283+ {
284+ "sid" : session_constants ["SESSION_ID" ],
285+ "org_id" : session_constants ["ORGANIZATION_ID" ],
286+ "role" : "admin" ,
287+ "roles" : ["admin" , "member" ],
218288 "permissions" : ["read" ],
219289 "entitlements" : ["feature_1" ],
220290 "exp" : int (datetime .now (timezone .utc ).timestamp ()) + 3600 ,
@@ -239,6 +309,7 @@ def test_authenticate_success(self, session_constants, mock_user_management):
239309 "sid" : session_constants ["SESSION_ID" ],
240310 "org_id" : session_constants ["ORGANIZATION_ID" ],
241311 "role" : "admin" ,
312+ "roles" : ["admin" , "member" ],
242313 "permissions" : ["read" ],
243314 "entitlements" : ["feature_1" ],
244315 }
@@ -257,6 +328,7 @@ def test_authenticate_success(self, session_constants, mock_user_management):
257328 assert response .session_id == session_constants ["SESSION_ID" ]
258329 assert response .organization_id == session_constants ["ORGANIZATION_ID" ]
259330 assert response .role == "admin"
331+ assert response .roles == ["admin" , "member" ]
260332 assert response .permissions == ["read" ]
261333 assert response .entitlements == ["feature_1" ]
262334 assert response .user .id == session_constants ["USER_ID" ]
@@ -335,6 +407,7 @@ def test_refresh_success(self, session_constants, mock_user_management):
335407 "sid" : session_constants ["SESSION_ID" ],
336408 "org_id" : session_constants ["ORGANIZATION_ID" ],
337409 "role" : "admin" ,
410+ "roles" : ["admin" ],
338411 "permissions" : ["read" ],
339412 "entitlements" : ["feature_1" ],
340413 },
@@ -435,6 +508,7 @@ async def test_refresh_success(self, session_constants, mock_user_management):
435508 "sid" : session_constants ["SESSION_ID" ],
436509 "org_id" : session_constants ["ORGANIZATION_ID" ],
437510 "role" : "admin" ,
511+ "roles" : ["admin" ],
438512 "permissions" : ["read" ],
439513 "entitlements" : ["feature_1" ],
440514 },
0 commit comments