def create_sample_data(self):
        """ Add some data """
        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 email_confirmed_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        role_res1 = RoleEntity.create(name=ROLE_RESEARCHER_ONE, description='')
        role_res2 = RoleEntity.create(name=ROLE_RESEARCHER_TWO, description='')
        user.roles.extend([role_admin, role_tech, role_res1, role_res2])

        # == Create subject
        subject = SubjectEntity.create(
            redcap_id="001",
            added_at=added_date,
            last_checked_at=added_date,
            was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 day_offset=1,
                                 added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1',
                                  redcap_event='Event 2',
                                  day_offset=2.3,
                                  added_at=added_date)

        self.assertIsNotNone(evt.id)
        self.assertIsNotNone(evt2.id)

        files = [
            {'name': 'a.png', 'size': '123', 'event': evt.id},
            {'name': 'b.png', 'size': '1234', 'event': evt.id},
            {'name': 'c.png', 'size': '12345', 'event': evt2.id},
            {'name': 'd.png', 'size': '123456', 'event': evt2.id},
            {'name': 'e.png', 'size': '1234567', 'event': evt2.id},
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            self.assertIsNotNone(subject_file.id)
Пример #2
0
 def create_user_agents(self):
     user_agent_string = ""
     hash = utils.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")
     self.assertIsNotNone(user_agent)
 def create_user_agents(self):
     user_agent_string = ""
     hash = utils.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")
     self.assertIsNotNone(user_agent)
    def test_subject(self):
        """ verify save and find operations """

        added_date = datetime.today()
        subject = SubjectEntity.create(
            redcap_id="001",
            added_at=added_date,
            last_checked_at=added_date,
            was_deleted=False)

        self.assertEquals(1, subject.id)
        self.assertEquals("001", subject.redcap_id)
        self.assertIsNotNone(subject.added_at)
        self.assertIsNotNone(subject.last_checked_at)
        self.assertFalse(subject.was_deleted)
        self.assertEqual(subject.added_at, subject.last_checked_at)

        subjects = SubjectEntity.query.all()
        self.assertEquals(1, len(subjects))

        # test usage of CRUDMixin.get_by_id() and create()
        subject = SubjectEntity.update(subject, redcap_id="002")
        self.assertEquals("002", subject.redcap_id)

        evt = EventEntity.create(redcap_arm='Arm 1', redcap_event='Event 1',
                                 added_at=added_date)

        fdata = {'name': 'a.png', 'size': '1MB', 'event': evt.id}
        user = UserEntity.create(email="*****@*****.**",
                                 first="",
                                 last="",
                                 minitial="",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=added_date)

        subject_file = SubjectFileEntity.create(
            subject_id=subject.id,
            event_id=fdata['event'],
            file_name=fdata['name'],
            file_check_sum=utils.compute_text_md5(fdata['name']),
            file_size=fdata['size'],
            uploaded_at=added_date,
            user_id=user.get_id())
        self.assertIsNotNone(subject_file.id)

        actual_count = SubjectFileEntity.query.count()
        self.assertEqual(1, actual_count)

        actual_count = SubjectFileEntity.query.filter_by(
            event_id=1).count()
        self.assertEqual(1, actual_count)

        sfile = SubjectFileEntity.query.first()
        self.assertEqual(user.id, sfile.user_id)
    def test_subject(self):
        """ verify save and find operations """

        added_date = datetime.today()
        subject = SubjectEntity.create(redcap_id="001",
                                       added_at=added_date,
                                       last_checked_at=added_date,
                                       was_deleted=False)

        self.assertEquals(1, subject.id)
        self.assertEquals("001", subject.redcap_id)
        self.assertIsNotNone(subject.added_at)
        self.assertIsNotNone(subject.last_checked_at)
        self.assertFalse(subject.was_deleted)
        self.assertEqual(subject.added_at, subject.last_checked_at)

        subjects = SubjectEntity.query.all()
        self.assertEquals(1, len(subjects))

        # test usage of CRUDMixin.get_by_id() and create()
        subject = SubjectEntity.update(subject, redcap_id="002")
        self.assertEquals("002", subject.redcap_id)

        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 added_at=added_date)

        fdata = {'name': 'a.png', 'size': '1MB', 'event': evt.id}
        user = UserEntity.create(email="*****@*****.**",
                                 first="",
                                 last="",
                                 minitial="",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=added_date)

        subject_file = SubjectFileEntity.create(
            subject_id=subject.id,
            event_id=fdata['event'],
            file_name=fdata['name'],
            file_check_sum=utils.compute_text_md5(fdata['name']),
            file_size=fdata['size'],
            uploaded_at=added_date,
            user_id=user.get_id())
        self.assertIsNotNone(subject_file.id)

        actual_count = SubjectFileEntity.query.count()
        self.assertEqual(1, actual_count)

        actual_count = SubjectFileEntity.query.filter_by(event_id=1).count()
        self.assertEqual(1, actual_count)

        sfile = SubjectFileEntity.query.first()
        self.assertEqual(user.id, sfile.user_id)
    def create_sample_data(self):
        """ Add some data """

        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        user.roles.extend([role_admin, role_tech])

        # == Create subject
        subject = SubjectEntity.create(
            redcap_id="001",
            added_at=added_date,
            last_checked_at=added_date,
            was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1', redcap_event='Event 1',
                          added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1', redcap_event='Event 2',
                           added_at=added_date)

        assert evt.id is not None

        files = [
            {'name': 'a.png', 'size': '1MB', 'event': evt.id},
            {'name': 'b.png', 'size': '2MB', 'event': evt.id},
            {'name': 'c.png', 'size': '3MB', 'event': evt2.id},
            {'name': 'd.png', 'size': '4MB', 'event': evt2.id},
            {'name': 'e.png', 'size': '5MB', 'event': evt2.id},
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            app.logger.debug("Init test case with: {}".format(subject_file))
Пример #7
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)
Пример #8
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)
Пример #9
0
def get_user_agent():
    """Find an existing user agent or insert a new one"""
    # The raw user agent string received from the browser
    uag = request.user_agent
    hash = utils.compute_text_md5(uag.string)

    # The entity representing the user agent
    user_agent = UserAgentEntity.get_by_hash(hash)

    if user_agent is None:
        platform = uag.platform if uag.platform is not None else ''
        browser = uag.browser if uag.browser is not None else ''
        version = uag.version if uag.version is not None else ''
        language = uag.language if uag.language is not None else ''
        user_agent = UserAgentEntity.create(user_agent=uag.string,
                                            hash=hash,
                                            platform=platform,
                                            browser=browser,
                                            version=version,
                                            language=language)

    # app.logger.debug(user_agent)
    return user_agent
Пример #10
0
 def test_compute_text_md5(self):
     """ verify md5 generator """
     text = 'text'
     self.assertEquals('1cb251ec0d568de6a929b520c4aed8d1',
                       utils.compute_text_md5(text))
Пример #11
0
    def create_sample_data(self):
        """ Add some data """
        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 email_confirmed_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        role_res1 = RoleEntity.create(name=ROLE_RESEARCHER_ONE, description='')
        role_res2 = RoleEntity.create(name=ROLE_RESEARCHER_TWO, description='')
        user.roles.extend([role_admin, role_tech, role_res1, role_res2])

        # == Create subject
        subject = SubjectEntity.create(redcap_id="001",
                                       added_at=added_date,
                                       last_checked_at=added_date,
                                       was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 day_offset=1,
                                 added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1',
                                  redcap_event='Event 2',
                                  day_offset=2.3,
                                  added_at=added_date)

        self.assertIsNotNone(evt.id)
        self.assertIsNotNone(evt2.id)

        files = [
            {
                'name': 'a.png',
                'size': '123',
                'event': evt.id
            },
            {
                'name': 'b.png',
                'size': '1234',
                'event': evt.id
            },
            {
                'name': 'c.png',
                'size': '12345',
                'event': evt2.id
            },
            {
                'name': 'd.png',
                'size': '123456',
                'event': evt2.id
            },
            {
                'name': 'e.png',
                'size': '1234567',
                'event': evt2.id
            },
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            self.assertIsNotNone(subject_file.id)
Пример #12
0
 def test_compute_text_md5(self):
     """ verify md5 generator """
     text = 'text'
     self.assertEquals('1cb251ec0d568de6a929b520c4aed8d1',
                       utils.compute_text_md5(text))
    def create_sample_data(self):
        """ Add some data """

        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        user.roles.extend([role_admin, role_tech])

        # == Create subject
        subject = SubjectEntity.create(redcap_id="001",
                                       added_at=added_date,
                                       last_checked_at=added_date,
                                       was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1',
                                  redcap_event='Event 2',
                                  added_at=added_date)

        assert evt.id is not None

        files = [
            {
                'name': 'a.png',
                'size': '1MB',
                'event': evt.id
            },
            {
                'name': 'b.png',
                'size': '2MB',
                'event': evt.id
            },
            {
                'name': 'c.png',
                'size': '3MB',
                'event': evt2.id
            },
            {
                'name': 'd.png',
                'size': '4MB',
                'event': evt2.id
            },
            {
                'name': 'e.png',
                'size': '5MB',
                'event': evt2.id
            },
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            app.logger.debug("Init test case with: {}".format(subject_file))