Ejemplo n.º 1
0
def test_get_view_code(organisation):
    """Test get view code stored in organisation."""
    store_organisation(None, {'view': 'org'})
    assert get_view_code() == 'org'

    g.pop('organisation', None)
    assert get_view_code() == 'sonar'
Ejemplo n.º 2
0
def test_get_view_code(app, organisation):
    """Test get view code stored in organisation."""
    with app.test_request_context() as req:
        req.request.view_args['view'] = 'org'
        store_organisation()

    assert get_view_code() == 'org'

    g.pop('organisation', None)
    assert get_view_code() == 'global'
Ejemplo n.º 3
0
def test_get_specific_theme(app, organisation, make_organisation):
    """Test getting a theme by organisation."""
    with app.test_request_context() as req:
        req.request.view_args['view'] = 'org'
        store_organisation()

    # Not dedicated --> global theme
    assert get_specific_theme() == 'global-theme.css'

    # Dedicated, but no specific style --> global theme
    g.organisation['isDedicated'] = True
    assert get_specific_theme() == 'global-theme.css'

    # Dedicated and specific style --> specific theme
    make_organisation('usi')
    with app.test_request_context() as req:
        req.request.view_args['view'] = 'usi'
        store_organisation()

    g.organisation['isDedicated'] = True
    assert get_specific_theme() == 'usi-theme.css'
Ejemplo n.º 4
0
def test_get_current_organisation_code(app, organisation):
    """Test get current organisation."""
    # No globals and no args
    assert views.get_current_organisation_code() == 'sonar'

    # Default globals and no args
    views.store_organisation(None, {'view': 'sonar'})
    assert views.get_current_organisation_code() == 'sonar'

    # Organisation globals and no args
    views.store_organisation(None, {'view': 'org'})
    assert views.get_current_organisation_code() == 'org'

    # Args is global
    with app.test_request_context() as req:
        req.request.args = {'view': 'sonar'}
        assert views.get_current_organisation_code() == 'sonar'

    # Args has organisation view
    with app.test_request_context() as req:
        req.request.args = {'view': 'unisi'}
        assert views.get_current_organisation_code() == 'unisi'
Ejemplo n.º 5
0
def test_store_organisation(db, organisation):
    """Test store organisation in globals."""
    # Default view, no organisation stored.
    views.store_organisation(None, {'view': 'sonar'})
    assert not g.get('organisation')

    # Existing organisation stored
    views.store_organisation(None, {'view': 'org'})
    assert g.organisation['code'] == 'org'
    assert g.organisation['isShared']

    # Non existing organisation
    with pytest.raises(Exception) as exception:
        views.store_organisation(None, {'view': 'not-existing-org'})
        assert str(exception.value) == 'Organisation\'s view is not accessible'

    # Existing organisation without shared view
    organisation.update({'isShared': False})
    with pytest.raises(Exception) as exception:
        views.store_organisation(None, {'view': 'org'})
        assert str(exception.value) == 'Organisation\'s view is not accessible'
Ejemplo n.º 6
0
def test_is_file_restricted(app, organisation):
    """Test if a file is restricted by embargo date and/or organisation."""
    g.pop('organisation', None)
    views.store_organisation(None, {'view': 'sonar'})

    record = {'organisation': {'pid': 'org'}}

    # No restriction and no embargo date
    assert views.is_file_restricted({}, {}) == {
        'date': None,
        'restricted': False
    }

    # Restricted by internal, but IP is allowed
    with app.test_request_context(environ_base={'REMOTE_ADDR': '127.0.0.1'}):
        assert views.is_file_restricted({'restricted': 'internal'}, {}) == {
            'date': None,
            'restricted': False
        }

    # Restricted by internal, but IP is not allowed
    with app.test_request_context(environ_base={'REMOTE_ADDR': '10.1.2.3'}):
        assert views.is_file_restricted({'restricted': 'internal'}, {}) == {
            'date': None,
            'restricted': True
        }

    # Restricted by organisation and organisation is global
    assert views.is_file_restricted({'restricted': 'organisation'},
                                    record) == {
                                        'date': None,
                                        'restricted': True
                                    }

    # Restricted by organisation and current organisation match
    views.store_organisation(None, {'view': 'org'})
    assert views.is_file_restricted({'restricted': 'organisation'},
                                    record) == {
                                        'date': None,
                                        'restricted': False
                                    }

    # Restricted by organisation and record don't have organisation
    assert views.is_file_restricted({'restricted': 'organisation'}, {}) == {
        'date': None,
        'restricted': True
    }

    # Restricted by organisation and organisation don't match
    assert views.is_file_restricted({'restricted': 'organisation'},
                                    {'organisation': {
                                        'pid': 'some-org'
                                    }}) == {
                                        'date': None,
                                        'restricted': True
                                    }

    # Restricted by embargo date only, but embargo date is in the past
    assert views.is_file_restricted({'embargo_date': '2020-01-01'}, {}) == {
        'date': None,
        'restricted': False
    }

    # Restricted by embargo date only and embargo date is in the future
    with app.test_request_context(environ_base={'REMOTE_ADDR': '10.1.2.3'}):
        assert views.is_file_restricted({'embargo_date': '2021-01-01'},
                                        {}) == {
                                            'date': datetime.datetime(
                                                2021, 1, 1, 0, 0),
                                            'restricted': True
                                        }

    # Restricted by embargo date and organisation
    g.pop('organisation', None)
    views.store_organisation(None, {'view': 'sonar'})
    with app.test_request_context(environ_base={'REMOTE_ADDR': '10.1.2.3'}):
        assert views.is_file_restricted(
            {
                'embargo_date': '2021-01-01',
                'restricted': 'organisation'
            }, record) == {
                'restricted': True,
                'date': datetime.datetime(2021, 1, 1, 0, 0)
            }

    # Restricted by embargo date but internal IP gives access
    with app.test_request_context(environ_base={'REMOTE_ADDR': '127.0.0.1'}):
        assert views.is_file_restricted(
            {
                'embargo_date': '2021-01-01',
                'restricted': 'internal'
            }, {}) == {
                'date': None,
                'restricted': False
            }