diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 8dc6cae4b..27ef72278 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -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 diff --git a/tests/app/user/test_rest_verify.py b/tests/app/user/test_rest_verify.py index b437aee3c..3ca379171 100644 --- a/tests/app/user/test_rest_verify.py +++ b/tests/app/user/test_rest_verify.py @@ -216,18 +216,18 @@ def test_user_verify_password_missing_password(notify_api, @freeze_time("2016-01-01 11:09:00.061258") def test_send_user_sms_code(notify_api, - notify_db, - notify_db_session, sample_user, - mock_encryption, + sms_code_template, mocker): """ Tests POST endpoint /user//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( @@ -235,10 +235,18 @@ def test_send_user_sms_code(notify_api, data=data, headers=[('Content-Type', 'application/json'), auth_header]) 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( ([current_app.config['NOTIFY_SERVICE_ID'], "some_uuid", - "something_encrypted", + encrypted, "2016-01-01T11:09:00.061258"]), queue="sms-code" ) @@ -247,15 +255,17 @@ def test_send_user_sms_code(notify_api, @freeze_time("2016-01-01 11:09:00.061258") def test_send_user_code_for_sms_with_optional_to_field(notify_api, sample_user, - mock_secret_code, + sms_code_template, + mock_encryption, mocker): """ - Tests POST endpoint '//code' successful sms with optional to field - """ + Tests POST endpoint '//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') + 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( @@ -271,6 +281,7 @@ def test_send_user_code_for_sms_with_optional_to_field(notify_api, '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",