def test_current_app_and_config_not_visible(app): """Test current_app and config are not visible to template.""" app.config.update(SECRET_KEY='super secret') InvenioPages(app) app.register_blueprint(blueprint) with app.app_context(): page = Page( url='/dynamic', title='Dynamic page', content="{{SECRET_KEY}}", template_name='invenio_pages/dynamic.html', ) db.session.add(page) db.session.commit() with app.test_request_context('/dynamic'): assert app.config['SECRET_KEY'] not in render_page('/dynamic') page.content = '{{config.SECRET_KEY}}' db.session.commit() with pytest.raises(UndefinedError): render_page('/dynamic') page.content = "{{current_app.config['SECRET_KEY']}}" db.session.commit() with pytest.raises(UndefinedError): render_page('/dynamic')
def pages(): """Register CDS static pages.""" pages = [ Page( url="/about", title="About", description="About", content="Library about page", template_name="invenio_pages/default.html", ), Page( url="/terms", title="Terms", description="Terms", content="Terms and Privacy", template_name="invenio_pages/default.html", ), Page( url="/faq", title="F.A.Q.", description="F.A.Q.", content="Frequently Asked Questions", template_name="invenio_pages/default.html", ), ] with db.session.begin_nested(): Page.query.delete() db.session.add_all(pages) db.session.commit() click.echo("static pages created :)")
def test_current_app_and_config_not_visible(app): """Test current_app and config are not visible to template.""" app.config.update( SECRET_KEY='super secret' ) InvenioPages(app) app.register_blueprint(blueprint) with app.app_context(): page = Page( url='/dynamic', title='Dynamic page', content="{{SECRET_KEY}}", template_name='invenio_pages/dynamic.html', ) db.session.add(page) db.session.commit() with app.test_request_context('/dynamic'): assert app.config['SECRET_KEY'] not in render_page('/dynamic') page.content = '{{config.SECRET_KEY}}' db.session.commit() with pytest.raises(UndefinedError): render_page('/dynamic') page.content = "{{current_app.config['SECRET_KEY']}}" db.session.commit() with pytest.raises(UndefinedError): render_page('/dynamic')
def pages(): """Register CDS static pages.""" def page_data(page): return (pkg_resources.resource_stream( "cds_ils", os.path.join("static_pages", page)).read().decode("utf8")) pages = [ Page( url="/about", title="About", description="About", content=page_data("about.html"), template_name="invenio_pages/dynamic.html", ), Page( url="/terms", title="Terms", description="Terms", content=page_data("terms.html"), template_name="invenio_pages/dynamic.html", ), Page( url="/faq", title="F.A.Q.", description="F.A.Q.", content=page_data("faq.html"), template_name="invenio_pages/dynamic.html", ), Page( url="/contact", title="Contact", description="Contact", content=page_data("contact.html"), template_name="invenio_pages/dynamic.html", ), Page( url="/guide/search", title="Search guide", description="Search guide", content=page_data("search_guide.html"), template_name="invenio_pages/dynamic.html", ), ] with db.session.begin_nested(): Page.query.delete() db.session.add_all(pages) db.session.commit() click.echo("static pages created :)")
def test_page_versions(pages_fixture): app = pages_fixture InvenioPages(app) app.register_blueprint(blueprint) with app.app_context(): dog_page = Page.get_by_url('/dogs') dog_page.title = 'Just a dog!' db.session.commit() with app.app_context(): dog_page = Page.get_by_url('/dogs') assert 'Just a dog!' == dog_page.title assert 2 == dog_page.versions.count() assert 'Page for Dogs!' == dog_page.versions[0].title
def test_pre_existing_404_function(pages_fixture): """Test pre existing 404.""" app = pages_fixture app.existing_called = False def existing_handler(error): current_app.existing_called = True return error app.register_error_handler(404, existing_handler) InvenioPages(app) app.register_blueprint(blueprint) with app.test_request_context('/runtime/added'): with app.test_client() as client: resp = client.get('/runtime/added') assert resp.status_code == 404 assert app.existing_called new_page = Page( url='/runtime/added', title='Runtime added page!', content='added after initial page mapping.', template_name='invenio_pages/default.html', ) db.session.add(new_page) db.session.commit() resp = client.get('/runtime/added') assert resp.status_code == 200 assert 'added after initial page mapping.' in str(resp.get_data())
def test_page_repr(pages_fixture): app = pages_fixture InvenioPages(app) app.register_blueprint(blueprint) with app.app_context(): dog_page = Page.get_by_url('/dogs/shiba') assert dog_page.__repr__() == 'URL: /dogs/shiba, title: Page for doge!'
def get(self, page_id): """Get the details of requested page.""" try: page = Page.get_by_id(page_id) except NoResultFound: abort(404) # check if the page got a new version etag = str(len(page.versions.all())) self.check_etag(etag) response = self.make_response( page, links_item_factory=default_links_item_factory) response.set_etag(etag) return response
def pages_fixture(app): """Page fixtures.""" InvenioPages(app) InvenioPagesREST(app) with app.app_context(): pages = [ Page( url='/dogs', title='Page for Dogs!', content='Generic dog.', template_name='invenio_pages/default.html', ), Page( url='/dogs/shiba', title='Page for doge!', content='so doge!', template_name='invenio_pages/default.html', ), Page( url='/cows/', title='Page for Cows!', content='Generic cow.', template_name='invenio_pages/default.html', ), Page( url='/htmldog', title='Page for modern dogs!', content='<h1>HTML aware dog.</h1>.\n' '<p class="test">paragraph<br /></p>', template_name='invenio_pages/default.html', ), ] for page in pages: db.session.add(page) db.session.commit() return app
def pages(): """Register CDS static pages.""" pages = [ Page(url='/about', title='About', description='About', content='Library about page', template_name='invenio_pages/default.html'), Page(url='/terms', title='Terms', description='Terms', content='Terms and Privacy', template_name='invenio_pages/default.html'), Page(url='/faq', title='F.A.Q.', description='F.A.Q.', content='Frequently Asked Questions', template_name='invenio_pages/default.html'), ] with db.session.begin_nested(): Page.query.delete() db.session.add_all(pages) db.session.commit() click.echo('static pages created :)')
def pages(): """Register CDS static pages.""" def page_data(page): return pkg_resources.resource_stream( 'invenio_app_ils', os.path.join('templates/static_pages', page)).read().decode('utf8') pages = [ Page(url='/about', title='About', description='About', content=page_data('about.html'), template_name='invenio_pages/default.html'), Page(url='/contact', title='Contact', description='Contact', content=page_data('contact.html'), template_name='invenio_pages/default.html'), ] with db.session.begin_nested(): Page.query.delete() db.session.add_all(pages) db.session.commit() click.echo('static pages created :)')
def test_runtime_added_page(pages_fixture): """Test runtime added page.""" app = pages_fixture InvenioPages(app) app.register_blueprint(blueprint) with app.test_request_context('/runtime/added'): with app.test_client() as client: resp = client.get('/runtime/added') assert resp.status_code == 404 new_page = Page( url='/runtime/added', title='Runtime added page!', content='added after initial page mapping.', template_name='invenio_pages/default.html', ) db.session.add(new_page) db.session.commit() resp = client.get('/runtime/added') assert resp.status_code == 200 assert 'added after initial page mapping.' in str(resp.get_data())
def pages(): """Register CDS static pages.""" def page_data(page): return pkg_resources.resource_stream( 'cds.modules.fixtures', os.path.join('data/pages', page) ).read().decode('utf8') pages = [ Page(url='/about', title='About', description='About', content=page_data('about.html'), template_name='invenio_pages/dynamic.html'), Page(url='/contact', title='Contact', description='Contact', content=page_data('contact.html'), template_name='invenio_pages/dynamic.html'), Page(url='/faq', title='FAQ', description='FAQ', content=page_data('faq.html'), template_name='invenio_pages/dynamic.html'), Page(url='/feedback', title='Feedback', description='Feedback', content=page_data('feedback.html'), template_name='invenio_pages/dynamic.html'), Page(url='/help', title='Help', description='Help', content=page_data('help.html'), template_name='invenio_pages/dynamic.html'), Page(url='/terms', title='Terms of Use', description='Terms of Use', content=page_data('terms_of_use.html'), template_name='invenio_pages/dynamic.html') ] with db.session.begin_nested(): Page.query.delete() db.session.add_all(pages) db.session.commit() click.echo('DONE :)')
def test_page_content_dynamic(app): """Test page content.""" app.config['PAGES_WHITELIST_CONFIG_KEYS'] = ['MYVAR'] InvenioPages(app) app.register_blueprint(blueprint) content = 'dynamic-content' with app.app_context(): app.config['MYVAR'] = content page = Page( url='/dynamic', title='Dynamic page', content='{{MYVAR}}', template_name='invenio_pages/dynamic.html', ) db.session.add(page) db.session.commit() with app.test_request_context('/dynamic'): assert content in render_page('/dynamic') with app.test_client() as client: resp = client.get('/dynamic') assert resp.status_code == 200 assert content in resp.get_data(as_text=True)