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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
import pytest
from flask import current_app, render_template_string
from flask_bootstrap import CDN_BASE, raise_helper
@pytest.mark.usefixtures('client')
class TestBootstrap:
def test_raise_helper(self):
with pytest.raises(RuntimeError, match='Test message'):
raise_helper('Test message')
def test_deprecate_bootstrap_class(self):
from flask_bootstrap import Bootstrap
from flask import Flask
app = Flask(__name__)
with pytest.warns(UserWarning):
bootstrap = Bootstrap(app) # noqa: F841
def test_deprecate_bootstrap_template_path(self, app, client):
@app.route('/test')
def foo():
return render_template_string('''
{% from 'bootstrap/utils.html' import render_icon %}
{{ render_icon('heart') }}
''')
with pytest.warns(UserWarning):
client.get('/test')
def test_extension_init(self):
assert 'bootstrap' in current_app.extensions
def test_load_css_with_default_versions(self, bootstrap):
rv = bootstrap.load_css()
bootstrap_css = f'<link rel="stylesheet" href="{CDN_BASE}/bootstrap@{bootstrap.bootstrap_version}/' \
f'dist/css/bootstrap.min.css" integrity="{bootstrap.bootstrap_css_integrity}" ' \
'crossorigin="anonymous">'
assert bootstrap_css in rv
def test_load_css_with_bootswatch_theme(self, bootstrap):
rv = bootstrap.load_css(bootswatch_theme="mist")
bootstrap_css = f'<link rel="stylesheet" href="{CDN_BASE}/bootswatch@{bootstrap.bootstrap_version}/' \
f'dist/mist/bootstrap.min.css"'
assert bootstrap_css in rv
def test_load_css_with_non_default_versions(self, bootstrap):
def _check_assertions(rv):
assert 'bootstrap.min.css' in rv
assert 'integrity="' not in rv
assert 'crossorigin="anonymous"' not in rv
rv = bootstrap.load_css(version='4.5.6')
_check_assertions(rv)
rv = bootstrap.load_css(version='5.0.0')
_check_assertions(rv)
def test_load_js_with_default_versions(self, bootstrap):
rv = bootstrap.load_js()
bootstrap_js = f'<script src="{CDN_BASE}/bootstrap@{bootstrap.bootstrap_version}/dist/js/bootstrap.min.js"' \
f' integrity="{bootstrap.bootstrap_js_integrity}" crossorigin="anonymous"></script>'
jquery_js = f'<script src="{CDN_BASE}/jquery@{bootstrap.jquery_version}/dist/jquery.min.js"' \
f' integrity="{bootstrap.jquery_integrity}" crossorigin="anonymous"></script>'
popper_js = f'<script src="{CDN_BASE}/popper.js@{bootstrap.popper_version}/dist/umd/popper.min.js"' \
f' integrity="{bootstrap.popper_integrity}" crossorigin="anonymous"></script>'
assert bootstrap_js in rv
assert jquery_js in rv
assert popper_js in rv
def test_load_js_with_non_default_versions(self, bootstrap):
def _check_assertions(rv):
assert 'bootstrap.min.js' in rv
assert 'jquery.min.js' in rv
assert 'popper.min.js' in rv
assert 'integrity="' not in rv
assert 'crossorigin="anonymous"' not in rv
rv = bootstrap.load_js(version='4.5.6', jquery_version='4.5.6',
popper_version='4.5.6')
_check_assertions(rv)
rv = bootstrap.load_js(version='4.0.0', jquery_version='4.0.0',
popper_version='4.0.0')
_check_assertions(rv)
def test_load_js_with_nonce(self, bootstrap):
nonce = "rAnd0m"
rv = bootstrap.load_js(nonce=nonce)
bootstrap_js = f'<script src="{CDN_BASE}/bootstrap@{bootstrap.bootstrap_version}/dist/js/bootstrap.min.js"' \
f' integrity="{bootstrap.bootstrap_js_integrity}" crossorigin="anonymous"' \
f' nonce="{nonce}"></script>'
jquery_js = f'<script src="{CDN_BASE}/jquery@{bootstrap.jquery_version}/dist/jquery.min.js"' \
f' integrity="{bootstrap.jquery_integrity}" crossorigin="anonymous"' \
f' nonce="{nonce}"></script>'
popper_js = f'<script src="{CDN_BASE}/popper.js@{bootstrap.popper_version}/dist/umd/popper.min.js"' \
f' integrity="{bootstrap.popper_integrity}" crossorigin="anonymous"' \
f' nonce="{nonce}"></script>'
assert bootstrap_js in rv
assert jquery_js in rv
assert popper_js in rv
def test_local_resources(self, bootstrap, client):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = True
response = client.get('/')
data = response.get_data(as_text=True)
assert f'{CDN_BASE}/bootstrap' not in data
assert 'bootstrap.min.js' in data
assert 'bootstrap.min.css' in data
assert 'jquery.min.js' in data
assert 'integrity="' not in data
assert 'crossorigin="anonymous"' not in data
with client.get('/bootstrap/static/css/bootstrap.min.css') as css_response:
assert css_response.status_code != 404
with client.get('/bootstrap/static/js/bootstrap.min.js') as js_response:
assert js_response.status_code != 404
with client.get('/bootstrap/static/jquery.min.js') as jquery_response:
assert jquery_response.status_code != 404
css_rv = bootstrap.load_css()
js_rv = bootstrap.load_js()
assert '/bootstrap/static/css/bootstrap.min.css' in css_rv
assert '/bootstrap/static/js/bootstrap.min.js' in js_rv
assert f'{CDN_BASE}/bootstrap' not in css_rv
assert f'{CDN_BASE}/bootstrap' not in js_rv
assert 'integrity="' not in css_rv
assert 'crossorigin="anonymous"' not in css_rv
assert 'integrity="' not in js_rv
assert 'crossorigin="anonymous"' not in js_rv
def test_local_resources_with_sri(self, bootstrap):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = True
css_rv = bootstrap.load_css(bootstrap_sri='sha384-bootstrap-sri')
js_rv = bootstrap.load_js(
bootstrap_sri='sha384-bootstrap-sri',
jquery_sri='sha384-jquery-sri',
popper_sri='sha384-popper-sri'
)
assert '/bootstrap/static/css/bootstrap.min.css' in css_rv
assert '/bootstrap/static/js/bootstrap.min.js' in js_rv
assert f'{CDN_BASE}/bootstrap' not in css_rv
assert f'{CDN_BASE}/bootstrap' not in js_rv
assert 'integrity="sha384-bootstrap-sri"' in css_rv
assert 'crossorigin="anonymous"' in css_rv
assert 'integrity="sha384-bootstrap-sri"' in js_rv
assert 'integrity="sha384-jquery-sri"' in js_rv
assert 'integrity="sha384-popper-sri"' in js_rv
assert 'crossorigin="anonymous"' in js_rv
def test_cdn_resources(self, bootstrap, client):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = False
response = client.get('/')
data = response.get_data(as_text=True)
assert current_app.config['BOOTSTRAP_SERVE_LOCAL'] is not True
assert f'{CDN_BASE}/bootstrap' in data
assert 'bootstrap.min.js' in data
assert 'bootstrap.min.css' in data
css_rv = bootstrap.load_css()
js_rv = bootstrap.load_js()
assert '/bootstrap/static/css/bootstrap.min.css' not in css_rv
assert '/bootstrap/static/js/bootstrap.min.js' not in js_rv
assert f'{CDN_BASE}/bootstrap' in css_rv
assert f'{CDN_BASE}/bootstrap' in js_rv
def test_cdn_resources_with_custom_sri_hash(self, bootstrap):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = False
css_rv = bootstrap.load_css(bootstrap_sri='sha384-bootstrap-sri')
js_rv = bootstrap.load_js(
bootstrap_sri='sha384-bootstrap-sri',
jquery_sri='sha384-jquery-sri',
popper_sri='sha384-popper-sri'
)
assert 'integrity="sha384-bootstrap-sri"' in css_rv
assert 'crossorigin="anonymous"' in css_rv
assert 'integrity="sha384-bootstrap-sri"' in js_rv
assert 'integrity="sha384-jquery-sri"' in js_rv
assert 'integrity="sha384-popper-sri"' in js_rv
assert 'crossorigin="anonymous"' in js_rv
def test_disabling_sri(self, bootstrap):
css_rv = bootstrap.load_css(bootstrap_sri=False)
js_rv = bootstrap.load_js(
bootstrap_sri=False,
jquery_sri=False,
popper_sri=False
)
assert 'href="' in css_rv
assert 'integrity="' not in css_rv
assert 'crossorigin="anonymous"' not in css_rv
assert 'src="' in js_rv
assert 'integrity="' not in js_rv
assert 'crossorigin="anonymous"' not in js_rv
@pytest.mark.parametrize(
['with_jquery', 'with_popper'],
[
(True, True),
(False, False),
(True, False),
(False, True),
]
)
def test_load_js_args(self, with_jquery, with_popper, bootstrap):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = True
js_rv = bootstrap.load_js(with_jquery=with_jquery, with_popper=with_popper)
assert ('jquery.min.js' in js_rv) == with_jquery
assert ('popper.min.js' in js_rv) == with_popper
|