| 1 |
import hashlib |
| 2 |
import random |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from flask_caching import Cache |
| 7 |
|
| 8 |
|
| 9 |
def _query_string_key(path, args_as_sorted_tuple, hash_method, key_prefix="view/%s"): |
| 10 |
"""Rebuild the key that ``cached(query_string=True)`` produces.""" |
| 11 |
return (key_prefix % path) + hash_method( |
| 12 |
str(args_as_sorted_tuple).encode() |
| 13 |
).hexdigest() |
| 14 |
|
| 15 |
|
| 16 |
def test_default_hash_method_is_sha256(app): |
| 17 |
cache = Cache(app) |
| 18 |
|
| 19 |
@app.route("/works") |
| 20 |
@cache.cached(query_string=True) |
| 21 |
def view(): |
| 22 |
return "value" |
| 23 |
|
| 24 |
with app.test_request_context("/works?a=1&b=2"): |
| 25 |
assert view.make_cache_key() == _query_string_key( |
| 26 |
"/works", (("a", "1"), ("b", "2")), hashlib.sha256 |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def test_query_string_key_includes_key_prefix(app, cache): |
| 31 |
@app.route("/works") |
| 32 |
@cache.cached(query_string=True, key_prefix="custom/%s") |
| 33 |
def view(): |
| 34 |
return "value" |
| 35 |
|
| 36 |
with app.test_request_context("/works?a=1"): |
| 37 |
assert view.make_cache_key() == _query_string_key( |
| 38 |
"/works", (("a", "1"),), hashlib.sha256, key_prefix="custom/%s" |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def test_cached_uses_configured_hash_method(app): |
| 43 |
app.config["CACHE_HASH_METHOD"] = hashlib.md5 |
| 44 |
cache = Cache(app) |
| 45 |
|
| 46 |
@app.route("/works") |
| 47 |
@cache.cached(query_string=True) |
| 48 |
def view(): |
| 49 |
return "value" |
| 50 |
|
| 51 |
with app.test_request_context("/works?a=1&b=2"): |
| 52 |
assert view.make_cache_key() == _query_string_key( |
| 53 |
"/works", (("a", "1"), ("b", "2")), hashlib.md5 |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def test_decorator_argument_overrides_configured_hash_method(app): |
| 58 |
app.config["CACHE_HASH_METHOD"] = hashlib.md5 |
| 59 |
cache = Cache(app) |
| 60 |
|
| 61 |
@app.route("/works") |
| 62 |
@cache.cached(query_string=True, hash_method=hashlib.sha512) |
| 63 |
def view(): |
| 64 |
return "value" |
| 65 |
|
| 66 |
with app.test_request_context("/works?a=1&b=2"): |
| 67 |
assert view.make_cache_key() == _query_string_key( |
| 68 |
"/works", (("a", "1"), ("b", "2")), hashlib.sha512 |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
def test_hash_method_read_from_extension_config(app): |
| 73 |
"""The option also works when passed to ``Cache`` instead of ``app``.""" |
| 74 |
cache = Cache(app, config={"CACHE_HASH_METHOD": hashlib.md5}) |
| 75 |
|
| 76 |
@app.route("/works") |
| 77 |
@cache.cached(query_string=True) |
| 78 |
def view(): |
| 79 |
return "value" |
| 80 |
|
| 81 |
with app.test_request_context("/works?a=1&b=2"): |
| 82 |
assert view.make_cache_key() == _query_string_key( |
| 83 |
"/works", (("a", "1"), ("b", "2")), hashlib.md5 |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
def test_non_callable_hash_method_raises(app): |
| 88 |
app.config["CACHE_HASH_METHOD"] = "sha256" |
| 89 |
|
| 90 |
with pytest.raises(ValueError, match="CACHE_HASH_METHOD"): |
| 91 |
Cache(app) |
| 92 |
|
| 93 |
|
| 94 |
def test_hash_method_resolved_after_decoration(app): |
| 95 |
"""Decorators run at import time, before ``init_app``. |
| 96 |
|
| 97 |
The configured hash method must therefore be picked up when the key is |
| 98 |
built, not when the decorator is applied. |
| 99 |
""" |
| 100 |
cache = Cache() |
| 101 |
|
| 102 |
@cache.cached(query_string=True) |
| 103 |
def view(): |
| 104 |
return "value" |
| 105 |
|
| 106 |
app.config["CACHE_HASH_METHOD"] = hashlib.md5 |
| 107 |
cache.init_app(app) |
| 108 |
|
| 109 |
with app.test_request_context("/works?a=1&b=2"): |
| 110 |
assert view.make_cache_key() == _query_string_key( |
| 111 |
"/works", (("a", "1"), ("b", "2")), hashlib.md5 |
| 112 |
) |
| 113 |
|
| 114 |
|
| 115 |
def test_delete_memoized_matches_configured_hash_method(app): |
| 116 |
"""``make_cache_key`` is built at decoration time and must agree with the |
| 117 |
key used at call time, otherwise ``delete_memoized`` deletes nothing.""" |
| 118 |
app.config["CACHE_HASH_METHOD"] = hashlib.md5 |
| 119 |
cache = Cache(app) |
| 120 |
|
| 121 |
@cache.memoize(50) |
| 122 |
def add(a, b): |
| 123 |
return a + b + random.random() |
| 124 |
|
| 125 |
with app.test_request_context(): |
| 126 |
result = add(1, 2) |
| 127 |
assert add(1, 2) == result |
| 128 |
|
| 129 |
cache.delete_memoized(add, 1, 2) |
| 130 |
|
| 131 |
assert add(1, 2) != result |
| 132 |
|
| 133 |
|
| 134 |
def test_configured_hash_method_changes_memoize_key(app): |
| 135 |
app.config["CACHE_HASH_METHOD"] = hashlib.md5 |
| 136 |
md5_cache = Cache(app) |
| 137 |
|
| 138 |
@md5_cache.memoize(50) |
| 139 |
def add(a, b): |
| 140 |
return a + b |
| 141 |
|
| 142 |
with app.test_request_context(): |
| 143 |
md5_key = add.make_cache_key(add.uncached, 1, 2) |
| 144 |
# Swap the configured method on the same cache so that the version |
| 145 |
# hash stays identical and only the digest can differ. |
| 146 |
md5_cache.hash_method = hashlib.sha512 |
| 147 |
assert add.make_cache_key(add.uncached, 1, 2) != md5_key |
| 148 |
|
| 149 |
|
| 150 |
def test_file_hash_method_is_independent(app, tmp_path): |
| 151 |
"""``CACHE_HASH_METHOD`` must not change the FileSystemCache file names.""" |
| 152 |
app.config.update( |
| 153 |
CACHE_TYPE="FileSystemCache", |
| 154 |
CACHE_DIR=str(tmp_path), |
| 155 |
CACHE_HASH_METHOD=hashlib.md5, |
| 156 |
) |
| 157 |
cache = Cache(app) |
| 158 |
|
| 159 |
with app.test_request_context(): |
| 160 |
assert cache.cache._hash_method is hashlib.sha256 |