def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) # import database model from app.users.model import Users db.init_app(app) with app.app_context(): db.create_all() # Blueprint # [User Module] from app.users.api import user_bp app.register_blueprint(user_bp, url_prefix='/user') @app.before_request def before_request(): ip = request.remote_addr url = request.url # [CORS] @app.after_request def after_request(response): response.headers.add('Access-Control-Allow-Origin', '*') if request.method == 'OPTIONS': response.headers[ 'Access-Control-Allow-Methods'] = 'DELETE, GET, POST, PUT' headers = request.headers.get('Access-Control-Request-Headers') if headers: response.headers['Access-Control-Allow-Headers'] = headers return response return app
def setUp(self): self.app = create_app() self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all() self.user = User() self.user.id = self.user_id self.user.email, self.user.name, self.user.secret = \ '*****@*****.**', self.publisher_name, 'super_secret' self.publisher = Publisher(name=self.publisher_name) association = PublisherUser(role=UserRoleEnum.owner) association.publisher = self.publisher metadata = MetaDataDB(name=self.package) self.publisher.packages.append(metadata) self.user.publishers.append(association) db.session.add(self.user) db.session.commit() response = self.client.post(self.jwt_url, data=json.dumps({ 'username': self.publisher_name, 'secret': 'super_secret' }), content_type='application/json') data = json.loads(response.data) self.jwt = data['token'] self.auth = "bearer %s" % self.jwt
def create_app() -> Flask: """ Application factory, returns an instance of a Flask App. This could be expanded to receive a config object. """ # Retrieve the database uri db_uri = os.environ.get("db_uri") if not db_uri: raise SystemError("No db_uri set in the environment") logger.info("Creating application with database uri %s", db_uri) app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = db_uri app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.url_map.strict_slashes = False db.init_app(app) # Add a cli command for populating data app.cli.add_command(seed) # Create all the models in our database if they aren't already present # TODO: This should be handled externally, outside of our ORM. with app.app_context(): db.create_all() # For region related endpoints app.add_url_rule('/api/regions', methods=['GET'], view_func=RegionController.get_regions) return app
def create_app(): app = Flask(__name__) app.config['DEBUG'] = True app.config['SQLALCHEMY_DATABASE_URI'] = f'postgresql://{POSTGRES["user"]}:{POSTGRES["pw"]}@{POSTGRES["host"]}: \ {POSTGRES["port"]}/{POSTGRES["db"]}' app.config['CORS_HEADERS'] = 'Content-Type' app.secret_key = SECRET_KEY from app.database import db, migrate from app.logic.services.auth_service import argon2, login_manager from app.http.router import cors, router db.init_app(app) migrate.init_app(app, db) argon2.init_app(app) login_manager.init_app(app) cors.init_app(app) app.app_context().push() db.create_all() app.register_blueprint(router) return app
def make_db(num_users=5): db.drop_all() db.create_all() users = [ User( admin_username, admin_email, admin_password, fake.ipv4(), active=True, is_admin=True ) ] for _ in range(num_users): u = User( fake.userName(), fake.email(), fake.word() + fake.word(), fake.ipv4() ) users.append(u) [db.session.add(x) for x in users] db.session.commit()
def setUp(self): """Define las variables del test e inicializa la app""" self.app = create_app("testing") self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client(use_cookies=True) db.create_all()
def setUp(self): self.app = app.test_client() with app.app_context(): db.session.close() db.drop_all() db.create_all() setup_db(db.session)
def createdb(): """ Creates a database with all of the tables defined in your Alchemy models """ # print(app.config['SQLALCHEMY_DATABASE_URI']) db.create_all() manager_role = Role(name='management') employee_role = Role(name='employee') db.session.add(manager_role) db.session.add(employee_role) db.session.commit() if not Users.query.filter(Users.username=='user007').first(): user1 = Users(username='******', email='*****@*****.**', active=True, password=user_manager.hash_password('Password1')) user1.roles.append(employee_role) # user1.set_role() db.session.add(user1) db.session.commit() user2 = Users(username='******', email='*****@*****.**', active=True, password=user_manager.hash_password('Password1')) # user2.set_role() user2.roles.append(manager_role) db.session.add(user2) db.session.commit() user3 = Users(username='******', email='*****@*****.**', active=True, password=user_manager.hash_password('Password1')) # user3.set_role() user3.roles.append(manager_role) db.session.add(user3) db.session.commit()
def setup_class(self): self.publisher_one = 'test_publisher1' self.publisher_two = 'test_publisher2' self.package_one = 'test_package1' self.package_two = 'test_package2' self.package_three = 'test_package3' self.app = create_app() self.app.app_context().push() with self.app.test_request_context(): db.drop_all() db.create_all() user1 = User(name=self.publisher_one) publisher1 = Publisher(name=self.publisher_one) association1 = PublisherUser(role=UserRoleEnum.owner) association1.publisher = publisher1 user1.publishers.append(association1) user2 = User(name=self.publisher_two) publisher2 = Publisher(name=self.publisher_two) association2 = PublisherUser(role=UserRoleEnum.owner) association2.publisher = publisher2 user2.publishers.append(association2) metadata1 = Package(name=self.package_one) tag1 = PackageTag(descriptor=dict(name='test_one')) metadata1.tags.append(tag1) publisher1.packages.append(metadata1) db.session.add(user1) db.session.add(user2) db.session.commit()
def create_database(): db.drop_all() db.create_all() with open('asf_list.json') as bee_file: bees = json.load(bee_file) db.session.bulk_save_objects([BeeModel(**bee) for bee in bees]) db.session.commit()
def init_database(): # Create the database and the database table db.create_all() # Fake data documents = [ Document( text='It was the best of times, it was the worst of times', title='A Tale of Two Cities', ), Document( text='<html><head /><body><article>Hello</article></body></html>', title='A random HTML page', ), Document( text='<p>A fragment of text', title='A fragment of text', ), Document( text='', title='Doc1', ), ] map(lambda doc: db.session.add(doc), documents) db.session.add(documents[0]) # Commit the changes for the users db.session.commit() yield db # this is where the testing happens! db.drop_all()
def db(app): _db.app = app _db.create_all() yield _db _db.drop_all()
def setUp(self): self.app = create_app() # self.app.app_context().push() self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all() self.user = User() self.user.id = self.user_id self.user.email, self.user.name, self.user.secret = \ '*****@*****.**', self.publisher, 'super_secret' publisher = Publisher(name=self.publisher) association = PublisherUser(role="OWNER") association.publisher = publisher self.user.publishers.append(association) db.session.add(self.user) db.session.commit() response = self.client.post(self.jwt_url, data=json.dumps({ 'username': self.publisher, 'secret': 'super_secret' }), content_type='application/json') data = json.loads(response.data) self.jwt = data['token']
def setUp(self): self.app_context = self.app.app_context() self.app_context.push() db.create_all() login(self.client, os.getenv('LOGIN_USERNAME'), os.getenv('LOGIN_PASSWORD'))
def setUp(self): self.app = create_app(AuthTestConfig) self.app_context = self.app.test_request_context() self.app_context.push() db.app = self.app db.create_all() self.client = self.app.test_client(use_cookies=True)
def test_adds_local_urls(self): descriptor = { 'name': 'test', 'resources': [{ 'path': 'first.csv' }, { 'path': 'second.csv' }] } with self.app.app_context(): db.drop_all() db.create_all() publisher = Publisher(name=self.publisher) metadata = Package(name=self.package) metadata.descriptor = json.dumps(descriptor) publisher.packages.append(metadata) db.session.add(publisher) db.session.commit() response = self.client.get('/api/package/%s/%s'%\ (self.publisher, self.package)) catalog = Catalog(json.loads(response.data)) dataset = catalog.construct_dataset('http://example.com/') self.assertEqual(dataset.\ get('resources')[0].get('localurl'), 'http://example.com/api/dataproxy/demo/demo-package/r/first.csv') self.assertEqual(dataset.\ get('resources')[1].get('localurl'), 'http://example.com/api/dataproxy/demo/demo-package/r/second.csv')
def setUp(self): self.app = create_app() self.app.app_context().push() with self.app.test_request_context(): db.drop_all() db.create_all() self.pub1_name = 'pub1' self.pub2_name = 'pub2' self.pub1 = Publisher(name=self.pub1_name) self.pub2 = Publisher(name=self.pub2_name) self.pub1.packages.append( Package(name='pack1', descriptor='{"title": "pack1 details one"}', readme="Big readme one")) self.pub1.packages.append( Package(name='pack2', descriptor='{"title": "pack2 details two"}', readme="Big readme two")) self.pub1.packages.append( Package(name='pack3', descriptor='{"title": "pack3 details three"}')) self.pub2.packages.append( Package(name='pack4', descriptor='{"title": "pack4 details four"}')) self.pub2.packages.append( Package(name='pack5', descriptor='{"title": "pack5 details five"}')) self.pub2.packages.append( Package(name='pack6', descriptor='{"title": "pack6 details six"}')) db.session.add(self.pub1) db.session.add(self.pub2) db.session.commit()
def setUp(self): self.publisher = 'demo' self.package = 'demo-package' self.publisher_one = 'test_publisher1' self.publisher_two = 'test_publisher2' self.package_one = 'test_package1' self.package_two = 'test_package2' self.package_three = 'test_package3' self.descriptor = json.loads(open('fixtures/datapackage.json').read()) self.app = create_app() self.app.app_context().push() with self.app.test_request_context(): db.drop_all() db.create_all() create_test_package(self.publisher, self.package, self.descriptor) user1 = User(name=self.publisher_one) publisher1 = Publisher(name=self.publisher_one) association1 = PublisherUser(role=UserRoleEnum.owner) association1.publisher = publisher1 user1.publishers.append(association1) user2 = User(name=self.publisher_two) publisher2 = Publisher(name=self.publisher_two) association2 = PublisherUser(role=UserRoleEnum.owner) association2.publisher = publisher2 user2.publishers.append(association2) metadata1 = Package(name=self.package_one) tag1 = PackageTag(descriptor=dict(name='test_one')) metadata1.tags.append(tag1) publisher1.packages.append(metadata1) metadata2 = Package(name=self.package_two) tag2 = PackageTag(descriptor=dict(name='test_two')) metadata2.tags.append(tag2) publisher1.packages.append(metadata2) metadata3 = Package(name=self.package_one) tag3 = PackageTag(descriptor=dict(name='test_three')) metadata3.tags.append(tag3) publisher2.packages.append(metadata3) metadata4 = Package(name=self.package_two) tag4 = PackageTag(descriptor=dict(name='test_four')) metadata4.tags.append(tag4) publisher2.packages.append(metadata4) metadata5 = Package(name=self.package_three) tag5 = PackageTag(descriptor=dict(name='test_four')) metadata5.tags.append(tag5) publisher2.packages.append(metadata5) db.session.add(user1) db.session.add(user2) db.session.commit()
def setUp(self): self.publisher = 'test_publisher' self.package = 'test_package' self.app = create_app() self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all()
def db(app): _db.app = app with app.app_context(): _db.create_all() yield _db _db.drop_all()
def setUpClass(cls): """Run once at the start""" app = create_app(test_config) db.app = app # hack for using db.init_app(app) in app/__init__.py db.drop_all() db.create_all() populate_db() cls.app = app
def _reset_db(): from flask_migrate import upgrade, stamp db.drop_all() db.engine.execute('DROP TABLE IF EXISTS alembic_version;') db.create_all() stamp() upgrade()
def setup_class(self): self.app = create_app() self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all() base.make_fixtures(self.app, self.package, self.publisher, self.user_id)
def test_throw_error_if_role_is_invalid(self): with self.app.test_request_context(): db.drop_all() db.create_all() publisher = Publisher(name='test_pub_id') association = PublisherUser() association.publisher = publisher self.assertRaises(ValueError, association.role, "NOT_MEMBER")
def setUp(self): self.app = create_app() self.client = self.app.test_client() with self.app.app_context(): self.bucket_name = self.app.config['S3_BUCKET_NAME'] db.drop_all() db.create_all() self.user = User() self.user.id = self.user_id self.user.email, self.user.name, self.user.secret = \ '*****@*****.**', self.publisher_name, 'super_secret' self.publisher = Publisher(name=self.publisher_name) association = PublisherUser(role=UserRoleEnum.owner) association.publisher = self.publisher metadata = Package(name=self.package) self.publisher.packages.append(metadata) self.user.publishers.append(association) self.user_not_allowed = User() self.user_not_allowed.id = self.user_not_allowed_id self.user_not_allowed.email, self.user_not_allowed.name, \ self.user_not_allowed.secret = \ '*****@*****.**', self.user_not_allowed_name, 'super_secret' self.publisher_not_allowed = Publisher( name=self.user_not_allowed_name) association_not_allowed = PublisherUser(role=UserRoleEnum.owner) association_not_allowed.publisher = self.publisher_not_allowed metadata = Package(name=self.package) self.publisher_not_allowed.packages.append(metadata) self.user_not_allowed.publishers.append(association_not_allowed) self.user_member = User() self.user_member.id = self.user_member_id self.user_member.email, self.user_member.name, self.user_member.secret = \ '*****@*****.**', self.user_member_name, 'super_secret' association_member = PublisherUser(role=UserRoleEnum.member) association_member.publisher = self.publisher self.user_member.publishers.append(association_member) db.session.add(self.user) db.session.add(self.user_not_allowed) db.session.commit() response = self.client.post(self.jwt_url, data=json.dumps({ 'username': self.publisher_name, 'secret': 'super_secret' }), content_type='application/json') data = json.loads(response.data) self.jwt = data['token'] self.auth = "bearer %s" % self.jwt
def test_throw_404_if_user_not_exists(self): with self.app.app_context(): db.drop_all() db.create_all() auth = "bearer %s" % self.jwt response = self.client.post(self.url, data=json.dumps(dict()), headers=dict(Authorization=auth)) self.assertEqual(404, response.status_code)
def flask_app(): app = create_app(environment='testing') from app.database import db with app.app_context(): db.create_all() yield app db.session.close_all() db.drop_all()
def setUp(self): self.app = create_app() self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all() base.make_fixtures(self.app, self.package, self.publisher, self.user_id) self.jwt = base.get_valid_token(self.publisher) self.jwt1 = base.get_valid_token('test1')
def setUp(self): self.user = '******' self.email = 'email' self.full_name = 'Demo' self.app = create_app() self.app.app_context().push() with self.app.test_request_context(): db.drop_all() db.create_all() db.session.commit()
def db(app): _db.app = app with app.app_context(): _db.create_all() yield _db _db.session.close() _db.drop_all()
def client(): with app.test_client() as client: app_ctx = app.app_context() app_ctx.push() db.drop_all() db.create_all() yield client db.session.remove() db.drop_all() app_ctx.pop()
def db(app): """A database for the tests.""" _db.app = app with app.app_context(): _db.create_all() yield _db # Explicitly close DB connection _db.session.close() _db.drop_all()
def create_app(config=None): app = Flask(__name__) # If no config file is passed in on the command line: if config is None: #config = os.path.join(app.root_path, os.environ.get('FLASK_APPLICATION_SETTINGS')) config = os.path.join(os.path.dirname( __file__ ), "../config/", "config.cfg") app.config.from_pyfile(config) # Secret key needed to use sessions. app.secret_key = app.config['SECRET_KEY'] app.mail = Mail(app) # Initialize SQL Alchemy and Flask-Login # Instantiate the Bcrypt extension db.init_app(app) if (app.config['CREATE_SCHEMA']): with app.app_context(): db.create_all() login_manager.init_app(app) bcrypt.init_app(app) # CSRF protection csrf.init_app(app) # Web assets (js, less) assets = Environment(app) js = Bundle('js/main.js', filters='jsmin', output='gen/bundle.js') assets.register('js_all', js) # Automatically tear down SQLAlchemy @app.teardown_request def shutdown_session(exception=None): db.session.remove() @app.before_request def before_request(): g.user = current_user app.register_blueprint(unauthenticated) app.register_blueprint(authenticated) app.register_blueprint(users) return app
def create_db(num_users=5): """Creates database tables and populates them.""" db.create_all() populate_db(num_users=num_users)
def init_db(): db.create_all()
def create_app(self): app = create_app('config.TestConfig') with app.app_context(): db.drop_all() db.create_all() return app
def createdb(): db.init_app(current_app) db.create_all()
def create_db(): db.create_all()
def create_db(): """Creates the database.""" db.create_all()
# -*- coding: utf-8 -*- from flask import Flask, jsonify from flask_sqlalchemy import SQLAlchemy from flask_restless import APIManager import datetime import os from app import config app = Flask(__name__) # init config app.config.from_object(config) # init views from app import views #init database from app.database import db, restless from app.model import user, system db.create_all() from app import db_init db_init.init() if __name__ == "__main__": app.run(debug=True)