Esempio n. 1
0
File: noi1.py Progetto: tekd/noi2
def add_user_to_db(user, password=''):
    if not password:
        password = generate_random_password()
    timestamp = datetime.datetime.strptime(user['timestamp'].split('.')[0],
                                           "%Y-%m-%dT%H:%M:%S")
    u = User(first_name=user['first_name'],
             last_name=user['last_name'],
             email=user['email'],
             organization=user['org'],
             projects=user['projects'],
             position=user['title'],
             city=user['city'],
             created_at=timestamp,
             updated_at=timestamp,
             password=password,
             active=True)
    if user['org_type']:
        if user['org_type'] in app.ORG_TYPES:
            u.organization_type = user['org_type']
        else:
            warn("invalid org_type: %s" % user['org_type'])
    if user['domains']:
        for domain in user['domains']:
            if domain in current_app.config['DOMAINS']:
                u.expertise_domains.append(UserExpertiseDomain(name=domain))
            else:
                warn("invalid domain: %s" % domain)
    if user['country_code']:
        try:
            u.country = Country(user['country_code'])
        except ValueError, e:
            warn(str(e))
Esempio n. 2
0
    def test_parameter_processing(self):
        user = self.User(country=Country(u'FI'))

        self.session.add(user)
        self.session.commit()

        user = self.session.query(self.User).first()
        assert user.country.name == u'Finland'
Esempio n. 3
0
def TestInput(data):
    if len(data) < 10:
        pass

    fdp = atheris.FuzzedDataProvider(data)

    metadata = MetaData()
    fuzz_table = Table(
        'fuzz_table', metadata,
        Column('id', Integer, Sequence('id_seq'), primary_key=True),
        Column('Col1', String), Column('Col2', ArrowType),
        Column('Col3', ChoiceType([(u'c1', u'Choice 1'),
                                   (u'c2', u'Choice 2')])),
        Column('Col4', ColorType), Column('Col5', CountryType),
        Column('Col6', EmailType), Column('Col7', JSONType),
        Column('Col8', IPAddressType), Column('Col9', ScalarListType(int)),
        Column('Col10', URLType), Column('Col11', UUIDType(binary=False)),
        Column('Col12', WeekDaysType))

    engine = create_engine('sqlite:///fuzz.db')
    metadata.create_all(engine)
    try:
        with engine.connect() as conn:
            conn.execute(text(fdp.ConsumeString(100)))
            ins = fuzz_table.insert().values(
                Col1=fdp.ConsumeString(100),
                Col2=utcnow(),
                Col3=u'c1' if fdp.ConsumeBool() else u'c2',
                Col4=Color("#{:02x}{:02x}{:02x}".format(
                    fdp.ConsumeIntInRange(0,
                                          255), fdp.ConsumeIntInRange(0, 255),
                    fdp.ConsumeIntInRange(0, 255))),
                Col5=Country('US'),
                Col6=fdp.ConsumeString(20),
                Col7={
                    fdp.ConsumeString(2): fdp.ConsumeString(10),
                    fdp.ConsumeString(2): fdp.ConsumeString(10),
                    fdp.ConsumeString(2): fdp.ConsumeString(10)
                },
                Col8="%d.%d.%d.%d" %
                (fdp.ConsumeIntInRange(0, 255), fdp.ConsumeIntInRange(0, 255),
                 fdp.ConsumeIntInRange(0, 255), fdp.ConsumeIntInRange(0, 255)),
                Col9=[fdp.ConsumeInt(8),
                      fdp.ConsumeInt(8),
                      fdp.ConsumeInt(8)],
                Col10=fdp.ConsumeUnicode(20),
                Col11=uuid4(),
                Col12=WeekDays("{0:07b}".format(fdp.ConsumeIntInRange(0, 31))))
            ins.compile()
            conn.execute(ins)
    except (SQLAlchemyError, UnicodeEncodeError) as e:
        pass
    except ValueError as e:
        if "the query contains a null character" not in str(e):
            raise e
Esempio n. 4
0
def update_user_fields_from_profile(user, info):
    location = info.get('location')
    if location:
        if 'name' in location and not user.city:
            user.city = location['name']
        if 'country' in location and 'code' in location['country']:
            country_code = location['country']['code'].upper()
            try:
                user.country = Country(country_code)
            except ValueError:
                pass

    positions = info.get('positions')
    if positions and len(positions.get('values', [])) >= 1:
        position = positions['values'][0]
        org = position.get('company') and position['company'].get('name')
        if org and not user.organization:
            user.organization = org
        if position.get('title') and not user.position:
            user.position = position['title']

    if info.get('headline') and not user.position:
        user.position = info['headline']
Esempio n. 5
0
 def country_only(self):
     user = models.User()
     user.country = Country('US')
     eq_(user.full_location, 'United States')
Esempio n. 6
0
 def test_city_and_country(self):
     user = models.User()
     user.city = 'New York'
     user.country = Country('US')
     eq_(user.full_location, 'New York, United States')
Esempio n. 7
0
 def test_country_is_zz(self):
     user = models.User()
     user.country = Country('ZZ')
     eq_(user.full_location, '')
Esempio n. 8
0
 def test_str(self):
     country = Country('FI')
     assert str(country) == 'Finland'
Esempio n. 9
0
 def test_validate_with_valid_codes(self, code):
     Country.validate(code)
Esempio n. 10
0
 def test_equality_operator(self):
     assert Country(u'fi') == u'fi'
     assert u'fi' == Country(u'fi')
     assert Country(u'fi') == Country(u'fi')
Esempio n. 11
0
 def test_hash(self):
     return hash(Country('FI')) == hash('FI')
Esempio n. 12
0
 def test_validate_with_invalid_code(self):
     with raises(ValueError) as e:
         Country.validate('SomeUnknownCode')
     assert str(e.value) == (
         'Could not convert string to country code: SomeUnknownCode')
Esempio n. 13
0
 def test_validate_with_valid_codes(self, code):
     Country.validate(code)
Esempio n. 14
0
 def test_constructor_with_wrong_type(self):
     with raises(TypeError) as e:
         Country(None)
     assert str(e.value) == (
         "Country() argument must be a string or a country, not 'NoneType'")
Esempio n. 15
0
 def test_validate_with_invalid_code(self):
     with pytest.raises(ValueError) as e:
         Country.validate('SomeUnknownCode')
     assert str(e.value) == (
         'Could not convert string to country code: SomeUnknownCode'
     )
Esempio n. 16
0
 def test_non_equality_operator(self):
     assert Country(u'fi') != u'sv'
     assert not (Country(u'fi') != u'fi')
Esempio n. 17
0
 def test_ordering(self, op, code_left, code_right, is_):
     country_left = Country(code_left)
     country_right = Country(code_right)
     assert op(country_left, country_right) is is_
     assert op(country_left, code_right) is is_
     assert op(code_left, country_right) is is_
Esempio n. 18
0
 def test_init(self):
     assert Country(u'fi') == Country(Country(u'fi'))
Esempio n. 19
0
 def test_unicode(self):
     country = Country('FI')
     assert six.text_type(country) == u'Finland'
Esempio n. 20
0
 def test_repr(self):
     return repr(Country('FI')) == "Country('FI')"
Esempio n. 21
0
 def test_constructor_with_invalid_code(self):
     with pytest.raises(ValueError) as e:
         Country('SomeUnknownCode')
     assert str(e.value) == (
         'Could not convert string to country code: SomeUnknownCode'
     )