Merge pull request #83 from alphagov/celery-send-email-code

Celery send email code
This commit is contained in:
minglis
2016-02-18 11:03:23 +00:00
13 changed files with 140 additions and 33 deletions

View File

@@ -1,10 +1,7 @@
import uuid
import pytest
from app.celery.tasks import (send_sms, send_sms_code)
from app import twilio_client, encryption
from app.clients.sms.twilio import TwilioClientException
from app.celery.tasks import send_sms
from app import firetext_client
from app.celery.tasks import (send_sms, send_sms_code, send_email_code)
from app import (firetext_client, aws_ses_client, encryption)
from app.clients.sms.firetext import FiretextClientException
from app.dao import notifications_dao
from sqlalchemy.exc import SQLAlchemyError
@@ -98,3 +95,20 @@ def test_should_throw_firetext_client_exception(mocker):
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException)
send_sms_code(encrypted_notification)
firetext_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'])
def test_should_send_email_code(mocker):
verification = {'to_address': 'someone@it.gov.uk',
'from_address': 'no-reply@notify.gov.uk',
'subject': 'Verification code',
'body': 11111}
encrypted_verification = encryption.encrypt(verification)
mocker.patch('app.aws_ses_client.send_email')
send_email_code(encrypted_verification)
aws_ses_client.send_email.assert_called_once_with(verification['from_address'],
verification['to_address'],
verification['subject'],
verification['body'])

View File

@@ -207,3 +207,13 @@ def sample_notification(notify_db,
@pytest.fixture(scope='function')
def mock_celery_send_sms_code(mocker):
return mocker.patch('app.celery.tasks.send_sms_code.apply_async')
@pytest.fixture(scope='function')
def mock_celery_send_email_code(mocker):
return mocker.patch('app.celery.tasks.send_email_code.apply_async')
@pytest.fixture(scope='function')
def mock_encryption(mocker):
return mocker.patch('app.encryption.encrypt', return_value="something_encrypted")

View File

@@ -294,17 +294,15 @@ def test_send_user_code_for_sms_with_optional_to_field(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
encrpyted = encryption.encrypt({'to': '+441119876757', 'secret_code': '11111'})
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrpyted], queue='sms_code')
encrypted = encryption.encrypt({'to': '+441119876757', 'secret_code': '11111'})
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrypted], queue='sms_code')
@moto.mock_sqs
def test_send_user_code_for_email(notify_api,
notify_db,
notify_db_session,
sample_email_code,
sqs_client_conn,
mock_secret_code):
mock_secret_code,
mock_celery_send_email_code,
mock_encryption):
"""
Tests POST endpoint '/<user_id>/code' successful email
"""
@@ -321,14 +319,15 @@ def test_send_user_code_for_email(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
app.celery.tasks.send_email_code.apply_async.assert_called_once_with(['something_encrypted'],
queue='email_code')
@moto.mock_sqs
def test_send_user_code_for_email_uses_optional_to_field(notify_api,
notify_db,
notify_db_session,
sample_email_code,
sqs_client_conn,
mock_secret_code):
mock_secret_code,
mock_celery_send_email_code,
mock_encryption):
"""
Tests POST endpoint '/<user_id>/code' successful email with included in body
"""
@@ -345,6 +344,9 @@ def test_send_user_code_for_email_uses_optional_to_field(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
app.celery.tasks.send_email_code.apply_async.assert_called_once_with(['something_encrypted'],
queue='email_code')
def test_request_verify_code_schema_invalid_code_type(notify_api, notify_db, notify_db_session, sample_user):
from app.schemas import request_verify_code_schema

View File

@@ -59,13 +59,6 @@ def notify_db_session(request):
request.addfinalizer(teardown)
@pytest.fixture(scope='function')
def notify_config(notify_api):
notify_api.config['NOTIFY_API_ENVIRONMENT'] = 'test'
notify_api.config.from_object(configs['test'])
return notify_api.config
@pytest.fixture(scope='function')
def os_environ(request):
env_patch = mock.patch('os.environ', {})