From 98314485d33f0665af9709b06bf368813df84959 Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Fri, 19 Feb 2016 17:07:59 +0000 Subject: [PATCH] Generate the email_from from the service name. --- app/service/rest.py | 14 +++++++++++--- app/user/rest.py | 9 +++------ tests/app/service/test_rest.py | 5 ++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 1f1974ab7..d6cc3cadf 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -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('/', methods=['POST']) def update_service(service_id): fetched_service = dao_fetch_service_by_id(service_id) diff --git a/app/user/rest.py b/app/user/rest.py index b1b807f81..7ac62f148 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -149,11 +149,8 @@ def send_user_code(user_id): @user.route('/', 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) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index dd1598735..49298ebbd 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -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):