def signup(): # duplicete check user_name and user_mail user_service = UserService() error = user_service.register(json.loads(request.data)) if error: return jsonify(ok=True) # alredy exist same user name or email return ""
def signup(): signup_form = SignupForm() if request.method == "POST" and signup_form.validate_on_submit(): # duplicete check user_name and user_mail user_service = UserService() error = user_service.register(request.form) if error == True: return redirect(url_for("user_page.login")) # alredy exist same user name ot email flash(f"{error} is already existing") return render_template("user/signup.html", title="Sign Up", form=signup_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)