Пример #1
0
def default_view_descriptors():
    return [
        common.FileDescriptor(
            relative_path='default.txt',
            serialization_method=common.SerializationMethod.TEXT,
            content='This is the default view')
    ]
Пример #2
0
async def test_xml_content(view_factory, view_manager_factory):
    view_name = 'binaries_view'
    file_descriptors = [
        common.FileDescriptor(
            relative_path='test.xml',
            serialization_method=common.SerializationMethod.TEXT,
            content='<div id="123"><div/></div>'),
        common.FileDescriptor(
            relative_path='test2.svg',
            serialization_method=common.SerializationMethod.TEXT,
            content='<svg></svg>')]
    conf = [view_factory(view_name, file_descriptors)]
    view_manager = await view_manager_factory(conf)

    view = await view_manager.get(view_name)
    assert view.name == view_name
    assert view.conf is None
    for fd in file_descriptors:
        assert fd.relative_path in view.data
        stream = io.StringIO(fd.content)
        assert hat.gui.vt.parse(stream) == view.data[fd.relative_path]
Пример #3
0
async def test_binary_content(view_factory, view_manager_factory):
    view_name = 'binaries_view'
    file_descriptors = [
        common.FileDescriptor(
            relative_path='test.bin',
            serialization_method=common.SerializationMethod.BINARY,
            content='Hello world'.encode('utf-8')),
        common.FileDescriptor(
            relative_path='test2.bin',
            serialization_method=common.SerializationMethod.BINARY,
            content='Hello2'.encode('utf-8'))]
    conf = [view_factory(view_name, file_descriptors)]

    view_manager = await view_manager_factory(conf)
    view = await view_manager.get(view_name)
    assert view.name == view_name
    assert view.conf is None
    for fd in file_descriptors:
        assert fd.relative_path in view.data
        assert fd.content == base64.b64decode(
            view.data[fd.relative_path].encode('utf-8'))
Пример #4
0
async def test_conf_validated(view_factory, view_manager_factory):
    conf_schema = """
        type: object
        id: hat://test_schema
        required:
            - required_property
        properties:
            required_property:
                type: string
        """
    file_descriptors = [
        common.FileDescriptor(
            relative_path='schema.yaml',
            serialization_method=common.SerializationMethod.TEXT,
            content=conf_schema)]
    schema_repo = hat.util.json.SchemaRepository()
    conf = {'abc': 'def'}
    view_name = 'test_conf_validated'
    manager_conf = [view_factory(view_name, file_descriptors, conf)]
    view_manager = await view_manager_factory(manager_conf, schema_repo)

    with pytest.raises(jsonschema.ValidationError):
        await view_manager.get(view_name)
Пример #5
0
import xml.sax
import yaml

import hat.util.json
from hat.util import aio
import hat.gui.view
import hat.gui.vt

from test_unit.test_gui import common


@pytest.mark.parametrize('views', [
    {
        'view1': [
            common.FileDescriptor(
                relative_path='src_js/test.js',
                serialization_method=common.SerializationMethod.TEXT,
                content="console.log('Hello world!')"),
            common.FileDescriptor(
                relative_path='src_js/test2.js',
                serialization_method=common.SerializationMethod.TEXT,
                content="console.log('Hello world! (2)')")],
        'view2': [
            common.FileDescriptor(
                relative_path='test.css',
                serialization_method=common.SerializationMethod.TEXT,
                content='body { background-color: pink; }'),
            common.FileDescriptor(
                relative_path='test.txt',
                serialization_method=common.SerializationMethod.TEXT,
                content='Lorem ipsum...')]},
    {
Пример #6
0
async def test_view(unused_tcp_port, view_factory, default_view_descriptors,
                    view_manager_factory, server_factory):
    ui_port = unused_tcp_port
    server_conf = conf(ui_port,
                       roles=[{
                           'name': 'role1',
                           'view': 'role1_view',
                           'adapters': []
                       }, {
                           'name': 'role2',
                           'view': 'role2_view',
                           'adapters': []
                       }],
                       users=[
                           user_conf('user1', 'pass1', 'salt1', ['role1']),
                           user_conf('user2', 'pass2', 'salt2', ['role2'])
                       ])

    role1_descriptors = [
        common.FileDescriptor(
            relative_path='user1.txt',
            serialization_method=common.SerializationMethod.TEXT,
            content='User1 view')
    ]
    role2_descriptors = [
        common.FileDescriptor(
            relative_path='user2.txt',
            serialization_method=common.SerializationMethod.TEXT,
            content='User2 view')
    ]
    views_conf = [
        view_factory('initial_view', default_view_descriptors),
        view_factory('role1_view', role1_descriptors, conf={'key': 'value'}),
        view_factory('role2_view', role2_descriptors, conf={'key': 'value'})
    ]
    view_manager = await view_manager_factory(views_conf)

    async with server_factory(server_conf, {}, view_manager):
        conn = await hat.juggler.connect(f'ws://127.0.0.1:{ui_port}/ws')

        state_message = await conn.receive()
        assert state_message['type'] == 'state'
        assert state_message['conf'] is None
        view_state = state_message['view']
        for descriptor in default_view_descriptors:
            assert descriptor.relative_path in view_state
            assert descriptor.content == view_state[descriptor.relative_path]

        await conn.send({
            'type': 'login',
            'name': 'user1',
            'password': sha256_hexstr('pass1')
        })
        state_message = await conn.receive()
        assert state_message['type'] == 'state'
        assert state_message['conf'] == {'key': 'value'}
        view_state = state_message['view']
        for descriptor in role1_descriptors:
            assert descriptor.relative_path in view_state
            assert descriptor.content == view_state[descriptor.relative_path]
        for descriptor in role2_descriptors:
            assert descriptor.relative_path not in view_state

        await conn.send({
            'type': 'login',
            'name': 'user2',
            'password': sha256_hexstr('pass2')
        })
        state_message = await conn.receive()
        assert state_message['type'] == 'state'
        assert state_message['conf'] == {'key': 'value'}
        view_state = state_message['view']
        for descriptor in role2_descriptors:
            assert descriptor.relative_path in view_state
            assert descriptor.content == view_state[descriptor.relative_path]
        for descriptor in role1_descriptors:
            assert descriptor.relative_path not in view_state

        await conn.send({'type': 'logout'})
        state_message = await conn.receive()
        assert state_message['type'] == 'state'
        assert state_message['conf'] is None
        view_state = state_message['view']
        for descriptor in default_view_descriptors:
            assert descriptor.relative_path in view_state
            assert descriptor.content == view_state[descriptor.relative_path]