Coverage for apps/kwai-api/src/kwai_api/v1/trainings/security_dependencies.py: 90%

10 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for defining security dependencies for the trainings API.""" 

2 

3from typing import Annotated 

4 

5from fastapi import Depends, HTTPException 

6from kwai_bc_club.domain.club_coach import ClubCoachEntity 

7from kwai_bc_identity.users.user import UserEntity 

8from starlette import status 

9 

10from kwai_api.dependencies import get_current_user 

11from kwai_api.security_dependencies import get_coach 

12 

13 

14def check_permission( 

15 user: Annotated[UserEntity, Depends(get_current_user)], 

16 coach: Annotated[ClubCoachEntity, Depends(get_coach)], 

17) -> None: 

18 """Dependency that checks if the user is authorized. 

19 

20 The user should be an administrator or a coach. 

21 

22 Raises: 

23 HttpException: Raised when the user is not allowed to access this endpoint (http code 403). 

24 """ 

25 if not user.admin and coach is None: 

26 raise HTTPException( 

27 status_code=status.HTTP_403_FORBIDDEN, 

28 detail="You must be an administrator or a coach.", 

29 )