def svc(app: Application) -> Iterator[Union[Iterator, Iterator[WhooshIndexService]]]: svc = app.services["indexing"] with app.app_context(): svc.start() # pyre-fixme[7]: Expected `Iterator[Union[Iterator[Any], # Iterator[WhooshIndexService]]]` but got `Generator[Service, None, None]`. yield svc
def test_datetime_field_naive(app: Application) -> None: """Test fields supports date with year < 1900.""" patch_babel(app) obj = mock.Mock() headers = {"Accept-Language": "fr-FR,fr;q=0.8"} with app.test_request_context(headers=headers): # NAIVE mode: dates without timezone. Those are the problematic ones # when year < 1900: strptime will raise an Exception use naive dates; by # default field = fields.DateTimeField().bind(Form(), "dt") field.process_formdata(["17/06/1789 | 10:42"]) # UTC stored assert field.data.tzinfo is pytz.UTC expected_datetime = datetime.datetime(1789, 6, 17, 10, 42, tzinfo=pytz.UTC) assert field.data == expected_datetime # naive stored field.populate_obj(obj, "dt") assert obj.dt == datetime.datetime(1789, 6, 17, 10, 42)
def svc( app: Application ) -> Iterator[Union[Iterator, Iterator[WhooshIndexService]]]: svc = app.services["indexing"] with app.app_context(): svc.start() yield svc
def test_datefield_force_4digit_year(app: Application) -> None: patch_babel(app) # use 'en': short date pattern is 'M/d/yy' headers = {"Accept-Language": "en"} with app.test_request_context(headers=headers): field = fields.DateField().bind(Form(), "dt") field.data = datetime.date(2011, 1, 23) assert field._value() == "1/23/2011"
def test_date_field(app: Application) -> None: """Test fields supports date with year < 1900.""" patch_babel(app) headers = {"Accept-Language": "fr-FR,fr;q=0.8"} with app.test_request_context(headers=headers): field = fields.DateField().bind(Form(), "dt") field.process_formdata(["17/06/1789"]) assert field.data == datetime.date(1789, 6, 17) assert field._value() == "17/06/1789"
def test_default_view_decorator(app: Application, registry: Registry, test_request_context: RequestContext) -> None: bp = Blueprint("registry", __name__, url_prefix="/blueprint") @default_view(bp, RegEntity) @bp.route("/<int:object_id>") def view(object_id): pass obj = RegEntity(id=1) # blueprint not registered: no rule set with raises(KeyError): registry.url_for(obj) # blueprint registered: default view is set app.register_blueprint(bp) assert registry.url_for(obj) == "/blueprint/1" assert (registry.url_for( obj, _external=True) == "http://localhost.localdomain/blueprint/1")
def test_get_redirect_target(app: Application, app_context: AppContext) -> None: get_redirect_target = views.get_redirect_target form_url = partial(url_for, "login.login_form") with app.test_request_context(form_url()): assert get_redirect_target() == "" url_root = request.url_root[:-1] with app.test_request_context(form_url(next="/")): assert get_redirect_target() == url_root + "/" # test "next" from referer referrer = url_root + "/some/path" with app.test_request_context(form_url(), headers=[("Referer", referrer)]): assert get_redirect_target() == referrer # don't cycle if coming from 'login.*' page, like this kind of cycle: # forgot password form -> login page -> success # -> redirect(next = forgot password form) -> ... referrer = url_root + url_for("login.forgotten_pw") with app.test_request_context(form_url(), headers=[("Referer", referrer)]): assert get_redirect_target() == "" # test open redirect is forbidden with app.test_request_context(form_url(next="http://google.com/test")): assert get_redirect_target() == "" # open redirect through malicious construct and browser not checking # Location with app.test_request_context(form_url(next="/////google.com")): assert get_redirect_target() == url_root + "///google.com"
def __init__( self, app: Application, modules: None = None, name: None = None ) -> None: if name is None: name = self.__class__.__module__ modules_signature = ",".join(str(module.id) for module in self.modules) name = name + "-" + modules_signature self.name = name self.app = app app.extensions[name] = self if modules: self.modules = modules for module in self.modules: self.add_module(module)
def test_datetime_field(app: Application) -> None: """Test fields supports date with year < 1900.""" assert "fr" in app.config["BABEL_ACCEPT_LANGUAGES"] patch_babel(app) obj = mock.Mock() headers = {"Accept-Language": "fr-FR,fr;q=0.8"} with app.test_request_context(headers=headers): field = fields.DateTimeField(use_naive=False).bind(Form(), "dt") field.process_formdata(["17/06/1789 | 10:42"]) # 1789: applied offset for HongKong is equal to LMT+7:37:00, # thus we compare with tzinfo=user_tz expected_datetime = datetime.datetime(1789, 6, 17, 10, 42, tzinfo=USER_TZ) assert field.data == expected_datetime # UTC stored assert field.data.tzinfo is pytz.UTC # displayed in user current timezone assert field._value() == "17/06/1789 10:42" # non-naive mode: test process_data change TZ to user's TZ field.process_data(field.data) assert field.data.tzinfo is USER_TZ assert field.data == expected_datetime field.populate_obj(obj, "dt") assert obj.dt == expected_datetime # test more recent date: offset is GMT+8 field.process_formdata(["23/01/2011 | 10:42"]) expected_datetime = datetime.datetime(2011, 1, 23, 2, 42, tzinfo=pytz.utc) assert field.data == expected_datetime
def __init__(self, app: Application) -> None: app.extensions["uploads"] = self app.add_template_global(self, "uploads") app.register_blueprint(blueprint) signals.register_js_api.connect(self._do_register_js_api) self.config: Dict[str, Any] = {} self.config.update(DEFAULT_CONFIG) self.config.update(app.config.get("FILE_UPLOADS", {})) app.config["FILE_UPLOADS"] = self.config # celery schedule CELERYBEAT_SCHEDULE = app.config.setdefault("CELERYBEAT_SCHEDULE", {}) if CLEANUP_SCHEDULE_ID not in CELERYBEAT_SCHEDULE: CELERYBEAT_SCHEDULE[CLEANUP_SCHEDULE_ID] = DEFAULT_CLEANUP_SCHEDULE path = self.UPLOAD_DIR = app.data_dir / "uploads" if not path.exists(): path.mkdir(mode=0o775, parents=True) path.resolve()
def register_plugin(app: Application) -> None: from .views import search app.register_blueprint(search) register_js_api.connect(_do_register_js_api)
def registry(app: Application) -> Registry: app.default_view = Registry() return app.default_view
def init_extensions(self): BaseApplication.init_extensions(self) sbe.init_app(self) repository.init_app(self) converter.init_app(self)
def __init__(self, name='abilian_sbe', config=None, **kwargs): BaseApplication.__init__(self, name, config=config, **kwargs) loader = jinja2.PackageLoader('abilian.sbe', 'templates') self.register_jinja_loaders(loader)
def default_app_factory(): from abilian.app import Application return Application()