예제 #1
0
def create_app(config_name):
    application = Flask(__name__)
    application.config['DM_ENVIRONMENT'] = config_name

    init_app(
        application,
        configs[config_name],
        db=db,
        search_api_client=search_api_client,
        error_handlers=api_error_handlers,
    )

    if not application.config['DM_API_AUTH_TOKENS']:
        raise Exception("No DM_API_AUTH_TOKENS provided")

    if application.config['VCAP_SERVICES']:
        cf_services = json.loads(application.config['VCAP_SERVICES'])
        application.config['SQLALCHEMY_DATABASE_URI'] = cf_services['postgres'][0]['credentials']['uri']

    from .metrics import metrics as metrics_blueprint, gds_metrics
    from .main import main as main_blueprint
    from .status import status as status_blueprint
    from .callbacks import callbacks as callbacks_blueprint

    application.register_blueprint(metrics_blueprint)
    application.register_blueprint(main_blueprint)
    application.register_blueprint(callbacks_blueprint, url_prefix='/callbacks')
    application.register_blueprint(status_blueprint)

    gds_metrics.init_app(application)

    return application
예제 #2
0
def create_app(config_name):
    application = Flask(__name__)
    application.config['DM_ENVIRONMENT'] = config_name

    init_app(
        application,
        configs[config_name],
        db=db,
        search_api_client=search_api_client,
        error_handlers=api_error_handlers,
    )

    if not application.config['DM_API_AUTH_TOKENS']:
        raise Exception("No DM_API_AUTH_TOKENS provided")

    if application.config['VCAP_SERVICES']:
        cf_services = json.loads(application.config['VCAP_SERVICES'])
        application.config['SQLALCHEMY_DATABASE_URI'] = cf_services[
            'postgres'][0]['credentials']['uri']

    from .metrics import metrics as metrics_blueprint, gds_metrics
    from .main import main as main_blueprint
    from .status import status as status_blueprint
    from .callbacks import callbacks as callbacks_blueprint

    application.register_blueprint(metrics_blueprint)
    application.register_blueprint(main_blueprint)
    application.register_blueprint(callbacks_blueprint,
                                   url_prefix='/callbacks')
    application.register_blueprint(status_blueprint)

    gds_metrics.init_app(application)

    return application
예제 #3
0
def create_app(config_name):
    application = Flask(__name__)
    application.config['DM_ENVIRONMENT'] = config_name

    init_app(
        application,
        configs[config_name],
        error_handlers=api_error_handlers,
    )

    if not application.config['DM_ANTIVIRUS_API_AUTH_TOKENS']:
        raise Exception("No DM_ANTIVIRUS_API_AUTH_TOKENS provided")

    if not application.config['DM_ANTIVIRUS_API_CALLBACK_AUTH_TOKENS']:
        raise Exception("No DM_ANTIVIRUS_API_CALLBACK_AUTH_TOKENS provided")

    from .metrics import metrics as metrics_blueprint, gds_metrics
    from .main import main as main_blueprint
    from .callbacks import callbacks as callbacks_blueprint
    from .status import status as status_blueprint

    application.register_blueprint(metrics_blueprint)
    application.register_blueprint(main_blueprint)
    application.register_blueprint(callbacks_blueprint,
                                   url_prefix='/callbacks')
    application.register_blueprint(status_blueprint)

    gds_metrics.init_app(application)

    return application
    def test_init_app_calls_optional_inits(self):
        bootstrap_mock = mock.Mock()
        data_api_client_mock = mock.Mock()
        db_mock = mock.Mock()
        login_manager_mock = mock.Mock()
        search_api_client_mock = mock.Mock()
        init_app(self.application, {},
                 bootstrap=bootstrap_mock,
                 data_api_client=data_api_client_mock,
                 db=db_mock,
                 login_manager=login_manager_mock,
                 search_api_client=search_api_client_mock)

        assert bootstrap_mock.init_app.call_args_list == [
            mock.call(self.application)
        ]
        assert data_api_client_mock.init_app.call_args_list == [
            mock.call(self.application)
        ]
        assert db_mock.init_app.call_args_list == [mock.call(self.application)]
        assert login_manager_mock.init_app.call_args_list == [
            mock.call(self.application)
        ]
        assert search_api_client_mock.init_app.call_args_list == [
            mock.call(self.application)
        ]
 def test_init_app_sets_request_id_config(self):
     init_app(self.application, {})
     assert self.application.config['DM_TRACE_ID_HEADERS'] == ('DM-Request-ID', "X-B3-TraceId")
     assert self.application.config['DM_SPAN_ID_HEADERS'] == ('X-B3-SpanId', )
     assert self.application.config['DM_PARENT_SPAN_ID_HEADERS'] == ('X-B3-ParentSpanId', )
     assert self.application.config['DM_IS_SAMPLED_HEADERS'] == ('X-B3-Sampled', )
     assert self.application.config['DM_DEBUG_FLAG_HEADERS'] == ('X-B3-Flags', )
     assert self.application.config['DM_REQUEST_ID_HEADER'] == 'DM-Request-ID'
 def test_init_app_sets_cookie_probe_config(self):
     init_app(self.application, {})
     assert self.application.config[
         'DM_COOKIE_PROBE_COOKIE_EXPECT_PRESENT'] is False
     assert self.application.config[
         'DM_COOKIE_PROBE_COOKIE_MAX_AGE'] == 31536000.0
     assert self.application.config[
         'DM_COOKIE_PROBE_COOKIE_NAME'] == 'dm_cookie_probe'
     assert self.application.config['DM_COOKIE_PROBE_COOKIE_VALUE'] == 'yum'
 def test_init_app_registers_custom_template_filters(self):
     init_app(self.application, {})
     expected_filters = [
         'capitalize_first', 'format_links', 'nbsp', 'smartjoin',
         'preserve_line_breaks', 'sub_country_codes', 'dateformat',
         'datetimeformat', 'datetodatetimeformat', 'displaytimeformat',
         'shortdateformat', 'timeformat', 'utcdatetimeformat',
         'utctoshorttimelongdateformat'
     ]
     for filter in expected_filters:
         assert filter in self.application.jinja_env.filters
 def setup(self):
     self.flask = Flask('test_app',
                        template_folder='tests/templates/',
                        static_url_path=self.config.ASSET_PATH)
     self.login_manager = LoginManager()
     init_app(
         self.flask,
         self.config,
         login_manager=self.login_manager,
         cache=self.cache,
     )
     init_frontend_app(self.flask, None, self.login_manager,
                       ['tests/templates'])
     self.app = self.flask.test_client()
    def test_init_uses_session(self, _mock_session):
        bootstrap_mock = mock.Mock()
        data_api_client_mock = mock.Mock()
        db_mock = mock.Mock()
        login_manager_mock = mock.Mock()
        search_api_client_mock = mock.Mock()
        dmutils.session = mock.Mock()
        init_app(self.application, {},
                 bootstrap=bootstrap_mock,
                 data_api_client=data_api_client_mock,
                 db=db_mock,
                 login_manager=login_manager_mock,
                 search_api_client=search_api_client_mock)

        dmutils.session.init_app.assert_called_once()
def create_app(config_name):
    application = Flask(__name__)

    init_app(
        application,
        configs[config_name],
        error_handlers=api_error_handlers,
    )

    if application.config['VCAP_SERVICES']:
        cf_services = json.loads(application.config['VCAP_SERVICES'])
        service = get_service_by_name_from_vcap_services(
            cf_services, application.config['DM_ELASTICSEARCH_SERVICE_NAME'])

        application.config['ELASTICSEARCH_HOST'] = service['credentials']['uri']

    elasticsearch_client.init_app(
        application,
        verify_certs=True,
    )

    from .metrics import metrics as metrics_blueprint, gds_metrics
    from .main import main as main_blueprint
    from .status import status as status_blueprint

    application.register_blueprint(metrics_blueprint)
    application.register_blueprint(status_blueprint)
    application.register_blueprint(main_blueprint)

    gds_metrics.init_app(application)

    # because the search api doesn't return any values in its bodies we would consider "secrets", we should be
    # able to safely gzip all response bodies without concerns about BREACH et al.
    DMGzipMiddleware(application, compress_by_default=True)

    return application
 def test_init_app_sets_logging_config(self):
     init_app(self.application, {})
     assert self.application.config['DM_LOG_LEVEL'] == 'INFO'
     assert self.application.config['DM_APP_NAME'] == 'none'
 def test_init_app_sets_digitalmarketplace_env_config(self):
     init_app(self.application, {})
     assert self.application.config['DM_ENVIRONMENT'] == 'development'
     assert self.application.config['ENV'] == 'development'