Пример #1
0
    def test_get_fxa_clients(self, profile_mock, oauth_mock):
        oauth, profile = get_fxa_clients()
        oauth_mock.Client.assert_called_with(server_url=fxa.constants.STABLE_URLS['oauth'])
        profile_mock.Client.assert_called_with(server_url=fxa.constants.STABLE_URLS['profile'])
        assert oauth == oauth_mock.Client.return_value
        assert profile == profile_mock.Client.return_value

        get_fxa_clients()
        assert oauth_mock.Client.call_count == 1
Пример #2
0
    def test_get_fxa_clients(self, profile_mock, oauth_mock):
        oauth, profile = get_fxa_clients()
        oauth_mock.Client.assert_called_with(server_url=fxa.constants.STABLE_URLS['oauth'])
        profile_mock.Client.assert_called_with(server_url=fxa.constants.STABLE_URLS['profile'])
        assert oauth == oauth_mock.Client.return_value
        assert profile == profile_mock.Client.return_value

        get_fxa_clients()
        assert oauth_mock.Client.call_count == 1
Пример #3
0
    def test_get_fxa_clients(self, profile_mock, oauth_mock):
        oauth, profile = get_fxa_clients()
        oauth_mock.Client.assert_called_with(
            server_url=fxa.constants.STABLE_URLS["oauth"],
            client_id="dude",
            client_secret="abides",
        )
        profile_mock.Client.assert_called_with(
            server_url=fxa.constants.STABLE_URLS["profile"], )
        assert oauth == oauth_mock.Client.return_value
        assert profile == profile_mock.Client.return_value

        get_fxa_clients()
        assert oauth_mock.Client.call_count == 1
Пример #4
0
def fxa_callback(request):
    # remove state from session to prevent multiple attempts
    error_url = f"https://{settings.FXA_EMAIL_PREFS_DOMAIN}/newsletter/fxa-error/"
    sess_state = request.session.pop("fxa_state", None)
    if sess_state is None:
        statsd.incr("news.views.fxa_callback.error.no_state")
        return HttpResponseRedirect(error_url)

    code = request.GET.get("code")
    state = request.GET.get("state")
    if not (code and state):
        statsd.incr("news.views.fxa_callback.error.no_code_state")
        return HttpResponseRedirect(error_url)

    if sess_state != state:
        statsd.incr("news.views.fxa_callback.error.no_state_match")
        return HttpResponseRedirect(error_url)

    fxa_oauth, fxa_profile = get_fxa_clients()
    try:
        access_token = fxa_oauth.trade_code(
            code, ttl=settings.FXA_OAUTH_TOKEN_TTL)["access_token"]
        user_profile = fxa_profile.get_profile(access_token)
    except Exception:
        statsd.incr("news.views.fxa_callback.error.fxa_comm")
        sentry_sdk.capture_exception()
        return HttpResponseRedirect(error_url)

    email = user_profile["email"]
    try:
        user_data = get_user_data(email=email)
    except SalesforceError:
        statsd.incr("news.views.fxa_callback.error.get_user_data")
        sentry_sdk.capture_exception()
        return HttpResponseRedirect(error_url)

    if user_data:
        token = user_data["token"]
    else:
        new_user_data = {
            "email":
            email,
            "optin":
            True,
            "format":
            "H",
            "newsletters": [settings.FXA_REGISTER_NEWSLETTER],
            "source_url":
            f"{settings.FXA_REGISTER_SOURCE_URL}?utm_source=basket-fxa-oauth",
        }
        locale = user_profile.get("locale")
        if locale:
            new_user_data["fxa_lang"] = locale
            lang = get_best_language(get_accept_languages(locale))
            if lang not in newsletter_languages():
                lang = "other"

            new_user_data["lang"] = lang

        try:
            token = upsert_contact(SUBSCRIBE, new_user_data, None)[0]
        except SalesforceError:
            statsd.incr("news.views.fxa_callback.error.upsert_contact")
            sentry_sdk.capture_exception()
            return HttpResponseRedirect(error_url)

    redirect_to = (
        f"https://{settings.FXA_EMAIL_PREFS_DOMAIN}/newsletter/existing/{token}/?fxa=1"
    )
    return HttpResponseRedirect(redirect_to)
Пример #5
0
def fxa_callback(request):
    # remove state from session to prevent multiple attempts
    error_url = f'https://{settings.FXA_EMAIL_PREFS_DOMAIN}/newsletter/fxa-error/'
    sess_state = request.session.pop('fxa_state', None)
    if sess_state is None:
        statsd.incr('news.views.fxa_callback.error.no_state')
        return HttpResponseRedirect(error_url)

    code = request.GET.get('code')
    state = request.GET.get('state')
    if not (code and state):
        statsd.incr('news.views.fxa_callback.error.no_code_state')
        return HttpResponseRedirect(error_url)

    if sess_state != state:
        statsd.incr('news.views.fxa_callback.error.no_state_match')
        return HttpResponseRedirect(error_url)

    fxa_oauth, fxa_profile = get_fxa_clients()
    try:
        access_token = fxa_oauth.trade_code(
            code, ttl=settings.FXA_OAUTH_TOKEN_TTL)['access_token']
        user_profile = fxa_profile.get_profile(access_token)
    except Exception:
        statsd.incr('news.views.fxa_callback.error.fxa_comm')
        sentry_client.captureException()
        return HttpResponseRedirect(error_url)

    email = user_profile['email']
    try:
        user_data = get_user_data(email=email)
    except SalesforceError:
        statsd.incr('news.views.fxa_callback.error.get_user_data')
        sentry_client.captureException()
        return HttpResponseRedirect(error_url)

    if user_data:
        token = user_data['token']
    else:
        new_user_data = {
            'email':
            email,
            'optin':
            True,
            'format':
            'H',
            'newsletters': [settings.FXA_REGISTER_NEWSLETTER],
            'source_url':
            f'{settings.FXA_REGISTER_SOURCE_URL}?utm_source=basket-fxa-oauth',
        }
        lang = user_profile.get('locale')
        if lang:
            lang = get_best_language(get_accept_languages(lang))
            new_user_data['lang'] = lang

        try:
            token = upsert_contact(SUBSCRIBE, new_user_data, None)[0]
        except SalesforceError:
            statsd.incr('news.views.fxa_callback.error.upsert_contact')
            sentry_client.captureException()
            return HttpResponseRedirect(error_url)

    redirect_to = f'https://{settings.FXA_EMAIL_PREFS_DOMAIN}/newsletter/existing/{token}/?fxa=1'
    return HttpResponseRedirect(redirect_to)