Coverage for benefits/oauth/analytics.py: 78%

41 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-19 16:31 +0000

1""" 

2The oauth application: analytics implementation. 

3""" 

4 

5from benefits.core import analytics as core, session 

6 

7 

8class OAuthEvent(core.Event): 

9 """Base OAuth analytics event.""" 

10 

11 def __init__(self, request, event_type): 

12 super().__init__(request, event_type) 

13 flow = session.flow(request) 

14 if flow and flow.uses_claims_verification: 

15 self.update_event_properties(claims_provider=flow.claims_provider.client_name) 

16 

17 

18class OAuthErrorEvent(OAuthEvent): 

19 """Analytics event representing an error using an OAuth client.""" 

20 

21 def __init__(self, request, message, operation): 

22 super().__init__(request, "oauth error") 

23 self.update_event_properties(message=message, operation=operation) 

24 

25 

26class StartedSignInEvent(OAuthEvent): 

27 """Analytics event representing the beginning of the OAuth sign in flow.""" 

28 

29 def __init__(self, request): 

30 super().__init__(request, "started sign in") 

31 

32 

33class CanceledSignInEvent(OAuthEvent): 

34 """Analytics event representing the canceling of application sign in.""" 

35 

36 def __init__(self, request): 

37 super().__init__(request, "canceled sign in") 

38 

39 

40class FinishedSignInEvent(OAuthEvent): 

41 """Analytics event representing the end of the OAuth sign in flow.""" 

42 

43 def __init__(self, request, error=None): 

44 super().__init__(request, "finished sign in") 

45 if error is not None: 

46 self.update_event_properties(error_code=error) 

47 

48 

49class StartedSignOutEvent(OAuthEvent): 

50 """Analytics event representing the beginning of application sign out.""" 

51 

52 def __init__(self, request): 

53 super().__init__(request, "started sign out") 

54 

55 

56class FinishedSignOutEvent(OAuthEvent): 

57 """Analytics event representing the end of application sign out.""" 

58 

59 def __init__(self, request): 

60 super().__init__(request, "finished sign out") 

61 self.update_event_properties(origin=session.origin(request)) 

62 

63 

64def error(request, message, operation): 

65 """Send the "oauth error" analytics event, specifying the message and operation""" 

66 core.send_event(OAuthErrorEvent(request, message, operation)) 

67 

68 

69def started_sign_in(request): 

70 """Send the "started sign in" analytics event.""" 

71 core.send_event(StartedSignInEvent(request)) 

72 

73 

74def canceled_sign_in(request): 

75 """Send the "canceled sign in" analytics event.""" 

76 core.send_event(CanceledSignInEvent(request)) 

77 

78 

79def finished_sign_in(request, error=None): 

80 """Send the "finished sign in" analytics event.""" 

81 core.send_event(FinishedSignInEvent(request, error)) 

82 

83 

84def started_sign_out(request): 

85 """Send the "started signed out" analytics event.""" 

86 core.send_event(StartedSignOutEvent(request)) 

87 

88 

89def finished_sign_out(request): 

90 """Send the "finished sign out" analytics event.""" 

91 core.send_event(FinishedSignOutEvent(request))