From 81cd230a79696ec4d6045a310170d9de09ea2801 Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Tue, 12 Jan 2016 10:59:27 +0000 Subject: [PATCH] More tests added. --- tests/app/service/views/test_rest.py | 42 +++++++++++++++++++++++++++- tests/app/user/views/test_rest.py | 39 ++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/tests/app/service/views/test_rest.py b/tests/app/service/views/test_rest.py index 450b7979a..3519e9f7d 100644 --- a/tests/app/service/views/test_rest.py +++ b/tests/app/service/views/test_rest.py @@ -142,6 +142,29 @@ def test_put_service(notify_api, notify_db, notify_db_session, sample_service): assert updated_service.name == new_name +def test_put_service_not_exists(notify_api, notify_db, notify_db_session, sample_service): + """ + Tests PUT endpoint '/' service doesn't exist. + """ + with notify_api.test_request_context(): + with notify_api.test_client() as client: + sample_user = sample_service.users[0] + new_name = 'updated service' + data = { + 'name': new_name, + 'users': [sample_user.id], + 'limit': 1000, + 'restricted': False, + 'active': False} + resp = client.put( + url_for('service.update_service', service_id="123"), + data=data, + headers=[('Content-Type', 'application/json')]) + assert resp.status_code == 404 + assert Service.query.first().name == sample_service.name + assert Service.query.first().name != new_name + + def test_put_service_add_user(notify_api, notify_db, notify_db_session, sample_service): """ Tests PUT endpoint '/' add user to the service. @@ -211,7 +234,10 @@ def test_put_service_remove_user(notify_api, notify_db, notify_db_session, sampl assert another_user.id in json_resp['data']['users'] -def test_delete_user(notify_api, notify_db, notify_db_session, sample_service): +def test_delete_service(notify_api, notify_db, notify_db_session, sample_service): + """ + Tests DELETE endpoint '/' delete service. + """ with notify_api.test_request_context(): with notify_api.test_client() as client: service = Service.query.first() @@ -220,4 +246,18 @@ def test_delete_user(notify_api, notify_db, notify_db_session, sample_service): headers=[('Content-Type', 'application/json')]) assert resp.status_code == 202 json_resp = json.loads(resp.get_data(as_text=True)) + json_resp['data']['name'] == sample_service.name assert Service.query.count() == 0 + + +def test_delete_service_not_exists(notify_api, notify_db, notify_db_session, sample_service): + """ + Tests DELETE endpoint '/' delete service doesn't exist. + """ + with notify_api.test_request_context(): + with notify_api.test_client() as client: + resp = client.delete( + url_for('service.update_service', service_id="123"), + headers=[('Content-Type', 'application/json')]) + assert resp.status_code == 404 + assert Service.query.count() == 1 diff --git a/tests/app/user/views/test_rest.py b/tests/app/user/views/test_rest.py index f59804c96..43cf08949 100644 --- a/tests/app/user/views/test_rest.py +++ b/tests/app/user/views/test_rest.py @@ -97,6 +97,28 @@ def test_put_user(notify_api, notify_db, notify_db_session, sample_user): assert json_resp['data']['id'] == user.id +def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_user): + """ + Tests PUT endpoint '/' to update a user doesn't exist. + """ + 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 = { + 'email_address': new_email} + headers = [('Content-Type', 'application/json')] + resp = client.put( + url_for('user.update_user', user_id="123"), + data=json.dumps(data), + headers=headers) + assert resp.status_code == 404 + assert User.query.count() == 1 + user = User.query.first() + json_resp = json.loads(resp.get_data(as_text=True)) + assert user.email_address != new_email + + def test_put_user_missing_email(notify_api, notify_db, notify_db_session, sample_user): """ Tests PUT endpoint '/' missing attribute email. @@ -198,6 +220,9 @@ def test_get_user_service_service_not_exists(notify_api, notify_db, notify_db_se def test_delete_user(notify_api, notify_db, notify_db_session, sample_user): + """ + Tests DELETE endpoint '/' delete user. + """ with notify_api.test_request_context(): with notify_api.test_client() as client: user = User.query.first() @@ -207,3 +232,17 @@ def test_delete_user(notify_api, notify_db, notify_db_session, sample_user): assert resp.status_code == 202 json_resp = json.loads(resp.get_data(as_text=True)) assert User.query.count() == 0 + + +def test_delete_user_not_exists(notify_api, notify_db, notify_db_session, sample_user): + """ + Tests DELETE endpoint '/' delete user. + """ + with notify_api.test_request_context(): + with notify_api.test_client() as client: + user = User.query.first() + resp = client.delete( + url_for('user.update_user', user_id="123"), + headers=[('Content-Type', 'application/json')]) + assert resp.status_code == 404 + assert User.query.count() == 1