Example #1
0
    def test_receiver_called_after_login(self, action_handler):
        """
        Test that hooks_handler is called the correct information after sending
        SESSION_LOGIN_COMPLETED event.
        """
        metadata = EventsMetadata(
            event_type="org.openedx.learning.auth.session.login.completed.v1",
            minorversion=0,
        )
        expected_metadata_subset = {
            "event_type": metadata.event_type,
            "minorversion": metadata.minorversion,
            "source": metadata.source,
            "sourcehost": metadata.sourcehost,
            "sourcelib": list(metadata.sourcelib),
        }
        action_handler.return_value.__name__ = "receiver"
        action_handler.return_value.__module__ = "receiver_module"
        SESSION_LOGIN_COMPLETED.connect(
            hooks_handler, dispatch_uid="eox-hooks:post_session_login")

        SESSION_LOGIN_COMPLETED.send_event(user=self.user, )

        self.assertDictContainsSubset(
            expected_metadata_subset,
            attr.asdict(action_handler.call_args.kwargs.get("metadata")),
        )
        self.assertEqual(self.user,
                         action_handler.call_args.kwargs.get("user"))
Example #2
0
def _handle_successful_authentication_and_login(user, request):
    """
    Handles clearing the failed login counter, login tracking, and setting session timeout.
    """
    if LoginFailures.is_feature_enabled():
        LoginFailures.clear_lockout_counter(user)

    _track_user_login(user, request)

    try:
        django_login(request, user)
        request.session.set_expiry(604800 * 4)
        log.debug("Setting user session expiry to 4 weeks")

        # .. event_implemented_name: SESSION_LOGIN_COMPLETED
        SESSION_LOGIN_COMPLETED.send_event(user=UserData(
            pii=UserPersonalData(
                username=user.username,
                email=user.email,
                name=user.profile.name,
            ),
            id=user.id,
            is_active=user.is_active,
        ), )
    except Exception as exc:
        AUDIT_LOG.critical(
            "Login failed - Could not create session. Is memcached running?")
        log.critical(
            "Login failed - Could not create session. Is memcached running?")
        log.exception(exc)
        raise
Example #3
0
    def test_send_login_event(self):
        """
        Test whether the student login event is sent after the user's
        login process.

        Expected result:
            - SESSION_LOGIN_COMPLETED is sent and received by the mocked receiver.
            - The arguments that the receiver gets are the arguments sent by the event
            except the metadata generated on the fly.
        """
        event_receiver = Mock(side_effect=self._event_receiver_side_effect)
        SESSION_LOGIN_COMPLETED.connect(event_receiver)
        data = {
            "email": "*****@*****.**",
            "password": "******",
        }

        self.client.post(self.url, data)

        user = User.objects.get(username=self.user.username)
        self.assertTrue(self.receiver_called)
        self.assertDictContainsSubset(
            {
                "signal":
                SESSION_LOGIN_COMPLETED,
                "sender":
                None,
                "user":
                UserData(
                    pii=UserPersonalData(
                        username=user.username,
                        email=user.email,
                        name=user.profile.name,
                    ),
                    id=user.id,
                    is_active=user.is_active,
                ),
            }, event_receiver.call_args.kwargs)