Merge branch 'master' into sms_sender_id-for-post-notfications

This commit is contained in:
Rebecca Law
2017-10-30 15:20:39 +00:00
14 changed files with 426 additions and 29 deletions

View File

@@ -15,6 +15,10 @@ from tests.app.db import (
from tests import create_authorization_header
from app.service.utils import get_current_financial_year_start_year
import uuid
APR_2016_MONTH_START = datetime(2016, 3, 31, 23, 00, 00)
APR_2016_MONTH_END = datetime(2016, 4, 30, 22, 59, 59, 99999)
@@ -251,3 +255,145 @@ def test_transform_billing_calculates_with_different_rate_multipliers(sample_ser
'month': 'April',
'rate': 0.12,
})
def test_create_update_free_sms_fragment_limit_invalid_schema(client, sample_service):
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
data={},
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert 'JSON' in json_resp['message']
def test_create_free_sms_fragment_limit(client, sample_service):
data = {'financial_year_start': 2017, 'free_sms_fragment_limit': 250}
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
response_get = client.get(
'service/{}/billing/free-sms-fragment-limit?financial_year_start=2017'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True))
assert response.status_code == 201
assert response_get.status_code == 200
assert json_resp['data']['financial_year_start'] == 2017
assert json_resp['data']['free_sms_fragment_limit'] == 250
def test_update_free_sms_fragment_limit(client, sample_service):
data_old = {'financial_year_start': 2016, 'free_sms_fragment_limit': 1000}
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
data=json.dumps(data_old),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
data_new = {'financial_year_start': 2016, 'free_sms_fragment_limit': 9999}
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
data=json.dumps(data_new),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
response_get = client.get(
'service/{}/billing/free-sms-fragment-limit?financial_year_start=2016'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True))
assert response.status_code == 201
assert response_get.status_code == 200
assert json_resp['data']['financial_year_start'] == 2016
assert json_resp['data']['free_sms_fragment_limit'] == 9999
def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_service):
years = [2016, 2017, 2018]
limits = [1000, 2000, 3000]
for i in range(0, len(years)):
annual_billing = {'financial_year_start': years[i], 'free_sms_fragment_limit': limits[i]}
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
data=json.dumps(annual_billing),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
for i in range(0, len(years)):
response_get = client.get(
'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, years[i]),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True))
assert response_get.status_code == 200
assert json_resp['data']['free_sms_fragment_limit'] == limits[i]
def test_get_free_sms_fragment_limit_for_all_years(client, sample_service):
years = [2016, 2017, 2018]
limits = [1000, 2000, 3000]
for i in range(0, len(years)):
annual_billing = {'financial_year_start': years[i], 'free_sms_fragment_limit': limits[i]}
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
data=json.dumps(annual_billing),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
response_get = client.get(
# Not specify a particular year to return all data for that service
'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True))
assert response_get.status_code == 200
assert len(json_resp['data']) == 3
print(json_resp)
for i in [0, 1, 2]:
assert json_resp['data'][i]['free_sms_fragment_limit'] == limits[i]
assert json_resp['data'][i]['financial_year_start'] == years[i]
def test_get_free_sms_fragment_limit_no_year_data_return_404(client, sample_service):
response_get = client.get(
'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, 1999),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True))
assert response_get.status_code == 404
def test_get_free_sms_fragment_limit_unknown_service_id_return_404(client):
response_get = client.get(
'service/{}/billing/free-sms-fragment-limit'.format(uuid.uuid4()),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True))
assert response_get.status_code == 404
def test_get_free_sms_fragment_limit_current_year(client, sample_service):
response = client.get(
'service/{}/billing/free-sms-fragment-limit/current-year'.format(sample_service.id, True),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert json_resp['data']['free_sms_fragment_limit'] == 250000
def test_post_free_sms_fragment_limit_current_year(client, sample_service):
data_new = {'free_sms_fragment_limit': 7777}
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
data=json.dumps(data_new),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
response_get = client.get(
'service/{}/billing/free-sms-fragment-limit/current-year'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True))
assert response.status_code == 201
assert response_get.status_code == 200
assert json_resp['data']['financial_year_start'] == get_current_financial_year_start_year()
assert json_resp['data']['free_sms_fragment_limit'] == 7777

View File

@@ -1398,7 +1398,15 @@ def test_process_ses_results(notify_db, notify_db_session, sample_email_template
assert process_ses_results(response=response) is None
def test_process_ses_results_does_not_retry_if_errors(notify_db, mocker):
mocked = mocker.patch('app.celery.tasks.process_ses_results.retry')
response = json.loads(ses_notification_callback())
process_ses_results(response=response)
assert mocked.call_count == 0
def test_process_ses_results_retry_called(notify_db, mocker):
mocker.patch("app.dao.notifications_dao.update_notification_status_by_reference", side_effect=Exception("EXPECTED"))
mocked = mocker.patch('app.celery.tasks.process_ses_results.retry')
response = json.loads(ses_notification_callback())
process_ses_results(response=response)

View File

@@ -0,0 +1,57 @@
from app.service.utils import get_current_financial_year_start_year
from app.models import AnnualBilling
from app.dao.annual_billing_dao import (
dao_create_or_update_annual_billing_for_year,
dao_get_free_sms_fragment_limit_for_year,
dao_get_annual_billing
)
def test_get_sample_service_has_default_free_sms_fragment_limit(notify_db_session, sample_service):
# when sample_service was created, it automatically create an entry in the annual_billing table
free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, get_current_financial_year_start_year())
assert free_limit.free_sms_fragment_limit == 250000
assert free_limit.financial_year_start == get_current_financial_year_start_year()
assert free_limit.service_id == sample_service.id
def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service):
year = 1999
old_limit = 1000
new_limit = 9999
data = AnnualBilling(
free_sms_fragment_limit=old_limit,
financial_year_start=year,
service_id=sample_service.id,
)
dao_create_or_update_annual_billing_for_year(data)
data.free_sms_fragment_limit = new_limit
dao_create_or_update_annual_billing_for_year(data)
new_free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, year)
assert new_free_limit.free_sms_fragment_limit == new_limit
def test_create_then_get_annual_billing(notify_db_session, sample_service):
years = [1999, 2001]
limits = [1000, 2000]
for i in [0, 1]:
data = AnnualBilling(
free_sms_fragment_limit=limits[i],
financial_year_start=years[i],
service_id=sample_service.id,
)
dao_create_or_update_annual_billing_for_year(data)
free_limit = dao_get_annual_billing(sample_service.id)
assert len(free_limit) == 3 # sample service already has one entry
assert free_limit[0].free_sms_fragment_limit == 1000
assert free_limit[0].financial_year_start == 1999
assert free_limit[0].service_id == sample_service.id
assert free_limit[1].free_sms_fragment_limit == 2000
assert free_limit[1].financial_year_start == 2001

View File

@@ -104,7 +104,7 @@ def test_cannot_create_two_services_with_same_name(sample_user):
email_from="email_from1",
message_limit=1000,
restricted=False,
created_by=sample_user)
created_by=sample_user,)
service2 = Service(name="service_name",
email_from="email_from2",

View File

@@ -37,6 +37,7 @@ from tests.app.db import (
create_service_sms_sender
)
from tests.app.db import create_user
from app.service.utils import get_current_financial_year_start_year
def test_get_service_list(client, service_factory):
@@ -300,6 +301,7 @@ def test_create_service(client, sample_user):
assert not json_resp['data']['research_mode']
assert json_resp['data']['dvla_organisation'] == '001'
assert json_resp['data']['sms_sender'] == current_app.config['FROM_NUMBER']
# TODO: Remove this after the new data is used
assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
service_db = Service.query.get(json_resp['data']['id'])
@@ -365,6 +367,15 @@ def test_create_service_free_sms_fragment_limit_is_optional(client, sample_user)
headers=headers)
json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 201
# Test data from the new annual billing table
service_id = json_resp['data']['id']
annual_billing = client.get('service/{}/billing/free-sms-fragment-limit?financial_year_start={}'
.format(service_id, get_current_financial_year_start_year()),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(annual_billing.get_data(as_text=True))
assert json_resp['data']['free_sms_fragment_limit'] == 9999
# TODO: Remove this after the new data is used
assert json_resp['data']['free_sms_fragment_limit'] == 9999
data2 = {
@@ -385,6 +396,14 @@ def test_create_service_free_sms_fragment_limit_is_optional(client, sample_user)
headers=headers)
json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 201
# Test data from the new annual billing table
service_id = json_resp['data']['id']
annual_billing = client.get('service/{}/billing/free-sms-fragment-limit?financial_year_start={}'
.format(service_id, get_current_financial_year_start_year()),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(annual_billing.get_data(as_text=True))
assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
# TODO: Remove this after the new data is used
assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
@@ -623,6 +642,7 @@ def test_update_service_flags_will_remove_service_permissions(client, notify_db,
assert set([p.permission for p in permissions]) == set([SMS_TYPE, EMAIL_TYPE])
# TODO: Remove after new table is created and verified
def test_update_service_free_sms_fragment_limit(client, notify_db, sample_service):
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
notify_db.session.add(org)

View File

@@ -0,0 +1,15 @@
from app.service.utils import get_current_financial_year_start_year
from freezegun import freeze_time
# see get_financial_year for conversion of financial years.
@freeze_time("2017-03-31 22:59:59.999999")
def test_get_current_financial_year_start_year_before_march():
current_fy = get_current_financial_year_start_year()
assert current_fy == 2016
@freeze_time("2017-03-31 23:00:00.000000")
def test_get_current_financial_year_start_year_after_april():
current_fy = get_current_financial_year_start_year()
assert current_fy == 2017