Beispiel #1
0
def email_web_hook(request):
  if request.GET.get('token') != settings.EXTERNAL_API_TOKEN and not settings.DEBUG:
    return HttpResponseForbidden()

  else:
    if not email_service.is_spam(**dict(request.POST.items())):

      try:
        email = Email.construct_incoming_email(**dict(request.POST.items()))
        email_service.create_incoming_mail(email)

      except (EmailParseError, IntegrityError):
        logger.info('ignoring invalid email')
      except Exception:
        logger.exception('error accepting email')

        return HttpResponseServerError()
    else:
      logger.info('spam detected')

    return HttpResponse(status=200)
def test_email_service_detects_spam(input_values, expected):
  assert expected == email_service.is_spam(**input_values)