Ejemplo n.º 1
0
def test_write_and_read_host_attributes(tmp_path, attributes, monkeypatch):
    folder_path = str(tmp_path)
    # Write/Read operations always require a valid user
    monkeypatch.setattr(config, "user", config.LoggedInSuperUser())

    # Used to write the data
    write_data_folder = watolib.Folder("testfolder",
                                       folder_path=folder_path,
                                       parent_folder=None)

    # Used to read the previously written data
    read_data_folder = watolib.Folder("testfolder",
                                      folder_path=folder_path,
                                      parent_folder=None)

    environ = dict(create_environ(), REQUEST_URI='')
    with AppContext(DummyApplication(environ, None)), \
            RequestContext(htmllib.html(Request(environ))):
        # Write data
        # Note: The create_hosts function modifies the attributes dict, adding a meta_data key inplace
        write_data_folder.create_hosts([("testhost", attributes, [])])
        write_folder_hosts = write_data_folder.hosts()
        assert len(write_folder_hosts) == 1

        # Read data back
        read_folder_hosts = read_data_folder.hosts()
        assert len(read_folder_hosts) == 1
        for _, host in read_folder_hosts.items():
            assert host.attributes() == attributes
Ejemplo n.º 2
0
def test_logged_in_super_user_permissions(mocker):
    user = config.LoggedInSuperUser()

    mocker.patch.object(
        config,
        'roles',
        {
            'admin': {
                'permissions': {
                    'eat_other_peoples_cake': True
                }
            },
        },
    )
    mocker.patch.object(permissions, 'permission_registry')

    assert user.may('eat_other_peoples_cake') is True
    assert user.may('drink_other_peoples_milk') is False
    user.need_permission('eat_other_peoples_cake')
    with pytest.raises(MKAuthException):
        user.need_permission('drink_other_peoples_milk')
Ejemplo n.º 3
0
def test_ruleset_to_config_sub_folder(register_builtin_html, monkeypatch,
                                      load_config, wato_use_git,
                                      expected_result):
    monkeypatch.setattr(config, "wato_use_git", wato_use_git)

    ruleset = rulesets.Ruleset(
        "checkgroup_parameters:local",
        ruleset_matcher.get_tag_to_group_map(config.tags))

    monkeypatch.setattr(config, "user", config.LoggedInSuperUser())
    hosts_and_folders.Folder.create_missing_folders("abc")
    folder = hosts_and_folders.Folder.folder("abc")

    ruleset.from_config(folder, [
        {
            "id": "1",
            "value": "VAL",
            "condition": {
                'host_name': ['HOSTLIST'],
                'service_description': [{
                    '$regex': 'SVC'
                }, {
                    '$regex': 'LIST'
                }],
            },
        },
        {
            "id": "2",
            "value": "VAL2",
            "condition": {
                'host_name': ['HOSTLIST'],
                'service_description': [{
                    '$regex': 'SVC'
                }, {
                    '$regex': 'LIST'
                }],
            },
        },
    ])
    assert ruleset.to_config(folder) == expected_result
Ejemplo n.º 4
0
        'tcp',
    ])


@pytest.mark.parametrize(
    "user, alias, email, role_ids, baserole_id",
    [
        (
            config.LoggedInNobody(),
            "Unauthenticated user",
            "nobody",
            [],
            "guest",  # TODO: Why is this guest "guest"?
        ),
        (
            config.LoggedInSuperUser(),
            "Superuser for unauthenticated pages",
            "admin",
            ["admin"],
            "admin",
        ),
    ])
def test_unauthenticated_users(user, alias, email, role_ids, baserole_id):
    assert user.id is None
    assert user.alias == alias
    assert user.email == email
    assert user.confdir is None

    assert user.role_ids == role_ids
    assert user.get_attribute('roles') == role_ids
    assert user.baserole_id == baserole_id
Ejemplo n.º 5
0
def fixture_mocked_user(monkeypatch):
    # Write/Read operations always require a valid user
    monkeypatch.setattr(config, "user", config.LoggedInSuperUser())