Coverage for apps/comments_views/journal/middleware.py: 90%

16 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-02-28 09:09 +0000

1from django.contrib.auth.models import AnonymousUser 

2from django.http import HttpRequest 

3 

4from .models import OIDCUser 

5 

6 

7class OIDCMiddleware: 

8 """ 

9 This middleware copies the user data from the session to the User object 

10 for OIDC users. 

11 If the user data is not present in the session, we replace the user object with 

12 an AnonymousUser instance. 

13 """ 

14 

15 def __init__(self, get_response): 

16 self.get_response = get_response 

17 

18 def __call__(self, request: HttpRequest): 

19 assert hasattr( 

20 request, "session" 

21 ), "The custom OIDCMiddleware requires session middleware to be installed." 

22 assert hasattr(request, "user"), ( 

23 "The custom OIDCMiddleware requires authentication middleware " "to be installed." 

24 ) 

25 if isinstance(request.user, OIDCUser): 

26 user_data = request.session.get("oidc_user_data") 

27 if user_data: 27 ↛ 30line 27 didn't jump to line 30, because the condition on line 27 was never false

28 request.user.populate_fields(user_data) 

29 else: 

30 request.user = AnonymousUser() 

31 response = self.get_response(request) 

32 

33 return response