mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-16 18:22:17 -05:00
[WIP]
use send_sms task to send sms code. Tests are broken because the template data for the Notify service is being delete after every test. Need a way to seed the data for the test.
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')
|
||||
|
||||
|
||||
@@ -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,10 +214,13 @@ 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):
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_user,
|
||||
mock_encryption,
|
||||
mocker):
|
||||
"""
|
||||
Tests POST endpoint /user/<user_id>/sms-code
|
||||
"""
|
||||
@@ -226,34 +228,56 @@ def test_send_user_sms_code(notify_api,
|
||||
with notify_api.test_client() as client:
|
||||
data = json.dumps({})
|
||||
auth_header = create_authorization_header()
|
||||
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')
|
||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||
([current_app.config['NOTIFY_SERVICE_ID'],
|
||||
"some_uuid",
|
||||
"something_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,
|
||||
sample_user,
|
||||
mock_secret_code,
|
||||
mock_celery_send_sms_code):
|
||||
mocker):
|
||||
"""
|
||||
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:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
|
||||
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'
|
||||
}
|
||||
})
|
||||
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