misc: invalidate requests wrapper cache if url contains ?nocache (#85755)
gitea/publik-django-templatetags/pipeline/head This commit looks good Details

Only if current session is authenticated.
This commit is contained in:
Benjamin Dauvergne 2024-01-16 18:54:42 +01:00
parent 701b3419cd
commit e392a9c6dd
2 changed files with 28 additions and 2 deletions

View File

@ -107,6 +107,10 @@ class Requests(RequestsSession):
url = urllib.parse.urlunparse((scheme, netloc, path, params, query, fragment))
if django_request and 'nocache' in django_request.GET and django_request.user.is_authenticated:
# do not use cache if ?nocache is given and user is authenticated
invalidate_cache = True
if method == 'GET' and cache_duration:
# handle cache
params = urlencode(kwargs.get('params', {}))

View File

@ -15,10 +15,11 @@ from publik_django_templatetags.wcs.context_processors import Cards
@pytest.fixture
def context():
request = RequestFactory().get('/')
ctx = Context(
{
'cards': Cards(),
'request': RequestFactory().get('/'),
'cards': Cards(request),
'request': request,
}
)
ctx['request'].user = None
@ -713,3 +714,24 @@ def test_cache(mock_send, settings, context, django_cache, freezer):
t = Template('{% for card in cards|objects:"foo" %}{{ card.id }} {% endfor %}')
assert t.render(context) == '1 2 '
assert mock_send.call_count == 3
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
def test_nocache(mock_send, settings, context, django_cache, freezer):
t = Template('{% for card in cards|objects:"foo" %}{{ card.id }} {% endfor %}')
assert t.render(context) == '1 2 '
assert mock_send.call_count == 1
context['request'] = RequestFactory().get('/?nocache')
context['cards'] = Cards(context['request'])
context['request'].user = MockAnonymousUser()
t = Template('{% for card in cards|objects:"foo" %}{{ card.id }} {% endfor %}')
assert t.render(context) == '1 2 '
assert mock_send.call_count == 1
context['request'].user = MockUser()
t = Template('{% for card in cards|objects:"foo" %}{{ card.id }} {% endfor %}')
assert t.render(context) == '1 2 '
assert mock_send.call_count == 2