mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 13:19:49 -04:00
Fix tests for sending sms codes.
Since the unit tests delete the data in between tests I need to add the template data for the test for send sms code.
This commit is contained in:
@@ -4,6 +4,7 @@ import uuid
|
|||||||
from datetime import (datetime, date)
|
from datetime import (datetime, date)
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from flask import current_app
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.models import (
|
from app.models import (
|
||||||
@@ -307,15 +308,6 @@ def sample_email_job(notify_db,
|
|||||||
return job
|
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')
|
@pytest.fixture(scope='function')
|
||||||
def sample_notification(notify_db,
|
def sample_notification(notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
@@ -563,3 +555,37 @@ def mock_mmg_client(mocker, statsd_client=None):
|
|||||||
})
|
})
|
||||||
client.init_app(current_app, statsd_client)
|
client.init_app(current_app, statsd_client)
|
||||||
return 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
|
||||||
|
|||||||
@@ -216,18 +216,18 @@ def test_user_verify_password_missing_password(notify_api,
|
|||||||
|
|
||||||
@freeze_time("2016-01-01 11:09:00.061258")
|
@freeze_time("2016-01-01 11:09:00.061258")
|
||||||
def test_send_user_sms_code(notify_api,
|
def test_send_user_sms_code(notify_api,
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_user,
|
sample_user,
|
||||||
mock_encryption,
|
sms_code_template,
|
||||||
mocker):
|
mocker):
|
||||||
"""
|
"""
|
||||||
Tests POST endpoint /user/<user_id>/sms-code
|
Tests POST endpoint /user/<user_id>/sms-code
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
with notify_api.test_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
data = json.dumps({})
|
data = json.dumps({})
|
||||||
auth_header = create_authorization_header()
|
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('app.celery.tasks.send_sms.apply_async')
|
||||||
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
|
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
|
||||||
resp = client.post(
|
resp = client.post(
|
||||||
@@ -235,10 +235,18 @@ def test_send_user_sms_code(notify_api,
|
|||||||
data=data,
|
data=data,
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
assert resp.status_code == 204
|
assert resp.status_code == 204
|
||||||
|
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(
|
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||||
([current_app.config['NOTIFY_SERVICE_ID'],
|
([current_app.config['NOTIFY_SERVICE_ID'],
|
||||||
"some_uuid",
|
"some_uuid",
|
||||||
"something_encrypted",
|
encrypted,
|
||||||
"2016-01-01T11:09:00.061258"]),
|
"2016-01-01T11:09:00.061258"]),
|
||||||
queue="sms-code"
|
queue="sms-code"
|
||||||
)
|
)
|
||||||
@@ -247,15 +255,17 @@ def test_send_user_sms_code(notify_api,
|
|||||||
@freeze_time("2016-01-01 11:09:00.061258")
|
@freeze_time("2016-01-01 11:09:00.061258")
|
||||||
def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
||||||
sample_user,
|
sample_user,
|
||||||
mock_secret_code,
|
sms_code_template,
|
||||||
|
mock_encryption,
|
||||||
mocker):
|
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_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
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('uuid.uuid4', return_value='some_uuid') # for the notification id
|
||||||
|
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||||
data = json.dumps({'to': '+441119876757'})
|
data = json.dumps({'to': '+441119876757'})
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
resp = client.post(
|
resp = client.post(
|
||||||
@@ -271,6 +281,7 @@ def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
|||||||
'verify_code': '11111'
|
'verify_code': '11111'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
assert mocked.call_count == 1
|
||||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||||
([current_app.config['NOTIFY_SERVICE_ID'],
|
([current_app.config['NOTIFY_SERVICE_ID'],
|
||||||
"some_uuid",
|
"some_uuid",
|
||||||
|
|||||||
Reference in New Issue
Block a user