mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-19 14:03:52 -04:00
109898688: Implementation of text-not-received and email-not-received
This commit is contained in:
@@ -37,3 +37,17 @@ def activate_user(id):
|
||||
user.state = 'active'
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def update_email_address(id, email_address):
|
||||
user = get_user_by_id(id)
|
||||
user.email_address = email_address
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def update_mobile_number(id, mobile_number):
|
||||
user = get_user_by_id(id)
|
||||
user.mobile_number = mobile_number
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
@@ -20,8 +20,8 @@ def get_code(user_id, code_type):
|
||||
return verify_code
|
||||
|
||||
|
||||
def get_code_by_code(user_id, code_type):
|
||||
return VerifyCodes.query.filter_by(user_id=user_id, code_type=code_type).first()
|
||||
def get_code_by_code(user_id, code, code_type):
|
||||
return VerifyCodes.query.filter_by(user_id=user_id, code=code, code_type=code_type).first()
|
||||
|
||||
|
||||
def use_code(id):
|
||||
|
||||
@@ -84,6 +84,21 @@ def validate_code(field, code):
|
||||
return False
|
||||
|
||||
|
||||
class EmailNotReceivedForm(Form):
|
||||
email_address = StringField('Email address', validators=[
|
||||
Length(min=5, max=255),
|
||||
DataRequired(message='Email cannot be empty'),
|
||||
Email(message='Please enter a valid email address'),
|
||||
Regexp(regex=gov_uk_email, message='Please enter a gov.uk email address')
|
||||
])
|
||||
|
||||
|
||||
class TextNotReceivedForm(Form):
|
||||
mobile_number = StringField('Mobile phone number',
|
||||
validators=[DataRequired(message='Please enter your mobile number'),
|
||||
Regexp(regex=mobile_number, message='Please enter a +44 mobile number')])
|
||||
|
||||
|
||||
class AddServiceForm(Form):
|
||||
service_name = StringField(validators=[DataRequired(message='Please enter your service name')])
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ def send_sms_code(user_id, mobile_number):
|
||||
sms_code = create_verify_code()
|
||||
try:
|
||||
verify_codes_dao.add_code(user_id=user_id, code=sms_code, code_type='sms')
|
||||
admin_api_client.send_sms(mobile_number, message=sms_code, token=admin_api_client.auth_token)
|
||||
admin_api_client.send_sms(mobile_number=mobile_number, message=sms_code, token=admin_api_client.auth_token)
|
||||
except:
|
||||
raise AdminApiClientException('Exception when sending sms.')
|
||||
return sms_code
|
||||
|
||||
@@ -1,23 +1,43 @@
|
||||
from flask import render_template
|
||||
from flask import render_template, redirect, jsonify, session
|
||||
|
||||
from app.main import main
|
||||
from app.main.dao import users_dao, verify_codes_dao
|
||||
from app.main.forms import EmailNotReceivedForm, TextNotReceivedForm
|
||||
from app.main.views import send_sms_code, send_email_code
|
||||
|
||||
|
||||
@main.route("/email-not-received", methods=['GET'])
|
||||
def email_not_received():
|
||||
return render_template('views/email-not-received.html')
|
||||
user = users_dao.get_user_by_id(session['user_id'])
|
||||
return render_template('views/email-not-received.html',
|
||||
form=EmailNotReceivedForm(email_address=user.email_address))
|
||||
|
||||
|
||||
@main.route('/email-not-received', methods=['POST'])
|
||||
def check_and_resend_email_code():
|
||||
return None
|
||||
form = EmailNotReceivedForm()
|
||||
if form.validate_on_submit():
|
||||
user = users_dao.get_user_by_id(session['user_id'])
|
||||
users_dao.update_email_address(id=user.id, email_address=form.email_address.data)
|
||||
verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='email')
|
||||
send_email_code(user_id=user.id, email=user.email_address)
|
||||
return redirect('/verify')
|
||||
return jsonify(form.errors), 400
|
||||
|
||||
|
||||
@main.route("/text-not-received", methods=['GET'])
|
||||
def text_not_received():
|
||||
return render_template('views/text-not-received.html')
|
||||
user = users_dao.get_user_by_id(session['user_id'])
|
||||
return render_template('views/text-not-received.html', form=TextNotReceivedForm(mobile_number=user.mobile_number))
|
||||
|
||||
|
||||
@main.route('/text-not-received', methods=['POST'])
|
||||
def check_and_resend_text_code():
|
||||
return None
|
||||
form = TextNotReceivedForm()
|
||||
if form.validate_on_submit():
|
||||
user = users_dao.get_user_by_id(session['user_id'])
|
||||
users_dao.update_mobile_number(id=user.id, mobile_number=form.mobile_number.data)
|
||||
verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='sms')
|
||||
send_sms_code(user_id=user.id, mobile_number=user.mobile_number)
|
||||
return redirect('/verify')
|
||||
return jsonify(form.errors), 400
|
||||
|
||||
@@ -13,9 +13,13 @@ GOV.UK Notify
|
||||
<p>Check your email address is correct and then resend the confirmation code.</p>
|
||||
|
||||
<p>
|
||||
<label class="form-label" for="email">Email address</label>
|
||||
<input class="form-control-2-3" id="email" type="text" value="wrongemail@wrong.com"><br>
|
||||
<span class="font-xsmall">Your email address must end in .gov.uk</span>
|
||||
<form autocomplete="off" action="" method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
<label class="form-label">Email address</label>
|
||||
|
||||
{{ form.email_address(class="form-control-2-3", autocomplete="off") }} <br>
|
||||
<span class="font-xsmall">Your email address must end in .gov.uk</span>
|
||||
</form>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
@@ -12,15 +12,15 @@ GOV.UK Notify
|
||||
|
||||
<p>Check your mobile phone number is correct and then resend the confirmation code.</p>
|
||||
|
||||
<form autocomplete="off" action="" method="post">
|
||||
<p>
|
||||
<label class="form-label" for="mobile">Mobile phone number</label>
|
||||
<input class="form-control-1-4" id="mobile" type="text" value="08983336666">
|
||||
</p>
|
||||
|
||||
|
||||
<p>
|
||||
<a class="button" href="verify" role="button">Resend confirmation code</a>
|
||||
</p>
|
||||
<label class="form-label">Mobile phone number</label>
|
||||
{{ form.mobile_number(class="form-control-1-4", autocomplete="off") }} <br>
|
||||
</p>
|
||||
<p>
|
||||
<button class="button" href="verify" role="button">Resend confirmation code</button>
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -144,3 +144,20 @@ def test_should_throws_error_when_id_does_not_exist(notifications_admin, notific
|
||||
with pytest.raises(AttributeError) as error:
|
||||
users_dao.activate_user(123)
|
||||
assert '''object has no attribute 'state''''' in str(error.value)
|
||||
|
||||
|
||||
def test_should_update_email_address(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
user = User(name='Update Email',
|
||||
password='somepassword',
|
||||
email_address='test@it.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='inactive')
|
||||
users_dao.insert_user(user)
|
||||
|
||||
saved = users_dao.get_user_by_id(user.id)
|
||||
assert saved.email_address == 'test@it.gov.uk'
|
||||
users_dao.update_email_address(user.id, 'new_email@testit.gov.uk')
|
||||
updated = users_dao.get_user_by_id(user.id)
|
||||
assert updated.email_address == 'new_email@testit.gov.uk'
|
||||
|
||||
@@ -1,18 +1,107 @@
|
||||
def test_should_render_email_code_not_received_template(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/email-not-received')
|
||||
assert response.status_code == 200
|
||||
assert 'Check your email address is correct and then resend the confirmation code' \
|
||||
in response.get_data(as_text=True)
|
||||
from app import admin_api_client
|
||||
from app.main.dao import verify_codes_dao, users_dao
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
# def test_should_check_and_resend_email_code(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
# response = notifications_admin.test_client().post('/email-not-received',
|
||||
# data={'email_adddress': 'test@user.gov.uk'})
|
||||
# assert response is None
|
||||
def test_should_render_email_code_not_received_template_and_populate_email_address(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
response = client.get('/email-not-received')
|
||||
assert response.status_code == 200
|
||||
assert 'Check your email address is correct and then resend the confirmation code' \
|
||||
in response.get_data(as_text=True)
|
||||
assert 'value="test@user.gov.uk"' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_render_text_code_not_received_template(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/text-not-received')
|
||||
assert response.status_code == 200
|
||||
assert 'Check your mobile phone number is correct and then resend the confirmation code.' \
|
||||
in response.get_data(as_text=True)
|
||||
def test_should_check_and_resend_email_code_redirect_to_verify(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user.id, code='12345', code_type='email')
|
||||
response = client.post('/email-not-received',
|
||||
data={'email_address': 'test@user.gov.uk'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/verify'
|
||||
|
||||
|
||||
def test_should_render_text_code_not_received_template(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user.id, code='12345', code_type='email')
|
||||
response = client.get('/text-not-received')
|
||||
assert response.status_code == 200
|
||||
assert 'Check your mobile phone number is correct and then resend the confirmation code.' \
|
||||
in response.get_data(as_text=True)
|
||||
assert 'value="+441234123412"'
|
||||
|
||||
|
||||
def test_should_check_and_redirect_to_verify(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user.id, code='12345', code_type='sms')
|
||||
response = client.post('/text-not-received',
|
||||
data={'mobile_number': '+441234123412'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/verify'
|
||||
|
||||
|
||||
def test_should_update_email_address_resend_code(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
|
||||
response = client.post('/email-not-received',
|
||||
data={'email_address': 'new@address.gov.uk'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/verify'
|
||||
updated_user = users_dao.get_user_by_id(user.id)
|
||||
assert updated_user.email_address == 'new@address.gov.uk'
|
||||
|
||||
|
||||
def test_should_update_mobile_number_resend_code(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
response = client.post('/text-not-received',
|
||||
data={'mobile_number': '+443456789012'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/verify'
|
||||
updated_user = users_dao.get_user_by_id(user.id)
|
||||
assert updated_user.mobile_number == '+443456789012'
|
||||
|
||||
|
||||
def _set_up_mocker(mocker):
|
||||
mocker.patch("app.admin_api_client.send_sms")
|
||||
mocker.patch("app.admin_api_client.send_email")
|
||||
|
||||
Reference in New Issue
Block a user