Ejemplo n.º 1
0
 def test_expired_confirmation_token(self):
     u = User(password='******')
     db.session.add(u)
     db.session.commit()
     token = u.generate_confirmation_token(1)
     time.sleep(2)
     self.assertFalse(u.confirm(token))
Ejemplo n.º 2
0
 def test_invalid_confirmation_token(self):
     u1 = User(password='******')
     u2 = User(password='******')
     db.session.add(u1)
     db.session.add(u2)
     db.session.commit()
     token = u1.generate_confirmation_token()
     self.assertFalse(u2.confirm(token))
Ejemplo n.º 3
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(email=form.email.data, username=form.username.data, password=form.password.data)
        try:
            db.session.add(user)
            db.session.commit()
            token = user.generate_confirmation_token()
            send_email(user.email, 'Confirm Your Account', 'auth/email/confirm', user=user, token=token)
        except Exception as e:
            db.session.rollback()
        flash('A Confirmation email has been sent to you by email.')
        return redirect(url_for('auth.login'))
    return render_template('auth/register.html', form=form)
class UserTest(unittest.TestCase):
    def setUp(self):
        self.app = create_app(config['testDB'])
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self.user = User(username='******',
                         email='*****@*****.**',
                         password='******',
                         image_file='Night view.png')
        db.session.add(self.user)
        db.session.commit()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_model_repr(self):
        self.assertEqual(repr(self.user),
                         "User: Haruka, [email protected], Night view.png")

    def test_token_valid(self):
        token = self.user.generate_confirmation_token()
        self.assertTrue(self.user.verify_token(token))

    def test_token_invalid(self):
        token = self.user.generate_confirmation_token(expires_sec=0.2)
        sleep(1)
        self.assertFalse(self.user.verify_token(token))

    def test_token_difference(self):
        user_test = User(username='******',
                         email='*****@*****.**',
                         password='******',
                         image_file='Day view.jpg')
        db.session.add(user_test)
        db.session.commit()
        token1 = self.user.get_reset_token()
        token2 = user_test.get_reset_token()
        self.assertNotEqual(token1, token2)

    def test_token_from_another_user(self):
        user_test = User(username='******',
                         email='*****@*****.**',
                         password='******',
                         image_file='Day view.jpg')
        db.session.add(user_test)
        db.session.commit()
        token = user_test.get_reset_token()
        self.assertFalse(self.user.verify_token(token))

    def test_follow_unfollow_feature(self):
        user_test = User(username='******',
                         email='*****@*****.**',
                         password='******',
                         image_file='Day view.jpg')
        db.session.add(user_test)
        db.session.commit()

        self.assertFalse(self.user.is_following(user_test))
        self.user.follow(user_test)
        self.assertTrue(self.user.is_following(user_test))
        self.user.unfollow(user_test)
        self.assertFalse(self.user.is_following(user_test))

        user_test.follow(self.user)
        self.assertTrue(self.user.is_followed_by(user_test))
        user_test.unfollow(self.user)
        self.assertFalse(self.user.is_followed_by(user_test))

    @patch('blog.models.User.to_json')
    def test_json(self, mock_obj):
        mock_obj.return_value = {
            'url': '/api/v1/users/',
            'username': '******',
            'posts_url': None
        }
        expected_keys = ['url', 'username', 'posts_url']
        self.assertEqual(sorted(mock_obj.return_value.keys()),
                         sorted(expected_keys))
        self.assertEqual(sorted((self.user.to_json())), sorted(expected_keys))
Ejemplo n.º 5
0
 def test_valid_confirmation_token(self):
     u = User(password='******')
     db.session.add(u)
     db.session.commit()
     token = u.generate_confirmation_token()
     self.assertTrue(u.confirm(token))