POST /service/{id}/deactivate deactivates a service:

* set active=False on the service
* renames service to "_archived_{old_name}"
* archives all templates for the service
* revokes all api keys for the service
This commit is contained in:
Leo Hemsted
2016-11-01 16:09:55 +00:00
parent da2fa5b4bc
commit 089ac099f3
2 changed files with 101 additions and 0 deletions

View File

@@ -32,6 +32,9 @@ from app.dao.service_whitelist_dao import (
dao_add_and_commit_whitelisted_contacts, dao_add_and_commit_whitelisted_contacts,
dao_remove_service_whitelist dao_remove_service_whitelist
) )
from app.dao.templates_dao import (
dao_update_template
)
from app.dao import notifications_dao from app.dao import notifications_dao
from app.dao.provider_statistics_dao import get_fragment_count from app.dao.provider_statistics_dao import get_fragment_count
from app.dao.users_dao import get_model_users from app.dao.users_dao import get_model_users
@@ -312,6 +315,29 @@ def update_whitelist(service_id):
return '', 204 return '', 204
@service_blueprint.route('/<uuid:service_id>/deactivate', methods=['POST'])
def deactivate_service(service_id):
service = dao_fetch_service_by_id(service_id)
if not service.active:
# assume already inactive, don't change service name
return '', 204
service.active = False
service.name = '_archived_' + service.name
service.email_from = '_archived_' + service.email_from
dao_update_service(service)
for template in service.templates:
template.archived = True
dao_update_template(template)
for api_key in service.api_keys:
expire_api_key(service.id, api_key.id)
return '', 204
@service_blueprint.route('/<uuid:service_id>/billable-units') @service_blueprint.route('/<uuid:service_id>/billable-units')
def get_billable_unit_count(service_id): def get_billable_unit_count(service_id):
try: try:

View File

@@ -0,0 +1,75 @@
import uuid
import pytest
from app.models import Service
from tests import create_authorization_header
from tests.app.conftest import (
sample_template as create_template,
sample_api_key as create_api_key
)
def test_deactivate_only_allows_post(client, sample_service):
auth_header = create_authorization_header(service_id=str(sample_service.id))
response = client.get('/service/{}/deactivate'.format(uuid.uuid4()), headers=[auth_header])
assert response.status_code == 405
def test_deactivate_service_errors_with_bad_service_id(client, sample_service):
auth_header = create_authorization_header(service_id=str(sample_service.id))
response = client.post('/service/{}/deactivate'.format(uuid.uuid4()), headers=[auth_header])
assert response.status_code == 404
def test_deactivating_inactive_service_does_nothing(client, sample_service):
auth_header = create_authorization_header(service_id=str(sample_service.id))
sample_service.active = False
response = client.post('/service/{}/deactivate'.format(sample_service.id), headers=[auth_header])
assert response.status_code == 204
assert sample_service.name == 'Sample service'
@pytest.fixture
def deactivated_service(client, notify_db, notify_db_session, sample_service):
create_template(notify_db, notify_db_session, template_name='a')
create_template(notify_db, notify_db_session, template_name='b')
create_api_key(notify_db, notify_db_session)
create_api_key(notify_db, notify_db_session)
auth_header = create_authorization_header(service_id=str(sample_service.id))
response = client.post('/service/{}/deactivate'.format(sample_service.id), headers=[auth_header])
assert response.status_code == 204
assert response.data == b''
return sample_service
def test_deactivating_service_changes_name_and_email(deactivated_service):
assert deactivated_service.name == '_archived_Sample service'
assert deactivated_service.email_from == '_archived_sample.service'
def test_deactivating_service_revokes_api_keys(deactivated_service):
assert deactivated_service.api_keys.count() == 2
for key in deactivated_service.api_keys:
assert key.expiry_date is not None
assert key.version == 2
def test_deactivating_service_archives_templates(deactivated_service):
assert deactivated_service.templates.count() == 2
for template in deactivated_service.templates:
assert template.archived is True
assert template.version == 2
def test_deactivating_service_creates_history(deactivated_service):
ServiceHistory = Service.get_history_model()
history = ServiceHistory.query.filter_by(
id=deactivated_service.id
).order_by(
ServiceHistory.version.desc()
).first()
assert history.version == 2
assert history.active is False