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

46 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-08 19:50 +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.oauth_config.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 

64class FailureToProofEvent(OAuthEvent): 

65 """Analytics event representing a failure to proof during identitity verification.""" 

66 

67 def __init__(self, request): 

68 super().__init__(request, "failure to proof") 

69 

70 

71def error(request, message, operation): 

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

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

74 

75 

76def started_sign_in(request): 

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

78 core.send_event(StartedSignInEvent(request)) 

79 

80 

81def canceled_sign_in(request): 

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

83 core.send_event(CanceledSignInEvent(request)) 

84 

85 

86def finished_sign_in(request, error=None): 

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

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

89 

90 

91def started_sign_out(request): 

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

93 core.send_event(StartedSignOutEvent(request)) 

94 

95 

96def finished_sign_out(request): 

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

98 core.send_event(FinishedSignOutEvent(request)) 

99 

100 

101def failure_to_proof(request): 

102 """Send the "failure to proof" analytics event.""" 

103 core.send_event(FailureToProofEvent(request))