예제 #1
0
def test_item_yaml_exploit(testapp):
    """Frontmatter processor has to be protected from YAML attacks."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\
                        ---
                        author: !!python/object/apply:subprocess.check_output
                          args: [ cat ~/.ssh/id_rsa ]
                          kwds: { shell: true }
                        ---

                        May the Force be with you!
                    """
                    )
                }
            )
        ],
        format="yaml",
    )

    assert isinstance(stream, collections.abc.Iterable)

    with pytest.raises(yaml.YAMLError):
        next(stream)
예제 #2
0
def test_item(testapp, frontsnippet):
    """Frontmatter has to be processed and removed from the content."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\
                        ---
                        %s
                        ---

                        May the Force be with you!
                        """
                    )
                    % textwrap.dedent(frontsnippet)
                }
            )
        ],
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item(
            {
                "content": "May the Force be with you!\n",
                "author": "Yoda",
                "master": True,
                "labels": ["force", "motto"],
            }
        )
    ]
예제 #3
0
def test_item_yaml_invalid(testapp):
    """Frontmatter processor has to fail in case of invalid YAML."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\
                        ---
                        author: Yoda
                         the best jedi ever:
                        ---

                        May the Force be with you!
                    """
                    )
                }
            )
        ],
        format="yaml",
    )

    assert isinstance(stream, collections.abc.Iterable)

    with pytest.raises(yaml.YAMLError):
        next(stream)
예제 #4
0
def test_item_with_frontmatter_in_text(testapp):
    """Only frontmatter at the beginning has to be processed."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\
                        I am a Jedi, like my father before me.

                        ---
                        author: Yoda
                        master: true
                        labels: [force, motto]
                        ---

                        May the Force be with you!
                    """
                    )
                }
            )
        ],
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item(
            {
                "content": textwrap.dedent(
                    """\
                    I am a Jedi, like my father before me.

                    ---
                    author: Yoda
                    master: true
                    labels: [force, motto]
                    ---

                    May the Force be with you!
                """
                )
            }
        )
    ]
예제 #5
0
def test_args_format(testapp, frontsnippet, format, exception):
    """Frontmatter has to respect 'format' argument."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\
                        ---
                        %s
                        ---

                        May the Force be with you!
                        """
                    )
                    % textwrap.dedent(frontsnippet)
                }
            )
        ],
        format=format,
    )

    assert isinstance(stream, collections.abc.Iterable)

    if exception:
        with pytest.raises(exception.__class__) as excinfo:
            next(stream)

        assert str(excinfo.value) == str(exception)

    else:
        assert list(stream) == [
            holocron.Item(
                {
                    "content": "May the Force be with you!\n",
                    "author": "Yoda",
                    "master": True,
                    "labels": ["force", "motto"],
                }
            )
        ]
예제 #6
0
def test_item_without_frontmatter(testapp):
    """Item without frontmatter has to be ignored."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\
                        ---
                        author: Yoda
                        master: true
                        labels: [force, motto]
                        ...

                        May the Force be with you!
                    """
                    )
                }
            )
        ],
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item(
            {
                "content": textwrap.dedent(
                    """\
                    ---
                    author: Yoda
                    master: true
                    labels: [force, motto]
                    ...

                    May the Force be with you!
                """
                )
            }
        )
    ]
예제 #7
0
def test_args_overwrite(testapp, overwrite):
    """Frontmatter processor has to respect 'overwrite' argument."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "author": "Obi-Wan Kenobi",
                    "content": textwrap.dedent(
                        """\
                        ---
                        author: Yoda
                        master: true
                        labels: [force, motto]
                        ---

                        May the Force be with you!
                    """
                    ),
                }
            )
        ],
        overwrite=overwrite,
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item(
            {
                "content": "May the Force be with you!\n",
                "author": "Yoda" if overwrite else "Obi-Wan Kenobi",
                "master": True,
                "labels": ["force", "motto"],
            }
        )
    ]
예제 #8
0
def test_item_with_frontmatter_leading_whitespaces(testapp):
    """Leading whitespaces before frontmatter has to be ignored."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\


                        ---
                        author: Yoda
                        master: true
                        labels: [force, motto]
                        ---

                        May the Force be with you!
                    """
                    )
                }
            )
        ],
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item(
            {
                "content": "May the Force be with you!\n",
                "author": "Yoda",
                "master": True,
                "labels": ["force", "motto"],
            }
        )
    ]
예제 #9
0
def test_args_delimiter(testapp, delimiter):
    """Frontmatter processor has to respect 'delimiter' argument."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        f"""\
                        {delimiter}
                        author: Yoda
                        master: true
                        labels: [force, motto]
                        {delimiter}

                        May the Force be with you!
                        """
                    )
                }
            )
        ],
        delimiter=delimiter,
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item(
            {
                "content": "May the Force be with you!\n",
                "author": "Yoda",
                "master": True,
                "labels": ["force", "motto"],
            }
        )
    ]
예제 #10
0
def test_item_many(testapp, amount):
    """Frontmatter processor has to work with stream."""

    stream = frontmatter.process(
        testapp,
        [
            holocron.Item(
                {
                    "content": textwrap.dedent(
                        """\
                        ---
                        master: %d
                        labels: [force, motto]
                        ---

                        May the Force be with you!
                    """
                        % i
                    )
                }
            )
            for i in range(amount)
        ],
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item(
            {
                "master": i,
                "labels": ["force", "motto"],
                "content": "May the Force be with you!\n",
            }
        )
        for i in range(amount)
    ]
예제 #11
0
def test_args_bad_value(testapp, args, error):
    """Frontmatter processor has to validate input arguments."""

    with pytest.raises(ValueError) as excinfo:
        next(frontmatter.process(testapp, [], **args))
    assert str(excinfo.value) == error