forked from danidee10/Staticfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
184 lines (150 loc) · 8.2 KB
/
tests.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#! /usr/bin/python3
"""Run tests."""
import unittest
import os
from staticfy.staticfy import staticfy
class DummyParser:
"""
Empty class that simulates the parser in the main program.
This was done so multiple arguments can be passed as an object.
"""
framework = None
namespace = None
static_endpoint = None
exc_tags = None
add_tags = None
class StaticfyTest(unittest.TestCase):
"""Test Case."""
@classmethod
def setUpClass(cls):
"""Setup file to be used for tests."""
cls.filename = "test.html"
data = (
"""<link rel='stylesheet' href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="/static/images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href='../css/style.css' />\n"""
"""<script src="/js/script.js">alert('hello world')</script>\n"""
)
with open(cls.filename, "w+") as f:
f.write(data)
def setUp(self):
"""Create a fresh DummyParser for each test case."""
self.args = DummyParser()
def test_normal_staticfy(self):
"""Flask => {{ url_for('static', filename='css/style.css') }}."""
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n""" # noqa
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />\n""" # noqa
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_static_endpoint(self):
"""Testing Static endpoint."""
self.args.static_endpoint = "my_static"
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{{ url_for('my_static', filename='images/staticfy.jpg') }}" />\n""" # noqa
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ url_for('my_static', filename='css/style.css') }}" />\n""" # noqa
"""<script src="{{ url_for('my_static', filename='js/script.js') }}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_additional_tags(self):
"""Testing additional tags."""
self.args.add_tags = {"img": "data-url"}
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n""" # noqa
"""<img data-url="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n""" # noqa
"""<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />\n""" # noqa
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_exclusive_tags(self):
"""Testing exclusive tags."""
self.args.exc_tags = {"link": "href", "img": "src"}
res = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="/static/images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="../css/style.css" />\n"""
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(res, expected_result)
def test_cleanup_html(self):
"""Testing HTML cleanup."""
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n""" # noqa
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />\n""" # noqa
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_django_project(self):
"""Testing Django => {% static 'css/style.css' %}."""
self.args.framework = "django"
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{% static 'images/staticfy.jpg' %}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{% static 'css/style.css' %}" />\n""" # noqa
"""<script src="{% static 'js/script.js' %}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_laravel_project(self):
"""Testing Laravel => {{ URL::asset('css/bootstrap.min.css') }}."""
self.args.framework = "laravel"
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{{ URL::asset('images/staticfy.jpg') }}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ URL::asset('css/style.css') }}" />\n""" # noqa
"""<script src="{{ URL::asset('js/script.js') }}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_namespace(self):
"""Testing namespace."""
self.args.framework = "django"
self.args.namespace = "admin"
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{% static 'admin/images/staticfy.jpg' %}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{% static 'admin/css/style.css' %}" />\n""" # noqa
"""<script src="{% static 'admin/js/script.js' %}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_replace_relative_links(self):
"""Testing replace_relative_links."""
self.args.framework = "django"
result = staticfy(self.filename, args=self.args)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n""" # noqa
"""<img src="{% static 'images/staticfy.jpg' %}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{% static 'css/style.css' %}" />\n""" # noqa
"""<script src="{% static 'js/script.js' %}">alert("hello world")</script>\n""" # noqa
)
self.assertEqual(result, expected_result)
def test_filenotfound_exception(self):
self.assertRaises(
IOError, staticfy, "Invalid html file", args=self.args
)
@classmethod
def tearDownClass(cls):
"""Remove test.html file."""
os.remove(cls.filename)
if __name__ == "__main__":
unittest.main()