示例#1
0
    def test_fail_to_create_user_using_existing_username(self):
        with self.app.app_context():
            User.create("user1", "password")

            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                self.fail("failed to commit transaction")

            User.create("user1", "password")

            self.assertRaises(IntegrityError, db.session.commit)
示例#2
0
    def test_change_user_password(self):
        with self.app.app_context():
            user = User.get_by_username("user1")

            user.change_password("password1")

            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                self.fail("failed to commit transaction")

            user = User.get_by_username("user1")

            self.assertTrue(check_password_hash(user.password, "password1"))
示例#3
0
    def test_authenticate_using_jti(self):
        with self.app.app_context():
            user = User.authenticate_using_jti(self.test_user_id,
                                               self.test_user_jti)

            self.assertIsNotNone(user)
            self.assertEqual(user.id, self.test_user_id)
            self.assertEqual(user.jti, self.test_user_jti)
示例#4
0
    def test_get_user_by_username(self):
        with self.app.app_context():
            user = User.get_by_username("user1")

            self.assertIsNotNone(user)
            self.assertIsNotNone(user.id)
            self.assertEqual(user.username, "user1")
            self.assertTrue(check_password_hash(user.password, "password"))
示例#5
0
    def run(self, username, password):
        user = User.get_by_username(username)

        if user is None:
            print("Failed to change user pasword")
            return

        user.change_password(password)

        db.session.commit()
    def test_payload_handler(self):
        user = User(id=1, username="******", jti="abcde")

        with self.app.app_context():
            payload = payload_handler(user)

            self.assertIn("identity", payload)
            self.assertIn("jti", payload)

            self.assertEqual(payload["identity"], 1)
            self.assertEqual(payload["jti"], "abcde")
示例#7
0
    def run(self, username, expires_at):
        secret = current_app.config['JWT_SECRET_KEY']
        algorithm = current_app.config['JWT_ALGORITHM']

        user = User.get_by_username(username)
        if user is None:
            print("Unknown user {}".format(username))
            return

        token = create_token(user.id, user.jti, secret, algorithm, expires_at)

        print(token)
示例#8
0
    def test_delete_user(self):
        with self.app.app_context():
            user = User.delete("user1")

            self.assertIsNotNone(user)
            self.assertIsNotNone(user.id)
            self.assertEqual(user.username, "user1")

            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                self.fail("failed to commit transaction")

            self.assertEqual(db.session.query(User).count(), 0)
示例#9
0
    def setUp(self):
        super(PagetagsTestsWithUser, self).setUp()

        with self.app.app_context():
            user = User.create("user1", "password")

            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                self.fail("failed to load mock data")

            self.test_user_id = user.id
            self.test_user_username = user.username
            self.test_user_jti = user.jti
            self.test_user_password = "******"
示例#10
0
    def test_create_user(self):
        with self.app.app_context():
            user = User.create("user1", "password")

            self.assertIsNone(user.id)
            self.assertEqual(user.username, "user1")
            self.assertTrue(check_password_hash(user.password, "password"))
            self.assertIsNotNone(user.jti)

            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                self.fail("failed to commit transaction")

            self.assertIsNotNone(user.id)
示例#11
0
    def test_fail_to_get_user_that_doesnt_exist(self):
        with self.app.app_context():
            user = User.get_by_username("user2")

            self.assertIsNone(user)
示例#12
0
    def run(self, username):
        User.delete(username)

        db.session.commit()
示例#13
0
    def run(self, username, password):
        User.create(username, password)

        db.session.commit()
示例#14
0
def identity(payload):
    user_id = payload["identity"]
    jti = payload["jti"]

    return User.authenticate_using_jti(user_id, jti)
示例#15
0
def authenticate(username, password):
    return User.authenticate(username, password)
示例#16
0
 def test_authenticate(self):
     with self.app.app_context():
         self.assertTrue(User.authenticate("user1", "password"))
示例#17
0
 def test_fail_to_authenticate_with_invalid_password(self):
     with self.app.app_context():
         self.assertFalse(User.authenticate("user1", "password1"))
示例#18
0
 def test_fail_to_authenticate_unknown_user(self):
     with self.app.app_context():
         self.assertFalse(User.authenticate("user2", "password"))
示例#19
0
    def test_fail_to_authenticate_using_invalid_user_id(self):
        with self.app.app_context():
            user = User.authenticate_using_jti(1234, self.test_user_jti)

            self.assertIsNone(user)