def test_verifier_false(self): """ test that verify_honeypot_value fails when HONEYPOT_VERIFIER returns False """ request = _get_POST_request() request.POST[settings.HONEYPOT_FIELD_NAME] = "" settings.HONEYPOT_VERIFIER = lambda x: False resp = verify_honeypot_value(request, None) self.assertEquals(resp.__class__, HttpResponseBadRequest)
def test_honeypot_value_callable(self): """ test that verify_honeypot_value succeeds when HONEYPOT_VALUE is a callable """ request = _get_POST_request() settings.HONEYPOT_VALUE = lambda: '(test string)' request.POST[settings.HONEYPOT_FIELD_NAME] = settings.HONEYPOT_VALUE() resp = verify_honeypot_value(request, None) self.assertEquals(resp, None)
def test_honeypot_value_string(self): """ test that verify_honeypot_value succeeds when HONEYPOT_VALUE is a string """ request = _get_POST_request() settings.HONEYPOT_VALUE = '(test string)' request.POST[settings.HONEYPOT_FIELD_NAME] = settings.HONEYPOT_VALUE resp = verify_honeypot_value(request, None) self.assertEquals(resp, None)
def test_verifier_false(self): """ test that verify_honeypot_value fails when HONEYPOT_VERIFIER returns False """ request = _get_POST_request() request.POST[settings.HONEYPOT_FIELD_NAME] = '' settings.HONEYPOT_VERIFIER = lambda x: False resp = verify_honeypot_value(request, None) self.assertEquals(resp.__class__, HttpResponseBadRequest)
def test_honeypot_value_callable(self): """ test that verify_honeypot_value succeeds when HONEYPOT_VALUE is a callable """ request = _get_POST_request() settings.HONEYPOT_VALUE = lambda: "(test string)" request.POST[settings.HONEYPOT_FIELD_NAME] = settings.HONEYPOT_VALUE() resp = verify_honeypot_value(request, None) self.assertEquals(resp, None)
def post(self, request, *args, **kwargs): from django.conf import settings honeypot_fieldname = self.honeypot_fieldname or getattr( settings, 'HONEYPOT_FIELD_NAME', 'family_name') response = verify_honeypot_value(request, honeypot_fieldname) if response: return response return super(HoneypotMixin, self).post(request, *args, **kwargs)
def dispatch(self, request, *args, **kwargs): is_yari_signup = request.session.get("yari_signup", False) if not is_yari_signup: response = verify_honeypot_value(request, None) if isinstance(response, HttpResponseBadRequest): return response return super(SignupView, self).dispatch(request, *args, **kwargs)
def check_honeypot(request, form): u""" Make sure that the hidden form field is empty, using django-honeypot. """ try: from honeypot.decorators import verify_honeypot_value return verify_honeypot_value(request, '') is None except ImportError: return True
def check_honeypot(request, form): """ Make sure that the hidden form field is empty, using django-honeypot. """ try: from honeypot.decorators import verify_honeypot_value return verify_honeypot_value(request, '') is None except ImportError: # pragma: no cover return True
def post(self, request): # check honeypot value manually honeypot = verify_honeypot_value(request, settings.HONEYPOT_FIELD_NAME) if honeypot: return honeypot # check for form validity form = self.form_class(request.POST) if form.is_valid(): # send message on valid form submission self.send_message(request, form.cleaned_data) # notify user of success and redirect messages.success(request, 'Thanks you for contacting us!') return HttpResponseRedirect( MESSAGE_SENT_URL if MESSAGE_SENT_URL else request.path) # form was not valid, show errors to user return render_to_response('hello/form.html', {'form': form}, context_instance=RequestContext(request))
def process_view(self, request, callback, callback_args, callback_kwargs): if request.is_ajax(): return None if getattr(callback, 'honeypot_exempt', False): return None return verify_honeypot_value(request, None)
def test_field_missing(self): """ test that verify_honeypot_value succeeds when HONEYPOT_FIELD_NAME is missing from request.POST """ request = _get_POST_request() resp = verify_honeypot_value(request, None) self.assertEquals(resp.__class__, HttpResponseBadRequest)
def test_field_blank(self): """ test that verify_honeypot_value succeeds when HONEYPOT_VALUE is blank """ request = _get_POST_request() request.POST[settings.HONEYPOT_FIELD_NAME] = '' resp = verify_honeypot_value(request, None) self.assertEquals(resp, None)
def test_no_call_on_get(self): """ test that verify_honeypot_value is not called when request.method == GET """ request = _get_GET_request() resp = verify_honeypot_value(request, None) self.assertEquals(resp, None)
def __call__(self, request, *args, **kwargs): """A view displaying a form, or JSON if request is AJAX.""" obj = self.get_object(request, *args, **kwargs) form_args, form_kwargs = self.form_args(request, obj) if self.form_prefix: form_kwargs['prefix'] = self.form_prefix if request.method == "POST": if self.honeypot: response = verify_honeypot_value(request, None) if response: return response # do I need to be logged in? if self.POST_login and not request.user.is_authenticated(): return require_login(request) form_kwargs['data'] = request.POST form = self.form_class(*form_args, **form_kwargs) if form.is_valid(): add_args = self.success(form, request) response_data = { 'success': True, 'message': self.success_message, 'redirect': request.GET.get('next') } if add_args: response_data.update(add_args) if not request.is_ajax() and response_data['redirect']: return HttpResponseRedirect( urlquote_plus(response_data['redirect'], safe='/?=&')) elif request.is_ajax(): # Form was sent with errors. Send them back. if self.form_prefix: errors = {} for key, value in form.errors.items(): errors["%s-%s" % (self.form_prefix, key)] = value else: errors = form.errors response_data = {'success': False, 'errors': errors} else: response_data = None if request.is_ajax(): return HttpResponse( LazyEncoder(ensure_ascii=False).encode(response_data)) else: if self.POST_login and not request.user.is_authenticated( ) and not request.is_ajax(): return require_login(request) form = self.form_class(*form_args, **form_kwargs) response_data = None title = self.title if request.is_ajax(): template = self.template else: template = self.full_template cd = self.context_description(request, obj) if cd: title += ": " + cd if self.placeholdize: form = placeholdized(form) context = { self.formname: form, "title": title, "honeypot": self.honeypot, "placeholdize": self.placeholdize, "submit": self.submit, "action": self.action, "response_data": response_data, "ajax_template": self.template, "view_args": args, "view_kwargs": kwargs, } context.update(self.extra_context(request, obj)) return render_to_response(template, context, context_instance=RequestContext(request))
def inner(request, *args, **kwargs): response = verify_honeypot_value(request, field_name) if response: return response else: return func(request, *args, **kwargs)
def dispatch(self, request, *args, **kwargs): response = verify_honeypot_value(request, None) if isinstance(response, HttpResponseBadRequest): return response return super(SignupView, self).dispatch(request, *args, **kwargs)
def process_view(self, request, callback, callback_args, callback_kwargs): if not honeypot_required(request): return None if request.is_ajax(): return None return verify_honeypot_value(request, None)
def check_honeypot(request, form): u""" Make sure that the hidden form field is empty, using django-honeypot. """ return verify_honeypot_value(request, '') is None
def process_view(self, request, callback, callback_args, callback_kwargs): if request.is_ajax() or request.path_info in getattr(settings, 'HONEYPOT_SKIP_URLS', []): return None return verify_honeypot_value(request, None)
def __call__(self, request, *args, **kwargs): """A view displaying a form, or JSON if request is AJAX.""" obj = self.get_object(request, *args, **kwargs) form_args, form_kwargs = self.form_args(request, obj) if self.form_prefix: form_kwargs['prefix'] = self.form_prefix if request.method == "POST": if self.honeypot: response = verify_honeypot_value(request, None) if response: return response # do I need to be logged in? if self.POST_login and not request.user.is_authenticated(): return require_login(request) form_kwargs['data'] = request.POST form = self.form_class(*form_args, **form_kwargs) if form.is_valid(): add_args = self.success(form, request) response_data = { 'success': True, 'message': self.success_message, 'redirect': request.GET.get('next') } if add_args: response_data.update(add_args) if not request.is_ajax() and response_data['redirect']: return HttpResponseRedirect(urlquote_plus( response_data['redirect'], safe='/?=&')) elif request.is_ajax(): # Form was sent with errors. Send them back. if self.form_prefix: errors = {} for key, value in form.errors.items(): errors["%s-%s" % (self.form_prefix, key)] = value else: errors = form.errors response_data = {'success': False, 'errors': errors} else: response_data = None if request.is_ajax(): return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data)) else: if (self.POST_login and not request.user.is_authenticated() and not request.is_ajax()): return require_login(request) form = self.form_class(*form_args, **form_kwargs) response_data = None title = self.title if request.is_ajax(): template = self.template else: template = self.full_template cd = self.context_description(request, obj) if cd: title += ": " + cd if self.placeholdize: form = placeholdized(form) context = { self.formname: form, "title": title, "honeypot": self.honeypot, "placeholdize": self.placeholdize, "submit": self.submit, "response_data": response_data, "ajax_template": self.template, "view_args": args, "view_kwargs": kwargs, } context.update(self.extra_context(request, obj)) return render_to_response(template, context, context_instance=RequestContext(request))
def process_view(self, request, callback, callback_args, callback_kwargs): if request.is_ajax(): return None return verify_honeypot_value(request, None)
def test_field_blank(self): """ test that verify_honeypot_value succeeds when HONEYPOT_VALUE is blank """ request = _get_POST_request() request.POST[settings.HONEYPOT_FIELD_NAME] = "" resp = verify_honeypot_value(request, None) self.assertEquals(resp, None)