From 35619553c8b72875a6d165706cdb1efd03364481 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 7 Jan 2016 17:41:21 +0000 Subject: [PATCH] Added NoDataFoundException --- app/main/dao/users_dao.py | 14 +++++++++----- app/main/exceptions.py | 7 +++++-- tests/app/main/views/test_new_password.py | 17 ++++++++++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/main/dao/users_dao.py b/app/main/dao/users_dao.py index 64e306b79..94510704c 100644 --- a/app/main/dao/users_dao.py +++ b/app/main/dao/users_dao.py @@ -3,6 +3,7 @@ from datetime import datetime from sqlalchemy.orm import load_only from app import db, login_manager +from app.main.exceptions import NoDataFoundException from app.models import User from app.main.encryption import hashpw @@ -61,11 +62,14 @@ def update_mobile_number(id, mobile_number): def update_password(email, password): user = get_user_by_email(email) - user.password = hashpw(password) - user.password_changed_at = datetime.now() - db.session.add(user) - db.session.commit() - return user + if user: + user.password = hashpw(password) + user.password_changed_at = datetime.now() + db.session.add(user) + db.session.commit() + return user + else: + raise NoDataFoundException('The user does not exist') def find_all_email_address(): diff --git a/app/main/exceptions.py b/app/main/exceptions.py index 45f0515c0..7b09af0eb 100644 --- a/app/main/exceptions.py +++ b/app/main/exceptions.py @@ -1,5 +1,8 @@ - - class AdminApiClientException(Exception): def __init__(self, message): self.value = message + + +class NoDataFoundException(Exception): + def __init__(self, message): + self.value = message diff --git a/tests/app/main/views/test_new_password.py b/tests/app/main/views/test_new_password.py index aa01d411e..9b645644d 100644 --- a/tests/app/main/views/test_new_password.py +++ b/tests/app/main/views/test_new_password.py @@ -1,5 +1,8 @@ +from pytest import fail + from app.main.dao import users_dao from app.main.encryption import check_hash +from app.main.exceptions import NoDataFoundException from app.main.views import generate_token from tests.app.main import create_test_user @@ -39,11 +42,15 @@ def test_should_redirect_to_forgot_password_with_flash_message_when_token_is_exp notifications_admin.config['TOKEN_MAX_AGE_SECONDS'] = 86400 -def test_should_return_500_error_page_when_email_addres_does_not_exist(notifications_admin, notifications_admin_db, - notify_db_session): +def test_should_return_raise_no_data_found_exception_when_email_address_does_not_exist(notifications_admin, + notifications_admin_db, + notify_db_session): with notifications_admin.test_request_context(): with notifications_admin.test_client() as client: token = generate_token('doesnotexist@it.gov.uk') - response = client.post('/new-password/{}'.format(token), - data={'new_password': 'a-new_password'}) - assert response.status_code == 500 + try: + client.post('/new-password/{}'.format(token), + data={'new_password': 'a-new_password'}) + fail('Expected NoDataFoundException') + except NoDataFoundException: + pass