Repository: saleor/saleor

Issue:
Exception Type: AttributeError when trying to create a password after succeful payment 
Got this exception:
`Exception Type: AttributeError at /order/5d555f01-0f99-4821-b52f-98fa48efce4b/create-password/
Exception Value: 'NoneType' object has no attribute 'addresses'`




Discussion:

Resolving patch:
diff --git a/saleor/order/views.py b/saleor/order/views.py
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -141,12 +141,11 @@ def create_password(request, token):
     else:
         login_form = None
     if register_form.is_valid():
-        register_form.save(request)
-        password = form_data.get('password1')
-        auth_user = auth.authenticate(email=email, password=password)
-        if auth_user is not None:
-            auth.login(request, auth_user)
-        attach_order_to_user(order, auth_user)
+        register_form.save()
+        password = register_form.cleaned_data.get('password')
+        user = auth.authenticate(email=email, password=password)
+        auth.login(request, user)
+        attach_order_to_user(order, user)
         return redirect('order:details', token=token)
     ctx = {'form': register_form, 'email': email, 'order': order,
            'login_form': login_form}
diff --git a/saleor/registration/forms.py b/saleor/registration/forms.py
--- a/saleor/registration/forms.py
+++ b/saleor/registration/forms.py
@@ -1,5 +1,4 @@
 from django import forms
-from django.contrib.auth import login, authenticate
 from django.contrib.auth import forms as django_forms
 from django.utils.translation import pgettext
 
@@ -39,10 +38,6 @@ def save(self, request=None, commit=True):
         user.set_password(password)
         if commit:
             user.save()
-            if request:
-                email = self.cleaned_data['email']
-                user = authenticate(username=email, password=password)
-                login(request, user)
         return user
 
 
diff --git a/saleor/registration/views.py b/saleor/registration/views.py
--- a/saleor/registration/views.py
+++ b/saleor/registration/views.py
@@ -27,7 +27,12 @@ def logout(request):
 def signup(request):
     form = SignupForm(request.POST or None)
     if form.is_valid():
-        form.save(request=request)
+        form.save()
+        password = form.cleaned_data.get('password')
+        email = form.cleaned_data.get('email')
+        user = auth.authenticate(email=email, password=password)
+        if user:
+            auth.login(request, user)
         messages.success(request, _('User has been created'))
         return redirect(settings.LOGIN_REDIRECT_URL)
     ctx = {'form': form}


Test patch:
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -1,9 +1,13 @@
+from django.contrib.auth import get_user_model
 from django.core.urlresolvers import reverse
 from payments import FraudStatus, PaymentStatus
 
 from tests.utils import get_redirect_location
 
 
+User = get_user_model()
+
+
 def test_checkout_flow(request_cart_with_item, client, shipping_method):  # pylint: disable=W0613,R0914
     """
     Basic test case that confirms if core checkout flow works
@@ -345,3 +349,19 @@ def test_language_is_saved_in_order(authorized_client, billing_address, customer
     # After summary step, order is created and it waits for payment
     order = payment_method_page.context['order']
     assert order.language_code == user_language
+
+
+def test_create_user_after_order(order, client):
+    order.user_email = 'hello@mirumee.com'
+    order.save()
+    assert not User.objects.filter(email='hello@mirumee.com').exists()
+    data = {'password': 'password'}
+    url = reverse('order:create-password', kwargs={'token': order.token})
+    response = client.post(url, data=data)
+    redirect_location = get_redirect_location(response)
+    detail_url = reverse('order:details', kwargs={'token': order.token})
+    assert redirect_location == detail_url
+    user = User.objects.filter(email='hello@mirumee.com')
+    assert user.exists()
+    user = user.first()
+    assert user.orders.filter(token=order.token).exists()