def register(): if current_user.is_authenticated: return redirect("/") register_form = af.RegisterForm() # Check that form is valid. if register_form.validate_on_submit(): # Generate only once the list of users userList = users.keys() # Get Username given in form username = register_form.username.data # Look for the user in the existing users. user_found = au.find_user(userList, username) if user_found: flash("Username already used", category="danger") return redirect("/register") au.add_user(users, username, register_form.first_name.data, register_form.last_name.data, register_form.dob.data, register_form.password.data) flash("Account successfully created", category="success") return redirect("/login") else: return render_template("register.html", form=register_form)
def test_get_users(test_app, test_database): utils.recreate_db() utils.add_user( {"username": "******", "email": "*****@*****.**"} ) utils.add_user( {"username": "******", "email": "*****@*****.**"} ) client = test_app.test_client() response = client.get("/users") assert response.status_code == 200 data = json.loads(response.data.decode()) assert "success" in data["status"] assert len(data["data"]["users"]) == 2 assert "mayankdcoder" in data["data"]["users"][0]["username"] assert "mayankdcoder1" in data["data"]["users"][1]["username"] assert "*****@*****.**" in data["data"]["users"][0]["email"] assert "*****@*****.**" in data["data"]["users"][1]["email"]
def test_get_user(test_app, test_database): public_id = utils.add_user( {"username": "******", "email": "*****@*****.**"} ) client = test_app.test_client() response = client.get(f"/users/{public_id}") assert response.status_code == 200 data = json.loads(response.data.decode()) assert "success" in data["status"] assert "onlinejudge95" in data["data"]["username"], data["data"] assert "*****@*****.**" in data["data"]["email"]
def test_update_user_empty_json(test_app, test_database): utils.recreate_db() public_id = utils.add_user( {"username": "******", "email": "*****@*****.**"} ) client = test_app.test_client() resp = client.put( f"/users/{public_id}", data=json.dumps({}), content_type="application/json", ) data = json.loads(resp.data.decode()) assert resp.status_code == 400 assert "Empty payload" in data["message"] assert "fail" in data["status"]
def test_update_user_wrong_permission(test_app, test_database): utils.recreate_db() public_id = utils.add_user( {"username": "******", "email": "*****@*****.**"} ) client = test_app.test_client() resp = client.put( f"/users/{public_id}", data=json.dumps({"public_id": "123"}), content_type="application/json", ) data = json.loads(resp.data.decode()) assert resp.status_code == 403 assert "Can not modify public_id attribute" in data["message"] assert "fail" in data["status"]
def register(): err_msg = "" if request.method == 'POST': password = request.form.get('password') confirm = request.form.get('confirm') if password == confirm: name = request.form.get('name') username = request.form.get('username') if utils.add_user(name=name, username=username, password=password): return redirect('/login') else: err_msg = "Hệ thống đang có lỗi! Vui lòng quay lại sau!" else: err_msg = "Mật khẩu KHÔNG khớp!" return render_template('register.html', err_msg=err_msg)
def login(): if request.method == 'GET': return render_template('login.html') if request.method == 'POST': check_username = request.form['username'] check_passhash = get_hash(request.form['password']) if auth_user(check_username, check_passhash) is True: browser_session['username'] = check_username flash("Successfully logged in, " + browser_session['username']) else: if add_user(check_username, check_passhash): flash("Your account does not exist, creating one now!") browser_session['username'] = check_username else: flash("Wrong credentials") return redirect('/', code=302)
def test_remove_user(test_app, test_database): utils.recreate_db() public_id = utils.add_user( {"username": "******", "email": "*****@*****.**"} ) client = test_app.test_client() resp_one = client.get("/users") data = json.loads(resp_one.data.decode()) assert resp_one.status_code == 200 assert len(data["data"]["users"]) == 1 resp_two = client.delete(f"/users/{public_id}") data = json.loads(resp_two.data.decode()) assert resp_two.status_code == 200 assert "[email protected] was removed!" in data["message"] assert "success" in data["status"] resp_three = client.get("/users") data = json.loads(resp_three.data.decode()) assert resp_three.status_code == 200 assert len(data["data"]["users"]) == 0
def test_update_user(test_app, test_database): utils.recreate_db() public_id = utils.add_user( {"username": "******", "email": "*****@*****.**"} ) client = test_app.test_client() resp_one = client.put( f"/users/{public_id}", data=json.dumps({"username": "******", "email": "*****@*****.**"}), content_type="application/json", ) data = json.loads(resp_one.data.decode()) assert resp_one.status_code == 200 assert f"{public_id} was updated!" in data["message"] assert "success" in data["status"] resp_two = client.get(f"/users/{public_id}") data = json.loads(resp_two.data.decode()) assert resp_two.status_code == 200 assert "me" in data["data"]["username"], data["data"] assert "*****@*****.**" in data["data"]["email"] assert "success" in data["status"]