2525_INTERNAL_ORG_ID = "org_internal_123"
2626_OTHER_ORG_ID = "org_other_456"
2727_USER_NOT_AUTHENTICATED = "User is not authenticated"
28+ _USER_SUB = "auth0|x"
29+ _USER_EMAIL = "x@x.com"
2830
2931
3032@pytest .mark .unit
@@ -113,7 +115,7 @@ async def test_get_user_returns_none_for_expired_session(self) -> None:
113115 cookie = "fake-cookie"
114116
115117 fake_client = MagicMock ()
116- expired_user = {"sub" : "auth0|x" , "email" : "x@x.com" , "exp" : int (time .time ()) - 3600 }
118+ expired_user = {"sub" : _USER_SUB , "email" : _USER_EMAIL , "exp" : int (time .time ()) - 3600 }
117119 fake_client .require_session = AsyncMock (return_value = {"user" : expired_user })
118120
119121 with (
@@ -155,7 +157,7 @@ async def test_get_user_returns_user_for_valid_session(self) -> None:
155157 """get_user returns the user dict when the session is valid and not expired."""
156158 request = MagicMock ()
157159 cookie = "fake-cookie"
158- user = {"sub" : "auth0|x" , "email" : "x@x.com" , "exp" : int (time .time ()) + 3600 }
160+ user = {"sub" : _USER_SUB , "email" : _USER_EMAIL , "exp" : int (time .time ()) + 3600 }
159161 fake_client = MagicMock ()
160162 fake_client .require_session = AsyncMock (return_value = {"user" : user })
161163
@@ -184,7 +186,7 @@ async def test_unauthenticated_user_raises_forbidden_error(self) -> None:
184186 async def test_authenticated_user_passes (self ) -> None :
185187 """require_authenticated returns None without raising when user is authenticated."""
186188 request = MagicMock ()
187- user = {"sub" : "auth0|x" , "email" : "x@x.com" }
189+ user = {"sub" : _USER_SUB , "email" : _USER_EMAIL }
188190 with patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )):
189191 result = await require_authenticated (request , None )
190192 assert result is None
@@ -205,7 +207,7 @@ async def test_wrong_role_raises_forbidden_error(self, monkeypatch: pytest.Monke
205207 """require_admin raises ForbiddenError when user has a non-admin role."""
206208 monkeypatch .delenv ("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM" , raising = False )
207209 request = MagicMock ()
208- user = {"sub" : "auth0|x" , DEFAULT_AUTH0_ROLE_CLAIM : "viewer" }
210+ user = {"sub" : _USER_SUB , DEFAULT_AUTH0_ROLE_CLAIM : "viewer" }
209211 with (
210212 patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )),
211213 pytest .raises (ForbiddenError , match = "does not match required role" ),
@@ -216,7 +218,7 @@ async def test_admin_role_passes(self, monkeypatch: pytest.MonkeyPatch) -> None:
216218 """require_admin returns None without raising when user has the admin role."""
217219 monkeypatch .delenv ("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM" , raising = False )
218220 request = MagicMock ()
219- user = {"sub" : "auth0|x" , DEFAULT_AUTH0_ROLE_CLAIM : AUTH0_ROLE_ADMIN }
221+ user = {"sub" : _USER_SUB , DEFAULT_AUTH0_ROLE_CLAIM : AUTH0_ROLE_ADMIN }
220222 with patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )):
221223 result = await require_admin (request , None )
222224 assert result is None
@@ -240,7 +242,7 @@ async def test_wrong_org_raises_forbidden_error(self, monkeypatch: pytest.Monkey
240242 """require_internal raises ForbiddenError when user belongs to a different org."""
241243 monkeypatch .setenv ("FOUNDRY_AUTH_INTERNAL_ORG_ID" , _INTERNAL_ORG_ID )
242244 request = MagicMock ()
243- user = {"sub" : "auth0|x" , "org_id" : _OTHER_ORG_ID }
245+ user = {"sub" : _USER_SUB , "org_id" : _OTHER_ORG_ID }
244246 with (
245247 patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )),
246248 pytest .raises (ForbiddenError , match = "not a member of the internal organization" ),
@@ -251,7 +253,7 @@ async def test_internal_org_member_passes(self, monkeypatch: pytest.MonkeyPatch)
251253 """require_internal returns None without raising when user is in the internal org."""
252254 monkeypatch .setenv ("FOUNDRY_AUTH_INTERNAL_ORG_ID" , _INTERNAL_ORG_ID )
253255 request = MagicMock ()
254- user = {"sub" : "auth0|x" , "org_id" : _INTERNAL_ORG_ID }
256+ user = {"sub" : _USER_SUB , "org_id" : _INTERNAL_ORG_ID }
255257 with patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )):
256258 result = await require_internal (request , None )
257259 assert result is None
@@ -275,7 +277,7 @@ async def test_wrong_org_raises_forbidden_error(self, monkeypatch: pytest.Monkey
275277 """require_internal_admin raises ForbiddenError when user belongs to a different org."""
276278 monkeypatch .setenv ("FOUNDRY_AUTH_INTERNAL_ORG_ID" , _INTERNAL_ORG_ID )
277279 request = MagicMock ()
278- user = {"sub" : "auth0|x" , "org_id" : _OTHER_ORG_ID }
280+ user = {"sub" : _USER_SUB , "org_id" : _OTHER_ORG_ID }
279281 with (
280282 patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )),
281283 pytest .raises (ForbiddenError , match = "not a member of the internal organization" ),
@@ -287,7 +289,7 @@ async def test_correct_org_wrong_role_raises_forbidden_error(self, monkeypatch:
287289 monkeypatch .setenv ("FOUNDRY_AUTH_INTERNAL_ORG_ID" , _INTERNAL_ORG_ID )
288290 monkeypatch .delenv ("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM" , raising = False )
289291 request = MagicMock ()
290- user = {"sub" : "auth0|x" , "org_id" : _INTERNAL_ORG_ID , DEFAULT_AUTH0_ROLE_CLAIM : "viewer" }
292+ user = {"sub" : _USER_SUB , "org_id" : _INTERNAL_ORG_ID , DEFAULT_AUTH0_ROLE_CLAIM : "viewer" }
291293 with (
292294 patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )),
293295 pytest .raises (ForbiddenError , match = "does not match required role" ),
@@ -299,7 +301,7 @@ async def test_internal_admin_passes(self, monkeypatch: pytest.MonkeyPatch) -> N
299301 monkeypatch .setenv ("FOUNDRY_AUTH_INTERNAL_ORG_ID" , _INTERNAL_ORG_ID )
300302 monkeypatch .delenv ("FOUNDRY_AUTH_AUTH0_ROLE_CLAIM" , raising = False )
301303 request = MagicMock ()
302- user = {"sub" : "auth0|x" , "org_id" : _INTERNAL_ORG_ID , DEFAULT_AUTH0_ROLE_CLAIM : AUTH0_ROLE_ADMIN }
304+ user = {"sub" : _USER_SUB , "org_id" : _INTERNAL_ORG_ID , DEFAULT_AUTH0_ROLE_CLAIM : AUTH0_ROLE_ADMIN }
303305 with patch (_PATCH_GET_USER , new = AsyncMock (return_value = user )):
304306 result = await require_internal_admin (request , None )
305307 assert result is None
0 commit comments