Beispiel #1
0
def send_recovery_message(request):
    """
    Send a recovery message to an email address.

    required form parameter: email

    If email not provided or not syntactically correct, returns 400.
    If email not known, returns 404.
    Otherwise, queues a task to send the message and returns 200.
    """
    try:
        validate_email(request.POST.get('email'))
    except EmailValidationError as e:
        return invalid_email_response(e)

    email = request.POST.get('email')
    if email_is_blocked(email):
        # don't let on there's a problem
        return HttpResponseJSON({'status': 'ok'})

    try:
        user_data = get_user_data(email=email)
    except NewsletterException as e:
        return newsletter_exception_response(e)

    if not user_data:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'Email address not known',
            'code': errors.BASKET_UNKNOWN_EMAIL,
        }, 404)  # Note: Bedrock looks for this 404

    send_recovery_message_task.delay(email)
    return HttpResponseJSON({'status': 'ok'})
Beispiel #2
0
    def test_invalid_email(self):
        """Should raise exception for invalid email."""
        with self.assertRaises(EmailValidationError):
            validate_email('dude@[email protected]')

        with self.assertRaises(EmailValidationError):
            validate_email('')

        with self.assertRaises(EmailValidationError):
            validate_email(None)
Beispiel #3
0
def subscribe(request):
    data = request.POST.dict()
    newsletters = data.get('newsletters', None)
    if not newsletters:
        # request.body causes tests to raise exceptions
        # while request.read() works.
        raw_request = request.read()
        if 'newsletters=' in raw_request:
            # malformed request from FxOS
            # Can't use QueryDict since the string is not url-encoded.
            # It will convert '+' to ' ' for example.
            data = dict(pair.split('=') for pair in raw_request.split('&'))
            email = data.get('email')
            if email:
                data['email'] = force_unicode(email)
            statsd.incr('subscribe-fxos-workaround')
        else:
            return HttpResponseJSON({
                'status': 'error',
                'desc': 'newsletters is missing',
                'code': errors.BASKET_USAGE_ERROR,
            }, 400)

    if 'email' not in data:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'email is required',
            'code': errors.BASKET_USAGE_ERROR,
        }, 401)

    if email_is_blocked(data['email']):
        # don't let on there's a problem
        return HttpResponseJSON({'status': 'ok'})

    optin = data.get('optin', 'N').upper() == 'Y'
    sync = data.get('sync', 'N').upper() == 'Y'

    if optin and (not request.is_secure() or not has_valid_api_key(request)):
        # for backward compat we just ignore the optin if
        # no valid API key is sent.
        optin = False

    if sync:
        if not request.is_secure():
            return HttpResponseJSON({
                'status': 'error',
                'desc': 'subscribe with sync=Y requires SSL',
                'code': errors.BASKET_SSL_REQUIRED,
            }, 401)
        if not has_valid_api_key(request):
            return HttpResponseJSON({
                'status': 'error',
                'desc': 'Using subscribe with sync=Y, you need to pass a '
                        'valid `api-key` GET or POST parameter or X-api-key header',
                'code': errors.BASKET_AUTH_ERROR,
            }, 401)

    try:
        validate_email(data.get('email'))
    except EmailValidationError as e:
        return invalid_email_response(e)

    return update_user_task(request, SUBSCRIBE, data=data, optin=optin, sync=sync)
Beispiel #4
0
def get_involved(request):
    data = request.POST.dict()
    if 'email' not in data:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'email is required',
            'code': errors.BASKET_USAGE_ERROR,
        }, 401)
    if email_is_blocked(data['email']):
        # don't let on there's a problem
        return HttpResponseJSON({'status': 'ok'})
    if 'interest_id' not in data:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'interest_id is required',
            'code': errors.BASKET_USAGE_ERROR,
        }, 401)

    try:
        Interest.objects.get(interest_id=data['interest_id'])
    except Interest.DoesNotExist:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'invalid interest_id',
            'code': errors.BASKET_USAGE_ERROR,
        }, 401)

    if 'lang' not in data:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'lang is required',
            'code': errors.BASKET_USAGE_ERROR,
        }, 401)
    if not language_code_is_valid(data['lang']):
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'invalid language',
            'code': errors.BASKET_INVALID_LANGUAGE,
        }, 400)
    if 'name' not in data:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'name is required',
            'code': errors.BASKET_USAGE_ERROR,
        }, 401)
    if 'country' not in data:
        return HttpResponseJSON({
            'status': 'error',
            'desc': 'country is required',
            'code': errors.BASKET_USAGE_ERROR,
        }, 401)

    try:
        validate_email(data.get('email'))
    except EmailValidationError as e:
        return invalid_email_response(e)

    update_get_involved.delay(
        data['interest_id'],
        data['lang'],
        data['name'],
        data['email'],
        data['country'],
        data.get('format', 'H'),
        data.get('subscribe', False),
        data.get('message', None),
        data.get('source_url', None),
    )
    return HttpResponseJSON({'status': 'ok'})
Beispiel #5
0
 def test_invalid_email(self, dj_validate_email):
     """Should raise an exception for an invalid email."""
     dj_validate_email.side_effect = ValidationError("Invalid email")
     with self.assertRaises(utils.EmailValidationError):
         utils.validate_email(self.email)
Beispiel #6
0
 def test_valid_email(self):
     """Should return without raising an exception for a valid email."""
     self.assertIsNone(utils.validate_email(self.email))
Beispiel #7
0
 def test_valid_email(self):
     """Should return None for valid email."""
     self.assertIsNone(validate_email('*****@*****.**'))
     self.assertIsNone(validate_email('*****@*****.**'))
     self.assertIsNone(validate_email('*****@*****.**'))
Beispiel #8
0
 def test_non_ascii_email_domain(self):
     """Should not raise exception, and should validate for non-ascii domains."""
     self.assertIsNone(validate_email(u'dude@黒川.日本'))
     self.assertIsNone(validate_email('dude@黒川.日本'))