mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-31 21:01:39 -05:00
Merge pull request #383 from alphagov/use-notify-to-send-sms-codes
Use notify to send sms codes
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from flask import (jsonify, request, abort, Blueprint, current_app)
|
||||
from app import encryption
|
||||
|
||||
from app import encryption, DATETIME_FORMAT
|
||||
from app.dao.users_dao import (
|
||||
get_model_users,
|
||||
save_model_user,
|
||||
@@ -11,12 +11,12 @@ from app.dao.users_dao import (
|
||||
use_user_code,
|
||||
increment_failed_login_count,
|
||||
reset_failed_login_count,
|
||||
get_user_by_email
|
||||
get_user_by_email,
|
||||
create_secret_code
|
||||
)
|
||||
|
||||
from app.dao.permissions_dao import permission_dao
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.schemas import (
|
||||
email_data_request_schema,
|
||||
user_schema,
|
||||
@@ -26,7 +26,7 @@ from app.schemas import (
|
||||
)
|
||||
|
||||
from app.celery.tasks import (
|
||||
send_sms_code,
|
||||
send_sms,
|
||||
email_reset_password,
|
||||
email_registration_verification
|
||||
)
|
||||
@@ -123,14 +123,26 @@ def send_user_sms_code(user_id):
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
from app.dao.users_dao import create_secret_code
|
||||
secret_code = create_secret_code()
|
||||
create_user_code(user_to_send_to, secret_code, 'sms')
|
||||
|
||||
mobile = user_to_send_to.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
|
||||
verification_message = {'to': mobile, 'secret_code': secret_code}
|
||||
sms_code_template_id = current_app.config['SMS_CODE_TEMPLATE_ID']
|
||||
sms_code_template = dao_get_template_by_id(sms_code_template_id)
|
||||
verification_message = encryption.encrypt({
|
||||
'template': sms_code_template_id,
|
||||
'template_version': sms_code_template.version,
|
||||
'to': mobile,
|
||||
'personalisation': {
|
||||
'verify_code': secret_code
|
||||
}
|
||||
|
||||
send_sms_code.apply_async([encryption.encrypt(verification_message)], queue='sms-code')
|
||||
})
|
||||
send_sms.apply_async([current_app.config['NOTIFY_SERVICE_ID'],
|
||||
str(uuid.uuid4()),
|
||||
verification_message,
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
], queue='sms-code')
|
||||
|
||||
return jsonify({}), 204
|
||||
|
||||
@@ -142,7 +154,6 @@ def send_user_email_verification(user_id):
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
from app.dao.users_dao import create_secret_code
|
||||
secret_code = create_secret_code()
|
||||
create_user_code(user_to_send_to, secret_code, 'email')
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import uuid
|
||||
from datetime import (datetime, date)
|
||||
|
||||
import pytest
|
||||
from flask import current_app
|
||||
|
||||
from app import db
|
||||
from app.models import (
|
||||
@@ -307,15 +308,6 @@ def sample_email_job(notify_db,
|
||||
return job
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_secret_code(mocker):
|
||||
def _create():
|
||||
return '11111'
|
||||
|
||||
mock_class = mocker.patch('app.dao.users_dao.create_secret_code', side_effect=_create)
|
||||
return mock_class
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_notification(notify_db,
|
||||
notify_db_session,
|
||||
@@ -563,3 +555,37 @@ def mock_mmg_client(mocker, statsd_client=None):
|
||||
})
|
||||
client.init_app(current_app, statsd_client)
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sms_code_template(notify_db,
|
||||
notify_db_session):
|
||||
user = sample_user(notify_db, notify_db_session)
|
||||
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
||||
if not service:
|
||||
data = {
|
||||
'id': current_app.config['NOTIFY_SERVICE_ID'],
|
||||
'name': 'Notify Service',
|
||||
'message_limit': 1000,
|
||||
'active': True,
|
||||
'restricted': False,
|
||||
'email_from': 'notify.service',
|
||||
'created_by': user
|
||||
}
|
||||
service = Service(**data)
|
||||
db.session.add(service)
|
||||
|
||||
template = Template.query.get(current_app.config['SMS_CODE_TEMPLATE_ID'])
|
||||
if not template:
|
||||
data = {
|
||||
'id': current_app.config['SMS_CODE_TEMPLATE_ID'],
|
||||
'name': 'Sms code template',
|
||||
'template_type': 'sms',
|
||||
'content': '((verify_code))',
|
||||
'service': service,
|
||||
'created_by': user,
|
||||
'archived': False
|
||||
}
|
||||
template = Template(**data)
|
||||
db.session.add(template)
|
||||
return template
|
||||
|
||||
@@ -6,8 +6,7 @@ from datetime import (
|
||||
timedelta
|
||||
)
|
||||
|
||||
from flask import url_for
|
||||
|
||||
from flask import url_for, current_app
|
||||
from app.models import (
|
||||
VerifyCode,
|
||||
User
|
||||
@@ -215,45 +214,81 @@ def test_user_verify_password_missing_password(notify_api,
|
||||
assert 'Required field missing data' in json_resp['message']['password']
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_send_user_sms_code(notify_api,
|
||||
sample_sms_code,
|
||||
mock_celery_send_sms_code,
|
||||
mock_encryption):
|
||||
sample_user,
|
||||
sms_code_template,
|
||||
mocker):
|
||||
"""
|
||||
Tests POST endpoint /user/<user_id>/sms-code
|
||||
"""
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = json.dumps({})
|
||||
auth_header = create_authorization_header()
|
||||
mocked = mocker.patch('app.user.rest.create_secret_code', return_value='11111')
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
|
||||
resp = client.post(
|
||||
url_for('user.send_user_sms_code', user_id=sample_sms_code.user.id),
|
||||
url_for('user.send_user_sms_code', user_id=sample_user.id),
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert resp.status_code == 204
|
||||
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with(['something_encrypted'],
|
||||
queue='sms-code')
|
||||
assert mocked.call_count == 1
|
||||
encrypted = encryption.encrypt({'template': current_app.config['SMS_CODE_TEMPLATE_ID'],
|
||||
'template_version': 1,
|
||||
'to': sample_user.mobile_number,
|
||||
'personalisation': {
|
||||
'verify_code': '11111'
|
||||
}
|
||||
})
|
||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||
([current_app.config['NOTIFY_SERVICE_ID'],
|
||||
"some_uuid",
|
||||
encrypted,
|
||||
"2016-01-01T11:09:00.061258"]),
|
||||
queue="sms-code"
|
||||
)
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
||||
sample_sms_code,
|
||||
mock_secret_code,
|
||||
mock_celery_send_sms_code):
|
||||
sample_user,
|
||||
sms_code_template,
|
||||
mock_encryption,
|
||||
mocker):
|
||||
"""
|
||||
Tests POST endpoint '/<user_id>/code' successful sms with optional to field
|
||||
"""
|
||||
Tests POST endpoint '/<user_id>/code' successful sms with optional to field
|
||||
"""
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.user.rest.create_secret_code', return_value='11111')
|
||||
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
data = json.dumps({'to': '+441119876757'})
|
||||
auth_header = create_authorization_header()
|
||||
resp = client.post(
|
||||
url_for('user.send_user_sms_code', user_id=sample_sms_code.user.id),
|
||||
url_for('user.send_user_sms_code', user_id=sample_user.id),
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert resp.status_code == 204
|
||||
encrypted = encryption.encrypt({'to': '+441119876757', 'secret_code': '11111'})
|
||||
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrypted], queue='sms-code')
|
||||
encrypted = encryption.encrypt({'template': current_app.config['SMS_CODE_TEMPLATE_ID'],
|
||||
'template_version': 1,
|
||||
'to': '+441119876757',
|
||||
'personalisation': {
|
||||
'verify_code': '11111'
|
||||
}
|
||||
})
|
||||
assert mocked.call_count == 1
|
||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||
([current_app.config['NOTIFY_SERVICE_ID'],
|
||||
"some_uuid",
|
||||
encrypted,
|
||||
"2016-01-01T11:09:00.061258"]),
|
||||
queue="sms-code"
|
||||
)
|
||||
|
||||
|
||||
def test_send_sms_code_returns_404_for_bad_input_data(notify_api, notify_db, notify_db_session):
|
||||
|
||||
Reference in New Issue
Block a user