def login(): login_form = Login() if request.method == "POST" and login_form.validate_on_submit(): # check if user already created a account user_service = UserService() error = user_service.login(request.form) if error: session["user_id"] = error session["login"] = True flash("Welcome back") return redirect(url_for("user_page.user_portfolio")) # alredy exist same user name ot email flash("User information is not existing Try again or please signup in signup page") return render_template("user/login.html", title="Login", form=login_form)
def login(): # check if user already created a account user_service = UserService() user = user_service.login(json.loads(request.data)) if user: session['user_id'] = user.user_id session['login'] = True print(session) # return user data return jsonify(id=user.user_id, username=user.user_name, mail=user.user_mail, password=user.user_password) # doesn't match post data with user info in db return ""
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)