def edit(id): if session.get("user_id") != int(id): return "" user_service = UserService() response = user_service.update(json.loads(request.data), id) if response: return request.data return ""
def update(): if "login" not in session or session["login"] == False: return redirect(url_for("user_page.login")) update_form = Update() if request.method == "POST" and update_form.validate_on_submit(): user_service = UserService() error = user_service.update(request.form, session["user_id"]) if error: flash("Update was Success") return redirect(url_for("user_page.account_page")) flash(error) return render_template("user/update.html", title="Update", form=update_form)
class TestUserService(unittest.TestCase): def setUp(self): print("setUp test env") # init test env self.app = create_app() db.create_all() self.user_service = UserService() # init user data self.user = User(user_name="Akon", user_mail="*****@*****.**", user_password="******") db.session.add(self.user) db.session.commit() def tearDown(self): print("tearDown") db.session.remove() db.drop_all() def test_user_register(self): # exist data form_data = {"username": "******", "email": "*****@*****.**", } res = self.user_service.register(form_data) self.assertFalse(res) # non exist data form_data = {"username": "******", "email": "*****@*****.**", "password": "******", "confirmPassword": "******"} res = self.user_service.register(form_data) self.assertTrue(res) # invalid username form_data = {"username": "******", "email": "*****@*****.**", "password": "******", "confirmPassword": "******"} res = self.user_service.register(form_data) self.assertFalse(res) def test_user_login(self): # inValid form data form_data = {"username": "******", "email": "*****@*****.**", "password": "******", "confirmPassword": "******"} res = self.user_service.login(form_data) self.assertFalse(res) # non exist user form_data = {"username": "******", "email": "*****@*****.**", "password": "******", "confirmPassword": "******"} res = self.user_service.login(form_data) self.assertFalse(res) # exist data form_data = {"username": "******", "email": "*****@*****.**", "password": "******", "confirmPassword": "******"} res = self.user_service.login(form_data) self.assertIsNotNone(res) def test_user_edit(self): # non exist user form_data = {"username": "******", "email": "*****@*****.**", "password": "******", "confirmPassword": "******"} res = self.user_service.update(form_data=form_data, user_id=1) self.assertFalse(res) # edit user by exist user id form_data = {"username": "******", "email": "*****@*****.**", "password": "******", "confirmPassword": "******"} res = self.user_service.update(form_data=form_data, user_id=1) self.assertTrue(res) def test_user_delete(self): res = self.user_service.delete(user_id=1) self.assertTrue(res) def test_find_user_by_id(self): res = self.user_service.get_user_info_by_user_id(user_id=1) self.assertEqual(self.user, res) def test_find_user_by_email(self): # Exist user res = self.user_service.get_user_info_by_email( email=self.user.user_mail) self.assertEqual(res, self.user) # non exist user res = self.user_service.get_user_info_by_email( email="*****@*****.**") self.assertFalse(res) def test_register_new_password(self): # Valid new password form_data = { "password": "******", "confirmPassword": "******"} res = self.user_service.register_new_password( user=self.user, form_data=form_data) self.assertTrue(res) # inValid new password form_data = { "password": "******", "confirmPassword": "******"} res = self.user_service.register_new_password( user=self.user, form_data=form_data) self.assertFalse(res)