Repository: mozmeao/basket

Issue:
FxA URL sometimes redirects to a 404
See [bug 1555961](https://bugzilla.mozilla.org/show_bug.cgi?id=1555961). It seems the redirect URL sometimes has a double slash. 


Discussion:

Resolving patch:
diff --git a/basket/news/views.py b/basket/news/views.py
--- a/basket/news/views.py
+++ b/basket/news/views.py
@@ -1,6 +1,7 @@
 import os
 import re
 from time import time
+from urllib import urlencode
 
 from django.conf import settings
 from django.core.exceptions import NON_FIELD_ERRORS
@@ -10,6 +11,7 @@
 from django.views.decorators.csrf import csrf_exempt
 from django.views.decorators.http import require_POST, require_safe
 
+import fxa.constants
 from basket import errors
 from django_statsd.clients import statsd
 from ratelimit.exceptions import Ratelimited
@@ -127,18 +129,30 @@ def generate_fxa_state():
     return os.urandom(16).encode('hex')
 
 
+def get_fxa_authorization_url(state, redirect_uri, email):
+    fxa_server = fxa.constants.ENVIRONMENT_URLS.get(settings.FXA_OAUTH_SERVER_ENV)['oauth']
+    params = {
+        'client_id': settings.FXA_CLIENT_ID,
+        'state': state,
+        'redirect_uri': redirect_uri,
+        'scope': 'profile',
+    }
+    if email:
+        params['email'] = email
+
+    return '{}/authorization?{}'.format(fxa_server, urlencode(params))
+
+
 @require_safe
 def fxa_start(request):
     if not settings.FXA_CLIENT_ID:
         redirect_to = 'https://www.mozilla.org/firefox/accounts/'
     else:
-        fxa_oauth = get_fxa_clients()[0]
         fxa_state = request.session['fxa_state'] = generate_fxa_state()
         redirect_uri = request.build_absolute_uri('/fxa/callback/')
-        redirect_to = fxa_oauth.get_redirect_url(state=fxa_state,
-                                                 redirect_uri=redirect_uri,
-                                                 scope='profile',
-                                                 email=request.GET.get('email'))
+        email = request.GET.get('email')
+        redirect_to = get_fxa_authorization_url(fxa_state, redirect_uri, email)
+
     return HttpResponseRedirect(redirect_to)
 
 


Test patch:
diff --git a/basket/news/tests/test_views.py b/basket/news/tests/test_views.py
--- a/basket/news/tests/test_views.py
+++ b/basket/news/tests/test_views.py
@@ -1350,31 +1350,25 @@ def test_new_user_without_locale(self):
 class FxAPrefCenterOauthStartTests(ViewsPatcherMixin, TestCase):
     def setUp(self):
         self.client = Client()
-        self._patch_views('get_fxa_clients')
+        self._patch_views('get_fxa_authorization_url')
         self._patch_views('generate_fxa_state')
 
     def test_get_redirect_url(self):
-        fxa_oauth_mock = Mock()
-        fxa_oauth_mock.get_redirect_url.return_value = good_redirect = 'https://example.com/oauth'
-        self.get_fxa_clients.return_value = fxa_oauth_mock, None
+        self.get_fxa_authorization_url.return_value = good_redirect = 'https://example.com/oauth'
         self.generate_fxa_state.return_value = 'the-dude-abides'
         resp = self.client.get('/fxa/')
-        fxa_oauth_mock.get_redirect_url.assert_called_with(state='the-dude-abides',
-                                                           redirect_uri='http://testserver/fxa/callback/',
-                                                           scope='profile',
-                                                           email=None)
+        self.get_fxa_authorization_url.assert_called_with('the-dude-abides',
+                                                          'http://testserver/fxa/callback/',
+                                                          None)
         assert resp.status_code == 302
         assert resp['location'] == good_redirect
 
     def test_get_redirect_url_with_email(self):
-        fxa_oauth_mock = Mock()
-        fxa_oauth_mock.get_redirect_url.return_value = good_redirect = 'https://example.com/oauth'
-        self.get_fxa_clients.return_value = fxa_oauth_mock, None
+        self.get_fxa_authorization_url.return_value = good_redirect = 'https://example.com/oauth'
         self.generate_fxa_state.return_value = 'the-dude-abides'
         resp = self.client.get('/fxa/?email=dude%40example.com')
-        fxa_oauth_mock.get_redirect_url.assert_called_with(state='the-dude-abides',
-                                                           redirect_uri='http://testserver/fxa/callback/',
-                                                           scope='profile',
-                                                           email='dude@example.com')
+        self.get_fxa_authorization_url.assert_called_with('the-dude-abides',
+                                                          'http://testserver/fxa/callback/',
+                                                          'dude@example.com')
         assert resp.status_code == 302
         assert resp['location'] == good_redirect