From 6d9de1a5009c0d59c87113e84f6d959df24ae68a Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Thu, 14 Apr 2016 15:38:27 +0100 Subject: [PATCH] Make service live and revert working, all tests passing. --- app/main/views/service_settings.py | 17 +++ app/templates/views/service-settings.html | 12 +- tests/app/main/views/test_service_settings.py | 108 +++++++++++++----- tests/conftest.py | 14 +++ 4 files changed, 123 insertions(+), 28 deletions(-) diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index dd4946a26..a86b45ce4 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -96,6 +96,23 @@ def service_request_to_go_live(service_id): return redirect(url_for('.service_settings', service_id=service_id)) +@main.route("/services//service-settings/switch-live") +@login_required +@user_has_permissions(admin_override=True) +def service_switch_live(service_id): + service_api_client.update_service( + current_service['id'], + current_service['name'], + current_service['active'], + # TODO This limit should be set depending on the agreement signed by + # with Notify. + 25000 if current_service['restricted'] else 50, + False if current_service['restricted'] else True, + current_service['users'], + current_service['email_from']) + return redirect(url_for('.service_settings', service_id=service_id)) + + @main.route("/services//service-settings/status", methods=['GET', 'POST']) @login_required @user_has_permissions('manage_settings', admin_override=True) diff --git a/app/templates/views/service-settings.html b/app/templates/views/service-settings.html index 5fb51c84c..8a1325961 100644 --- a/app/templates/views/service-settings.html +++ b/app/templates/views/service-settings.html @@ -18,7 +18,7 @@ 'title': 'Request to go live and turn off sending restrictions', 'link': url_for('.service_request_to_go_live', service_id=current_service.id), 'hint': 'A live service can send notifications to any phone number or email address', - } if not current_service.live else { + } if current_service.restricted else { }, { 'title': 'Temporarily suspend API keys', @@ -27,7 +27,15 @@ } if not current_service.active else { 'title': 'Reactivate API keys', 'link': url_for('.service_status_change', service_id=current_service.id) - } + }, + { + 'title': 'Make service live', + 'link': url_for('.service_switch_live', service_id=current_service.id) + } if current_service.restricted and current_user.has_permissions([], admin_override=True) else { + 'title': 'Revert service to trial', + 'link': url_for('.service_switch_live', service_id=current_service.id) + } if not current_service.restricted and current_user.has_permissions([], admin_override=True) else { + }, ]) }} {% endblock %} diff --git a/tests/app/main/views/test_service_settings.py b/tests/app/main/views/test_service_settings.py index 947c58350..24adcb380 100644 --- a/tests/app/main/views/test_service_settings.py +++ b/tests/app/main/views/test_service_settings.py @@ -4,6 +4,7 @@ import app from app.utils import email_safe from tests import validate_route_permission from bs4 import BeautifulSoup +from unittest.mock import ANY def test_should_show_overview(app_, @@ -37,13 +38,17 @@ def test_should_show_service_name(app_, def test_should_redirect_after_change_service_name(app_, - active_user_with_permissions, service_one, - mocker, - mock_get_services): + active_user_with_permissions, + mock_login, + mock_get_user, + mock_get_service, + mock_update_service, + mock_get_services, + mock_has_permissions): with app_.test_request_context(): with app_.test_client() as client: - client.login(active_user_with_permissions, mocker, service_one) + client.login(active_user_with_permissions) response = client.post( url_for('main.service_name_change', service_id=service_one['id']), data={'name': "new name"}) @@ -55,6 +60,46 @@ def test_should_redirect_after_change_service_name(app_, assert mock_get_services.called +def test_switch_service_to_live(app_, + service_one, + mock_login, + mock_get_user, + active_user_with_permissions, + mock_get_service, + mock_update_service, + mock_has_permissions): + with app_.test_request_context(): + with app_.test_client() as client: + client.login(active_user_with_permissions) + response = client.get( + url_for('main.service_switch_live', service_id=service_one['id'])) + assert response.status_code == 302 + assert response.location == url_for( + 'main.service_settings', + service_id=service_one['id'], _external=True) + mock_update_service.assert_called_with(ANY, ANY, ANY, 25000, False, ANY, ANY) + + +def test_switch_service_to_restricted(app_, + service_one, + mock_login, + mock_get_user, + active_user_with_permissions, + mock_get_live_service, + mock_update_service, + mock_has_permissions): + with app_.test_request_context(): + with app_.test_client() as client: + client.login(active_user_with_permissions) + response = client.get( + url_for('main.service_switch_live', service_id=service_one['id'])) + assert response.status_code == 302 + assert response.location == url_for( + 'main.service_settings', + service_id=service_one['id'], _external=True) + mock_update_service.assert_called_with(ANY, ANY, ANY, 50, True, ANY, ANY) + + def test_should_not_allow_duplicate_names(app_, active_user_with_permissions, mocker, @@ -127,7 +172,8 @@ def test_should_raise_duplicate_name_handled(app_, mocker, mock_get_services, mock_update_service_raise_httperror_duplicate_name, - mock_verify_password): + mock_verify_password, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(active_user_with_permissions, mocker, service_one) @@ -151,11 +197,12 @@ def test_should_show_request_to_go_live(app_, mock_get_user, mock_get_user_by_email, mock_login, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.get( url_for('main.service_request_to_go_live', service_id=service_id)) service = mock_get_service.side_effect(service_id)['data'] @@ -171,11 +218,12 @@ def test_should_redirect_after_request_to_go_live(app_, mock_get_user, mock_get_user_by_email, mock_login, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.post(url_for( 'main.service_request_to_go_live', service_id=service_id)) @@ -187,7 +235,7 @@ def test_should_redirect_after_request_to_go_live(app_, with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.post(url_for( 'main.service_request_to_go_live', service_id=service_id), follow_redirects=True) @@ -202,11 +250,12 @@ def test_should_show_status_page(app_, mock_get_user, mock_get_user_by_email, mock_login, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.get(url_for( 'main.service_status_change', service_id=service_id)) @@ -222,11 +271,12 @@ def test_should_show_redirect_after_status_change(app_, mock_get_user, mock_get_user_by_email, mock_login, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.post(url_for( 'main.service_status_change', service_id=service_id)) @@ -243,11 +293,12 @@ def test_should_show_status_confirmation(app_, mock_get_user, mock_get_user_by_email, mock_login, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.get(url_for( 'main.service_status_change_confirm', service_id=service_id)) @@ -265,11 +316,12 @@ def test_should_redirect_after_status_confirmation(app_, mock_get_user_by_email, mock_login, mock_verify_password, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.post(url_for( 'main.service_status_change_confirm', service_id=service_id)) @@ -287,11 +339,12 @@ def test_should_show_delete_page(app_, mock_get_service, mock_get_user, mock_get_user_by_email, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.get(url_for( 'main.service_delete', service_id=service_id)) @@ -306,11 +359,12 @@ def test_should_show_redirect_after_deleting_service(app_, mock_get_user, mock_get_user_by_email, mock_login, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.post(url_for( 'main.service_delete', service_id=service_id)) @@ -326,11 +380,12 @@ def test_should_show_delete_confirmation(app_, mock_get_user, mock_get_user_by_email, mock_login, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.get(url_for( 'main.service_delete_confirm', service_id=service_id)) @@ -347,11 +402,12 @@ def test_should_redirect_delete_confirmation(app_, mock_get_user_by_email, mock_login, mock_verify_password, - mock_has_permissions): + mock_has_permissions, + fake_uuid): with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - service_id = 123 + service_id = fake_uuid response = client.post(url_for( 'main.service_delete_confirm', service_id=service_id)) diff --git a/tests/conftest.py b/tests/conftest.py index 4b8bb2858..8a522ea89 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -65,6 +65,20 @@ def mock_get_service(mocker, api_user_active): return mocker.patch('app.service_api_client.get_service', side_effect=_get) +@pytest.fixture(scope='function') +def mock_get_live_service(mocker, api_user_active): + def _get(service_id): + service = service_json( + service_id, + "Test Service", + [api_user_active.id], + message_limit=1000, + active=False, + restricted=False) + return {'data': service} + return mocker.patch('app.service_api_client.get_service', side_effect=_get) + + @pytest.fixture(scope='function') def mock_create_service(mocker): def _create(service_name, active, message_limit, restricted, user_id, email_from):