Merge pull request #3197 from alphagov/update-annual-billing-if-org-changes

Set default free allow when organisation for a service changes
This commit is contained in:
Rebecca Law
2021-04-19 11:34:24 +01:00
committed by GitHub
37 changed files with 252 additions and 114 deletions

View File

@@ -91,3 +91,21 @@ def test_set_default_free_allowance_for_service_using_correct_year(sample_servic
25000,
2020
)
@freeze_time('2021-04-01 14:02:00')
def test_set_default_free_allowance_for_service_updates_existing_year(sample_service):
set_default_free_allowance_for_service(service=sample_service, year_start=None)
annual_billing = AnnualBilling.query.all()
assert not sample_service.organisation_type
assert len(annual_billing) == 1
assert annual_billing[0].service_id == sample_service.id
assert annual_billing[0].free_sms_fragment_limit == 10000
sample_service.organisation_type = 'central'
set_default_free_allowance_for_service(service=sample_service, year_start=None)
annual_billing = AnnualBilling.query.all()
assert len(annual_billing) == 1
assert annual_billing[0].service_id == sample_service.id
assert annual_billing[0].free_sms_fragment_limit == 150000

View File

@@ -3,13 +3,14 @@ from datetime import datetime
import pytest
from freezegun import freeze_time
from sqlalchemy.exc import SQLAlchemyError
from app.dao.organisation_dao import (
dao_add_service_to_organisation,
dao_add_user_to_organisation,
)
from app.dao.services_dao import dao_archive_service
from app.models import Organisation
from app.models import AnnualBilling, Organisation
from tests.app.db import (
create_annual_billing,
create_domain,
@@ -491,19 +492,63 @@ def test_post_update_organisation_set_mou_emails_signed_by(
}
def test_post_link_service_to_organisation(admin_request, sample_service, sample_organisation):
def test_post_link_service_to_organisation(admin_request, sample_service):
data = {
'service_id': str(sample_service.id)
}
organisation = create_organisation(organisation_type='central')
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=sample_organisation.id,
organisation_id=organisation.id,
_expected_status=204
)
assert len(organisation.services) == 1
assert sample_service.organisation_type == 'central'
def test_post_link_service_to_organisation_inserts_annual_billing(admin_request, sample_service):
data = {
'service_id': str(sample_service.id)
}
organisation = create_organisation(organisation_type='central')
assert len(organisation.services) == 0
assert len(AnnualBilling.query.all()) == 0
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=organisation.id,
_expected_status=204
)
assert len(sample_organisation.services) == 1
annual_billing = AnnualBilling.query.all()
assert len(annual_billing) == 1
assert annual_billing[0].free_sms_fragment_limit == 150000
def test_post_link_service_to_organisation_rollback_service_if_annual_billing_update_fails(
admin_request, sample_service, mocker
):
mocker.patch('app.dao.annual_billing_dao.dao_create_or_update_annual_billing_for_year',
side_effect=SQLAlchemyError)
data = {
'service_id': str(sample_service.id)
}
assert not sample_service.organisation_type
organisation = create_organisation(organisation_type='central')
assert len(organisation.services) == 0
assert len(AnnualBilling.query.all()) == 0
with pytest.raises(expected_exception=SQLAlchemyError):
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=organisation.id
)
assert not sample_service.organisation_type
assert len(organisation.services) == 0
assert len(AnnualBilling.query.all()) == 0
def test_post_link_service_to_another_org(
@@ -511,7 +556,8 @@ def test_post_link_service_to_another_org(
data = {
'service_id': str(sample_service.id)
}
assert len(sample_organisation.services) == 0
assert not sample_service.organisation_type
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
@@ -520,8 +566,9 @@ def test_post_link_service_to_another_org(
)
assert len(sample_organisation.services) == 1
assert not sample_service.organisation_type
new_org = create_organisation()
new_org = create_organisation(organisation_type='central')
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
@@ -530,6 +577,10 @@ def test_post_link_service_to_another_org(
)
assert not sample_organisation.services
assert len(new_org.services) == 1
assert sample_service.organisation_type == 'central'
annual_billing = AnnualBilling.query.all()
assert len(annual_billing) == 1
assert annual_billing[0].free_sms_fragment_limit == 150000
def test_post_link_service_to_organisation_nonexistent_organisation(

View File

@@ -6,6 +6,7 @@ from unittest.mock import ANY
import pytest
from flask import current_app, url_for
from freezegun import freeze_time
from sqlalchemy.exc import SQLAlchemyError
from app.dao.organisation_dao import dao_add_service_to_organisation
from app.dao.service_sms_sender_dao import dao_get_sms_senders_by_service_id
@@ -31,6 +32,7 @@ from app.models import (
SERVICE_PERMISSION_TYPES,
SMS_TYPE,
UPLOAD_LETTERS,
AnnualBilling,
EmailBranding,
InboundNumber,
Notification,
@@ -482,6 +484,47 @@ def test_create_service_with_domain_sets_organisation(
assert json_resp['data']['organisation'] is None
def test_create_service_should_create_annual_billing_for_service(
admin_request, sample_user
):
data = {
'name': 'created service',
'user_id': str(sample_user.id),
'message_limit': 1000,
'restricted': False,
'active': False,
'email_from': 'created.service',
'created_by': str(sample_user.id)
}
assert len(AnnualBilling.query.all()) == 0
admin_request.post('service.create_service', _data=data, _expected_status=201)
annual_billing = AnnualBilling.query.all()
assert len(annual_billing) == 1
def test_create_service_should_raise_exception_and_not_create_service_if_annual_billing_query_fails(
admin_request, sample_user, mocker
):
mocker.patch('app.service.rest.set_default_free_allowance_for_service', side_effect=SQLAlchemyError)
data = {
'name': 'created service',
'user_id': str(sample_user.id),
'message_limit': 1000,
'restricted': False,
'active': False,
'email_from': 'created.service',
'created_by': str(sample_user.id)
}
assert len(AnnualBilling.query.all()) == 0
with pytest.raises(expected_exception=SQLAlchemyError):
admin_request.post('service.create_service', _data=data)
annual_billing = AnnualBilling.query.all()
assert len(annual_billing) == 0
assert len(Service.query.filter(Service.name == 'created service').all()) == 0
def test_create_service_inherits_branding_from_organisation(
admin_request,
sample_user,