Merge branch 'master' of https://github.com/alphagov/notifications-api into sms_whitelist

This commit is contained in:
venusbb
2017-07-14 16:40:44 +01:00
39 changed files with 532 additions and 257 deletions

View File

@@ -1,14 +1,21 @@
import pytest
from sqlalchemy.exc import SQLAlchemyError
from app import create_uuid
from app.celery.statistics_tasks import (
record_initial_job_statistics,
record_outcome_job_statistics,
create_initial_notification_statistic_tasks,
create_outcome_notification_statistic_tasks)
from sqlalchemy.exc import SQLAlchemyError
from app import create_uuid
from app.models import (
NOTIFICATION_STATUS_TYPES_COMPLETED,
NOTIFICATION_SENDING,
NOTIFICATION_PENDING,
NOTIFICATION_CREATED,
NOTIFICATION_DELIVERED
)
from tests.app.conftest import sample_notification
from app.models import NOTIFICATION_STATUS_TYPES_COMPLETED, NOTIFICATION_SENT, NOTIFICATION_SENDING, \
NOTIFICATION_PENDING, NOTIFICATION_CREATED, NOTIFICATION_DELIVERED
def test_should_create_initial_job_task_if_notification_is_related_to_a_job(

View File

@@ -11,7 +11,6 @@ from app.models import (
NotificationHistory,
Job,
NotificationStatistics,
TemplateStatistics,
ScheduledNotification,
NOTIFICATION_STATUS_TYPES,
NOTIFICATION_STATUS_TYPES_FAILED,
@@ -588,7 +587,6 @@ def test_create_notification_creates_notification_with_personalisation(notify_db
sample_job, mmg_provider):
assert Notification.query.count() == 0
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
data = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
template=sample_template_with_placeholders,
@@ -612,7 +610,6 @@ def test_create_notification_creates_notification_with_personalisation(notify_db
def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
data = _notification_json(sample_template, job_id=sample_job.id)
@@ -631,10 +628,9 @@ def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider
assert notification_from_db.status == 'created'
def test_save_notification_and_create_email(sample_email_template, sample_job, ses_provider):
def test_save_notification_and_create_email(sample_email_template, sample_job):
assert Notification.query.count() == 0
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
data = _notification_json(sample_email_template, job_id=sample_job.id)
@@ -759,7 +755,6 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
assert Notification.query.count() == 0
assert Job.query.get(sample_job.id).notifications_sent == 0
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
def test_save_notification_and_increment_job(sample_template, sample_job, mmg_provider):

View File

@@ -0,0 +1,71 @@
import pytest
from sqlalchemy.exc import IntegrityError
from app.dao.organisations_dao import (
dao_create_organisation,
dao_get_organisations,
dao_get_organisation_by_id,
dao_update_organisation,
)
from app.models import Organisation
from tests.app.db import create_organisation
def test_create_organisation(notify_db, notify_db_session):
organisation = create_organisation()
assert Organisation.query.count() == 1
organisation_from_db = Organisation.query.first()
assert organisation == organisation_from_db
def test_create_organisation_without_name_or_colour_is_valid(notify_db, notify_db_session):
organisation = create_organisation(name=None, colour=None)
assert Organisation.query.count() == 1
organisation_from_db = Organisation.query.first()
assert organisation == organisation_from_db
def test_create_organisation_without_logo_raises_error(notify_db, notify_db_session):
with pytest.raises(IntegrityError) as excinfo:
create_organisation(logo=None)
assert 'column "logo" violates not-null constraint' in str(excinfo.value)
assert Organisation.query.count() == 0
def test_get_organisations_gets_all_organisations(notify_db, notify_db_session):
org_1 = create_organisation(name='test_org_1')
org_2 = create_organisation(name='test_org_2')
organisations = dao_get_organisations()
assert len(organisations) == 2
assert org_1 == organisations[0]
assert org_2 == organisations[1]
def test_get_organisation_by_id_gets_correct_organisation(notify_db, notify_db_session):
organisation = create_organisation()
organisation_from_db = dao_get_organisation_by_id(organisation.id)
assert organisation_from_db == organisation
def test_update_organisation(notify_db, notify_db_session):
updated_name = 'new name'
organisation = create_organisation()
organisations_1 = Organisation.query.all()
assert len(organisations_1) == 1
assert organisations_1[0].name != updated_name
dao_update_organisation(organisations_1[0], name=updated_name)
organisations_2 = Organisation.query.all()
assert len(organisations_2) == 1
assert organisations_2[0].name == updated_name

View File

@@ -32,7 +32,6 @@ from app.dao.service_permissions_dao import dao_add_service_permission, dao_remo
from app.dao.users_dao import save_model_user
from app.models import (
NotificationStatistics,
TemplateStatistics,
ProviderStatistics,
VerifyCode,
ApiKey,
@@ -55,7 +54,6 @@ from app.models import (
EMAIL_TYPE,
SMS_TYPE,
LETTER_TYPE,
INTERNATIONAL_SMS_TYPE,
SERVICE_PERMISSION_TYPES
)
@@ -433,7 +431,6 @@ def test_delete_service_and_associated_objects(notify_db,
delete_service_and_all_associated_db_objects(sample_service)
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
assert ProviderStatistics.query.count() == 0
assert VerifyCode.query.count() == 0
assert ApiKey.query.count() == 0

View File

@@ -13,6 +13,7 @@ from app.models import (
ServicePermission,
Job,
InboundSms,
Organisation,
EMAIL_TYPE,
SMS_TYPE,
KEY_TYPE_NORMAL,
@@ -23,6 +24,7 @@ from app.dao.templates_dao import dao_create_template
from app.dao.services_dao import dao_create_service
from app.dao.service_permissions_dao import dao_add_service_permission
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.dao.organisations_dao import dao_create_organisation
def create_user(mobile_number="+447700900986", email="notify@digital.cabinet-office.gov.uk", state='active'):
@@ -225,3 +227,15 @@ def create_service_inbound_api(
)
save_service_inbound_api(service_inbound_api)
return service_inbound_api
def create_organisation(colour='blue', logo='test_x2.png', name='test_org_1'):
data = {
'colour': colour,
'logo': logo,
'name': name
}
organisation = Organisation(**data)
dao_create_organisation(organisation)
return organisation

View File

@@ -1,39 +1,113 @@
from flask import json
import pytest
from app.models import Organisation
from tests import create_authorization_header
def test_get_organisations(notify_api, notify_db, notify_db_session):
def test_get_organisations(admin_request, notify_db, notify_db_session):
org1 = Organisation(colour='#FFFFFF', logo='/path/image.png', name='Org1')
org2 = Organisation(colour='#000000', logo='/path/other.png', name='Org2')
notify_db.session.add_all([org1, org2])
notify_db.session.commit()
with notify_api.test_request_context(), notify_api.test_client() as client:
auth_header = create_authorization_header()
response = client.get('/organisation', headers=[auth_header])
organisations = admin_request.get(
'organisation.get_organisations'
)['organisations']
assert response.status_code == 200
organisations = json.loads(response.get_data(as_text=True))['organisations']
assert len(organisations) == 2
assert {org['id'] for org in organisations} == {str(org1.id), str(org2.id)}
def test_get_organisation_by_id(notify_api, notify_db, notify_db_session):
def test_get_organisation_by_id(admin_request, notify_db, notify_db_session):
org = Organisation(colour='#FFFFFF', logo='/path/image.png', name='My Org')
notify_db.session.add(org)
notify_db.session.commit()
with notify_api.test_request_context(), notify_api.test_client() as client:
auth_header = create_authorization_header()
response = client.get('/organisation/{}'.format(org.id), headers=[auth_header])
response = admin_request.get(
'organisation.get_organisation_by_id',
_expected_status=200,
org_id=org.id
)
assert response.status_code == 200
organisation = json.loads(response.get_data(as_text=True))['organisation']
assert set(organisation.keys()) == {'colour', 'logo', 'name', 'id'}
assert organisation['colour'] == '#FFFFFF'
assert organisation['logo'] == '/path/image.png'
assert organisation['name'] == 'My Org'
assert organisation['id'] == str(org.id)
assert set(response['organisation'].keys()) == {'colour', 'logo', 'name', 'id'}
assert response['organisation']['colour'] == '#FFFFFF'
assert response['organisation']['logo'] == '/path/image.png'
assert response['organisation']['name'] == 'My Org'
assert response['organisation']['id'] == str(org.id)
def test_post_create_organisation(admin_request, notify_db_session):
data = {
'name': 'test organisation',
'colour': '#0000ff',
'logo': '/images/test_x2.png'
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201
)
assert data['name'] == response['data']['name']
assert data['colour'] == response['data']['colour']
assert data['logo'] == response['data']['logo']
def test_post_create_organisation_without_logo_raises_error(admin_request, notify_db_session):
data = {
'name': 'test organisation',
'colour': '#0000ff',
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=400
)
assert response['errors'][0]['message'] == "logo is a required property"
def test_post_create_organisation_without_name_or_colour_is_valid(admin_request, notify_db_session):
data = {
'logo': 'images/text_x2.png'
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201
)
assert response['data']['logo'] == data['logo']
assert response['data']['name'] is None
assert response['data']['colour'] is None
@pytest.mark.parametrize('data_update', [
({'name': 'test organisation 1'}),
({'logo': 'images/text_x3.png', 'colour': '#ffffff'}),
])
def test_post_update_organisation_updates_field(admin_request, notify_db_session, data_update):
data = {
'name': 'test organisation',
'logo': 'images/text_x2.png'
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201
)
org_id = response['data']['id']
response = admin_request.post(
'organisation.update_organisation',
_data=data_update,
organisation_id=org_id
)
organisations = Organisation.query.all()
assert len(organisations) == 1
assert str(organisations[0].id) == org_id
for key in data_update.keys():
assert getattr(organisations[0], key) == data_update[key]

View File

@@ -5,7 +5,7 @@ import pytest
from notifications_utils.recipients import InvalidPhoneError
from app.v2.errors import BadRequestError, TooManyRequestsError
from app.config import QueueNames
from app.celery import QueueNames
from app.service.send_notification import send_one_off_notification
from app.models import KEY_TYPE_NORMAL, PRIORITY, SMS_TYPE