wcs: do not add tryauth for bots (#68052)

This commit is contained in:
Thomas NOËL 2022-08-09 12:18:23 +02:00 committed by Thomas NOEL
parent 76167cab03
commit 12087eb075
3 changed files with 47 additions and 5 deletions

View File

@ -65,6 +65,10 @@ def get_formdef_css_classes(formdef):
return classes
def is_a_bot(request):
return request and 'bot' in (request.headers.get('User-Agent') or '').lower()
@register_cell_class
class WcsFormCell(CellBase):
formdef_reference = models.CharField(_('Form'), max_length=150)
@ -137,9 +141,9 @@ class WcsFormCell(CellBase):
context = super().get_cell_extra_context(context)
context['slug'] = self.formdef_reference.split(':')[-1]
context['title'] = self.cached_title
context['url'] = self.cached_url + 'tryauth'
if request:
context['url'] += '?cancelurl=%s' % urllib.parse.quote(request.build_absolute_uri())
context['url'] = self.cached_url
if not is_a_bot(request):
context['url'] += 'tryauth?cancelurl=%s' % urllib.parse.quote(request.build_absolute_uri())
if self.cached_json:
context['description'] = mark_safe(self.cached_json.get('description', ''))
context['css_classes'] = get_formdef_css_classes(self.cached_json)
@ -666,6 +670,7 @@ class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin):
def get_cell_extra_context(self, context):
extra_context = super().get_cell_extra_context(context)
extra_context.update(WcsBlurpMixin.get_cell_extra_context(self, context))
extra_context['request_is_a_bot'] = is_a_bot(context.get('request'))
if not self.category_reference:
return extra_context
extra_context['slug'] = self.category_reference.split(':')[-1]

View File

@ -1,5 +1,5 @@
{% block form-link-pre %}{% endblock %}
<a href="{{ form.url }}tryauth?cancelurl={{ uri|iriencode }}">{% block form-link-title %}{{ form.title }}{% endblock %}</a>
<a href="{{ form.url }}{% if not request_is_a_bot %}tryauth?cancelurl={{ uri|iriencode }}{% endif %}">{% block form-link-title %}{{ form.title }}{% endblock %}</a>
{% if form.description %}
<div class="description">{% block form-link-description %}{{ form.description|safe }}{% endblock %}</div>
{% endif %}

View File

@ -658,7 +658,7 @@ def test_form_cell_render(mock_send):
cell = WcsFormCell(page=page, placeholder='content', order=0)
cell.formdef_reference = 'default:form-title'
cell.save()
result = cell.render({})
result = cell.render({'request': RequestFactory().get('/')})
assert 'http://127.0.0.1:8999/form-title/tryauth' in result
assert 'form title' in result
@ -1424,6 +1424,43 @@ def test_forms_of_category_cell_check_validity(mock_send, context):
assert validity_info.invalid_since is not None
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
def test_no_tryauth_for_bot(mock_send):
page = Page(title='xxx', slug='test_form_cell_render', template_name='standard')
page.save()
request = RequestFactory().get('/')
request.META[
'HTTP_USER_AGENT'
] = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
context = {'request': request}
cell = WcsFormCell(page=page, placeholder='content', order=0)
cell.formdef_reference = 'default:form-title'
cell.save()
result = cell.render(context)
assert 'http://127.0.0.1:8999/form-title/' in result
assert 'form title' in result
# no tryout, no cancelurl
assert 'tryauth' not in result
assert 'cancelurl' not in result
cell = WcsFormsOfCategoryCell(page=page, placeholder='content', order=0)
cell.category_reference = 'default:test-9'
cell.ordering = 'alpha'
cell.save()
context['synchronous'] = True # to get fresh content
result = cell.render(context)
assert 'form title' in result and 'a second form title' in result
assert result.index('form title') > result.index('a second form title')
assert 'http://127.0.0.1:8999/form-title/' in result
assert 'http://127.0.0.1:8999/a-second-form-title/' in result
assert 'keyword-foo' in result
assert 'keyword-bar' in result
# no tryout, no cancelurl
assert 'tryauth' not in result
assert 'cancelurl' not in result
def test_current_drafts_cell_render_unlogged(context):
page = Page(title='xxx', slug='test_current_drafts_cell_render', template_name='standard')
page.save()