mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Celery task to send the sms verify code.
Each celery task will use it's own queue.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
import pytest
|
||||
from app.celery.tasks import send_sms
|
||||
from app import twilio_client
|
||||
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.dao import notifications_dao
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
@@ -74,3 +74,24 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
|
||||
with pytest.raises(NoResultFound) as e:
|
||||
notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||
assert 'No row was found for one' in str(e.value)
|
||||
|
||||
|
||||
def test_should_send_sms_code(mocker):
|
||||
notification = {'to': '+441234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
|
||||
mocker.patch('app.twilio_client.send_sms')
|
||||
send_sms_code(encrypted_notification)
|
||||
twilio_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'])
|
||||
|
||||
|
||||
def test_should_throw_twilio_exception(mocker):
|
||||
notification = {'to': '+441234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
mocker.patch('app.twilio_client.send_sms', side_effect=TwilioClientException)
|
||||
send_sms_code(encrypted_notification)
|
||||
twilio_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'])
|
||||
|
||||
@@ -202,3 +202,8 @@ def sample_notification(notify_db,
|
||||
notification = Notification(**data)
|
||||
save_notification(notification)
|
||||
return notification
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_celery_send_sms_code(mocker):
|
||||
return mocker.patch('app.celery.tasks.send_sms_code.apply_async')
|
||||
|
||||
@@ -4,7 +4,9 @@ from datetime import (datetime, timedelta)
|
||||
from flask import url_for
|
||||
|
||||
from app.models import (VerifyCode)
|
||||
from app import db
|
||||
|
||||
import app.celery.tasks
|
||||
from app import db, encryption
|
||||
from tests import create_authorization_header
|
||||
|
||||
|
||||
@@ -247,13 +249,10 @@ def test_user_verify_password_missing_password(notify_api,
|
||||
assert 'Required field missing data' in json_resp['message']['password']
|
||||
|
||||
|
||||
@moto.mock_sqs
|
||||
def test_send_user_code_for_sms(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_sms_code,
|
||||
sqs_client_conn,
|
||||
mock_secret_code):
|
||||
mock_secret_code,
|
||||
mock_celery_send_sms_code):
|
||||
"""
|
||||
Tests POST endpoint '/<user_id>/code' successful sms
|
||||
"""
|
||||
@@ -270,20 +269,20 @@ def test_send_user_code_for_sms(notify_api,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert resp.status_code == 204
|
||||
encrpyted = encryption.encrypt({'to': sample_sms_code.user.mobile_number, 'secret_code': '11111'})
|
||||
app.celery.tasks.send_sms_code.apply_async.assert_called_once_with([encrpyted], queue='sms_code')
|
||||
|
||||
|
||||
@moto.mock_sqs
|
||||
def test_send_user_code_for_sms_with_optional_to_field(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_sms_code,
|
||||
sqs_client_conn,
|
||||
mock_secret_code):
|
||||
mock_secret_code,
|
||||
mock_celery_send_sms_code):
|
||||
"""
|
||||
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:
|
||||
|
||||
data = json.dumps({'code_type': 'sms', 'to': '+441119876757'})
|
||||
auth_header = create_authorization_header(
|
||||
path=url_for('user.send_user_code', user_id=sample_sms_code.user.id),
|
||||
@@ -295,6 +294,8 @@ 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')
|
||||
|
||||
|
||||
@moto.mock_sqs
|
||||
|
||||
Reference in New Issue
Block a user