Permalink
Browse files

Fixed: #143 #142 #127 #79

  • Loading branch information...
1 parent 4a2f36e commit c773adfc07b35edc13f2aeb8cc686c11c7513975 @flashingpumpkin committed Jul 31, 2012
@@ -1,7 +1,6 @@
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
-from django.contrib.auth.views import login
from django.http import HttpResponseRedirect
from django.utils import importlib
from django.utils.translation import ugettext_lazy as _
@@ -0,0 +1 @@
+{{ error }}
@@ -79,7 +79,7 @@ def get_setup_callback_mock_response(self, *args, **kwargs):
"""
raise NotImplementedError
- def create_profile(self):
+ def create_profile(self, user):
raise NotImplementedError
def create_user(self, is_active=True):
@@ -116,7 +116,7 @@ def setup_callback(self, MockRequest):
def flow(self):
self.redirect()
self.callback()
- self.setup_callback()
+ return self.setup_callback()
def test_redirect_should_redirect_a_user(self,):
response = self.redirect()
@@ -152,7 +152,7 @@ def test_connected_user_should_be_logged_in(self):
self.create_profile(user)
self.flow()
-
+
self.assertEqual(1, self.client.session['_auth_user_id'])
def test_logged_in_user_should_be_connected(self):
@@ -165,6 +165,23 @@ def test_logged_in_user_should_be_connected(self):
self.assertEqual(1, self.profile.objects.filter(user=user).count())
+ def test_only_one_user_can_connect_with_a_provider(self):
+ user = self.create_user()
+ self.create_profile(user)
+
+ other = User.objects.create(username='other')
+ other.is_active = True
+ other.set_password('test')
+ other.save()
+
+ self.client.login(username='other', password='test')
+
+ response = self.flow()
+
+ self.assertEqual(200, response.status_code, response.content)
+ self.assertContains(response, 'This profile is already connected to another user account')
+
+
def test_logging_in_should_send_the_login_signal(self):
counter = self.get_counter()
@@ -196,7 +213,6 @@ def handler(sender, **kwargs):
self.flow()
self.assertEqual(1, counter.counter)
-
def test_setup_callback_should_indicate_an_inactive_user(self):
user = self.create_user(is_active=False)
@@ -2,8 +2,8 @@
from django.contrib.auth import logout
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
-from django.views.generic.base import View
from django.utils.translation import ugettext_lazy as _
+from django.views.generic.base import View, TemplateView
from socialregistration.clients.oauth import OAuthError
from socialregistration.mixins import SocialRegistration
@@ -216,10 +216,11 @@ def get(self, request):
except OAuthError, error:
return self.render_to_response({'error': error})
-class SetupCallback(SocialRegistration, View):
+class SetupCallback(SocialRegistration, TemplateView):
"""
Base class for OAuth and OAuth2 login / connects / registration.
"""
+ template_name = 'socialregistration/setup.error.html'
def get(self, request):
"""
@@ -244,20 +245,30 @@ def get(self, request):
# Get the lookup dictionary to find the user's profile
lookup_kwargs = self.get_lookup_kwargs(request, client)
- # Logged in user connecting an account
+ # Logged in user (re-)connecting an account
if request.user.is_authenticated():
- profile, created = self.get_or_create_profile(request.user,
- save=True, **lookup_kwargs)
+ try:
+ profile = self.get_profile(**lookup_kwargs)
+
+ # Make sure that there is only *one* account per profile.
+ if not profile.user == request.user:
+ self.delete_session_data(request)
+ return self.render_to_response({
+ 'error': _('This profile is already connected to another user account.')
+ })
+
+ except self.get_model().DoesNotExist:
+ profile, created = self.get_or_create_profile(request.user,
+ save=True, **lookup_kwargs)
- # Profile existed - but got reconnected. Send the signal and
- # send the 'em where they were about to go in the first place.
self.send_connect_signal(request, request.user, profile, client)
return self.redirect(request)
# Logged out user - let's see if we've got the identity saved already.
# If so - just log the user in. If not, create profile and redirect
# to the setup view
+
user = self.authenticate(**lookup_kwargs)
# No user existing - create a new one and redirect to the final setup view

0 comments on commit c773adf

Please sign in to comment.