def encode_auth_token(origin_data, exp): """ 生成认证Token :param user_id: int :param login_time: int(timestamp) :return: string """ try: payload = { 'exp': exp if exp else datetime.datetime.now() + datetime.timedelta(days=1), 'iat': datetime.datetime.now(), 'iss': 'paperpie', 'data': origin_data } encoded_jwt = jwt.encode(payload, get_config().SECRET_KEY, get_config().ALGORITHM) return encoded_jwt except Exception as e: return e
def get_user_group(security_scopes: SecurityScopes, token: str = Depends(jwt_scheme)): if security_scopes.scopes: authenticate_value = f'Bearer scope="{security_scopes.scope_str}"' else: authenticate_value = "Bearer" credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": authenticate_value}, ) try: payload = jwt.decode( token, app_config.get_config().jwt_secret_key, algorithms=[app_config.get_config().jwt_algorithm], ) user_group: str = payload.get("sub") if user_group is None: raise credentials_exception token_scopes = payload.get("scopes", []) token_data = api_models.TokenData(scopes=token_scopes, user_group=user_group) except (jwt.PyJWTError, ValidationError): raise credentials_exception for scope in security_scopes.scopes: if scope not in token_data.scopes: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Not enough permissions", headers={"WWW-Authenticate": authenticate_value}, ) return user_group
def login(self): options = webdriver.ChromeOptions() options.add_argument('--ignore-certificate-errors') options.add_argument("--test-type") options.binary_location = get_config().CHROME_BINARY self.driver = webdriver.Chrome(chrome_options=options) self.driver.get(get_config().JIRA_URL)
def test_should_raise_config_class_not_found_if_config_class_is_none( self, import_module_mock): config_module_mock = self.mock.MagicMock() config_module_mock.TestingConfig = None import_module_mock.return_value = config_module_mock with self.assertRaises(exceptions.ConfigClassNotFound): config.get_config()
def test_get_base_config_raises_if_no_class_found(self): os.environ['APP_SETTINGS'] = 'app.config.NonExists' try: config.get_config() self.assertFalse(True, 'get_config do not raise excpetion') except config.ConfigClassNotFound: self.assertTrue('Done') finally: os.environ['APP_SETTINGS'] = 'app.config.TestingConfig'
def db_indepotent(pytestconfig): """ DB session that roll back all DB modifications made in a test. So you do not need to write per-test tear-down for DB changes made by test. SQLAlchemy session for tests and for the code under tests started with SAVEPOINT. After test DB rollback to this SAVEPOINT. If we choose to use this approach the we better auto-add this fixture to all tests that use fixtures `client` and `celery_*` and is not marked as `does_not_change_db`. So to all tests that potentially could change DB in backend request handler or in Celery task. To do that uncomment the code inside `pytest_collection_modifyitems` hook. """ if app_config.get_config().host is not None: yield None # no sense using local DB when we test external Server return # stop iterations for this fixture generator try: connection = app_session.engine(app_config.get_config()).connect() except sqlalchemy.exc.OperationalError: pytest.exit( f"Tests have to connect to DB {app_config.get_config().db_uri}", returncode=2, ) # begin a non-ORM transaction trans = connection.begin() session = sessionmaker(info={"test_session_id": str(uuid.uuid4())})(bind=connection) session.begin_nested() # SAVEPOINT log.debug(f"[[[ savepoint transaction ]]] Start in {session.info}") app_session._session = session # Inject session to the server code under test # todo This session should not be used in parallel by backend handlers and Celery tasks. # todo So we have to implement custom session maker that wait for a mutex before giving # todo this session # todo And we should override `close` method in the DB session to release this mutex # todo but do not actually close the session so it will be given to next waiting handler/task. @event.listens_for(session, "after_transaction_end") def restart_savepoint(session, transaction): """ Each time that SAVEPOINT ends, reopen it """ if transaction.nested and not transaction._parent.nested: log.debug(f"[[[ savepoint transaction ]]] Restart in {session.info}") session.begin_nested() yield session app_session._session = ( None # remove so server code in future will generate session by itself ) session.close() log.debug(f"[[[ savepoint transaction ]]] Rollback in {session.info}") trans.rollback() # roll back to the SAVEPOINT connection.close()
def __init__(self, jira_cookie=None): super().__init__() if jira_cookie: self.jira_cookie = jira_cookie self.write_cookies() else: self.read_cookies() self.url = get_config().JIRA_URL self.username = get_config().USERNAME self.user_issues = []
def vcenter(vcenter_name): """ vcenter """ try: a = Action(v_server=get_config(vcenter_name, 'ip'), v_user=get_config(vcenter_name, 'user'), v_passwd=get_config(vcenter_name, 'password')) a.v_check_login() return jsonify({'status': 'success'}) except Exception: return jsonify({ 'status': "error connecting to VCenter {name}".format(name=vcenter_name) })
def create_app() -> FastAPI: app = FastAPI() app.include_router(tag.router, prefix="/api/tags") app.include_router(todo.router, prefix="/api/todo") config = get_config() app.mount("/todo", StaticFiles(directory=config.frontend_path, html=True)) return app
def test_config_development(): for config_name in ["default", "development"]: app = create_app(config_name) config = get_config(config_name) if os.getenv("TRAVIS") or False: assert config.TRAVIS == True assert app.__dict__["extra"]["TRAVIS"] == True else: assert config.TRAVIS == False assert app.__dict__["extra"]["TRAVIS"] == False assert config.SQLALCHEMY_DATABASE_URI == "postgresql://localhost/owat_dev" assert ( app.__dict__["extra"]["SQLALCHEMY_DATABASE_URI"] == "postgresql://localhost/owat_dev" ) assert config.DEBUG == True assert app.__dict__["extra"]["DEBUG"] == True assert app._debug == True assert config.SECRET_KEY == "my-secret-key" assert app.__dict__["extra"]["SECRET_KEY"] == "my-secret-key" assert config.ADMIN_EMAIL is None assert app.__dict__["extra"]["ADMIN_EMAIL"] is None assert config.APP_EMAIL is None assert app.__dict__["extra"]["APP_EMAIL"] is None assert config.MIN_CONNECTIONS_COUNT is None assert app.__dict__["extra"]["MIN_CONNECTIONS_COUNT"] is None assert config.MAX_CONNECTIONS_COUNT is None assert app.__dict__["extra"]["MAX_CONNECTIONS_COUNT"] is None assert config.TITLE == "owat_api" assert app.title == "owat_api" assert app.__dict__["extra"]["TITLE"] == "owat_api" assert app.version == "0.1.0" assert app.description == "RESTful API for Austrian open elections data" assert isinstance(app, FastAPI)
def create_app() -> Flask: flask_app = Flask(__name__) # 加载配置 from app.config import get_config _get_config = get_config() flask_app.config.from_object(_get_config) # 添加上下文 ctx = flask_app.app_context() ctx.push() # 添加跨域 CORS(flask_app, support_credentials=True) # 初始化数据库 db.init_app(flask_app) # 分配路由 from app.util.green_print import GreenPrint from app.api.v1.oauth import oauth_blueprint from app.api.v1.user import user_blueprint from app.api.v1.data import data_blueprint api_v1 = GreenPrint('api', __name__, url_prefix='/api/v1') api_v1.register_blueprint(oauth_blueprint) api_v1.register_blueprint(user_blueprint) api_v1.register_blueprint(data_blueprint) flask_app.register_blueprint(api_v1) return flask_app
def create_app() -> Flask: flask_app = Flask(__name__) # 加载配置 from app.config import get_config _get_config = get_config() flask_app.config.from_object(_get_config) # 添加上下文 ctx = flask_app.app_context() ctx.push() # 添加跨域 CORS(flask_app, support_credentials=True) # 初始化数据库 db.init_app(flask_app) # 分配路由 from app.util.GreenPrint import GreenPrint from app.api.v1.auth import auth_blueprint # from app.api.v1.sign import sign # from app.api.v1.base_info import base_info # from app.api.v1.domain import domain api_v1 = GreenPrint('api', __name__, url_prefix='/api/v1') api_v1.register_blueprint(auth_blueprint) # api_v1.register_blueprint(base_info) # api_v1.register_blueprint(domain) flask_app.register_blueprint(api_v1) return flask_app
def test_config_testing(): config_name = "testing" app = create_app(config_name) config = get_config(config_name) if os.getenv("TRAVIS") or False: assert config.TRAVIS == True assert app.__dict__["extra"]["TRAVIS"] == True else: assert config.TRAVIS == False assert app.__dict__["extra"]["TRAVIS"] == False assert config.SQLALCHEMY_DATABASE_URI == "postgresql://localhost/owat_test" assert ( app.__dict__["extra"]["SQLALCHEMY_DATABASE_URI"] == "postgresql://localhost/owat_test" ) assert config.DEBUG == False assert app.debug == False assert app.__dict__["extra"]["DEBUG"] == False assert config.SECRET_KEY == "secret-env-key" assert app.__dict__["extra"]["SECRET_KEY"] == "secret-env-key" assert config.ADMIN_EMAIL == "*****@*****.**" assert app.__dict__["extra"]["ADMIN_EMAIL"] == "*****@*****.**" assert config.APP_EMAIL == "*****@*****.**" assert app.__dict__["extra"]["APP_EMAIL"] == "*****@*****.**" assert config.MIN_CONNECTIONS_COUNT == 10 assert app.__dict__["extra"]["MIN_CONNECTIONS_COUNT"] == 10 assert config.MAX_CONNECTIONS_COUNT == 10 assert app.__dict__["extra"]["MAX_CONNECTIONS_COUNT"] == 10 assert config.TITLE == "owat_api" assert app.title == "owat_api" assert app.__dict__["extra"]["TITLE"] == "owat_api" assert app.version == "0.1.0" assert app.description == "RESTful API for Austrian open elections data" assert isinstance(app, FastAPI)
def test_config_travis(): config_name = "travis" app = create_app(config_name) config = get_config(config_name) assert config.TRAVIS == True assert app.__dict__["extra"]["TRAVIS"] == True assert ( config.SQLALCHEMY_DATABASE_URI == "postgresql+psycopg2://postgres@localhost:5432/travis_ci_test" ) assert ( app.__dict__["extra"]["SQLALCHEMY_DATABASE_URI"] == "postgresql+psycopg2://postgres@localhost:5432/travis_ci_test" ) assert config.DEBUG == False assert app.__dict__["extra"]["DEBUG"] == False assert app._debug == False assert config.SECRET_KEY == "my-secret-key" assert app.__dict__["extra"]["SECRET_KEY"] == "my-secret-key" assert config.ADMIN_EMAIL is None assert app.__dict__["extra"]["ADMIN_EMAIL"] is None assert config.APP_EMAIL is None assert app.__dict__["extra"]["APP_EMAIL"] is None assert config.MIN_CONNECTIONS_COUNT is None assert app.__dict__["extra"]["MIN_CONNECTIONS_COUNT"] is None assert config.MAX_CONNECTIONS_COUNT is None assert app.__dict__["extra"]["MAX_CONNECTIONS_COUNT"] is None assert config.TITLE == "owat_api" assert app.title == "owat_api" assert app.__dict__["extra"]["TITLE"] == "owat_api" assert app.version == "0.1.0" assert app.description == "RESTful API for Austrian open elections data" assert isinstance(app, FastAPI)
def main(): args = parse_args() config = get_config() configure_logging(config.debug) loop = asyncio.get_event_loop() loop.run_until_complete(async_main(loop, config, args))
def create_app(config_name=None): """Create application and load settings.""" print("* Start Offene Wahlen AT API...") if config_name is None: load_dotenv() config_name = get_config_name() config = get_config(config_name) SQLALCHEMY_DATABASE_URI = config.SQLALCHEMY_DATABASE_URI TITLE = config.TITLE DEBUG = config.DEBUG app = FastAPI( title=TITLE, debug=DEBUG, description=__description__, flask_config=config_name, **config.dict(), ) app.include_router(api_router, prefix="/api") print(' * Setting "{0}" loaded'.format(config_name)) print(" * Database: {0}".format(SQLALCHEMY_DATABASE_URI)) return app
def test_should_return_config_class_if_config_class( self, import_module_mock): config_module_mock = self.mock.MagicMock() testing_config_mock = self.mock.MagicMock() config_module_mock.TestingConfig = testing_config_mock import_module_mock.return_value = config_module_mock testing_config = config.get_config() self.assertEqual(testing_config, testing_config_mock)
def create_access_token( payload: dict, expires_delta: timedelta = timedelta(minutes=15) ): """ Create JWT """ return jwt.encode( payload={ key: val for key, val in chain( payload.copy().items(), {"exp": datetime.utcnow() + expires_delta}.items(), ) }, key=app_config.get_config().jwt_secret_key, algorithm=app_config.get_config().jwt_algorithm, )
def db(pytestconfig): """ SQLAlchemy DB session from app.db.session. We need this fixture 1) Simple and explicit way to get DB session in a test 2) Raise exception if in External test mode we use unittests (with direct access to DB) """ if app_config.get_config().host is not None: yield None # No sense using local DB when we in External server test mode. # We do not raise exception here because some tests can check External server test mode # by themselves and do not use the fixture in that mode. So this is ok if some # tests in External server test mode request but do not use the fixture. else: session = app_session.get_session(app_config.get_config()) yield session session.close()
def datacenter(vcenter_name): """ datacenter """ try: a = Action(v_server=get_config(vcenter_name, 'ip'), v_user=get_config(vcenter_name, 'user'), v_passwd=get_config(vcenter_name, 'password')) ret = json.dumps(a.v_get_datacenter()) resp = app.response_class(response=ret, status=200, mimetype="application/json") return resp except Exception: return jsonify({ 'status': 'error extracting datacenter for VCenter {name}'.format( name=vcenter_name) })
def configure_app(app, config=None): '''Handle different configurations''' app.config.from_object(Config.DefaultConfig) if config: app.config.from_object(config) application_mode = os.getenv('APPLICATION', 'LOCAL') app.config.from_object(Config.get_config(application_mode))
def create_app(env: str) -> Flask: """ Application factory """ app = Flask(__name__) conf: Type[Config] = get_config(env) app.config.from_object(conf) app.register_blueprint(view) return app
def vdportgroup(vcenter_name, vdswitch_name, datacenter_name): """ vdportgroup """ try: oformat = request.args.get('format', default='full', type=str) a = Action(v_server=get_config(vcenter_name, 'ip'), v_user=get_config(vcenter_name, 'user'), v_passwd=get_config(vcenter_name, 'password')) ret = json.dumps(a.v_get_vdportgroup(datacenter_name, vdswitch_name)) resp = app.response_class(response=ret, status=200, mimetype="application/json") return resp except Exception: return jsonify({ 'status': "error extracting vdportgroup for VCenter {vcenter_name} and Datacenter \ {datacenter_name}".format(vcenter_name=vcenter_name, datacenter_name=datacenter_name) })
def run(): # switch test config environ['APP_TYPE'] = 'test' config = get_config() paths = (config.TEST_ROOT_DIR, ) loader = TestLoader() for p in paths: tests = loader.discover(p) runner = TextTestRunner() runner.run(tests)
def _create_base_app(): flask_app = Flask(__name__) flask_app.config.update(config.get_config()) flask_app.cli.add_command(dev_cmd) postgres_db_config = (os.environ.get('DATABASE_URL') if 'DATABASE_URL' in os.environ else config.get_config()['app']['database_url']) flask_app.config.update({ 'TESTING': False, 'SQLALCHEMY_DATABASE_URI': _create_sql_alchemy_connection_str(postgres_db_config), # set SQLALCHEMY_TRACK_MODIFICATIONS to False because # default of None produces warnings, and track modifications # are not required 'SQLALCHEMY_TRACK_MODIFICATIONS': False, }) flask_app = _register_components(flask_app) return flask_app
def make_current_app_test_app(test_db_name): flask_app = get_or_create() postgres_db_config = (os.environ.get('DATABASE_URL') if 'DATABASE_URL' in os.environ else config.get_config()['app']['database_url']) flask_app.config.update({ 'TESTING': True, 'SQLALCHEMY_DATABASE_URI': _create_sql_alchemy_connection_str(postgres_db_config, test_db_name), }) return flask_app
def create_app(config_prefix): app = Flask(__name__) app.config.from_object(get_config(config_prefix)) # Catch all marshmallow ValidationError's and respond with 400 status. handle_validation_errors(app) db.init_app(app) from app.api.v1 import api as api_v1 app.register_blueprint(api_v1, url_prefix='/api/v1') return app
def create_app(config_name): app = Flask(__name__) app.config.from_object(get_config(config_name)) from app.controllers import api_bp app.register_blueprint(api_bp) cors.init_app(app) db.init_app(app) migrate.init_app(app, db) return app
def save_sql(phone, city, soure, source_id, result): host = get_config('CRM_MYSQL_HOST') user = get_config('CRM_MYSQL_USER') password = get_config('CRM_MYSQL_PASSWORD') db = get_config('CRM_MYSQL_DB') conn = pymysql.connect(host=host, port=3306, user=user, password=password, db=db) sql = "insert into phone_profile_log (PHONE,CITY,SOURCE,SOURCE_ID,BEDROOM,AVG_PRICE,SUM_PRICE,KITCHEN,TOLIET,LIVINGROOM,AREA)values (%s,'%s',%s,%s,%s,%s,%s,%s,%s,%s,%s)" % ( phone, city, soure, source_id, BuriedPoint.get_default(result['bedroom']), BuriedPoint.get_default(result['avg_price']), BuriedPoint.get_default(result['sum_price']), BuriedPoint.get_default( result['kitchen']), BuriedPoint.get_default(result['toliet']), BuriedPoint.get_default( result['livingroom']), BuriedPoint.get_default(result['area'])) cursor = conn.cursor() cursor.execute(sql) conn.commit()
def cluster(vcenter_name, datacenter_name): """ cluster """ try: oformat = request.args.get('format', default='full', type=str) a = Action(v_server=get_config(vcenter_name, 'ip'), v_user=get_config(vcenter_name, 'user'), v_passwd=get_config(vcenter_name, 'password')) if oformat == 'full': return jsonify(a.v_get_cluster(datacenter_name)) ret = json.dumps( a.v_get_cluster(datacenter_name)[datacenter_name].keys()) resp = app.response_class(response=ret, status=200, mimetype="application/json") return resp except Exception: return jsonify({ 'status': "error extracting datastore for VCenter {vcenter_name} and Datacenter \ {datacenter_name}".format(vcenter_name=vcenter_name, datacenter_name=datacenter_name) })
async def db_session_middleware(request: Request, call_next): """ Opens DB session for each API request """ try: request.state.db = app_session.get_session(app_config.get_config()) response = await call_next(request) except Exception as e: logging.exception(f"Cannot get session in middleware: {e}") raise finally: request.state.db.close() return response
def setUp(self): #This function is run before every test app.config.from_object(config.get_config('TEST')) self.app = app.test_client()
from flask import Flask from flask.ext.login import LoginManager from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.bcrypt import Bcrypt from app import test_mode, doc_mode, log, static from app.config import get_config from app.res.restfulApi import RestfulApiWithoutSimpleAuth config = get_config() flask_args = {} # static sharing flask_args.update(static.get_flask_parameters(config)) app = Flask(__name__, **flask_args) app.config.update(config['Flask']) # logging if not app.debug and not app.testing: log.initialize(app, config) # flask-sqlalchemy db = SQLAlchemy(app) # flask-login lm = LoginManager() lm.init_app(app) # flask-restful
import os from flask.ext.script import Manager from flask.ext.migrate import Migrate, MigrateCommand from app import app, db, config app.config.from_object(config.get_config()) migrate = Migrate(app, db) manager = Manager(app) manager.add_command('db', MigrateCommand) if __name__ == '__main__': manager.run()
def test_get_config(self): config_result = config.get_config() config_result.TESTING.should.be.truthy config_result.KEY_ON_TEST.should.be.equal('KEY ON TEST')
# -*- coding: utf-8 -*- from datetime import datetime, timedelta import jwt from passlib.apps import custom_app_context from app import config as config_module import database db = database.AppRepository.db config = config_module.get_config() class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(), unique=True, nullable=False) password_hash = db.Column(db.String(128), nullable=False) name = db.Column(db.String()) pivotal_token = db.Column(db.String()) @classmethod def get_by_id(cls, user_id): return cls.query.get(user_id) @classmethod def create(cls, user_data): user = cls() db.session.add(user)