-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_utils.py
More file actions
103 lines (88 loc) · 2.8 KB
/
Copy pathtest_utils.py
File metadata and controls
103 lines (88 loc) · 2.8 KB
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
import datetime as dt
from types import SimpleNamespace
import pytest
from django.template import Context, Template
from django.test import RequestFactory
from django.test.utils import override_settings
from pytest_django.asserts import assertHTMLEqual
from feincms3.shortcuts import render_list
from feincms3.utils import is_first_party_link, upload_to
from testapp.models import Article
@pytest.mark.parametrize(
("url", "hosts", "result"),
[
(
"http://example.com/",
["*"],
False,
),
(
"http://example.com:80/path",
[".example.com"],
True,
),
(
"https://example.com:80/path",
[".example.com"],
True,
),
(
"http://www.example.com:80/path",
["example.com"],
False,
),
(
"/path",
[".example.com"],
True,
),
(
"mailto:max@example.com",
[".example.com"],
False,
),
(
"ftp://example.com:80/path",
[".example.com"],
False,
),
],
)
def test_is_first_party_link(url, hosts, result):
"""is_first_party_link test battery"""
assert is_first_party_link(url, first_party_hosts=hosts) == result
@override_settings(ALLOWED_HOSTS=[".example.com"])
def test_maybe_target_blank_template_tag():
template = Template(
'{% load feincms3 %}<a href="{{ url }}" {% maybe_target_blank url %}>link</a>'
)
html = template.render(Context({"url": "/relative/"}))
assertHTMLEqual(
html,
'<a href="/relative/">link</a>',
)
html = template.render(Context({"url": "http://example.org/relative/"}))
assertHTMLEqual(
html,
'<a href="http://example.org/relative/" target="_blank" rel="noopener">link</a>',
)
def test_upload_to():
instance = SimpleNamespace(_meta=SimpleNamespace(model_name="image"))
ordinal = str(dt.date.today().toordinal())
filename = "upload.jpg"
assert upload_to(instance, filename) == "/".join(
["image", ordinal[1:3], ordinal[3:6], filename]
)
@pytest.mark.django_db
def test_render_list():
"""render_list, automatic template selection and pagination"""
for i in range(7):
Article.objects.create(title=f"Article {i}", category="publications")
request = RequestFactory().get("/", data={"page": 2})
response = render_list(
request, list(Article.objects.all()), model=Article, paginate_by=2
)
assert response.template_name == "testapp/article_list.html"
assert len(response.context_data["object_list"]) == 2
assert response.context_data["object_list"].number == 2
assert response.context_data["object_list"].paginator.num_pages == 4