Merge pull request #66 from alphagov/remove_alpha_client_from_api

Remove alpha client from api
This commit is contained in:
Adam Shimali
2016-02-09 16:26:01 +00:00
7 changed files with 14 additions and 181 deletions

View File

@@ -10,12 +10,10 @@ from flask_marshmallow import Marshmallow
from werkzeug.local import LocalProxy
from config import configs
from utils import logging
from notify_client import NotifyAPIClient
db = SQLAlchemy()
ma = Marshmallow()
notify_alpha_client = NotifyAPIClient()
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
@@ -30,7 +28,6 @@ def create_app(config_name, config_overrides=None):
ma.init_app(application)
init_app(application, config_overrides)
logging.init_app(application)
notify_alpha_client.init_app(application)
from app.service.rest import service as service_blueprint
from app.user.rest import user as user_blueprint

View File

@@ -6,7 +6,7 @@ from flask import (
request
)
from app import (notify_alpha_client, api_user)
from app import api_user
from app.aws_sqs import add_notification_to_queue
from app.dao import (templates_dao)
from app.schemas import (
@@ -17,7 +17,8 @@ notifications = Blueprint('notifications', __name__)
@notifications.route('/<notification_id>', methods=['GET'])
def get_notifications(notification_id):
return jsonify(notify_alpha_client.fetch_notification_by_id(notification_id)), 200
# TODO return notification id details
return jsonify({'id': notification_id}), 200
@notifications.route('/sms', methods=['POST'])
@@ -27,14 +28,10 @@ def create_sms_notification():
notification, errors = sms_template_notification_schema.load(resp_json)
if errors:
return jsonify(result="error", message=errors), 400
template_id = notification['template']
# TODO: remove once beta is reading notifications from the queue
message = templates_dao.get_model_templates(template_id).content
add_notification_to_queue(api_user['client'], template_id, 'sms', notification)
# TODO: remove once beta is reading notifications from the queue
return jsonify(notify_alpha_client.send_sms(
mobile_number=notification['to'], message=message)), 200
add_notification_to_queue(api_user['client'], notification['template'], 'sms', notification)
# TODO data to be returned
return jsonify({}), 204
@notifications.route('/email', methods=['POST'])
@@ -43,14 +40,9 @@ def create_email_notification():
notification, errors = email_notification_schema.load(resp_json)
if errors:
return jsonify(result="error", message=errors), 400
# At the moment we haven't hooked up
# template handling for sending email notifications.
add_notification_to_queue(api_user['client'], "admin", 'email', notification)
return jsonify(notify_alpha_client.send_email(
notification['to_address'],
notification['body'],
notification['from_address'],
notification['subject']))
# TODO data to be returned
return jsonify({}), 204
@notifications.route('/sms/service/<service_id>', methods=['POST'])
@@ -74,10 +66,6 @@ def create_sms_for_service(service_id):
message = "Invalid template: id {} for service id: {}".format(template.id, service_id)
return jsonify(result="error", message=message), 400
# Actual client is delivery app, but this is sent on behalf of service
add_notification_to_queue(service_id, template_id, 'sms', notification)
# TODO: remove once beta is reading notifications from the queue
content = template.content
return jsonify(notify_alpha_client.send_sms(
mobile_number=notification['to'], message=content)), 200
# TODO data to be returned
return jsonify({}), 204

View File

@@ -17,7 +17,7 @@ from app.dao.users_dao import (
from app.schemas import (
user_schema, users_schema, service_schema, services_schema,
request_verify_code_schema, user_schema_load_json)
from app import (notify_alpha_client, api_user)
from app import api_user
user = Blueprint('user', __name__)
@@ -133,15 +133,10 @@ def send_user_code(user_id):
from app.dao.users_dao import create_secret_code
secret_code = create_secret_code()
create_user_code(user, secret_code, verify_code.get('code_type'))
# TODO this will need to fixed up when we stop using
# notify_alpha_client
if verify_code.get('code_type') == 'sms':
mobile = user.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
notification = {'to': mobile, 'content': secret_code}
add_notification_to_queue(api_user['client'], 'admin', 'sms', notification)
notify_alpha_client.send_sms(
mobile_number=mobile,
message=secret_code)
elif verify_code.get('code_type') == 'email':
email = user.email_address if verify_code.get('to', None) is None else verify_code.get('to')
notification = {
@@ -150,11 +145,6 @@ def send_user_code(user_id):
'subject': 'Verification code',
'body': secret_code}
add_notification_to_queue(api_user['client'], 'admin', 'email', notification)
notify_alpha_client.send_email(
email,
secret_code,
notification['from_address'],
notification['subject'])
else:
abort(500)
return jsonify({}), 204

View File

@@ -17,5 +17,3 @@ boto3==1.2.3
git+https://github.com/alphagov/notifications-python-client.git@0.2.1#egg=notifications-python-client==0.2.1
git+https://github.com/alphagov/notifications-utils.git@0.0.3#egg=notifications-utils==0.0.3
git+https://github.com/alphagov/notify-api-client.git@0.1.6#egg=notify-api-client==0.1.6

View File

@@ -156,24 +156,6 @@ def sample_admin_service_id(notify_db, notify_db_session):
return admin_service.id
@pytest.fixture(scope='function')
def mock_notify_client_send_sms(mocker):
def _send(mobile_number, message):
pass
mock_class = mocker.patch('app.notify_alpha_client.send_sms', side_effect=_send)
return mock_class
@pytest.fixture(scope='function')
def mock_notify_client_send_email(mocker):
def _send(email_address, message, from_address, subject):
pass
mock_class = mocker.patch('app.notify_alpha_client.send_email', side_effect=_send)
return mock_class
@pytest.fixture(scope='function')
def mock_secret_code(mocker):
def _create():

View File

@@ -3,7 +3,6 @@ import uuid
from tests import create_authorization_header
from flask import url_for, json
from app import notify_alpha_client
from app.models import Service
@@ -14,18 +13,6 @@ def test_get_notifications(
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch(
'app.notify_alpha_client.fetch_notification_by_id',
return_value={
'notifications': [
{
'id': 'my_id',
'notification': 'some notify'
}
]
}
)
auth_header = create_authorization_header(
service_id=sample_api_key.service_id,
path=url_for('notifications.get_notifications', notification_id=123),
@@ -35,12 +22,7 @@ def test_get_notifications(
url_for('notifications.get_notifications', notification_id=123),
headers=[auth_header])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert len(json_resp['notifications']) == 1
assert json_resp['notifications'][0]['id'] == 'my_id'
assert json_resp['notifications'][0]['notification'] == 'some notify'
notify_alpha_client.fetch_notification_by_id.assert_called_with("123")
def test_get_notifications_empty_result(
@@ -50,14 +32,6 @@ def test_get_notifications_empty_result(
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch(
'app.notify_alpha_client.fetch_notification_by_id',
return_value={
'notifications': [
]
}
)
auth_header = create_authorization_header(
service_id=sample_api_key.service_id,
path=url_for('notifications.get_notifications', notification_id=123),
@@ -67,10 +41,7 @@ def test_get_notifications_empty_result(
url_for('notifications.get_notifications', notification_id=123),
headers=[auth_header])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert len(json_resp['notifications']) == 0
notify_alpha_client.fetch_notification_by_id.assert_called_with("123")
def test_create_sms_should_reject_if_no_phone_numbers(
@@ -80,10 +51,6 @@ def test_create_sms_should_reject_if_no_phone_numbers(
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value='success'
)
data = {
'template': "my message"
}
@@ -102,7 +69,6 @@ def test_create_sms_should_reject_if_no_phone_numbers(
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert 'Missing data for required field.' in json_resp['message']['to'][0]
assert not notify_alpha_client.send_sms.called
def test_should_reject_bad_phone_numbers(
@@ -112,10 +78,6 @@ def test_should_reject_bad_phone_numbers(
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value='success'
)
data = {
'to': 'invalid',
'template': "my message"
@@ -134,7 +96,6 @@ def test_should_reject_bad_phone_numbers(
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert 'Invalid phone number, must be of format +441234123123' in json_resp['message']['to']
assert not notify_alpha_client.send_sms.called
def test_send_notification_restrict_mobile(notify_api,
@@ -150,14 +111,9 @@ def test_send_notification_restrict_mobile(notify_api,
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
Service.query.filter_by(
id=sample_template.service.id).update({'restricted': True})
invalid_mob = '+449999999999'
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value={}
)
data = {
'to': invalid_mob,
'template': sample_template.id
@@ -177,7 +133,6 @@ def test_send_notification_restrict_mobile(notify_api,
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert 'Invalid phone number for restricted service' in json_resp['message']['restricted']
assert not notify_alpha_client.send_sms.called
def test_send_notification_invalid_template_id(notify_api,
@@ -196,10 +151,6 @@ def test_send_notification_invalid_template_id(notify_api,
Service.query.filter_by(
id=sample_template.service.id).update({'restricted': True})
invalid_mob = '+449999999999'
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value={}
)
data = {
'to': invalid_mob,
'template': 9999
@@ -219,7 +170,6 @@ def test_send_notification_invalid_template_id(notify_api,
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert 'Template not found' in json_resp['message']['template']
assert not notify_alpha_client.send_sms.called
@moto.mock_sqs
@@ -235,20 +185,6 @@ def test_should_allow_valid_message(notify_api,
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value={
"notification": {
"createdAt": "2015-11-03T09:37:27.414363Z",
"id": 100,
"jobId": 65,
"message": "valid",
"method": "sms",
"status": "created",
"to": sample_user.mobile_number
}
}
)
data = {
'to': '+441234123123',
'template': sample_template.id
@@ -263,11 +199,7 @@ def test_should_allow_valid_message(notify_api,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert json_resp['notification']['id'] == 100
notify_alpha_client.send_sms.assert_called_with(mobile_number='+441234123123',
message=sample_template.content)
assert response.status_code == 204
@moto.mock_sqs
@@ -284,22 +216,6 @@ def test_send_email_valid_data(notify_api,
from_address = "from@notify.com"
subject = "This is the subject"
message = "This is the message"
mocker.patch(
'app.notify_alpha_client.send_email',
return_value={
"notification": {
"createdAt": "2015-11-03T09:37:27.414363Z",
"id": 100,
"jobId": 65,
"subject": subject,
"message": message,
"method": "email",
"status": "created",
"to": to_address,
"from": from_address
}
}
)
data = {
'to': to_address,
'from': from_address,
@@ -316,11 +232,7 @@ def test_send_email_valid_data(notify_api,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert json_resp['notification']['id'] == 100
notify_alpha_client.send_email.assert_called_with(
to_address, message, from_address, subject)
assert response.status_code == 204
@moto.mock_sqs
@@ -333,20 +245,6 @@ def test_valid_message_with_service_id(notify_api,
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value={
"notification": {
"createdAt": "2015-11-03T09:37:27.414363Z",
"id": 100,
"jobId": 65,
"message": "valid",
"method": "sms",
"status": "created",
"to": sample_user.mobile_number
}
}
)
job_id = uuid.uuid4()
service_id = sample_template.service.id
url = url_for('notifications.create_sms_for_service', service_id=service_id)
@@ -365,11 +263,7 @@ def test_valid_message_with_service_id(notify_api,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert json_resp['notification']['id'] == 100
notify_alpha_client.send_sms.assert_called_with(mobile_number='+441234123123',
message=sample_template.content)
assert response.status_code == 204
@moto.mock_sqs

View File

@@ -253,7 +253,6 @@ def test_send_user_code_for_sms(notify_api,
notify_db_session,
sample_sms_code,
sqs_client_conn,
mock_notify_client_send_sms,
mock_secret_code):
"""
Tests POST endpoint '/<user_id>/code' successful sms
@@ -271,8 +270,6 @@ def test_send_user_code_for_sms(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
mock_notify_client_send_sms.assert_called_once_with(mobile_number=sample_sms_code.user.mobile_number,
message='11111')
@moto.mock_sqs
@@ -281,7 +278,6 @@ def test_send_user_code_for_sms_with_optional_to_field(notify_api,
notify_db_session,
sample_sms_code,
sqs_client_conn,
mock_notify_client_send_sms,
mock_secret_code):
"""
Tests POST endpoint '/<user_id>/code' successful sms with optional to field
@@ -299,8 +295,6 @@ 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
mock_notify_client_send_sms.assert_called_once_with(mobile_number='+441119876757',
message='11111')
@moto.mock_sqs
@@ -309,7 +303,6 @@ def test_send_user_code_for_email(notify_api,
notify_db_session,
sample_email_code,
sqs_client_conn,
mock_notify_client_send_email,
mock_secret_code):
"""
Tests POST endpoint '/<user_id>/code' successful email
@@ -326,10 +319,6 @@ def test_send_user_code_for_email(notify_api,
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
mock_notify_client_send_email.assert_called_once_with(sample_email_code.user.email_address,
'11111',
'notify@digital.cabinet-office.gov.uk',
'Verification code')
@moto.mock_sqs
@@ -338,7 +327,6 @@ def test_send_user_code_for_email_uses_optional_to_field(notify_api,
notify_db_session,
sample_email_code,
sqs_client_conn,
mock_notify_client_send_email,
mock_secret_code):
"""
Tests POST endpoint '/<user_id>/code' successful email with included in body
@@ -355,10 +343,6 @@ def test_send_user_code_for_email_uses_optional_to_field(notify_api,
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
mock_notify_client_send_email.assert_called_once_with('different@email.gov.uk',
'11111',
'notify@digital.cabinet-office.gov.uk',
'Verification code')
def test_request_verify_code_schema_invalid_code_type(notify_api, notify_db, notify_db_session, sample_user):