Exemple #1
0
def check_session_id():
    """
    Generate a UUID and store it in the session
    as well as in the WebSession table.
    """
    # TODO: Create UserAgentEntity and populate
    user_agent = get_user_agent()

    if 'uuid' not in session:
        session['uuid'] = str(uuid.uuid4())
        WebSessionEntity.create(session_id=session['uuid'],
                                user_id=current_user.get_id(),
                                ip=request.remote_addr,
                                date_time=datetime.datetime.now(),
                                user_agent=user_agent)
        return
    if current_user.is_authenticated():
        # update the user_id on the first request after login is completed
        session_id = session['uuid']
        web_session = WebSessionEntity.get_by_session_id(session_id)
        if web_session is not None:
            web_session = WebSessionEntity.update(
                web_session,
                user_id=current_user.get_id())
        else:
            app.logger.error("No row found for sess_id: {}".format(session_id))
    def test_log_creation(self):
        """ Test CRUD operations """

        # LogType
        log_login = LogTypeEntity.create(type=LOG_TYPE_LOGIN, description='')
        log_logout = LogTypeEntity.create(type=LOG_TYPE_LOGOUT, description='')
        self.assertEquals(1, log_login.id)
        self.assertEquals(2, log_logout.id)

        # UserAgent
        user_agent_string = "Long text..."
        hash = compute_text_md5(user_agent_string)
        user_agent = UserAgentEntity.create(user_agent=user_agent_string,
                                            hash=hash,
                                            platform="Linux",
                                            browser="Firefox",
                                            version="latest",
                                            language="EN-US")
        # print(user_agent)
        self.assertEquals(1, user_agent.id)
        self.assertEquals("467ffa17419afeffe09bb98af4828a30", user_agent.hash)
        self.assertEquals("Linux", user_agent.platform)
        self.assertEquals("latest", user_agent.version)
        self.assertEquals("EN-US", user_agent.language)

        # WebSession
        web_session = WebSessionEntity.create(user_agent_id=user_agent.id)
        self.assertEquals(1, web_session.id)
        self.assertEquals(user_agent, web_session.user_agent)
        # print(web_session.user_agent)

        # Log
        log = LogEntity.create(type_id=log_login.id,
                               web_session_id=web_session.id,
                               date_time=datetime.now(),
                               details='just a test')
        log2 = LogEntity.create(type_id=log_logout.id,
                                web_session_id=web_session.id,
                                date_time=datetime.now(),
                                details='just a test')

        self.assertEquals(1, log.id)
        self.assertEquals(2, log2.id)
        self.assertEquals(LOG_TYPE_LOGIN, log.log_type.type)
        self.assertEquals(LOG_TYPE_LOGOUT, log2.log_type.type)
Exemple #3
0
    def test_log_creation(self):
        """ Test CRUD operations """

        # LogType
        log_login = LogTypeEntity.create(type=LOG_TYPE_LOGIN, description='')
        log_logout = LogTypeEntity.create(type=LOG_TYPE_LOGOUT, description='')
        self.assertEquals(1, log_login.id)
        self.assertEquals(2, log_logout.id)

        # UserAgent
        user_agent_string = "Long text..."
        hash = compute_text_md5(user_agent_string)
        user_agent = UserAgentEntity.create(user_agent=user_agent_string,
                                            hash=hash,
                                            platform="Linux",
                                            browser="Firefox",
                                            version="latest",
                                            language="EN-US")
        # print(user_agent)
        self.assertEquals(1, user_agent.id)
        self.assertEquals("467ffa17419afeffe09bb98af4828a30", user_agent.hash)
        self.assertEquals("Linux", user_agent.platform)
        self.assertEquals("latest", user_agent.version)
        self.assertEquals("EN-US", user_agent.language)

        # WebSession
        web_session = WebSessionEntity.create(user_agent_id=user_agent.id)
        self.assertEquals(1, web_session.id)
        self.assertEquals(user_agent, web_session.user_agent)
        # print(web_session.user_agent)

        # Log
        log = LogEntity.create(type_id=log_login.id,
                               web_session_id=web_session.id,
                               date_time=datetime.now(),
                               details='just a test')
        log2 = LogEntity.create(type_id=log_logout.id,
                                web_session_id=web_session.id,
                                date_time=datetime.now(),
                                details='just a test')

        self.assertEquals(1, log.id)
        self.assertEquals(2, log2.id)
        self.assertEquals(LOG_TYPE_LOGIN, log.log_type.type)
        self.assertEquals(LOG_TYPE_LOGOUT, log2.log_type.type)
Exemple #4
0
    def _log(log_type, session_id, details=''):
        """ Helper for logging """
        logt = LogTypeEntity.query.filter_by(type=log_type).first()
        if logt is None:
            app.logger.error("Developer error. Invalid log type: {}"
                             .format(log_type))
            return

        web_session = WebSessionEntity.get_by_session_id(session_id)
        if web_session is None:
            app.logger.error("Developer error. Invalid session id: {}"
                             .format(session_id))
            return

        LogEntity.create(log_type=logt,
                         date_time=datetime.datetime.now(),
                         details=details,
                         web_session=web_session)