From 12a2d8db0ab5f623e53dbbb0943fcb054e90f29d Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 23 Feb 2016 17:52:55 +0000 Subject: [PATCH 1/2] New endpoint to get users for service id. /service//users returns a list of all users associated with the service --- app/service/rest.py | 14 ++++++++++++-- tests/app/service/test_rest.py | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 51eae44cf..b3fb67dfa 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -27,8 +27,8 @@ from app.models import ApiKey from app.schemas import ( services_schema, service_schema, - api_keys_schema -) + api_keys_schema, + users_schema) from app import email_safe from flask import Blueprint @@ -155,3 +155,13 @@ def get_api_keys(service_id, key_id=None): return jsonify(result="error", message="API key not found"), 404 return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200 + + +@service.route('//users', methods=['GET']) +def get_users_for_service(service_id): + fetched = dao_fetch_service_by_id(service_id) + if not fetched: + return jsonify(result="error", message="Service not found"), 404 + print(fetched.users) + result = users_schema.dump(fetched.users) + return jsonify(data=result.data) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 324b77d2a..999993715 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -4,6 +4,7 @@ import uuid from app.dao.users_dao import save_model_user from app.models import User from tests import create_authorization_header +from tests.app.conftest import sample_service as create_service def test_get_service_list(notify_api, service_factory): @@ -341,3 +342,27 @@ def test_update_service_should_404_if_id_is_invalid(notify_api, notify_db): headers=[('Content-Type', 'application/json'), auth_header] ) assert resp.status_code == 404 + + +def test_get_users_by_service(notify_api, notify_db, notify_db_session, sample_user): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + + sample_service = create_service(notify_db=notify_db, notify_db_session=notify_db_session, + service_name='Sample service', user=sample_user) + auth_header = create_authorization_header( + path='/service/{}/users'.format(sample_service.id), + method='GET' + ) + + resp = client.get( + '/service/{}/users'.format(sample_service.id), + headers=[('Content-Type', 'application/json'), auth_header] + ) + + assert resp.status_code == 200 + result = json.loads(resp.get_data(as_text=True)) + assert len(result['data']) == 1 + assert result['data'][0]['name'] == sample_user.name + assert result['data'][0]['email_address'] == sample_user.email_address + assert result['data'][0]['mobile_number'] == sample_user.mobile_number From f1fdfbb308aae5ab70a048803ff1dc82d3782140 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 24 Feb 2016 10:30:00 +0000 Subject: [PATCH 2/2] Return empty list when there are no users for the service. Added a test for when there are no users for the service. Added a test_url_for - do we want to add this test and use url_for in our tests? Or explictly write the url in the test? --- app/service/rest.py | 4 ++-- tests/app/service/test_rest.py | 22 +++++++++++++++++ tests/app/service/test_url_for.py | 40 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/app/service/test_url_for.py diff --git a/app/service/rest.py b/app/service/rest.py index b3fb67dfa..3363e210e 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -161,7 +161,7 @@ def get_api_keys(service_id, key_id=None): def get_users_for_service(service_id): fetched = dao_fetch_service_by_id(service_id) if not fetched: - return jsonify(result="error", message="Service not found"), 404 - print(fetched.users) + return jsonify(data=[]) + result = users_schema.dump(fetched.users) return jsonify(data=result.data) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 999993715..8df0b526f 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -366,3 +366,25 @@ def test_get_users_by_service(notify_api, notify_db, notify_db_session, sample_u assert result['data'][0]['name'] == sample_user.name assert result['data'][0]['email_address'] == sample_user.email_address assert result['data'][0]['mobile_number'] == sample_user.mobile_number + + +def test_get_users_for_service_returns_empty_list_if_no_users_associated_with_service(notify_api, + notify_db, + notify_db_session, + sample_service, + sample_user): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + sample_service.users.remove(sample_user) + auth_header = create_authorization_header( + path='/service/{}/users'.format(sample_service.id), + method='GET' + ) + + response = client.get( + '/service/{}/users'.format(sample_service.id), + headers=[('Content-Type', 'application/json'), auth_header] + ) + assert response.status_code == 200 + result = json.loads(response.get_data(as_text=True)) + assert result['data'] == [] diff --git a/tests/app/service/test_url_for.py b/tests/app/service/test_url_for.py new file mode 100644 index 000000000..286b949d1 --- /dev/null +++ b/tests/app/service/test_url_for.py @@ -0,0 +1,40 @@ +import uuid + +from flask import url_for + +service_id = str(uuid.uuid4()) + + +def test_url_for_get_services(notify_api): + with notify_api.test_request_context(): + url = url_for('service.get_services') + assert str(url) == '/service' + url_with_user_id = url_for('service.get_services', user_id=1) + assert str(url_with_user_id) == '/service?user_id=1' + + +def test_url_for_get_service_by_id(notify_api): + with notify_api.test_request_context(): + url = url_for('service.get_service_by_id', service_id=service_id) + assert str(url) == '/service/{}'.format(service_id) + + url_with_user_id = url_for('service.get_service_by_id', service_id=service_id, user_id=1) + assert str(url_with_user_id) == '/service/{0}?user_id={1}'.format(service_id, 1) + + +def test_url_for_create_service(notify_api): + with notify_api.test_request_context(): + url = url_for('service.create_service') + assert str(url) == '/service'.format(service_id) + + +def test_url_for_update_service(notify_api): + with notify_api.test_request_context(): + url = url_for('service.update_service', service_id=service_id) + assert str(url) == '/service/{}'.format(service_id) + + +def test_url_for_renew_api_key(notify_api): + with notify_api.test_request_context(): + url = url_for('service.renew_api_key', service_id=service_id) + assert str(url) == '/service/{}/api-key'.format(service_id)