summaryrefslogtreecommitdiff
path: root/test/test_lookup.py
blob: 6a797d7a4b6f83aa475110ca9378db7273179fc7 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import tempfile

from mako import exceptions
from mako import lookup
from mako import runtime
from mako.template import Template
from mako.testing.assertions import assert_raises_message
from mako.testing.assertions import assert_raises_with_given_cause
from mako.testing.config import config
from mako.testing.helpers import file_with_template_code
from mako.testing.helpers import replace_file_with_dir
from mako.testing.helpers import result_lines
from mako.testing.helpers import rewind_compile_time
from mako.util import FastEncodingBuffer

tl = lookup.TemplateLookup(directories=[config.template_base])


class LookupTest:
    def test_basic(self):
        t = tl.get_template("index.html")
        assert result_lines(t.render()) == ["this is index"]

    def test_subdir(self):
        t = tl.get_template("/subdir/index.html")
        assert result_lines(t.render()) == [
            "this is sub index",
            "this is include 2",
        ]

        assert (
            tl.get_template("/subdir/index.html").module_id
            == "_subdir_index_html"
        )

    def test_updir(self):
        t = tl.get_template("/subdir/foo/../bar/../index.html")
        assert result_lines(t.render()) == [
            "this is sub index",
            "this is include 2",
        ]

    def test_directory_lookup(self):
        """test that hitting an existent directory still raises
        LookupError."""

        assert_raises_with_given_cause(
            exceptions.TopLevelLookupException,
            KeyError,
            tl.get_template,
            "/subdir",
        )

    def test_no_lookup(self):
        t = Template("hi <%include file='foo.html'/>")

        assert_raises_message(
            exceptions.TemplateLookupException,
            "Template 'memory:%s' has no TemplateLookup associated"
            % hex(id(t)),
            t.render,
        )

    def test_uri_adjust(self):
        tl = lookup.TemplateLookup(directories=["/foo/bar"])
        assert (
            tl.filename_to_uri("/foo/bar/etc/lala/index.html")
            == "/etc/lala/index.html"
        )

        tl = lookup.TemplateLookup(directories=["./foo/bar"])
        assert (
            tl.filename_to_uri("./foo/bar/etc/index.html") == "/etc/index.html"
        )

    def test_uri_cache(self):
        """test that the _uri_cache dictionary is available"""
        tl._uri_cache[("foo", "bar")] = "/some/path"
        assert tl._uri_cache[("foo", "bar")] == "/some/path"

    def test_check_not_found(self):
        tl = lookup.TemplateLookup()
        tl.put_string("foo", "this is a template")
        f = tl.get_template("foo")
        assert f.uri in tl._collection
        f.filename = "nonexistent"
        assert_raises_with_given_cause(
            exceptions.TemplateLookupException,
            FileNotFoundError,
            tl.get_template,
            "foo",
        )
        assert f.uri not in tl._collection

    def test_dont_accept_relative_outside_of_root(self):
        """test the mechanics of an include where
        the include goes outside of the path"""
        tl = lookup.TemplateLookup(
            directories=[os.path.join(config.template_base, "subdir")]
        )
        index = tl.get_template("index.html")

        ctx = runtime.Context(FastEncodingBuffer())
        ctx._with_template = index

        assert_raises_message(
            exceptions.TemplateLookupException,
            'Template uri "../index.html" is invalid - it '
            "cannot be relative outside of the root path",
            runtime._lookup_template,
            ctx,
            "../index.html",
            index.uri,
        )

        assert_raises_message(
            exceptions.TemplateLookupException,
            'Template uri "../othersubdir/foo.html" is invalid - it '
            "cannot be relative outside of the root path",
            runtime._lookup_template,
            ctx,
            "../othersubdir/foo.html",
            index.uri,
        )

        # this is OK since the .. cancels out
        runtime._lookup_template(ctx, "foo/../index.html", index.uri)

    def test_checking_against_bad_filetype(self):
        with tempfile.TemporaryDirectory() as tempdir:
            tl = lookup.TemplateLookup(directories=[tempdir])
            index_file = file_with_template_code(
                os.path.join(tempdir, "index.html")
            )

            with rewind_compile_time():
                tmpl = Template(filename=index_file)

            tl.put_template("index.html", tmpl)

            replace_file_with_dir(index_file)

            assert_raises_with_given_cause(
                exceptions.TemplateLookupException,
                OSError,
                tl._check,
                "index.html",
                tl._collection["index.html"],
            )