def setUp(self):
        """Generate some fake data."""
        self.app = Flask('test')
        util.init_app(self.app)
        self.app.config['CLASSIC_DATABASE_URI'] = 'sqlite:///test.db'
        self.app.config['CLASSIC_SESSION_HASH'] = 'foohash'

        with self.app.app_context():
            util.create_all()
            with util.transaction() as session:
                person = Person('en')
                net = Internet('en')
                ip_addr = net.ip_v4()
                email = person.email()
                approved = 1
                deleted = 0
                banned = 0
                first_name = person.name()
                last_name = person.surname()
                suffix_name = person.title()
                joined_date = util.epoch(
                    Datetime('en').datetime().replace(tzinfo=EASTERN))
                db_user = models.DBUser(
                    first_name=first_name,
                    last_name=last_name,
                    suffix_name=suffix_name,
                    share_first_name=1,
                    share_last_name=1,
                    email=email,
                    flag_approved=approved,
                    flag_deleted=deleted,
                    flag_banned=banned,
                    flag_edit_users=0,
                    flag_edit_system=0,
                    flag_email_verified=1,
                    share_email=8,
                    email_bouncing=0,
                    policy_class=2,  # Public user. TODO: consider admin.
                    joined_date=joined_date,
                    joined_ip_num=ip_addr,
                    joined_remote_host=ip_addr)
                session.add(db_user)

                self.user = domain.User(user_id=str(db_user.user_id),
                                        username='******',
                                        email=db_user.email,
                                        name=domain.UserFullName(
                                            forename=db_user.first_name,
                                            surname=db_user.last_name,
                                            suffix=db_user.suffix_name))
Exemple #2
0
class CSVData:
    def __init__(self):
        self.person = Person(locale='zh')
        self.address = Address(locale='zh')
        self.code = Code()
        self.business = Business(locale='zh')
        self.text = Text(locale='zh')
        self.datetime = Datetime(locale='zh')
        self.file = File()
        self.path = Path()
        self.internet = Internet()
        self.structure = Structure()

    def mime_data(self):
        # col1 = self.person.full_name()
        col1 = self.person.last_name() + self.person.first_name()
        col2 = self.address.city()
        col3 = self.address.street_name()
        col4 = self.address.calling_code()
        col5 = self.address.longitude()
        col6 = self.code.imei()
        col7 = self.business.company()
        col8 = self.text.hex_color()
        col9 = self.datetime.formatted_datetime()
        col10 = self.datetime.time()

        col11 = self.file.file_name()
        col12 = self.path.dev_dir()
        col13 = self.internet.ip_v4()
        col14 = self.internet.ip_v6()
        col15 = self.internet.home_page()
        col16 = self.internet.stock_image()
        col17 = self.internet.user_agent()
        col18 = self.internet.mac_address()
        col19 = self.person.email()
        col20 = self.person.telephone()

        col21 = self.code.issn()
        col22 = self.person.social_media_profile()
        col23 = self.structure.html()

        line = '\"{0}\", \"{1}\", \"{2}\", \"{3}\", {4}, \"{5}\", \"{6}\" , \"{7}\" , \"{8}\" , \"{9}\" , \"{10}\" , \"{11}\" , \"{12}\" , \"{13}\" , \"{14}\" , \"{15}\" , \"{16}\" , \"{17}\" , \"{18}\" , \"{19}\" , \"{20}\" , \"{21}\" , \"{22}\"\n'.format(
                col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22, col23)

        # line = "mime data"
        # print(line)
        return line
def users(count: int = 500) -> List[models.User]:
    """Generate a bunch of random users."""
    _users = []
    for i in range(count):
        locale = _get_locale()
        person = Person(locale)
        net = Internet(locale)
        ip_addr = net.ip_v4()
        _users.append(
            models.User(
                first_name=person.name(),
                last_name=person.surname(),
                suffix_name=person.title(),
                share_first_name=1,
                share_last_name=1,
                email=person.email(),
                share_email=8,
                email_bouncing=0,
                policy_class=2,  # Public user.
                joined_date=_epoch(Datetime(locale).datetime()),
                joined_ip_num=ip_addr,
                joined_remote_host=ip_addr))
    return _users
    def setUp(self):
        """Generate some fake data."""
        self.app = Flask('test')
        util.init_app(self.app)
        self.app.config['CLASSIC_DATABASE_URI'] = 'sqlite:///test.db'
        self.app.config['CLASSIC_SESSION_HASH'] = 'foohash'

        with self.app.app_context():
            util.create_all()
            with util.transaction() as session:
                person = Person('en')
                net = Internet('en')
                ip_addr = net.ip_v4()
                email = "*****@*****.**"
                approved = 1
                deleted = 0
                banned = 0
                first_name = person.name()
                last_name = person.surname()
                suffix_name = person.title()
                joined_date = util.epoch(
                    Datetime('en').datetime().replace(tzinfo=EASTERN))
                db_user = models.DBUser(
                    first_name=first_name,
                    last_name=last_name,
                    suffix_name=suffix_name,
                    share_first_name=1,
                    share_last_name=1,
                    email=email,
                    flag_approved=approved,
                    flag_deleted=deleted,
                    flag_banned=banned,
                    flag_edit_users=0,
                    flag_edit_system=0,
                    flag_email_verified=1,
                    share_email=8,
                    email_bouncing=0,
                    policy_class=2,  # Public user. TODO: consider admin.
                    joined_date=joined_date,
                    joined_ip_num=ip_addr,
                    joined_remote_host=ip_addr)
                session.add(db_user)

                self.user = domain.User(user_id=str(db_user.user_id),
                                        username='******',
                                        email=db_user.email,
                                        name=domain.UserFullName(
                                            forename=db_user.first_name,
                                            surname=db_user.last_name,
                                            suffix=db_user.suffix_name))

            ok_patterns = ['%w3.org', '%aaas.org', '%agu.org', '%ams.org']
            bad_patterns = ['%.com', '%.net', '%.biz.%']

            with util.transaction() as session:
                for pattern in ok_patterns:
                    session.add(models.DBEmailWhitelist(pattern=str(pattern)))
                for pattern in bad_patterns:
                    session.add(models.DBEmailBlacklist(pattern=str(pattern)))

                session.add(
                    models.DBEndorsementDomain(
                        endorsement_domain='test_domain',
                        endorse_all='n',
                        mods_endorse_all='n',
                        endorse_email='y',
                        papers_to_endorse=3))

                for category, definition in taxonomy.CATEGORIES_ACTIVE.items():
                    if '.' in category:
                        archive, subject_class = category.split('.', 1)
                    else:
                        archive, subject_class = category, ''
                    session.add(
                        models.DBCategory(archive=archive,
                                          subject_class=subject_class,
                                          definitive=1,
                                          active=1,
                                          endorsement_domain='test_domain'))
    def setUpClass(cls):
        """Generate some fake data."""
        cls.app = Flask('test')
        util.init_app(cls.app)
        cls.app.config['CLASSIC_DATABASE_URI'] = 'sqlite:///test.db'
        cls.app.config['CLASSIC_SESSION_HASH'] = 'foohash'
        cls.app.config['CLASSIC_SESSION_TIMEOUT'] = '36000'

        with cls.app.app_context():
            util.create_all()
            COUNT = 50

            cls.users = []

            _users = []
            _domain_users = []
            for i in range(COUNT):
                with util.transaction() as session:
                    locale = _get_locale()
                    person = Person(locale)
                    net = Internet(locale)
                    ip_addr = net.ip_v4()
                    email = person.email()
                    approved = 1 if _prob(90) else 0
                    deleted = 1 if _prob(2) else 0
                    banned = 1 if random.randint(0, 100) <= 1 else 0
                    first_name = person.name()
                    last_name = person.surname()
                    suffix_name = person.title()
                    name = (first_name, last_name, suffix_name)
                    joined_date = util.epoch(
                        Datetime(locale).datetime().replace(tzinfo=EASTERN))
                    db_user = models.DBUser(
                        first_name=first_name,
                        last_name=last_name,
                        suffix_name=suffix_name,
                        share_first_name=1,
                        share_last_name=1,
                        email=email,
                        flag_approved=approved,
                        flag_deleted=deleted,
                        flag_banned=banned,
                        flag_edit_users=0,
                        flag_edit_system=0,
                        flag_email_verified=1,
                        share_email=8,
                        email_bouncing=0,
                        policy_class=2,  # Public user. TODO: consider admin.
                        joined_date=joined_date,
                        joined_ip_num=ip_addr,
                        joined_remote_host=ip_addr)
                    session.add(db_user)

                    # Create a username.
                    username_is_valid = 1 if _prob(90) else 0
                    username = person.username()
                    db_nick = models.DBUserNickname(
                        user=db_user,
                        nickname=username,
                        flag_valid=username_is_valid,
                        flag_primary=1)

                    # Create the user's profile.
                    archive, subject_class = _random_category()
                    db_profile = models.DBProfile(
                        user=db_user,
                        country=locale,
                        affiliation=person.university(),
                        url=net.home_page(),
                        rank=random.randint(1, 5),
                        archive=archive,
                        subject_class=subject_class,
                        original_subject_classes='',
                        flag_group_math=1 if _prob(5) else 0,
                        flag_group_cs=1 if _prob(5) else 0,
                        flag_group_nlin=1 if _prob(5) else 0,
                        flag_group_q_bio=1 if _prob(5) else 0,
                        flag_group_q_fin=1 if _prob(5) else 0,
                        flag_group_stat=1 if _prob(5) else 0)

                    # Set the user's password.
                    password = person.password()
                    db_password = models.DBUserPassword(
                        user=db_user,
                        password_storage=2,
                        password_enc=util.hash_password(password))

                    # Create some endorsements.
                    archive, subject_class = _random_category()
                    net_points = 0
                    for _ in range(0, random.randint(1, 4)):
                        etype = random.choice(['auto', 'user', 'admin'])
                        point_value = random.randint(-10, 10)
                        net_points += point_value
                        if len(_users) > 0 and etype == 'auto':
                            endorser_id = random.choice(_users).user_id
                        else:
                            endorser_id = None
                        issued_when = util.epoch(
                            Datetime(locale).datetime().replace(
                                tzinfo=EASTERN))
                        session.add(
                            models.DBEndorsement(endorsee=db_user,
                                                 endorser_id=endorser_id,
                                                 archive=archive,
                                                 subject_class=subject_class,
                                                 flag_valid=1,
                                                 endorsement_type=etype,
                                                 point_value=point_value,
                                                 issued_when=issued_when))

                    session.add(db_password)
                    session.add(db_nick)
                    session.add(db_profile)
                    _users.append(db_user)
                    _domain_users.append(
                        (domain.User(user_id=str(db_user.user_id),
                                     username=db_nick.nickname,
                                     email=db_user.email,
                                     name=domain.UserFullName(
                                         forename=db_user.first_name,
                                         surname=db_user.last_name,
                                         suffix=db_user.suffix_name),
                                     verified=bool(
                                         db_user.flag_email_verified)),
                         domain.Authorizations(
                             classic=util.compute_capabilities(db_user), )))
                    session.commit()
                    # We'll use these data to run tests.
                    cls.users.append((
                        email,
                        username,
                        password,
                        name,
                        (archive, subject_class, net_points),
                        (approved, deleted, banned),
                    ))
    def test_authenticate_and_use_session(self):
        """Attempt to authenticate users and create/load auth sessions."""
        with self.app.app_context():
            for datum in self.users:
                email, username, password, name, endorsement, status = datum
                approved, deleted, banned = status

                # Approved users may log in.
                if approved and not deleted and not banned:
                    user, auths = authenticate.authenticate(email, password)
                    self.assertIsInstance(user, domain.User,
                                          "User data is returned")
                    self.assertEqual(user.email, email,
                                     "Email is set correctly")
                    self.assertEqual(user.username, username,
                                     "Username is set correctly")

                    first_name, last_name, suffix_name = name
                    self.assertEqual(user.name.forename, first_name,
                                     "Forename is set correctly")
                    self.assertEqual(user.name.surname, last_name,
                                     "Surname is set correctly")
                    self.assertEqual(user.name.suffix, suffix_name,
                                     "Suffix is set correctly")
                    self.assertIsInstance(auths, domain.Authorizations,
                                          "Authorizations data are returned")
                    if endorsement[2] > 0:
                        self.assertIn(
                            domain.Category(archive=endorsement[0],
                                            subject=endorsement[1]),
                            auths.endorsements,
                            "Endorsements are included in authorizations")

                # Banned or deleted users may not log in.
                elif deleted or banned:
                    with self.assertRaises(exceptions.AuthenticationFailed):
                        authenticate.authenticate(email, password)
                    continue

                # Users who are not approved may not log in.
                elif not approved:
                    with self.assertRaises(exceptions.AuthenticationFailed):
                        authenticate.authenticate(email, password)
                    continue

                locale = _get_locale()
                net = Internet(locale)
                ip = net.ip_v4()
                session = sessions.create(auths, ip, ip, user=user)
                cookie = sessions.generate_cookie(session)

                session_loaded = sessions.load(cookie)
                self.assertEqual(session.user, session_loaded.user,
                                 "Loaded the correct user")
                self.assertEqual(session.session_id, session_loaded.session_id,
                                 "Loaded the correct session")

                # Invalidate 10% of the sessions, and try again.
                if _prob(10):
                    sessions.invalidate(cookie)
                    with self.assertRaises(exceptions.SessionExpired):
                        sessions.load(cookie)