mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-06 05:48:25 -04:00
Add more tests.
This commit is contained in:
@@ -24,7 +24,7 @@ def get_model_services(service_id=None, user_id=None):
|
|||||||
# TODO need better mapping from function params to sql query.
|
# TODO need better mapping from function params to sql query.
|
||||||
if user_id and service_id:
|
if user_id and service_id:
|
||||||
return Service.query.filter(
|
return Service.query.filter(
|
||||||
Service.users.any(id=user_id), id=service_id).one()
|
Service.users.any(id=user_id)).filter_by(id=service_id).one()
|
||||||
elif service_id:
|
elif service_id:
|
||||||
return Service.query.filter_by(id=service_id).one()
|
return Service.query.filter_by(id=service_id).one()
|
||||||
elif user_id:
|
elif user_id:
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ from app.schemas import (services_schema, service_schema)
|
|||||||
def create_service():
|
def create_service():
|
||||||
# TODO what exceptions get passed from schema parsing?
|
# TODO what exceptions get passed from schema parsing?
|
||||||
service, errors = service_schema.load(request.get_json())
|
service, errors = service_schema.load(request.get_json())
|
||||||
|
if errors:
|
||||||
|
return jsonify(result="error", message=errors), 400
|
||||||
# I believe service is already added to the session but just needs a
|
# I believe service is already added to the session but just needs a
|
||||||
# db.session.commit
|
# db.session.commit
|
||||||
save_model_service(service)
|
save_model_service(service)
|
||||||
@@ -30,6 +32,8 @@ def update_service(service_id):
|
|||||||
return jsonify(result="error", message="Service not found"), 404
|
return jsonify(result="error", message="Service not found"), 404
|
||||||
# TODO there has got to be a better way to do the next three lines
|
# TODO there has got to be a better way to do the next three lines
|
||||||
update_service, errors = service_schema.load(request.get_json())
|
update_service, errors = service_schema.load(request.get_json())
|
||||||
|
if errors:
|
||||||
|
return jsonify(result="error", message=errors), 400
|
||||||
update_dict, errors = service_schema.dump(update_service)
|
update_dict, errors = service_schema.dump(update_service)
|
||||||
# TODO FIX ME
|
# TODO FIX ME
|
||||||
# Remove update_service model which is added to db.session
|
# Remove update_service model which is added to db.session
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ from app import db
|
|||||||
# TODO auth to be added
|
# TODO auth to be added
|
||||||
@user.route('/', methods=['POST'])
|
@user.route('/', methods=['POST'])
|
||||||
def create_user():
|
def create_user():
|
||||||
user = user_schema.load(request.get_json()).data
|
user, errors = user_schema.load(request.get_json())
|
||||||
|
if errors:
|
||||||
|
return jsonify(result="error", message=errors), 400
|
||||||
save_model_user(user)
|
save_model_user(user)
|
||||||
return jsonify(data=user_schema.dump(user).data), 201
|
return jsonify(data=user_schema.dump(user).data), 201
|
||||||
|
|
||||||
@@ -28,6 +30,8 @@ def update_user(user_id):
|
|||||||
return jsonify(result="error", message="User not found"), 404
|
return jsonify(result="error", message="User not found"), 404
|
||||||
# TODO there has got to be a better way to do the next three lines
|
# TODO there has got to be a better way to do the next three lines
|
||||||
update_user, errors = user_schema.load(request.get_json())
|
update_user, errors = user_schema.load(request.get_json())
|
||||||
|
if errors:
|
||||||
|
return jsonify(result="error", message=errors), 400
|
||||||
update_dict, errors = user_schema.dump(update_user)
|
update_dict, errors = user_schema.dump(update_user)
|
||||||
# TODO FIX ME
|
# TODO FIX ME
|
||||||
# Remove update_service model which is added to db.session
|
# Remove update_service model which is added to db.session
|
||||||
@@ -45,7 +49,7 @@ def get_user(user_id=None):
|
|||||||
except DataError:
|
except DataError:
|
||||||
return jsonify(result="error", message="Invalid user id"), 400
|
return jsonify(result="error", message="Invalid user id"), 400
|
||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
return jsonify(result="error", message="User doesn't exist"), 404
|
return jsonify(result="error", message="User not found"), 404
|
||||||
result = users_schema.dump(users) if isinstance(users, list) else user_schema.dump(users)
|
result = users_schema.dump(users) if isinstance(users, list) else user_schema.dump(users)
|
||||||
return jsonify(data=result.data)
|
return jsonify(data=result.data)
|
||||||
|
|
||||||
@@ -59,13 +63,13 @@ def get_service_by_user_id(user_id, service_id=None):
|
|||||||
except DataError:
|
except DataError:
|
||||||
return jsonify(result="error", message="Invalid user id"), 400
|
return jsonify(result="error", message="Invalid user id"), 400
|
||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
return jsonify(result="error", message="User doesn't exist"), 400
|
return jsonify(result="error", message="User not found"), 404
|
||||||
|
|
||||||
try:
|
try:
|
||||||
services = get_model_services(user_id=user.id, service_id=service_id)
|
services = get_model_services(user_id=user.id, service_id=service_id)
|
||||||
except DataError:
|
except DataError:
|
||||||
return jsonify(result="error", message="Invalid service id"), 400
|
return jsonify(result="error", message="Invalid service id"), 400
|
||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
return jsonify(result="error", message="Service doesn't exist"), 404
|
return jsonify(result="error", message="Service not found"), 404
|
||||||
result = services_schema.dump(services) if isinstance(services, list) else service_schema.dump(services)
|
services, errors = services_schema.dump(services) if isinstance(services, list) else service_schema.dump(services)
|
||||||
return jsonify(data=result.data)
|
return jsonify(data=services)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
from app.models import User
|
from app.models import (User, Service)
|
||||||
|
from tests.app.conftest import sample_service as create_sample_service
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
|
|
||||||
|
|
||||||
@@ -53,7 +54,30 @@ def test_post_user(notify_api, notify_db, notify_db_session):
|
|||||||
assert json_resp['data']['id'] == user.id
|
assert json_resp['data']['id'] == user.id
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_user_missing_attribute_email(notify_api, notify_db, notify_db_session):
|
||||||
|
"""
|
||||||
|
Tests POST endpoint '/' missing attribute email.
|
||||||
|
"""
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
assert User.query.count() == 0
|
||||||
|
data = {
|
||||||
|
'blah': 'blah.blah'}
|
||||||
|
headers = [('Content-Type', 'application/json')]
|
||||||
|
resp = client.post(
|
||||||
|
url_for('user.create_user'),
|
||||||
|
data=json.dumps(data),
|
||||||
|
headers=headers)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
assert User.query.count() == 0
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert {'email_address': ['Missing data for required field.']} == json_resp['message']
|
||||||
|
|
||||||
|
|
||||||
def test_put_user(notify_api, notify_db, notify_db_session, sample_user):
|
def test_put_user(notify_api, notify_db, notify_db_session, sample_user):
|
||||||
|
"""
|
||||||
|
Tests PUT endpoint '/' to update a user.
|
||||||
|
"""
|
||||||
with notify_api.test_request_context():
|
with notify_api.test_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
assert User.query.count() == 1
|
assert User.query.count() == 1
|
||||||
@@ -71,3 +95,103 @@ def test_put_user(notify_api, notify_db, notify_db_session, sample_user):
|
|||||||
json_resp = json.loads(resp.get_data(as_text=True))
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
assert json_resp['data']['email_address'] == new_email
|
assert json_resp['data']['email_address'] == new_email
|
||||||
assert json_resp['data']['id'] == user.id
|
assert json_resp['data']['id'] == user.id
|
||||||
|
|
||||||
|
|
||||||
|
def test_put_user_missing_email(notify_api, notify_db, notify_db_session, sample_user):
|
||||||
|
"""
|
||||||
|
Tests PUT endpoint '/' missing attribute email.
|
||||||
|
"""
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
assert User.query.count() == 1
|
||||||
|
new_email = 'new@digital.cabinet-office.gov.uk'
|
||||||
|
data = {
|
||||||
|
'blah': new_email}
|
||||||
|
headers = [('Content-Type', 'application/json')]
|
||||||
|
resp = client.put(
|
||||||
|
url_for('user.update_user', user_id=sample_user.id),
|
||||||
|
data=json.dumps(data),
|
||||||
|
headers=headers)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
assert User.query.count() == 1
|
||||||
|
user = User.query.first()
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert user.email_address == sample_user.email_address
|
||||||
|
assert {'email_address': ['Missing data for required field.']} == json_resp['message']
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_user_services(notify_api, notify_db, notify_db_session, sample_service):
|
||||||
|
"""
|
||||||
|
Tests GET endpoint "/<user_id>/service/<service_id>" to retrieve services for a user.
|
||||||
|
"""
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
user = User.query.first()
|
||||||
|
another_name = "another name"
|
||||||
|
another_service = create_sample_service(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service_name=another_name,
|
||||||
|
user=user)
|
||||||
|
assert Service.query.count() == 2
|
||||||
|
resp = client.get(
|
||||||
|
url_for('user.get_service_by_user_id', user_id=user.id),
|
||||||
|
headers=[('Content-Type', 'application/json')])
|
||||||
|
assert resp.status_code == 200
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert len(json_resp['data']) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_user_service(notify_api, notify_db, notify_db_session, sample_service):
|
||||||
|
"""
|
||||||
|
Tests GET endpoint "/<user_id>/service/<service_id>" to retrieve a service for a user.
|
||||||
|
"""
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
user = User.query.first()
|
||||||
|
another_name = "another name"
|
||||||
|
another_service = create_sample_service(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service_name=another_name,
|
||||||
|
user=user)
|
||||||
|
assert Service.query.count() == 2
|
||||||
|
resp = client.get(
|
||||||
|
url_for('user.get_service_by_user_id', user_id=user.id, service_id=another_service.id),
|
||||||
|
headers=[('Content-Type', 'application/json')])
|
||||||
|
assert resp.status_code == 200
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert json_resp['data']['name'] == another_name
|
||||||
|
assert json_resp['data']['id'] == another_service.id
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_user_service_user_not_exists(notify_api, notify_db, notify_db_session, sample_service):
|
||||||
|
"""
|
||||||
|
Tests GET endpoint "/<user_id>/service/<service_id>" 404 is returned for invalid user.
|
||||||
|
"""
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
user = User.query.first()
|
||||||
|
assert Service.query.count() == 1
|
||||||
|
resp = client.get(
|
||||||
|
url_for('user.get_service_by_user_id', user_id="123", service_id=sample_service.id),
|
||||||
|
headers=[('Content-Type', 'application/json')])
|
||||||
|
assert resp.status_code == 404
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert "User not found" in json_resp['message']
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_user_service_service_not_exists(notify_api, notify_db, notify_db_session, sample_service):
|
||||||
|
"""
|
||||||
|
Tests GET endpoint "/<user_id>/service/<service_id>" 404 is returned for invalid service.
|
||||||
|
"""
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
user = User.query.first()
|
||||||
|
assert Service.query.count() == 1
|
||||||
|
resp = client.get(
|
||||||
|
url_for('user.get_service_by_user_id', user_id=user.id, service_id="123"),
|
||||||
|
headers=[('Content-Type', 'application/json')])
|
||||||
|
assert resp.status_code == 404
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert "Service not found" in json_resp['message']
|
||||||
|
|||||||
Reference in New Issue
Block a user