def test_cannot_create_two_users_with_same_email(self):
        email = '*****@*****.**'
        user = User(email=email)
        self.user_repo.create_unconfirmed_user(user)

        user2 = User(email=email)
        with self.assertRaises(AlreadyExistsException):
            self.user_repo.create_unconfirmed_user(user2)
Exemple #2
0
 def create_ready_to_use_user(self):
     email = '%s@%s.com' % (uuid.uuid4(), TestCase.get_and_increment_random_index())
     password = str(uuid.uuid4())
     new_user = User(email=email)
     self.user_repo.create_unconfirmed_user(new_user)
     self.user_repo.confirm_user(email=new_user.email, confirmation_code=new_user.confirmation_code)
     self.user_repo.set_password(email=new_user.email, password=password, confirmation_code=new_user.confirmation_code)
     return TestUser(email, password)
 def test_cannot_log_in_if_password_not_set(self):
     email = '*****@*****.**'
     password = '******'
     new_user = User(email=email)
     self.user_repo.create_unconfirmed_user(new_user)
     self.user_repo.confirm_user(
         email=email, confirmation_code=new_user.confirmation_code)
     with self.assertRaises(BadLoginException):
         self.user_repo.authenticate(email=email, password='')
    def test_creates_unconfirmed_user(self):
        email = '*****@*****.**'
        new_user = User(email=email)
        self.user_repo.create_unconfirmed_user(new_user)

        with self.assertRaises(MissingValueException):
            self.user_repo.get_user(email)
        returned_user = self.user_repo.get_unconfirmed_user(email)
        self.assertEquals(returned_user.email, email)
        self.assertEquals(returned_user.status, UserStatus.UNCONFIRMED)
    def test_cannot_set_password_without_confirmation_code_if_unconfirmed(
            self):
        email = '*****@*****.**'
        password = '******'
        new_user = User(email=email)
        self.user_repo.create_unconfirmed_user(new_user)

        with self.assertRaises(IllegalArgumentException):
            self.user_repo.set_password(email=new_user.email,
                                        password=password)
Exemple #6
0
    def create_unconfirmed_user(self, user, send_email=False):
        # Creates a user whose account cannot be used without confirmation

        if User.all().filter('email =', user.email).count(limit=1) > 0:
            raise AlreadyExistsException()

        user.status = UserStatus.UNCONFIRMED
        user.confirmation_code = self._generate_confirmation_code()
        user.put()
        if send_email:
            self.email_service.send_user_confirmation_mail(user)
Exemple #7
0
    def post(self):
        POST = self.request.POST
        email = POST['email']
        user = User(email=email)

        try:
            self.user_repo.create_unconfirmed_user(user=user, send_email=True)
            response = self.redirect(Url().for_page('signedup'))
            return response
        except AlreadyExistsException:
            self.write_template('signup', {'title': 'Givabit - Sign up', 'success': False, 'error': 'User already exists'})
Exemple #8
0
    def create_unconfirmed_user(self, user, send_email=False):
        # Creates a user whose account cannot be used without confirmation

        if User.all().filter('email =', user.email).count(limit=1) > 0:
            raise AlreadyExistsException()

        user.status = UserStatus.UNCONFIRMED
        user.confirmation_code = self._generate_confirmation_code()
        user.put()
        if send_email:
            self.email_service.send_user_confirmation_mail(user)
    def test_can_log_in(self):
        email = '*****@*****.**'
        password = '******'
        new_user = User(email=email)
        self.user_repo.create_unconfirmed_user(new_user)

        self.user_repo.set_password(
            email=new_user.email,
            password=password,
            confirmation_code=new_user.confirmation_code)
        self._assert_can_log_in(new_user, password)
Exemple #10
0
 def _get_user(self, email, filter=None):
     # filter : lambda (query -> query)
     if filter is None:
         filter = lambda f: f
     users = [user for user in filter(User.all().filter('email =', email)).run()]
     count = len(users)
     if count == 1:
         return users[0]
     elif count == 0:
         raise MissingValueException('Could not find user email=%s' % email)
     elif count > 1:
         raise MultipleValueException('Expected one user to be returned, but got multiple', users)
    def test_confirms_user(self):
        email = '*****@*****.**'
        new_user = User(email=email)

        self.user_repo.create_unconfirmed_user(new_user)
        with self.assertRaises(IncorrectConfirmationCodeException):
            self.user_repo.confirm_user(email=email,
                                        confirmation_code='wrong_code')

        confirmation_code = new_user.confirmation_code
        self.user_repo.confirm_user(email=email,
                                    confirmation_code=confirmation_code)
        self.assertEquals(self.user_repo.get_user(email), new_user)
Exemple #12
0
    def test_fails_if_email_already_exists(self):
        email = '*****@*****.**'
        user = User(email=email)
        self.user_repo.create_unconfirmed_user(user)

        request = webapp2.Request.blank(Url().for_page('signup'))
        request.method = 'POST'
        request.POST['email'] = email

        response = webapp2.Response()
        SignupPage(request=request, response=response).post()

        self.assertIn('already exists', response.body)
    def test_cannot_set_password_with_wrong_second_confirmation(self):
        email = '*****@*****.**'
        password = '******'
        new_user = User(email=email)
        self.user_repo.create_unconfirmed_user(new_user)

        self.user_repo.confirm_user(
            email=email, confirmation_code=new_user.confirmation_code)
        self.user_repo.confirm_user(
            email=email, confirmation_code=new_user.confirmation_code)

        with self.assertRaises(IncorrectConfirmationCodeException):
            self.user_repo.set_password(email=new_user.email,
                                        password=password,
                                        confirmation_code='wrong')
Exemple #14
0
 def _get_user(self, email, filter=None):
     # filter : lambda (query -> query)
     if filter is None:
         filter = lambda f: f
     users = [
         user for user in filter(User.all().filter('email =', email)).run()
     ]
     count = len(users)
     if count == 1:
         return users[0]
     elif count == 0:
         raise MissingValueException('Could not find user email=%s' % email)
     elif count > 1:
         raise MultipleValueException(
             'Expected one user to be returned, but got multiple', users)
Exemple #15
0
    def test_user_confirmation_mail_contains_confirmation_code(self):
        confirmation_code = str(uuid.uuid4())
        email = '*****@*****.**' % str(uuid.uuid4())
        user = User(email=email, confirmation_code=confirmation_code)
        EmailService().send_user_confirmation_mail(user)

        messages = self.mail_stub.get_sent_messages()
        self.assertEquals(len(messages), 1)

        message = messages[0]
        self.assertEquals(message.to, email)
        self.assertIn(
            Url().for_page('confirmation',
                           confirmation_code=confirmation_code,
                           email=email), str(message.body))
    def test_can_change_password(self):
        email = '*****@*****.**'
        old_password = '******'
        new_password = '******'
        new_user = User(email=email)
        self.user_repo.create_unconfirmed_user(new_user)

        self.user_repo.set_password(
            email=new_user.email,
            password=old_password,
            confirmation_code=new_user.confirmation_code)
        self.user_repo.set_password(email=new_user.email,
                                    password=new_password,
                                    old_password=old_password)

        with self.assertRaises(BadLoginException):
            self.user_repo.authenticate(email=email, password=old_password)
        self._assert_can_log_in(new_user, new_password)
    def test_cannot_set_password_without_existing_password_if_confirmed(self):
        email = '*****@*****.**'
        old_password = '******'
        new_password = '******'
        new_user = User(email=email)
        self.user_repo.create_unconfirmed_user(new_user)
        confirmation_code = new_user.confirmation_code

        self.user_repo.set_password(
            email=new_user.email,
            password=old_password,
            confirmation_code=new_user.confirmation_code)
        with self.assertRaises(IllegalArgumentException):
            self.user_repo.set_password(
                email=new_user.email,
                password=new_password,
                confirmation_code=new_user.confirmation_code)
        with self.assertRaises(IllegalArgumentException):
            self.user_repo.set_password(email=new_user.email,
                                        password=new_password)
        self._assert_can_log_in(new_user, old_password)
 def test_cannot_log_in_if_unconfirmed(self):
     email = '*****@*****.**'
     new_user = User(email=email)
     self.user_repo.create_unconfirmed_user(new_user)
     with self.assertRaises(BadLoginException):
         self.user_repo.authenticate(email=email, password='')
Exemple #19
0
from givabit.backend.charity import Charity
from givabit.backend.user import User

c1 = Charity(name='Shelter')
c2 = Charity(name='Oxfam')
c3 = Charity(name='BHF')
c4 = Charity(name='BHF')

u1 = User(email='*****@*****.**')
u2 = User(email='*****@*****.**')
u3 = User(email='*****@*****.**')
u4 = User(email='*****@*****.**')
u5 = User(email='*****@*****.**')