File: test_core_documents.py

package info (click to toggle)
fava 1.30.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,856 kB
  • sloc: javascript: 45,789; python: 11,087; makefile: 112; sh: 25
file content (79 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from __future__ import annotations

import datetime
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from fava.beans import create
from fava.core.documents import filepath_in_document_folder
from fava.core.documents import is_document_or_import_file
from fava.core.group_entries import group_entries_by_type
from fava.helpers import FavaAPIError

if TYPE_CHECKING:  # pragma: no cover
    from fava.core import FavaLedger


def test_is_document_or_import_file(
    example_ledger: FavaLedger,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    path = str(Path(__file__))
    monkeypatch.setattr(example_ledger.fava_options, "import_dirs", ["/test/"])
    monkeypatch.setattr(
        example_ledger,
        "all_entries_by_type",
        group_entries_by_type(
            [create.document({}, datetime.date(2022, 1, 1), "Assets", path)]
        ),
    )
    assert not is_document_or_import_file("/asdfasdf", example_ledger)
    assert not is_document_or_import_file("/test/../../err", example_ledger)
    assert is_document_or_import_file(path, example_ledger)
    assert is_document_or_import_file("/test/err/../err", example_ledger)
    assert is_document_or_import_file("/test/err/../err", example_ledger)


def test_filepath_in_documents_folder(
    example_ledger: FavaLedger,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setitem(example_ledger.options, "documents", ["/test"])

    def _join(start: str, *args: str) -> Path:
        return Path(start).joinpath(*args).resolve()

    assert filepath_in_document_folder(
        "/test",
        "Assets:US:BofA:Checking",
        "filename",
        example_ledger,
    ) == _join("/test", "Assets", "US", "BofA", "Checking", "filename")
    assert filepath_in_document_folder(
        "/test",
        "Assets:US:BofA:Checking",
        "file/name",
        example_ledger,
    ) == _join("/test", "Assets", "US", "BofA", "Checking", "file name")
    assert filepath_in_document_folder(
        "/test",
        "Assets:US:BofA:Checking",
        "/../file/name",
        example_ledger,
    ) == _join("/test", "Assets", "US", "BofA", "Checking", " .. file name")
    with pytest.raises(FavaAPIError):
        filepath_in_document_folder(
            "/test",
            "notanaccount",
            "filename",
            example_ledger,
        )
    with pytest.raises(FavaAPIError):
        filepath_in_document_folder(
            "/notadocumentsfolder",
            "Assets:US:BofA:Checking",
            "filename",
            example_ledger,
        )