def test_get_fxa_profile_email_error(self, gfc_mock): profile_mock = Mock() gfc_mock.return_value = None, profile_mock get_email = profile_mock.get_email get_email.side_effect = fxa.errors.ClientError() assert not get_fxa_profile_email('ralphs-card') get_email.assert_called_with('ralphs-card')
def oauth_fxa(request): """ Acts as an OAuth relier for Firefox Accounts. Currently specifically tuned to handle the OAuth flow for the Firefox Concert Series (Q4 2018). If additional OAuth flows are required in the future, please refactor this method. """ if not switch('firefox_concert_series'): return HttpResponseRedirect(reverse('mozorg.home')) # expected state should be in user's cookies stateExpected = request.COOKIES.get('fxaOauthState', None) # provided state passed back from FxA - these state values should match stateProvided = request.GET.get('state', None) # code must be present - is in redirect querystring from FxA code = request.GET.get('code', None) error = False cookie_age = 86400 # 1 day # ensure all the data we need is present and valid if not (stateExpected and stateProvided and code): error = True elif stateExpected != stateProvided: error = True else: token = get_fxa_oauth_token(code) if not token: error = True else: email = get_fxa_profile_email(token) if not email: error = True else: # add email to mailing list # check for Firefox include_re = re.compile(r'\bFirefox\b', flags=re.I) exclude_re = re.compile(r'\b(Camino|Iceweasel|SeaMonkey)\b', flags=re.I) value = request.META.get('HTTP_USER_AGENT', '') isFx = bool(include_re.search(value) and not exclude_re.search(value)) # add user to mailing list for future concert updates rsvp_ok = fxa_concert_rsvp(email, isFx) if not rsvp_ok: error = True if error: # send user to a custom error page response = HttpResponseRedirect(reverse('mozorg.oauth.fxa-error')) else: # send user back to the concerts page response = HttpResponseRedirect(reverse('firefox.concerts')) response.set_cookie('fxaOauthVerified', True, max_age=cookie_age, httponly=False) return response