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)
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"))
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)
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"))
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")
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)
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)
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 = "******"
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)
def test_fail_to_get_user_that_doesnt_exist(self): with self.app.app_context(): user = User.get_by_username("user2") self.assertIsNone(user)
def run(self, username): User.delete(username) db.session.commit()
def run(self, username, password): User.create(username, password) db.session.commit()
def identity(payload): user_id = payload["identity"] jti = payload["jti"] return User.authenticate_using_jti(user_id, jti)
def authenticate(username, password): return User.authenticate(username, password)
def test_authenticate(self): with self.app.app_context(): self.assertTrue(User.authenticate("user1", "password"))
def test_fail_to_authenticate_with_invalid_password(self): with self.app.app_context(): self.assertFalse(User.authenticate("user1", "password1"))
def test_fail_to_authenticate_unknown_user(self): with self.app.app_context(): self.assertFalse(User.authenticate("user2", "password"))
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)