Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
''' initialise temporary new database. '''
self.ctx = streetsign_server.app.test_request_context
# streetsign_server.app.config['MODE'] = 'testing'
models.bcrypt = MockBcrypt()
streetsign_server.app.config['DATABASE_FILE'] = ':memory:'
streetsign_server.app.config['TESTING'] = True
models.DB = SqliteDatabase(None)
models.DB.init(streetsign_server.app.config['DATABASE_FILE'])
model_list = []
for modelname in models.__all__:
model = getattr(models, modelname)
try:
model._meta.database = models.DB # pylint: disable=protected-access
model_list.append(model)
except AttributeError:
pass
models.DB.create_tables(model_list)
self.client = streetsign_server.app.test_client()
tc.column('text', 'col_text')
tc.column('time', 'col_time')
tc.column('uuid', 'col_uuid')
assert isinstance(tc.model.id, peewee.AutoField)
assert isinstance(tc.model.col_bare, peewee.BareField)
assert isinstance(tc.model.col_biginteger, peewee.BigIntegerField)
assert isinstance(tc.model.col_binary, peewee.BlobField)
assert isinstance(tc.model.col_blob, peewee.BlobField)
assert isinstance(tc.model.col_bool, peewee.BooleanField)
assert isinstance(tc.model.col_char, peewee.CharField)
assert isinstance(tc.model.col_date, peewee.DateField)
assert isinstance(tc.model.col_datetime, peewee.DateTimeField)
assert isinstance(tc.model.col_decimal, peewee.DecimalField)
assert isinstance(tc.model.col_double, peewee.DoubleField)
assert isinstance(tc.model.col_fixed, peewee.CharField)
assert isinstance(tc.model.col_float, peewee.FloatField)
assert isinstance(tc.model.col_int, peewee.IntegerField)
assert isinstance(tc.model.col_integer, peewee.IntegerField)
assert isinstance(tc.model.col_smallint, peewee.SmallIntegerField)
assert isinstance(tc.model.col_smallinteger, peewee.SmallIntegerField)
assert isinstance(tc.model.col_text, peewee.TextField)
assert isinstance(tc.model.col_time, peewee.TimeField)
assert isinstance(tc.model.col_uuid, peewee.UUIDField)
import peewee as pw
import datetime as dt
import marshmallow as ma
from playhouse.db_url import connect
database = connect('sqlite:///:memory:')
class Role(pw.Model):
name = pw.CharField(255, default='user')
class Meta:
database = database
class User(pw.Model):
created = pw.DateTimeField(default=dt.datetime.utcnow)
login = pw.CharField(255)
name = pw.CharField(255, null=True)
password = pw.CharField(127, null=True)
is_active = pw.BooleanField(default=True)
role = pw.ForeignKeyField(Role, null=True)
class Role(pw.Model):
name = pw.CharField(255, default='user')
class Meta:
database = database
class User(pw.Model):
created = pw.DateTimeField(default=dt.datetime.utcnow)
login = pw.CharField(255)
name = pw.CharField(255, null=True)
password = pw.CharField(127, null=True)
is_active = pw.BooleanField(default=True)
role = pw.ForeignKeyField(Role, null=True)
class Meta:
database = database
database.create_tables([User, Role], safe=True)
def test_resource(app, api, client):
from flask_restler.peewee import ModelResource
@api.route
class UserResouce(ModelResource):
def test_database_creation(tmpdir):
db = peewee.SqliteDatabase(':memory:')
manager = DatabaseManager(db, directory=tmpdir)
assert isinstance(manager.database, peewee.SqliteDatabase)
db = {'engine': 'peewee.SqliteDatabase', 'name': ':memory:'}
manager = DatabaseManager(db, directory=tmpdir)
assert isinstance(manager.database, peewee.SqliteDatabase)
db = 'sqlite:///:memory:'
manager = DatabaseManager(db, directory=tmpdir)
assert isinstance(manager.database, peewee.SqliteDatabase)
def test_org_switch(client):
"""Test organisation switching."""
user = User.get(orcid=User.select(fn.COUNT(User.orcid).alias("id_count"), User.orcid).group_by(
User.orcid).having(fn.COUNT(User.orcid) > 1).objects().first().orcid)
user_orgs = UserOrg.select().join(User, on=UserOrg.user).where(User.orcid == user.orcid)
new_org = Organisation.select().where(Organisation.id.not_in([uo.org_id for uo in user_orgs])).first()
UserOrg.create(user=user, org=new_org, affiliations=0)
resp = client.login(user, follow_redirects=True)
assert user.email.encode() in resp.data
assert len(user.org_links) > 1
assert current_user == user
# Nothing changes if it is the same organisation
uo = user.user_orgs.where(UserOrg.org_id == user.organisation_id).first()
resp = client.get(f"/select/user_org/{uo.id}", follow_redirects=True)
assert User.get(user.id).organisation_id == user.organisation_id
assert user.email.encode() in resp.data
# The current org changes if it's a dirrerent org on the list
def promote_user_to_admin(client, user_info):
"""Assuming user_info is the info for the logged-in user, promote them
to admin and leave them logged in.
"""
log_out_current_user(client)
admin = User.get(fn.Lower(User.name) == user_info['username'])
UserMetadata.create(uid=admin.uid, key='admin', value='1')
log_in_user(client, user_info)
tc.column('datetime', 'col_datetime')
tc.column('decimal', 'col_decimal')
tc.column('double', 'col_double')
tc.column('fixed', 'col_fixed')
tc.column('float', 'col_float')
tc.column('int', 'col_int')
tc.column('integer', 'col_integer')
tc.column('smallint', 'col_smallint')
tc.column('smallinteger', 'col_smallinteger')
tc.column('text', 'col_text')
tc.column('time', 'col_time')
tc.column('uuid', 'col_uuid')
assert isinstance(tc.model.id, peewee.AutoField)
assert isinstance(tc.model.col_bare, peewee.BareField)
assert isinstance(tc.model.col_biginteger, peewee.BigIntegerField)
assert isinstance(tc.model.col_binary, peewee.BlobField)
assert isinstance(tc.model.col_blob, peewee.BlobField)
assert isinstance(tc.model.col_bool, peewee.BooleanField)
assert isinstance(tc.model.col_char, peewee.CharField)
assert isinstance(tc.model.col_date, peewee.DateField)
assert isinstance(tc.model.col_datetime, peewee.DateTimeField)
assert isinstance(tc.model.col_decimal, peewee.DecimalField)
assert isinstance(tc.model.col_double, peewee.DoubleField)
assert isinstance(tc.model.col_fixed, peewee.CharField)
assert isinstance(tc.model.col_float, peewee.FloatField)
assert isinstance(tc.model.col_int, peewee.IntegerField)
assert isinstance(tc.model.col_integer, peewee.IntegerField)
assert isinstance(tc.model.col_smallint, peewee.SmallIntegerField)
assert isinstance(tc.model.col_smallinteger, peewee.SmallIntegerField)
assert isinstance(tc.model.col_text, peewee.TextField)
assert isinstance(tc.model.col_time, peewee.TimeField)
def test_unique_constraint(env):
"""
Test that we are prevented from recreating existing objects
"""
with pytest.raises(peewee.IntegrityError):
Namespace.create(namespace='system')
with pytest.raises(peewee.IntegrityError):
FlowModel.create(namespace=env['ns']['test'], name='f1', version=1)
with pytest.raises(peewee.IntegrityError):
TaskModel.create(name='t1', flow=env['f']['f1'])