def test_mapbox_token_tween(temporary_directory, redis_url):

    class App(Framework, MapboxApp):
        pass

    @App.webasset_output()
    def get_output_path():
        return temporary_directory

    @App.path(path='')
    class Root(object):
        pass

    @App.html(model=Root)
    def view_root(self, request):
        return '<body></body>'

    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(mapbox_token='pk.asdf', redis_url=redis_url)
    app.namespace = 'foo'
    app.set_application_id('foo/bar')

    assert '<body data-mapbox-token="pk.asdf"></body>' in Client(app).get('/')
def test_alternate_id_property(es_url, postgres_dsn):

    class App(Framework, ElasticsearchApp):
        pass

    Base = declarative_base()

    class User(Base, ORMSearchable):
        __tablename__ = 'users'

        name = Column(Text, primary_key=True)
        fullname = Column(Text, nullable=False)

        @property
        def es_suggestion(self):
            return self.name

        es_id = 'name'
        es_properties = {
            'fullname': {'type': 'string'},
        }
        es_language = 'en'
        es_public = True

    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(
        dsn=postgres_dsn,
        base=Base,
        elasticsearch_hosts=[es_url]
    )

    app.namespace = 'users'
    app.set_application_id('users/corporate')

    session = app.session()
    session.add(User(
        name="root",
        fullname="Lil' Root"
    ))
    session.add(User(
        name="user",
        fullname="Lil' User"
    ))
    transaction.commit()
    app.es_indexer.process()
    app.es_client.indices.refresh(index='_all')

    assert app.es_search().count() == 2
    assert app.es_search().query('match', fullname='Root').count() == 1

    assert app.es_search().execute().query(type='users').count() == 2
    assert len(app.es_search().execute().load()) == 2

    root = app.es_search().query('match', fullname='Root').execute()[0]
    assert root.query().count() == 1
    assert root.load().name == 'root'
    assert root.load().fullname == "Lil' Root"
def app(request, postgres_dsn, temporary_path, redis_url):

    with (temporary_path / 'bust').open('w') as f:
        f.write('\n'.join((
            f'#!/bin/bash',
            f'touch {temporary_path}/$1'
        )))

    signing_services = (temporary_path / 'signing-services')
    signing_services.mkdir()

    cert_file = module_path('onegov.file', 'tests/fixtures/test.crt')
    cert_key = module_path('onegov.file', 'tests/fixtures/test.crt')

    with (signing_services / '__default__.yml').open('w') as f:
        f.write(textwrap.dedent(f"""
            name: swisscom_ais
            parameters:
                customer: foo
                key_static: bar
                cert_file: {cert_file}
                cert_key: {cert_key}
        """))

    os.chmod(temporary_path / 'bust', 0o775)

    backend = request.param

    class App(Framework, DepotApp):
        anonymous_access = False

    @App.permission_rule(model=object, permission=object, identity=None)
    def test_has_permission_not_logged_in(app, identity, model, permission):
        if app.anonymous_access:
            return True

        return has_permission_not_logged_in(app, identity, model, permission)

    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(
        dsn=postgres_dsn,
        depot_backend=backend,
        depot_storage_path=str(temporary_path),
        frontend_cache_buster=f'{temporary_path}/bust',
        redis_url=redis_url,
        signing_services=str(signing_services),
        yubikey_client_id='foo',
        yubikey_secret_key='dGhlIHdvcmxkIGlzIGNvbnRyb2xsZWQgYnkgbGl6YXJkcyE='
    )

    app.namespace = 'apps'
    app.set_application_id('apps/my-app')

    return app
def test_town_create(onboarding_app, temporary_directory, smtp):
    c = Client(onboarding_app)
    a = c.get('/for-towns/1')

    a.form['name'] = 'New York'
    a.form['user'] = '******'
    a.form['color'] = '#ff00ff'
    a = a.form.submit().follow()

    assert 'New York' in a
    assert '*****@*****.**' in a
    assert '#f0f' in a
    assert 'new-york.example.org' in a

    a = a.form.submit()

    assert 'https://new-york.example.org' in a
    assert '*****@*****.**' in a
    assert len(smtp.outbox) == 1

    username = '******'
    password = a.pyquery('.product dd:nth-child(4)').text()

    scan_morepath_modules(onegov.town.TownApp)
    morepath.commit(onegov.town.TownApp)

    town = TownApp()
    town.namespace = onboarding_app.onboarding['onegov.town']['namespace']
    town.configure_application(
        dsn=onboarding_app.dsn,
        filestorage='fs.osfs.OSFS',
        filestorage_options={
            'root_path': temporary_directory,
            'create': True
        },
        identity_secure=False,
        disable_memcached=True,
        enable_elasticsearch=False,
        depot_backend='depot.io.memory.MemoryFileStorage'
    )
    town.set_application_id(town.namespace + '/' + 'new_york')
    town.settings.cronjobs = Bunch(enabled=False)

    c = Client(town)
    p = c.get('/')

    assert "Willkommen bei OneGov" in p
    assert "New York" in p

    p = c.get('/auth/login')
    p.forms[1]['username'] = username
    p.forms[1]['password'] = password
    p = p.forms[1].submit().follow()

    assert 'Benutzerprofil' in p
def test_town_create(onboarding_app, temporary_directory, smtp, redis_url):
    c = Client(onboarding_app)
    a = c.get('/for-towns/1')

    a.form['name'] = 'New York'
    a.form['user'] = '******'
    a.form['color'] = '#ff00ff'
    a = a.form.submit().follow()

    assert 'New York' in a
    assert '*****@*****.**' in a
    assert '#f0f' in a
    assert 'new-york.example.org' in a

    a = a.form.submit()

    assert 'https://new-york.example.org' in a
    assert '*****@*****.**' in a
    assert len(smtp.outbox) == 1

    username = '******'
    password = a.pyquery('.product dd:nth-child(4)').text()

    scan_morepath_modules(onegov.town.TownApp)
    morepath.commit(onegov.town.TownApp)

    town = TownApp()
    town.namespace = onboarding_app.onboarding['onegov.town']['namespace']
    town.configure_application(
        dsn=onboarding_app.dsn,
        filestorage='fs.osfs.OSFS',
        filestorage_options={
            'root_path': temporary_directory,
            'create': True
        },
        identity_secure=False,
        redis_url=redis_url,
        enable_elasticsearch=False,
        depot_backend='depot.io.memory.MemoryFileStorage')
    town.set_application_id(town.namespace + '/' + 'new_york')
    town.settings.cronjobs = Bunch(enabled=False)

    c = Client(town)
    p = c.get('/')

    assert "Willkommen bei der OneGov Cloud" in p
    assert "New York" in p

    p = c.get('/auth/login')
    p.forms[1]['username'] = username
    p.forms[1]['password'] = password
    p = p.forms[1].submit().follow()

    assert 'Benutzerprofil' in p
Exemple #6
0
def test_setup_database(postgres_dsn):
    Base = declarative_base()

    class App(Framework, LibresIntegration):
        pass

    class Document(Base):
        __tablename__ = 'documents'
        id = Column(Integer, primary_key=True)

    @App.path(path='/')
    class Root(object):
        pass

    @App.json(model=Root)
    def get_root(self, request):
        return []

    # this is required for the transactions to actually work, usually this
    # would be onegov.server's job
    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(dsn=postgres_dsn, base=Base)
    app.namespace = 'libres'
    app.set_application_id('libres/foo')

    c = Client(app)
    c.get('/')

    tables = app.session().execute(
        "SELECT table_name FROM information_schema.tables "
        "WHERE table_schema = 'public'"
    )

    assert not tables.fetchall()

    tables = app.session().execute(
        "SELECT table_name FROM information_schema.tables "
        "WHERE table_schema = 'libres-foo'"
    )

    tables = set(r[0] for r in tables.fetchall())

    assert tables == {
        'documents',
        'resources',
        'allocations',
        'reserved_slots',
        'reservations'
    }

    app.session_manager.dispose()
Exemple #7
0
def onboarding_app(postgres_dsn, temporary_directory, smtp, es_url, redis_url):

    scan_morepath_modules(onegov.onboarding.OnboardingApp)
    morepath.commit(onegov.onboarding.OnboardingApp)

    app = onegov.onboarding.OnboardingApp()
    app.namespace = 'test_' + uuid4().hex
    app.configure_application(
        dsn=postgres_dsn,
        filestorage='fs.osfs.OSFS',
        filestorage_options={
            'root_path': os.path.join(temporary_directory, 'file-storage'),
            'create': True
        },
        identity_secure=False,
        depot_backend='depot.io.memory.MemoryFileStorage',
        redis_url=redis_url,
        onboarding={
            'onegov.town': {
                'namespace': 'town_' + uuid4().hex,
                'domain': 'example.org',
                'configuration': {
                    'depot_backend': 'depot.io.memory.MemoryFileStorage'
                }
            }
        },
        elasticsearch_hosts=[es_url]
    )
    app.set_application_id(app.namespace + '/' + 'test')

    app.mail = {
        'marketing': {
            'host': smtp.address[0],
            'port': smtp.address[1],
            'force_tls': False,
            'username': None,
            'password': None,
            'use_directory': False,
            'sender': '*****@*****.**'
        },
        'transactional': {
            'host': smtp.address[0],
            'port': smtp.address[1],
            'force_tls': False,
            'username': None,
            'password': None,
            'use_directory': False,
            'sender': '*****@*****.**'
        }
    }

    yield app
Exemple #8
0
def test_transaction_integration(postgres_dsn):
    Base = declarative_base()

    class App(Framework, LibresIntegration):
        pass

    class Document(Base):
        __tablename__ = 'documents'
        id = Column(Integer, primary_key=True)

    @App.path(path='/')
    class Root(object):
        pass

    @App.json(model=Root)
    def handle_root(self, request):
        collection = ResourceCollection(request.app.libres_context)

        resource = collection.add('Test', 'Europe/Zurich')
        scheduler = resource.get_scheduler(request.app.libres_context)
        scheduler.allocate(
            (datetime(2015, 7, 30, 11), datetime(2015, 7, 30, 12))
        )

        # this will fail and then abort everything
        request.app.session().add(Document(id=1))
        request.app.session().add(Document(id=1))

    # this is required for the transactions to actually work, usually this
    # would be onegov.server's job
    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(dsn=postgres_dsn, base=Base)
    app.namespace = 'libres'
    app.set_application_id('libres/foo')

    c = Client(app)
    try:
        c.get('/', expect_errors=True)
    except:
        pass

    collection = ResourceCollection(app.libres_context)
    assert collection.query().count() == 0
Exemple #9
0
def test_setup_database(postgres_dsn):
    Base = declarative_base()

    class App(Framework, LibresIntegration):
        pass

    class Document(Base):
        __tablename__ = "documents"
        id = Column(Integer, primary_key=True)

    @App.path(path="/")
    class Root(object):
        pass

    @App.json(model=Root)
    def get_root(self, request):
        return []

    # this is required for the transactions to actually work, usually this
    # would be onegov.server's job
    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(dsn=postgres_dsn, base=Base)
    app.namespace = "libres"
    app.set_application_id("libres/foo")

    c = Client(app)
    c.get("/")

    tables = app.session().execute("SELECT table_name FROM information_schema.tables " "WHERE table_schema = 'public'")

    assert not tables.fetchall()

    tables = app.session().execute(
        "SELECT table_name FROM information_schema.tables " "WHERE table_schema = 'libres-foo'"
    )

    tables = set(r[0] for r in tables.fetchall())

    assert tables == {"documents", "resources", "allocations", "reserved_slots", "reservations"}

    app.session_manager.dispose()
Exemple #10
0
def test_transaction_integration(postgres_dsn):
    Base = declarative_base()

    class App(Framework, LibresIntegration):
        pass

    class Document(Base):
        __tablename__ = "documents"
        id = Column(Integer, primary_key=True)

    @App.path(path="/")
    class Root(object):
        pass

    @App.json(model=Root)
    def handle_root(self, request):
        collection = ResourceCollection(request.app.libres_context)

        resource = collection.add("Test", "Europe/Zurich")
        scheduler = resource.get_scheduler(request.app.libres_context)
        scheduler.allocate((datetime(2015, 7, 30, 11), datetime(2015, 7, 30, 12)))

        # this will fail and then abort everything
        request.app.session().add(Document(id=1))
        request.app.session().add(Document(id=1))

    # this is required for the transactions to actually work, usually this
    # would be onegov.server's job
    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(dsn=postgres_dsn, base=Base)
    app.namespace = "libres"
    app.set_application_id("libres/foo")

    c = Client(app)
    try:
        c.get("/", expect_errors=True)
    except:
        pass

    collection = ResourceCollection(app.libres_context)
    assert collection.query().count() == 0
Exemple #11
0
def onboarding_app(postgres_dsn, temporary_directory, smtp):

    scan_morepath_modules(onegov.onboarding.OnboardingApp)
    morepath.commit(onegov.onboarding.OnboardingApp)

    app = onegov.onboarding.OnboardingApp()
    app.namespace = 'test_' + uuid4().hex
    app.configure_application(
        dsn=postgres_dsn,
        filestorage='fs.osfs.OSFS',
        filestorage_options={
            'root_path': os.path.join(temporary_directory, 'file-storage'),
            'create': True
        },
        identity_secure=False,
        disable_memcached=True,
        depot_backend='depot.io.memory.MemoryFileStorage',
        onboarding={
            'onegov.town': {
                'namespace': 'town_' + uuid4().hex,
                'domain': 'example.org'
            }
        },

    )
    app.set_application_id(app.namespace + '/' + 'test')

    app.mail_host, app.mail_port = smtp.address
    app.mail_sender = '*****@*****.**'
    app.mail_force_tls = False
    app.mail_username = None
    app.mail_password = None
    app.mail_use_directory = False
    app.smtp = smtp

    yield app
def test_orm_polymorphic(es_url, postgres_dsn):

    class App(Framework, ElasticsearchApp):
        pass

    Base = declarative_base()

    class Page(Base, ORMSearchable):
        __tablename__ = 'pages'

        es_properties = {
            'content': {'type': 'localized'}
        }
        es_language = 'en'
        es_public = True

        @property
        def es_suggestion(self):
            return self.content

        id = Column(Integer, primary_key=True)
        content = Column(Text, nullable=True)
        type = Column(Text, nullable=False)

        __mapper_args__ = {
            "polymorphic_on": 'type'
        }

    class Topic(Page):
        __mapper_args__ = {'polymorphic_identity': 'topic'}
        es_type_name = 'topic'

    class News(Page):
        __mapper_args__ = {'polymorphic_identity': 'news'}
        es_type_name = 'news'

    class Breaking(News):
        __mapper_args__ = {'polymorphic_identity': 'breaking'}
        es_type_name = 'breaking'

    scan_morepath_modules(App)
    morepath.commit()

    app = App()
    app.configure_application(
        dsn=postgres_dsn,
        base=Base,
        elasticsearch_hosts=[es_url]
    )

    app.namespace = 'pages'
    app.set_application_id('pages/site')

    session = app.session()
    session.add(Topic(content="Topic", type='topic'))
    session.add(News(content="News", type='news'))
    session.add(Breaking(content="Breaking", type='breaking'))

    def update():
        transaction.commit()
        app.es_indexer.process()
        app.es_client.indices.refresh(index='_all')

    update()
    assert app.es_search().count() == 3

    newsitem = session.query(Page).filter(Page.type == 'news').one()
    assert isinstance(newsitem, News)

    newsitem.content = 'Story'
    update()
    assert app.es_search().query('match', content='story').count() == 1

    session.query(Page).filter(Page.type == 'news').delete()
    update()
    assert app.es_search().count() == 2

    session.delete(session.query(Page).filter(Page.type == 'breaking').one())
    update()
    assert app.es_search().count() == 1

    session.query(Page).delete()
    update()
    assert app.es_search().count() == 0
def test_suggestions(es_url, postgres_dsn):

    class App(Framework, ElasticsearchApp):
        pass

    Base = declarative_base()

    class Document(Base, ORMSearchable):
        __tablename__ = 'documents'

        id = Column(Integer, primary_key=True)
        title = Column(Text, nullable=False)
        public = Column(Boolean, nullable=False)
        language = Column(Text, nullable=False)

        es_properties = {
            'title': {'type': 'localized'}
        }

        @property
        def es_public(self):
            return self.public

        @property
        def es_language(self):
            return self.language

    class Person(Base, ORMSearchable):
        __tablename__ = 'people'
        id = Column(Integer, primary_key=True)
        first_name = Column(Text, nullable=False)
        last_name = Column(Text, nullable=False)

        @property
        def title(self):
            return ' '.join((self.first_name, self.last_name))

        es_properties = {'title': {'type': 'localized'}}
        es_public = True
        es_language = 'en'

        @property
        def es_suggestion(self):
            return [
                ' '.join((self.first_name, self.last_name)),
                ' '.join((self.last_name, self.first_name))
            ]

    scan_morepath_modules(App)
    morepath.commit()

    app = App()
    app.configure_application(
        dsn=postgres_dsn,
        base=Base,
        elasticsearch_hosts=[es_url]
    )

    app.namespace = 'documents'
    app.set_application_id('documents/home')

    session = app.session()
    session.add(Document(
        title="Public Document",
        language='en',
        public=True
    ))
    session.add(Document(
        title="Private Document",
        language='en',
        public=False
    ))
    session.add(Document(
        title="Öffentliches Dokument",
        language='de',
        public=True
    ))
    session.add(Document(
        title="Privates Dokument",
        language='de',
        public=False
    ))
    session.add(Person(
        first_name='Jeff',
        last_name='Winger'
    ))
    transaction.commit()
    app.es_indexer.process()
    app.es_client.indices.refresh(index='_all')

    assert set(app.es_suggestions(query='p')) == {"Public Document"}
    assert set(app.es_suggestions(query='p', include_private=True)) == {
        "Public Document",
        "Private Document",
        "Privates Dokument"
    }
    assert set(app.es_suggestions(query='ö', languages=['de'])) == {
        "Öffentliches Dokument",
    }

    assert set(app.es_suggestions(
        query='ö', languages=['de'], include_private=True)) == {
        "Öffentliches Dokument",
    }

    assert set(app.es_suggestions(
        query='p', languages=['de'], include_private=True)) == {
        "Privates Dokument",
    }

    assert set(app.es_suggestions(query='j', languages=['en'])) == {
        'Jeff Winger'
    }

    assert set(app.es_suggestions(query='w', languages=['en'])) == {
        'Jeff Winger'
    }
def test_search_query(es_url, postgres_dsn):

    class App(Framework, ElasticsearchApp):
        pass

    Base = declarative_base()

    class Document(Base, ORMSearchable):
        __tablename__ = 'documents'

        id = Column(Integer, primary_key=True)
        title = Column(Text, nullable=False)
        body = Column(Text, nullable=True)
        public = Column(Boolean, nullable=False)
        language = Column(Text, nullable=False)

        es_properties = {
            'title': {'type': 'localized'},
            'body': {'type': 'localized'}
        }

        @property
        def es_suggestion(self):
            return self.title

        @property
        def es_public(self):
            return self.public

        @property
        def es_language(self):
            return self.language

    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(
        dsn=postgres_dsn,
        base=Base,
        elasticsearch_hosts=[es_url]
    )

    app.namespace = 'documents'
    app.set_application_id('documents/home')

    session = app.session()
    session.add(Document(
        title="Public",
        body="This document can be seen by anyone",
        language='en',
        public=True
    ))
    session.add(Document(
        title="Private",
        body="This document is a secret",
        language='en',
        public=False
    ))
    session.add(Document(
        title="Öffentlich",
        body="Dieses Dokument kann jeder sehen",
        language='de',
        public=True
    ))
    session.add(Document(
        title="Privat",
        body="Dieses Dokument ist geheim",
        language='de',
        public=False
    ))
    transaction.commit()
    app.es_indexer.process()
    app.es_client.indices.refresh(index='_all')

    assert app.es_search().execute().hits.total == 2
    assert app.es_search(include_private=True).execute().hits.total == 4

    result = app.es_search(languages=['en']).execute()
    assert result.hits.total == 1

    result = app.es_search(languages=['de'], include_private=True).execute()
    assert result.hits.total == 2

    search = app.es_search(languages=['de'])
    assert search.query('match', body='Dokumente').execute().hits.total == 1

    search = app.es_search(languages=['de'], include_private=True)
    assert search.query('match', body='Dokumente').execute().hits.total == 2

    # test result loading in one query
    result = app.es_search(languages=['de'], include_private=True).execute()
    records = result.load()
    assert len(records) == 2
    assert isinstance(records[0], Document)
    assert True in (records[0].es_public, records[1].es_public)
    assert False in (records[0].es_public, records[1].es_public)

    # test result loading query
    result = app.es_search(languages=['de'], include_private=True).execute()
    query = result.query(type='documents')
    assert query.count() == 2
    assert query.filter(Document.public == True).count() == 1

    # test single result loading
    document = app.es_search(languages=['de']).execute()[0].load()
    assert document.title == "Öffentlich"
    assert document.public

    # test single result query
    document = app.es_search(languages=['de']).execute()[0].query().one()
    assert document.title == "Öffentlich"
    assert document.public
Exemple #15
0
def new_town_app(postgres_dsn, filestorage, town_password, smtp,
                 form_definitions, es_url=None):

    config = setup()
    scan_morepath_modules(onegov.town.TownApp, config)
    config.commit()

    app = onegov.town.TownApp()
    app.namespace = 'test_' + uuid4().hex
    app.configure_application(
        dsn=postgres_dsn,
        filestorage='fs.osfs.OSFS',
        filestorage_options={
            'root_path': filestorage,
            'create': True
        },
        identity_secure=False,
        disable_memcached=True,
        enable_elasticsearch=es_url and True or False,
        elasticsearch_hosts=[es_url]
    )
    app.set_application_id(app.namespace + '/' + 'test')
    add_initial_content(
        app.libres_registry,
        app.session_manager,
        'Govikon',
        form_definitions
    )

    # cronjobs leave lingering sessions open, in real life this is not a
    # problem, but in testing it leads to connection pool exhaustion
    app.registry.settings.cronjobs = Bunch(enabled=False)

    session = app.session()

    town = session.query(Town).one()
    town.meta['reply_to'] = '*****@*****.**'

    app.mail_host, app.mail_port = smtp.address
    app.mail_sender = '*****@*****.**'
    app.mail_force_tls = False
    app.mail_username = None
    app.mail_password = None
    app.mail_use_directory = False
    app.smtp = smtp

    # usually we don't want to create the users directly, anywhere else you
    # *need* to go through the UserCollection. Here however, we can improve
    # the test speed by not hashing the password for every test.

    session.add(User(
        username='******',
        password_hash=town_password,
        role='admin'
    ))
    session.add(User(
        username='******',
        password_hash=town_password,
        role='editor'
    ))

    transaction.commit()
    session.close_all()

    return app
def test_orm_integration(es_url, postgres_dsn):

    class App(Framework, ElasticsearchApp):
        pass

    Base = declarative_base()

    class Document(Base, ORMSearchable):
        __tablename__ = 'documents'

        id = Column(Integer, primary_key=True)
        title = Column(Text, nullable=False)
        body = Column(Text, nullable=True)

        @property
        def es_suggestion(self):
            return self.title

        es_public = True
        es_language = 'en'
        es_properties = {
            'title': {'type': 'localized'},
            'body': {'type': 'localized'}
        }

    @App.path(path='/')
    class Root(object):
        pass

    @App.json(model=Root)
    def view_documents(self, request):

        # make sure the changes are propagated in testing
        request.app.es_client.indices.refresh(index='_all')

        query = request.params.get('q')
        if query:
            return request.app.es_client.search(index='_all', body={
                'query': {
                    'multi_match': {
                        'query': query,
                        'fields': ['title', 'body']
                    }
                }
            })
        else:
            return request.app.es_client.search(index='_all')

    @App.json(model=Root, name='new')
    def view_add_document(self, request):
        session = request.app.session()
        session.add(Document(
            id=request.params.get('id'),
            title=request.params.get('title'),
            body=request.params.get('body')
        ))

    @App.json(model=Root, name='update')
    def view_update_document(self, request):
        session = request.app.session()
        query = session.query(Document)
        query = query.filter(Document.id == request.params.get('id'))

        document = query.one()
        document.title = request.params.get('title'),
        document.body = request.params.get('body'),

    @App.json(model=Root, name='delete')
    def view_delete_document(self, request):
        session = request.app.session()
        query = session.query(Document)
        query = query.filter(Document.id == request.params.get('id'))
        query.delete('fetch')

    scan_morepath_modules(App)
    morepath.commit(App)

    app = App()
    app.configure_application(
        dsn=postgres_dsn,
        base=Base,
        elasticsearch_hosts=[es_url]
    )

    app.namespace = 'documents'
    app.set_application_id('documents/home')

    client = Client(app)
    client.get('/new?id=1&title=Shop&body=We sell things and stuff')
    client.get('/new?id=2&title=About&body=We are a company')
    client.get('/new?id=3&title=Terms&body=Stuff we pay lawyers for')

    documents = client.get('/').json
    assert documents['hits']['total'] == 3

    documents = client.get('/?q=stuff').json
    assert documents['hits']['total'] == 2

    documents = client.get('/?q=company').json
    assert documents['hits']['total'] == 1

    client.get('/delete?id=3')

    documents = client.get('/?q=stuff').json
    assert documents['hits']['total'] == 1

    client.get('/update?id=2&title=About&body=We are a business')

    documents = client.get('/?q=company').json
    assert documents['hits']['total'] == 0

    documents = client.get('/?q=business').json
    assert documents['hits']['total'] == 1
Exemple #17
0
def create_app(app_class, request, use_elasticsearch=False,
               reuse_filestorage=True, use_smtp=True,
               depot_backend='depot.io.local.LocalFileStorage',
               depot_storage_path=None):

    # filestorage can be reused between tries as it is nowadays mainly (if not
    # exclusively) used by the theme compiler
    if reuse_filestorage:
        filestorage_object = request.getfixturevalue('long_lived_filestorage')
    else:
        filestorage_object = None

    if not app_class.is_committed():
        scan_morepath_modules(app_class)
        app_class.commit()

    if use_elasticsearch:
        elasticsearch_hosts = [
            request.getfixturevalue('es_url')
        ]
    else:
        elasticsearch_hosts = []

    if depot_backend == 'depot.io.local.LocalFileStorage':
        if not depot_storage_path:
            depot_storage_path = request.getfixturevalue('temporary_directory')

    temporary_path = request.getfixturevalue('temporary_path')
    signing_services = (temporary_path / 'signing-services')
    signing_services.mkdir()

    cert_file = module_path('onegov_testing', 'tests/fixtures/test.crt')
    cert_key = module_path('onegov_testing', 'tests/fixtures/test.crt')

    with (signing_services / '__default__.yml').open('w') as f:
        f.write(textwrap.dedent(f"""
            name: swisscom_ais
            parameters:
                customer: foo
                key_static: bar
                cert_file: {cert_file}
                cert_key: {cert_key}
        """))

    app = app_class()
    app.namespace = random_namespace()
    app.configure_application(
        dsn=request.getfixturevalue('postgres_dsn'),
        filestorage='fs.osfs.OSFS',
        filestorage_object=filestorage_object,
        depot_backend=depot_backend,
        depot_storage_path=depot_storage_path,
        identity_secure=False,
        enable_elasticsearch=use_elasticsearch,
        elasticsearch_hosts=elasticsearch_hosts,
        redis_url=request.getfixturevalue('redis_url'),
        yubikey_client_id='foo',
        yubikey_secret_key='dGhlIHdvcmxkIGlzIGNvbnRyb2xsZWQgYnkgbGl6YXJkcyE=',
        signing_services=str(signing_services)
    )

    app.set_application_id(app.namespace + '/test')
    app.clear_request_cache()

    if hasattr(app, 'bind_depot'):
        app.bind_depot()

    # cronjobs leave lingering sessions open, in real life this is not a
    # problem, but in testing it leads to connection pool exhaustion
    app.settings.cronjobs = Bunch(enabled=False)

    if use_smtp:
        smtp = request.getfixturevalue('smtp')

        app.mail = {
            'marketing': {
                'host': smtp.address[0],
                'port': smtp.address[1],
                'force_tls': False,
                'username': None,
                'password': None,
                'use_directory': False,
                'sender': '*****@*****.**'
            },
            'transactional': {
                'host': smtp.address[0],
                'port': smtp.address[1],
                'force_tls': False,
                'username': None,
                'password': None,
                'use_directory': False,
                'sender': '*****@*****.**'
            }
        }

        app.smtp = smtp

    return app
Exemple #18
0
def create_app(app_class,
               request,
               use_elasticsearch=False,
               reuse_filestorage=True,
               use_smtp=True,
               depot_backend='depot.io.local.LocalFileStorage',
               depot_storage_path=None):

    # filestorage can be reused between tries as it is nowadays mainly (if not
    # exclusively) used by the theme compiler
    if reuse_filestorage:
        filestorage_object = request.getfixturevalue('long_lived_filestorage')
    else:
        filestorage_object = None

    if not app_class.is_committed():
        scan_morepath_modules(app_class)
        app_class.commit()

    if use_elasticsearch:
        elasticsearch_hosts = [request.getfixturevalue('es_url')]
    else:
        elasticsearch_hosts = []

    if depot_backend == 'depot.io.local.LocalFileStorage':
        if not depot_storage_path:
            depot_storage_path = request.getfixturevalue('temporary_directory')

    temporary_path = request.getfixturevalue('temporary_path')
    signing_services = (temporary_path / 'signing-services')
    signing_services.mkdir()

    cert_file = module_path('onegov_testing', 'tests/fixtures/test.crt')
    cert_key = module_path('onegov_testing', 'tests/fixtures/test.crt')

    with (signing_services / '__default__.yml').open('w') as f:
        f.write(
            textwrap.dedent(f"""
            name: swisscom_ais
            parameters:
                customer: foo
                key_static: bar
                cert_file: {cert_file}
                cert_key: {cert_key}
        """))

    app = app_class()
    app.namespace = random_namespace()
    app.configure_application(
        dsn=request.getfixturevalue('postgres_dsn'),
        filestorage='fs.osfs.OSFS',
        filestorage_object=filestorage_object,
        depot_backend=depot_backend,
        depot_storage_path=depot_storage_path,
        identity_secure=False,
        enable_elasticsearch=use_elasticsearch,
        elasticsearch_hosts=elasticsearch_hosts,
        redis_url=request.getfixturevalue('redis_url'),
        yubikey_client_id='foo',
        yubikey_secret_key='dGhlIHdvcmxkIGlzIGNvbnRyb2xsZWQgYnkgbGl6YXJkcyE=',
        signing_services=str(signing_services))

    app.set_application_id(app.namespace + '/test')
    app.clear_request_cache()

    if hasattr(app, 'bind_depot'):
        app.bind_depot()

    # cronjobs leave lingering sessions open, in real life this is not a
    # problem, but in testing it leads to connection pool exhaustion
    app.settings.cronjobs = Bunch(enabled=False)

    if use_smtp:
        smtp = request.getfixturevalue('smtp')

        app.mail = {
            'marketing': {
                'host': smtp.address[0],
                'port': smtp.address[1],
                'force_tls': False,
                'username': None,
                'password': None,
                'use_directory': False,
                'sender': '*****@*****.**'
            },
            'transactional': {
                'host': smtp.address[0],
                'port': smtp.address[1],
                'force_tls': False,
                'username': None,
                'password': None,
                'use_directory': False,
                'sender': '*****@*****.**'
            }
        }

        app.smtp = smtp

    return app