-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from flask import Blueprint, jsonify, request | ||
from flask import Blueprint, jsonify, request, current_app | ||
from flask_cors import cross_origin | ||
from flask_jwt_extended import ( | ||
create_access_token, | ||
|
@@ -13,6 +13,8 @@ | |
from ..database import User, UserRole, db, Invitation, StagedInvitation | ||
from ..dto import LoginUserDTO, RegisterUserDTO | ||
from ..schemas import UserSchema, validate | ||
from flask_mail import Message | ||
from ..config import TestingConfig | ||
|
||
bp = Blueprint("auth", __name__, url_prefix="/api/v1/auth") | ||
|
||
|
@@ -198,3 +200,47 @@ def reset_password(): | |
user.password = user_manager.hash_password(body.password) | ||
db.session.commit() | ||
return {"message": "Password successfully changed"}, 200 | ||
|
||
|
||
class PhoneDTO(BaseModel): | ||
phoneNumber : str | ||
|
||
|
||
""" | ||
Endpoint to use when user has forgotten their | ||
Username/Email | ||
Username in this case is email of the user | ||
""" | ||
|
||
|
||
@bp.route("/forgotUsername", methods=["POST"]) | ||
@validate(auth=False, json=PhoneDTO) | ||
def send_email(): | ||
body: PhoneDTO = request.context.json | ||
if not body.phoneNumber: | ||
return { | ||
"status": "Error", | ||
"message": "Message request body empty" | ||
}, 400 | ||
user_obj = User.query.filter_by( | ||
phone_number=body.phoneNumber | ||
).first() | ||
mail = current_app.extensions.get('mail') | ||
if not user_obj: | ||
return { | ||
"status" : "Error", | ||
"message" : "No account with the request phone number found" | ||
}, 400 | ||
else: | ||
# change TestingConfig email to Production Config in Prod | ||
msg = Message("Your Username for National Police Data Coalition", | ||
sender=TestingConfig.MAIL_USERNAME, | ||
recipients=['[email protected]']) | ||
msg.body = f"The account email associated with {body.phoneNumber} is \ | ||
{user_obj.email}" | ||
mail.send(msg) | ||
return { | ||
"status": "ok", | ||
"message" : "Email sent to the user notifying them \ | ||
of their username" | ||
} , 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,3 +148,61 @@ def test_reset_password(client, example_user, use_correct_token): | |
|
||
def test_access_token_fixture(access_token): | ||
assert len(access_token) > 0 | ||
|
||
|
||
""" | ||
Forgot username test | ||
""" | ||
|
||
|
||
def test_forgot_username( | ||
client, | ||
db_session | ||
|
||
): | ||
""" | ||
register a new user | ||
use the forgot email/username endpoint to see | ||
if email is sent | ||
""" | ||
|
||
res = client.post( | ||
"api/v1/auth/register", | ||
json={ | ||
"email": "[email protected]", | ||
"password": "examplepassword123", | ||
"phoneNumber": "123456789" | ||
}, | ||
) | ||
res = client.post( | ||
"api/v1/auth/forgotUsername", | ||
json={ | ||
"phoneNumber": "123456789" | ||
}, | ||
) | ||
res.status_code == 200 | ||
|
||
|
||
"""Phone Number doesnot exist in DB | ||
""" | ||
|
||
|
||
def test_forgot_username_no_phone( | ||
client, | ||
): | ||
""" | ||
register a new user | ||
use the forgot email/username endpoint to see | ||
if email is sent | ||
""" | ||
res = client.post( | ||
"api/v1/auth/forgotUsername", | ||
json={ | ||
"phoneNumber": "123456789" | ||
}, | ||
) | ||
assert res.status_code == 400 | ||
user_obj = User.query.filter_by( | ||
phone_number="123456789" | ||
).first() | ||
assert user_obj is None |