From ef1ecc838b68a06ccfa7534afae00de7628858af Mon Sep 17 00:00:00 2001 From: Samuel Hoffman Date: Tue, 28 Jan 2020 18:53:16 -0500 Subject: [PATCH] API for checking if a username is already taken /api/v1/users//exists returns JSON body with boolean 'exists' property --- CodeChallenge/api/users.py | 8 ++++++++ tests/test_auth.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CodeChallenge/api/users.py b/CodeChallenge/api/users.py index 1493f92..f450dfb 100644 --- a/CodeChallenge/api/users.py +++ b/CodeChallenge/api/users.py @@ -209,3 +209,11 @@ def reset_password(): return json_error("password reset failed") return jsonify(status="success", reason="password reset successfully") + + +@bp.route("//exists", methods=["GET"]) +def username_exists(username): + exists = Users.query.filter_by(username=username).first() is not None + return jsonify(status="success", + exists=exists, + username=username) diff --git a/tests/test_auth.py b/tests/test_auth.py index 693fc69..8ec321e 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -97,3 +97,16 @@ def test_forgot_password(client, app): )) assert retval.status_code == 200 + + +def test_user_exists(client): + + rv = client.get("/api/v1/users/cwhqsam/exists") + assert rv.status_code == 200 + assert rv.json["exists"] is True + assert rv.json["username"] == "cwhqsam" + + rv2 = client.get("/api/v1/users/foobar/exists") + assert rv2.status_code == 200 + assert rv2.json["username"] == "foobar" + assert rv2.json["exists"] is False