def test_init(): """Test extension initialization.""" app = Flask('testapp') ext = InvenioPages(app) assert 'invenio-pages' in app.extensions app = Flask('testapp') ext = InvenioPages() assert 'invenio-pages' not in app.extensions ext.init_app(app) assert 'invenio-pages' in app.extensions
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 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 test_non_existing_page(pages_fixture): """Test non-existing page content.""" app = pages_fixture InvenioPages(app) app.register_blueprint(blueprint) # render_page function with app.test_request_context('/invalid/url/errors'): with pytest.raises(NotFound): render_page('/invalid/url/errors')
def test_page_content(pages_fixture): """Test page content.""" app = pages_fixture InvenioPages(app) app.register_blueprint(blueprint) with app.app_context(): with app.test_client() as client: resp = client.get('/dogs/shiba') assert resp.status_code == 200 assert 'so doge!' in str(resp.get_data()) assert 'so doge!' in str(render_page('/dogs/shiba'))
def test_fixture_pages(app, script_info, db, client): """Test load pages fixtures.""" InvenioPages(app) Page.query.delete() assert len(Page.query.all()) == 0 about_response = client.get('/about') assert about_response.status_code == 404 runner = CliRunner() res = runner.invoke(cli_pages, [], obj=script_info) assert res.exit_code == 0 pages = Page.query.all() assert len(pages) == 6 about_response = client.get('/about') assert about_response.status_code == 200
def test_pages_admin(admin_fixture): """Test field validator.""" app = admin_fixture InvenioPages(app) app.register_blueprint(blueprint) with app.test_request_context(): with app.test_client() as client: resp = client.get('/admin/page/') assert resp.status_code == 200 for page in Page.query.all(): assert page.url in str(resp.get_data()) assert page.title in str(resp.get_data()) resp = client.get('/admin/page/new/') assert resp.status_code == 200
def test_template_exists(app): """Test field validator.""" InvenioPages(app) app.register_blueprint(blueprint) class Field(object): def __init__(self, data): self.data = data with app.app_context(): with pytest.raises(ValidationError): template_exists(None, Field('inexistent_template')) template_exists(None, Field('invenio_pages/base.html')) template_exists(None, Field('invenio_pages/default.html')) template_exists(None, Field('invenio_pages/edit.html'))
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_init(): """Test extension initialization.""" app = Flask('testapp') ext = InvenioPages(app) assert 'invenio-pages' in app.extensions app = Flask('testapp') ext = InvenioPages() assert 'invenio-pages' not in app.extensions ext.init_app(app) assert 'invenio-pages' in app.extensions
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 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 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)
from invenio_pages.views import blueprint # Create Flask application app = Flask(__name__) app.config.update( PAGES_TEMPLATES=[ ('invenio_pages/default.html', 'Default'), ('app/mytemplate.html', 'App'), ], SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:///app.db'), ) InvenioDB(app) InvenioPages(app) app.register_blueprint(blueprint) @app.cli.group() def fixtures(): """Command for working with test data.""" @fixtures.command() def pages(): """Load pages.""" p1 = Page( url='/example1', title='My page with default template',