Coverage for app / dependency / org / context.py: 53%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1from sqlalchemy.orm import Session 

2from fastapi import Depends, Header 

3from uuid import UUID 

4from app.dependency.base import get_db 

5from app.dependency.user import get_current_user 

6from app.db.postgres.models import Organisation, User 

7from app.common.exceptions import OrgContextInvalidException, OrgUserInvalidException 

8 

9 

10def get_current_organisation( 

11 *, 

12 db: Session = Depends(get_db), 

13 user: User = Depends(get_current_user), 

14 x_organisation_id: UUID = Header(alias="X-Organisation-ID"), 

15) -> Organisation: 

16 """ 

17 Resolve the active organisation and ensure the user is a member. 

18 """ 

19 

20 organisation: Organisation | None = ( 

21 db.query(Organisation) 

22 .filter(Organisation.id == x_organisation_id) 

23 .one_or_none() 

24 ) 

25 

26 if organisation is None: 

27 raise OrgContextInvalidException 

28 

29 is_member = any(m.organisation_id == organisation.id for m in user.organisations) 

30 

31 if not is_member: 

32 raise OrgUserInvalidException 

33 

34 return organisation