def authenticate(self, oauth_token, fb_id, name, user_id=None): """Authenticates user via Facebook API and saves user information. """ try: # Find existing user facebook_user = FacebookProfile.objects.get(pk=fb_id, name=name) user = facebook_user.user # Update user's access token facebook_user.oauth_token = oauth_token facebook_user.save() # Set last visit to now user.userprofile.last_visit_date = datetime.now() user.userprofile.save() except FacebookProfile.DoesNotExist: # If requested user has not yet been registered # Create or get User and add FacebookProfile if user_id == None: user = User.objects.create_user(fb_id) user.save() # Create UserProfile to hold information userprofile = UserProfile(user=user) userprofile.save() else: user = User.objects.get(pk=user_id) facebook_user = FacebookProfile(user=user, pk=fb_id, oauth_token=oauth_token) facebook_user.save() return user
def test_facebook_profile_creation(self, fbuid = '123'): from werkzeug.utils import import_string dudes_profile = _user_creation(self) dudes_facebook = FacebookProfile(fb_uid = fbuid, userprofile = dudes_profile) dudes_facebook.put() auth_model_class = import_string(settings.AUTH_USER_MODEL) user = auth_model_class.get_user_by_fbuid(fbuid) self.assertEquals(dudes_facebook.userprofile.user.key(), user.key())
def test_facebook_first_visit(self): profiles = UserProfile.all().fetch(100) self.assertEquals(len(profiles), 0) fb_profiles = FacebookProfile.all().fetch(100) self.assertEquals(len(fb_profiles), 0) users = DatastoreUser.all().fetch(100) self.assertEquals(len(users), 0) _facebook_login(self) fb_profiles = FacebookProfile.all().fetch(100) self.assertEquals(len(fb_profiles), 1) profiles = UserProfile.all().fetch(100) self.assertEquals(len(profiles), 1) users = DatastoreUser.all().fetch(100) self.assertEquals(len(users), 1)
def test_facebook_return_visit(self): _create_fb_user(FACEBOOK_UID) profiles = UserProfile.all().fetch(100) self.assertEquals(len(profiles), 1) fb_profiles = FacebookProfile.all().fetch(100) self.assertEquals(len(fb_profiles), 1) users = DatastoreUser.all().fetch(100) self.assertEquals(len(users), 1) _facebook_login(self) fb_profiles = FacebookProfile.all().fetch(100) self.assertEquals(len(fb_profiles), 1) profiles = UserProfile.all().fetch(100) self.assertEquals(len(profiles), 1) users = DatastoreUser.all().fetch(100) self.assertEquals(len(users), 1)