def test_username_uniqueness(self): data = { 'username': '******', 'password': '******', 'email': '*****@*****.**', } user = User(**data) user.save() user2 = User(**data) self.assertExcMsg(Exception, 'The username %s is already in use.' % data.get('username'), user2.save)
def test_email_uniqueness(self): data = { 'username': '******', 'password': '******', 'email': '*****@*****.**', } user = User(**data) user.save() data.update({ 'username': '******', }) user2 = User(**data) self.assertExcMsg(Exception, 'The email address %s is already in use.' % data.get('email'), user2.save)
def do_c(self, request, token=''): if not token: # TODO: show a form where they can paste in their token? raise Http404 token = token.rstrip('/').encode('utf8') try: value = signed.unsign( token, key=(self.confirm_link_secret or settings.SECRET_KEY) + self.confirm_link_salt) except signed.BadSignature: return self.show_message(request, self.invalid_token_message, self.invalid_token_message + ': ' + token) # Only line change compared with django-openid user_id = value user = self.lookup_user_by_id(user_id) if not user: # Maybe the user was deleted? return self.show_error(request, self.r_user_not_found_message) # Check user is NOT active but IS in the correct group if self.user_is_unconfirmed(user): # Confirm them try: user = User.view('%s/users_by_username' % User._meta.app_label, reduce=False, key=user.username, include_docs=True).first() except ResourceNotFound: user = None if user: self.mark_user_confirmed(user) self.log_in_user(request, user) return self.on_registration_complete(request) else: return self.show_error(request, self.c_already_confirmed_message)
def do_c(self, request, token = ''): if not token: # TODO: show a form where they can paste in their token? raise Http404 token = token.rstrip('/').encode('utf8') try: value = signed.unsign(token, key = ( self.confirm_link_secret or settings.SECRET_KEY ) + self.confirm_link_salt) except signed.BadSignature: return self.show_message( request, self.invalid_token_message, self.invalid_token_message + ': ' + token ) # Only line change compared with django-openid user_id = value user = self.lookup_user_by_id(user_id) if not user: # Maybe the user was deleted? return self.show_error(request, self.r_user_not_found_message) # Check user is NOT active but IS in the correct group if self.user_is_unconfirmed(user): # Confirm them try: user = User.view('%s/users_by_username' % User._meta.app_label, reduce=False, key=user.username, include_docs=True).first() except ResourceNotFound: user = None if user: self.mark_user_confirmed(user) self.log_in_user(request, user) return self.on_registration_complete(request) else: return self.show_error(request, self.c_already_confirmed_message)
def lookup_openid(self, request, identity_url): openid = UserOpenidAssociation.view('%s/openid_view' % UserOpenidAssociation._meta.app_label, key=identity_url, include_docs=True).first() if openid: return User.view('%s/users_by_username' % User._meta.app_label, reduce=False, key=openid['user_id'], include_docs=True).all()
def clean_email(self): email = self.cleaned_data.get('email', '') try: email_count = User.view('%s/users_by_email' % User._meta.app_label, key = email).count() except ResourceNotFound: email_count = 0 if self.no_duplicate_emails and email_count > 0: raise forms.ValidationError, self.duplicate_email_error return email
def test_user_change_email(self): data = { 'username': '******', 'password': '******', 'email': '*****@*****.**', } user = User(**data) user.save() user = User.get_user_by_email(data['email']) user.email = '*****@*****.**' user.save()
def lookup_openid(self, request, identity_url): openid = UserOpenidAssociation.view( '%s/openid_view' % UserOpenidAssociation._meta.app_label, key=identity_url, include_docs=True).first() if openid: return User.view('%s/users_by_username' % User._meta.app_label, reduce=False, key=openid['user_id'], include_docs=True).all()
def user_is_unconfirmed(self, user): count = 0 try: count = User.view('%s/users_by_username' % User._meta.app_label, reduce=False, key=user.username, include_docs=True).count() except ResourceNotFound: return False if count: return True return False
def test_user_profile(self): settings.AUTH_PROFILE_MODULE = 'couchauth.UserProfile' data = { 'username': '******', 'password': '******', 'email': '*****@*****.**', } user = User(**data) user.save() profiledata = { 'user_id': user.get_id, 'age': 7, } userprofile = UserProfile(**profiledata) userprofile.save() userprofile = UserProfile.get_userprofile(profiledata['user_id']) self.assertIsNotNone(userprofile) self.assertEqual(user.get_profile().age, profiledata['age'])
def create_user(self, request, data, openid=None): user = User( username=data['username'], first_name=data.get('first_name', ''), last_name=data.get('last_name', ''), email=data.get('email', ''), ) # Set OpenID, if one has been associated if openid: uoa = UserOpenidAssociation(user_id=user.username, openid=openid, created=datetime.now()) uoa.store() # Set password, if one has been specified password = data.get('password') if password: user.set_password(password) else: user.set_unusable_password() user.store() return user
def test_user_authentication(self): authdata = { 'username': '******', 'password': '******', 'email': '*****@*****.**', } data = authdata.copy() data.update({ 'email': '*****@*****.**', }) user = User(**data) user.set_password(data.get('password')) user.save() user = authenticate(username=authdata.get('username'), password=authdata.get('password')) self.assertIsNotNone(user)
def test_user_registration(self): data = { 'username': '******', 'password': '******', 'email': '*****@*****.**', } user = User(**data) user.save() user = User.get_user(data['username']) self.assertIsNotNone(user) self.assertEqual(user.username, data['username']) user = User.get_user_by_email(data['email']) self.assertIsNotNone(user) self.assertEqual(user.username, data['username'])
def suggest_nickname(self, nickname): "Return a suggested nickname that has not yet been taken" if not nickname: return '' original_nickname = nickname suffix = None username_exists = True while username_exists: try: username_exists = User.view('%s/users_by_username' % User._meta.app_label, reduce=False, key=nickname, include_docs=True).count() except ResourceNotFound: username_exists = False if not username_exists: break if suffix is None: suffix = 1 else: suffix += 1 nickname = original_nickname + str(suffix) return nickname
def create_user(self, request, data, openid=None): user = User( username = data['username'], first_name = data.get('first_name', ''), last_name = data.get('last_name', ''), email = data.get('email', ''), ) # Set OpenID, if one has been associated if openid: uoa = UserOpenidAssociation(user_id = user.username, openid = openid, created = datetime.now()) uoa.store() # Set password, if one has been specified password = data.get('password') if password: user.set_password(password) else: user.set_unusable_password() user.store() return user
def lookup_user_by_username(self, username): return User.view('%s/users_by_username' % User._meta.app_label, reduce=False, key=username, include_docs=True).first()
def lookup_users_by_email(self, email): return User.view('%s/users_by_email' % User._meta.app_label, key=email, include_docs=True).first()
def save(self): user = User(**self.cleaned_data) return user.store()