Merge branch 'master' into rate-limit-api-calls

This commit is contained in:
Martyn Inglis
2017-04-25 14:27:05 +01:00
21 changed files with 344 additions and 84 deletions

View File

@@ -1038,15 +1038,14 @@ def test_create_dvla_file_contents(sample_letter_template, mocker):
assert calls[1][1]['contact_block'] == 'London,\nSW1A 1AA'
assert calls[0][1]['notification_reference'] == '1'
assert calls[1][1]['notification_reference'] == '2'
assert calls[1][1]['org_id'] == '001'
@freeze_time("2017-03-23 11:09:00.061258")
def test_dvla_letter_template(sample_letter_notification):
t = {"content": sample_letter_notification.template.content,
"subject": sample_letter_notification.template.subject}
letter = LetterDVLATemplate(t,
sample_letter_notification.personalisation,
"random-string")
letter = LetterDVLATemplate(t, sample_letter_notification.personalisation, "random-string")
assert str(letter) == "140|500|001||random-string|||||||||||||A1||A2|A3|A4|A5|A6|A_POST|||||||||23 March 2017<cr><cr><h1>Template subject<normal><cr><cr>Dear Sir/Madam, Hello. Yours Truly, The Government.<cr><cr>" # noqa

View File

@@ -273,3 +273,12 @@ def test_get_sms_provider_with_equal_priority_returns_provider(
dao_get_sms_provider_with_equal_priority(current_provider.identifier, current_provider.priority)
assert conflicting_provider
def test_get_current_sms_provider_returns_active_only(restore_provider_details):
current_provider = get_current_provider('sms')
current_provider.active = False
dao_update_provider_details(current_provider)
new_current_provider = get_current_provider('sms')
assert current_provider.identifier != new_current_provider.identifier

View File

@@ -0,0 +1,18 @@
from datetime import datetime
from decimal import Decimal
from app.dao.rates_dao import get_rate_for_type_and_date
def test_get_rate_for_type_and_date(notify_db):
rate = get_rate_for_type_and_date('sms', datetime.utcnow())
assert rate.rate == Decimal("1.58")
rate = get_rate_for_type_and_date('sms', datetime(2016, 6, 1))
assert rate.rate == Decimal("1.65")
def test_get_rate_for_type_and_date_early_date(notify_db):
rate = get_rate_for_type_and_date('sms', datetime(2014, 6, 1))
assert not rate

View File

@@ -43,6 +43,7 @@ from app.models import (
InvitedUser,
Service,
BRANDING_GOVUK,
DVLA_ORG_HM_GOVERNMENT,
KEY_TYPE_NORMAL,
KEY_TYPE_TEAM,
KEY_TYPE_TEST
@@ -77,6 +78,7 @@ def test_create_service(sample_user):
assert service_db.name == "service_name"
assert service_db.id == service.id
assert service_db.branding == BRANDING_GOVUK
assert service_db.dvla_organisation_id == DVLA_ORG_HM_GOVERNMENT
assert service_db.research_mode is False
assert service.active is True
assert sample_user in service_db.users
@@ -263,7 +265,9 @@ def test_create_service_creates_a_history_record_with_current_data(sample_user):
assert sample_user.id == service_history.created_by_id
assert service_from_db.created_by.id == service_history.created_by_id
assert service_from_db.branding == BRANDING_GOVUK
assert service_from_db.dvla_organisation_id == DVLA_ORG_HM_GOVERNMENT
assert service_history.branding == BRANDING_GOVUK
assert service_history.dvla_organisation_id == DVLA_ORG_HM_GOVERNMENT
def test_update_service_creates_a_history_record_with_current_data(sample_user):

View File

View File

@@ -0,0 +1,13 @@
from flask import json
from tests import create_authorization_header
def test_get_dvla_organisations(client):
auth_header = create_authorization_header()
response = client.get('/dvla_organisations', headers=[auth_header])
assert response.status_code == 200
dvla_organisations = json.loads(response.get_data(as_text=True))
assert dvla_organisations == {'001': 'HM Government', '500': 'Land Registry'}

View File

@@ -10,7 +10,7 @@ from freezegun import freeze_time
from app.dao.users_dao import save_model_user
from app.dao.services_dao import dao_remove_user_from_service
from app.models import User, Organisation
from app.models import User, Organisation, DVLA_ORG_LAND_REGISTRY
from tests import create_authorization_header
from tests.app.conftest import (
sample_service as create_service,
@@ -129,21 +129,20 @@ def test_get_service_list_should_return_empty_list_if_no_services(notify_api, no
assert len(json_resp['data']) == 0
def test_get_service_by_id(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header()
resp = client.get(
'/service/{}'.format(sample_service.id),
headers=[auth_header]
)
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == sample_service.name
assert json_resp['data']['id'] == str(sample_service.id)
assert not json_resp['data']['research_mode']
assert json_resp['data']['organisation'] is None
assert json_resp['data']['branding'] == 'govuk'
def test_get_service_by_id(client, sample_service):
auth_header = create_authorization_header()
resp = client.get(
'/service/{}'.format(sample_service.id),
headers=[auth_header]
)
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == sample_service.name
assert json_resp['data']['id'] == str(sample_service.id)
assert not json_resp['data']['research_mode']
assert json_resp['data']['organisation'] is None
assert json_resp['data']['branding'] == 'govuk'
assert json_resp['data']['dvla_organisation'] == '001'
def test_get_service_by_id_should_404_if_no_service(notify_api, notify_db):
@@ -191,41 +190,40 @@ def test_get_service_by_id_should_404_if_no_service_for_user(notify_api, sample_
assert json_resp['message'] == 'No result found'
def test_create_service(notify_api, sample_user):
with notify_api.test_request_context():
with notify_api.test_client() as client:
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)}
auth_header = create_authorization_header()
headers = [('Content-Type', 'application/json'), auth_header]
resp = client.post(
'/service',
data=json.dumps(data),
headers=headers)
json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 201
assert json_resp['data']['id']
assert json_resp['data']['name'] == 'created service'
assert json_resp['data']['email_from'] == 'created.service'
assert not json_resp['data']['research_mode']
def test_create_service(client, 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)}
auth_header = create_authorization_header()
headers = [('Content-Type', 'application/json'), auth_header]
resp = client.post(
'/service',
data=json.dumps(data),
headers=headers)
json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 201
assert json_resp['data']['id']
assert json_resp['data']['name'] == 'created service'
assert json_resp['data']['email_from'] == 'created.service'
assert not json_resp['data']['research_mode']
assert json_resp['data']['dvla_organisation'] == '001'
auth_header_fetch = create_authorization_header()
auth_header_fetch = create_authorization_header()
resp = client.get(
'/service/{}?user_id={}'.format(json_resp['data']['id'], sample_user.id),
headers=[auth_header_fetch]
)
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == 'created service'
assert not json_resp['data']['research_mode']
assert not json_resp['data']['can_send_letters']
resp = client.get(
'/service/{}?user_id={}'.format(json_resp['data']['id'], sample_user.id),
headers=[auth_header_fetch]
)
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == 'created service'
assert not json_resp['data']['research_mode']
assert not json_resp['data']['can_send_letters']
def test_should_not_create_service_with_missing_user_id_field(notify_api, fake_uuid):
@@ -371,41 +369,41 @@ def test_create_service_should_throw_duplicate_key_constraint_for_existing_email
assert "Duplicate service name '{}'".format(service_name) in json_resp['message']['name']
def test_update_service(notify_api, notify_db, sample_service):
def test_update_service(client, notify_db, sample_service):
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
notify_db.session.add(org)
notify_db.session.commit()
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header()
resp = client.get(
'/service/{}'.format(sample_service.id),
headers=[auth_header]
)
json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 200
assert json_resp['data']['name'] == sample_service.name
auth_header = create_authorization_header()
resp = client.get(
'/service/{}'.format(sample_service.id),
headers=[auth_header]
)
json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 200
assert json_resp['data']['name'] == sample_service.name
data = {
'name': 'updated service name',
'email_from': 'updated.service.name',
'created_by': str(sample_service.created_by.id),
'organisation': str(org.id)
}
data = {
'name': 'updated service name',
'email_from': 'updated.service.name',
'created_by': str(sample_service.created_by.id),
'organisation': str(org.id),
'dvla_organisation': DVLA_ORG_LAND_REGISTRY
}
auth_header = create_authorization_header()
auth_header = create_authorization_header()
resp = client.post(
'/service/{}'.format(sample_service.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]
)
result = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 200
assert result['data']['name'] == 'updated service name'
assert result['data']['email_from'] == 'updated.service.name'
assert result['data']['organisation'] == str(org.id)
resp = client.post(
'/service/{}'.format(sample_service.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]
)
result = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 200
assert result['data']['name'] == 'updated service name'
assert result['data']['email_from'] == 'updated.service.name'
assert result['data']['organisation'] == str(org.id)
assert result['data']['dvla_organisation'] == DVLA_ORG_LAND_REGISTRY
def test_update_service_flags(notify_api, sample_service):