mirror of
https://github.com/GSA/notifications-api.git
synced 2026-04-19 17:00:03 -04:00
Generate the email_from from the service name.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
from flask import (jsonify, request)
|
||||
@@ -70,16 +71,16 @@ def get_service_by_id(service_id):
|
||||
@service.route('', methods=['POST'])
|
||||
def create_service():
|
||||
data = request.get_json()
|
||||
|
||||
if not data.get('user_id', None):
|
||||
return jsonify(result="error", message={'user_id': ['Missing data for required field.']}), 400
|
||||
|
||||
user = get_model_users(data['user_id'])
|
||||
|
||||
if not user:
|
||||
return jsonify(result="error", message={'user_id': ['not found']}), 400
|
||||
|
||||
request.get_json().pop('user_id', None)
|
||||
data.pop('user_id', None)
|
||||
if 'name' in data:
|
||||
data['email_from'] = _email_safe(data.get('name', None))
|
||||
|
||||
valid_service, errors = service_schema.load(request.get_json())
|
||||
|
||||
@@ -90,6 +91,13 @@ def create_service():
|
||||
return jsonify(data=service_schema.dump(valid_service).data), 201
|
||||
|
||||
|
||||
def _email_safe(string):
|
||||
return "".join([
|
||||
character.lower() if character.isalnum() or character == "." else ""
|
||||
for character in re.sub("\s+", ".", string.strip())
|
||||
])
|
||||
|
||||
|
||||
@service.route('/<service_id>', methods=['POST'])
|
||||
def update_service(service_id):
|
||||
fetched_service = dao_fetch_service_by_id(service_id)
|
||||
|
||||
@@ -149,11 +149,8 @@ def send_user_code(user_id):
|
||||
@user.route('/<int:user_id>', methods=['GET'])
|
||||
@user.route('', methods=['GET'])
|
||||
def get_user(user_id=None):
|
||||
try:
|
||||
users = get_model_users(user_id=user_id)
|
||||
except DataError:
|
||||
return jsonify(result="error", message="Invalid user id"), 400
|
||||
except NoResultFound:
|
||||
return jsonify(result="error", message="User not found"), 404
|
||||
users = get_model_users(user_id=user_id)
|
||||
if not users:
|
||||
return jsonify(result="error", message="not found"), 404
|
||||
result = users_schema.dump(users) if isinstance(users, list) else user_schema.dump(users)
|
||||
return jsonify(data=result.data)
|
||||
|
||||
@@ -172,7 +172,6 @@ def test_create_service(notify_api, sample_user):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
'email_from': 'service',
|
||||
'name': 'created service',
|
||||
'user_id': sample_user.id,
|
||||
'limit': 1000,
|
||||
@@ -192,6 +191,7 @@ def test_create_service(notify_api, sample_user):
|
||||
assert resp.status_code == 201
|
||||
assert json_resp['data']['id']
|
||||
assert json_resp['data']['name'] == 'created service'
|
||||
assert json_resp['data']['email_from'] == 'created.service'
|
||||
|
||||
auth_header_fetch = create_authorization_header(
|
||||
path='/service/{}'.format(json_resp['data']['id']),
|
||||
@@ -260,7 +260,7 @@ def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(no
|
||||
assert 'not found' in json_resp['message']['user_id']
|
||||
|
||||
|
||||
def test_should_not_create_service_with_missing_if_missing_data(notify_api, sample_user):
|
||||
def test_should_not_create_service_if_missing_data(notify_api, sample_user):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
@@ -283,7 +283,6 @@ def test_should_not_create_service_with_missing_if_missing_data(notify_api, samp
|
||||
assert 'Missing data for required field.' in json_resp['message']['active']
|
||||
assert 'Missing data for required field.' in json_resp['message']['limit']
|
||||
assert 'Missing data for required field.' in json_resp['message']['restricted']
|
||||
assert 'Missing data for required field.' in json_resp['message']['email_from']
|
||||
|
||||
|
||||
def test_update_service(notify_api, sample_service):
|
||||
|
||||
Reference in New Issue
Block a user