Example #1
0
 def test_valid_reset_token(self):
     u = User(password='******')
     db.session.add(u)
     db.session.commit()
     token = u.generate_reset_token()
     self.assertTrue(u.reset_password(token, 'dog'))
     self.assertTrue(u.verify_password('dog'))
Example #2
0
 def test_invalid_reset_token(self):
     u1 = User(password='******')
     u2 = User(password='******')
     db.session.add(u1)
     db.session.add(u2)
     db.session.commit()
     token = u1.generate_reset_token()
     self.assertFalse(u2.reset_password(token, 'horse'))
     self.assertTrue(u2.verify_password('dog'))
Example #3
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash(_('Congratulations, you are now a registered user!'))
        return redirect(url_for('login'))
    return render_template('register.html', form=form, title=_('Register'))
Example #4
0
def reset_password(token):
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    user = User.verify_reset_password_token(token)
    if not user:
        return redirect(url_for('index'))
    form = ResetPasswordFrom()
    if form.validate_on_submit():
        user.set_password(form.password.data)
        db.session.commit()
        flash(_('Your password has been reset.'))
        return redirect(url_for('login'))
    return render_template('reset_passsword.html', form=form)
Example #5
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))

    form = RegistrationForm()
    # If form validated create user then flash message
    if form.validate_on_submit():
        # noinspection PyArgumentList
        user = User(username=form.username.data, email=form.email.data,
                    password=bcrypt.generate_password_hash(form.password.data).decode('utf-8'))
        db.session.add(user)
        db.session.commit()
        flash(f'Account created! You can now login.', 'success')
        return redirect(url_for('login'))

    return render_template('register.html', title='Register', form=form)
Example #6
0
    def test_follow(self):
        u1 = User(username='******', email='*****@*****.**')
        u2 = User(username='******', email='*****@*****.**')
        db.session.add(u1)
        db.session.add(u2)
        db.session.commit()
        self.assertEqual(u1.followed.all(), [])
        self.assertEqual(u2.followed.all(), [])

        u1.follow(u2)
        db.session.commit()
        self.assertTrue(u1.is_following(u2))
        self.assertEqual(u1.followed.count(), 1)
        self.assertEqual(u1.followed.first().username, 'susan')
        self.assertEqual(u2.followers.count(), 1)
        self.assertEqual(u2.followers.first().username, 'john')

        u1.unfollow(u2)
        db.session.commit()
        self.assertFalse(u1.is_following(u2))
        self.assertEqual(u1.followed.count(), 0)
        self.assertEqual(u2.followers.count(), 0)
Example #7
0
 def test_password_getter(self):
     u = User(password='******')
     with self.assertRaises(AttributeError):
         u.password
Example #8
0
 def test_password_setter(self):
     u = User(password='******')
     self.assertTrue(u.password_hash is not None)
Example #9
0
def initdb():
    db.create_all()
    db.session.add(User(username='******', email='*****@*****.**', password='******'))
    db.session.commit()
    print('Initialized the database')
Example #10
0
 def test_roles_and_permissions(self):
     Role.insert_roles()
     u = User(email='*****@*****.**', password='******')
     self.assertTrue(u.can(Permission.WRITE_ARTICLES))
     self.assertFalse(u.can(Permission.MODERATE_COMMENTS))
Example #11
0
 def test_password_hashing(self):
     u = User(username='******')
     u.set_password('cat')
     self.assertFalse(u.check_password('dog'))
     self.assertTrue(u.check_password('cat'))
Example #12
0
    def create(self, data) -> User:
        new_user = User(**data)
        self.session.add(new_user)
        self.session.commit()

        return new_user
Example #13
0
    def test_followed_posts(self):
        u1 = User(username='******', email='*****@*****.**')
        u2 = User(username='******', email='*****@*****.**')
        u3 = User(username='******', email='*****@*****.**')
        u4 = User(username='******', email='*****@*****.**')
        db.session.add_all([u1, u2, u3, u4])

        now = datetime.utcnow()
        p1 = Post(body='post from john',
                  author=u1,
                  timestamp=now + timedelta(seconds=1))
        p2 = Post(body='post from susan',
                  author=u2,
                  timestamp=now + timedelta(seconds=4))
        p3 = Post(body='post from ayo',
                  author=u3,
                  timestamp=now + timedelta(seconds=3))
        p4 = Post(body='post from ore',
                  author=u4,
                  timestamp=now + timedelta(seconds=2))
        db.session.add_all([p1, p2, p3, p4])
        db.session.commit()

        u1.follow(u2)
        u1.follow(u4)
        u2.follow(u3)
        u3.follow(u4)
        db.session.commit()

        f1 = u1.followed_posts().all()
        f2 = u2.followed_posts().all()
        f3 = u3.followed_posts().all()
        f4 = u4.followed_posts().all()
        self.assertEqual(f1, [p2, p4, p1])
        self.assertEqual(f2, [p2, p3])
        self.assertEqual(f3, [p3, p4])
        self.assertEqual(f4, [p4])
Example #14
0
 def test_avatar(self):
     u = User(username='******', email='*****@*****.**')
     self.assertEqual(u.avatar(128), ('https://www.gravatar.com/avatar/'
                                      'd4c74594d841139328695756648b6bd6'
                                      '?d=identicon&s=128'))
Example #15
0
 def test_pasword_verification(self):
     u = User(password='******')
     self.assertTrue(u.verify_password('cat'))
     self.assertFalse(u.verify_password('dog'))
Example #16
0
 def test_password_salts_are_random(self):
     u = User(password='******')
     u2 = User(password='******')
     self.assertTrue(u.password_hash != u2.password_hash)
Example #17
0
 def test_password_verification(self):
     user = User(password="******")
     self.assertTrue(user.verify_password("cat"))
     self.assertFalse(user.verify_password("dog"))