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))
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'
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
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']
def country_only(self): user = models.User() user.country = Country('US') eq_(user.full_location, 'United States')
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')
def test_country_is_zz(self): user = models.User() user.country = Country('ZZ') eq_(user.full_location, '')
def test_str(self): country = Country('FI') assert str(country) == 'Finland'
def test_validate_with_valid_codes(self, code): Country.validate(code)
def test_equality_operator(self): assert Country(u'fi') == u'fi' assert u'fi' == Country(u'fi') assert Country(u'fi') == Country(u'fi')
def test_hash(self): return hash(Country('FI')) == hash('FI')
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')
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'")
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' )
def test_non_equality_operator(self): assert Country(u'fi') != u'sv' assert not (Country(u'fi') != u'fi')
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_
def test_init(self): assert Country(u'fi') == Country(Country(u'fi'))
def test_unicode(self): country = Country('FI') assert six.text_type(country) == u'Finland'
def test_repr(self): return repr(Country('FI')) == "Country('FI')"
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' )