2016-01-08 12:18:12 +00:00
|
|
|
import pytest
|
2016-04-28 12:01:27 +01:00
|
|
|
from datetime import (datetime, date)
|
2016-03-31 17:46:18 +01:00
|
|
|
from app import db
|
2016-02-26 12:00:16 +00:00
|
|
|
from app.models import (
|
2016-04-28 12:01:27 +01:00
|
|
|
User,
|
|
|
|
|
Service,
|
|
|
|
|
Template,
|
|
|
|
|
ApiKey,
|
|
|
|
|
Job,
|
|
|
|
|
Notification,
|
|
|
|
|
InvitedUser,
|
|
|
|
|
Permission,
|
2016-05-05 10:45:47 +01:00
|
|
|
ProviderStatistics,
|
2016-05-10 09:13:02 +01:00
|
|
|
ProviderDetails,
|
2016-05-05 10:45:47 +01:00
|
|
|
NotificationStatistics)
|
2016-01-21 17:29:24 +00:00
|
|
|
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
|
2016-03-22 13:14:23 +00:00
|
|
|
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
|
2016-02-24 11:51:02 +00:00
|
|
|
from app.dao.templates_dao import dao_create_template
|
2016-01-19 12:07:00 +00:00
|
|
|
from app.dao.api_key_dao import save_model_api_key
|
2016-02-24 17:12:30 +00:00
|
|
|
from app.dao.jobs_dao import dao_create_job
|
2016-02-25 09:59:50 +00:00
|
|
|
from app.dao.notifications_dao import dao_create_notification
|
2016-02-25 11:22:36 +00:00
|
|
|
from app.dao.invited_user_dao import save_invited_user
|
2016-01-15 12:16:07 +00:00
|
|
|
import uuid
|
2016-01-08 12:18:12 +00:00
|
|
|
|
2016-02-16 17:42:04 +00:00
|
|
|
|
2016-02-16 14:06:56 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def service_factory(notify_db, notify_db_session):
|
|
|
|
|
class ServiceFactory(object):
|
2016-03-31 17:46:18 +01:00
|
|
|
def get(self, service_name, user=None, template_type=None, email_from=None):
|
2016-02-19 15:54:11 +00:00
|
|
|
if not user:
|
|
|
|
|
user = sample_user(notify_db, notify_db_session)
|
2016-03-31 17:46:18 +01:00
|
|
|
service = sample_service(notify_db, notify_db_session, service_name, user, email_from=email_from)
|
2016-02-22 17:17:29 +00:00
|
|
|
if template_type == 'email':
|
|
|
|
|
sample_template(
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template_type=template_type,
|
2016-03-31 17:46:18 +01:00
|
|
|
subject_line=service.email_from,
|
2016-02-22 17:17:29 +00:00
|
|
|
service=service
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
sample_template(
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=service
|
|
|
|
|
)
|
2016-02-16 14:06:56 +00:00
|
|
|
return service
|
|
|
|
|
|
|
|
|
|
return ServiceFactory()
|
|
|
|
|
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_user(notify_db,
|
|
|
|
|
notify_db_session,
|
2016-03-03 12:05:18 +00:00
|
|
|
mobile_numnber="+447700900986",
|
2016-01-08 12:18:12 +00:00
|
|
|
email="notify@digital.cabinet-office.gov.uk"):
|
2016-04-25 16:28:08 +01:00
|
|
|
data = {
|
|
|
|
|
'name': 'Test User',
|
|
|
|
|
'email_address': email,
|
|
|
|
|
'password': 'password',
|
|
|
|
|
'mobile_number': mobile_numnber,
|
|
|
|
|
'state': 'active'
|
|
|
|
|
}
|
|
|
|
|
usr = User.query.filter_by(email_address=email).first()
|
|
|
|
|
if not usr:
|
|
|
|
|
usr = User(**data)
|
|
|
|
|
save_model_user(usr)
|
|
|
|
|
|
2016-01-21 17:29:24 +00:00
|
|
|
return usr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_code(notify_db, notify_db_session, code_type, usr=None, code=None):
|
|
|
|
|
if code is None:
|
|
|
|
|
code = create_secret_code()
|
|
|
|
|
if usr is None:
|
|
|
|
|
usr = sample_user(notify_db, notify_db_session)
|
|
|
|
|
return create_user_code(usr, code, code_type), code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_email_code(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
code=None,
|
|
|
|
|
code_type="email",
|
|
|
|
|
usr=None):
|
|
|
|
|
code, txt_code = create_code(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
code_type,
|
|
|
|
|
usr=usr,
|
|
|
|
|
code=code)
|
|
|
|
|
code.txt_code = txt_code
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_sms_code(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
code=None,
|
|
|
|
|
code_type="sms",
|
|
|
|
|
usr=None):
|
|
|
|
|
code, txt_code = create_code(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
code_type,
|
|
|
|
|
usr=usr,
|
|
|
|
|
code=code)
|
|
|
|
|
code.txt_code = txt_code
|
|
|
|
|
return code
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_service(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service_name="Sample service",
|
2016-03-03 12:05:18 +00:00
|
|
|
user=None,
|
2016-03-09 11:06:37 +00:00
|
|
|
restricted=False,
|
2016-03-31 17:46:18 +01:00
|
|
|
limit=1000,
|
|
|
|
|
email_from="sample.service"):
|
2016-01-08 12:18:12 +00:00
|
|
|
if user is None:
|
|
|
|
|
user = sample_user(notify_db, notify_db_session)
|
2016-01-11 15:07:13 +00:00
|
|
|
data = {
|
|
|
|
|
'name': service_name,
|
2016-04-08 16:13:10 +01:00
|
|
|
'message_limit': limit,
|
2016-01-11 15:07:13 +00:00
|
|
|
'active': False,
|
2016-03-03 12:05:18 +00:00
|
|
|
'restricted': restricted,
|
2016-04-14 15:09:59 +01:00
|
|
|
'email_from': email_from,
|
|
|
|
|
'created_by': user
|
2016-02-19 15:54:11 +00:00
|
|
|
}
|
2016-01-22 14:43:30 +00:00
|
|
|
service = Service.query.filter_by(name=service_name).first()
|
|
|
|
|
if not service:
|
|
|
|
|
service = Service(**data)
|
2016-02-19 15:54:11 +00:00
|
|
|
dao_create_service(service, user)
|
2016-03-22 13:14:23 +00:00
|
|
|
else:
|
|
|
|
|
if user not in service.users:
|
|
|
|
|
dao_add_user_to_service(service, user)
|
2016-01-11 15:07:13 +00:00
|
|
|
return service
|
2016-01-13 11:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_template(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template_name="Template Name",
|
|
|
|
|
template_type="sms",
|
|
|
|
|
content="This is a template",
|
2016-04-25 16:28:08 +01:00
|
|
|
archived=False,
|
2016-04-13 15:31:08 +01:00
|
|
|
subject_line='Subject',
|
2016-04-25 10:38:37 +01:00
|
|
|
user=None,
|
2016-04-25 16:28:08 +01:00
|
|
|
service=None,
|
|
|
|
|
created_by=None):
|
2016-04-25 10:38:37 +01:00
|
|
|
if user is None:
|
|
|
|
|
user = sample_user(notify_db, notify_db_session)
|
2016-01-13 11:04:13 +00:00
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
2016-04-25 16:28:08 +01:00
|
|
|
if created_by is None:
|
|
|
|
|
created_by = sample_user(notify_db, notify_db_session)
|
2016-01-13 11:04:13 +00:00
|
|
|
data = {
|
|
|
|
|
'name': template_name,
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
'content': content,
|
2016-04-25 10:38:37 +01:00
|
|
|
'service': service,
|
2016-04-25 16:28:08 +01:00
|
|
|
'created_by': created_by,
|
|
|
|
|
'archived': archived
|
2016-01-13 11:04:13 +00:00
|
|
|
}
|
2016-02-22 17:17:29 +00:00
|
|
|
if template_type == 'email':
|
|
|
|
|
data.update({
|
|
|
|
|
'subject': subject_line
|
|
|
|
|
})
|
|
|
|
|
template = Template(**data)
|
2016-02-24 11:51:02 +00:00
|
|
|
dao_create_template(template)
|
2016-02-22 17:17:29 +00:00
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 11:23:34 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_template_with_placeholders(notify_db, notify_db_session):
|
|
|
|
|
return sample_template(notify_db, notify_db_session, content="Hello ((name))")
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_email_template(
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template_name="Email Template Name",
|
|
|
|
|
template_type="email",
|
2016-04-25 10:38:37 +01:00
|
|
|
user=None,
|
2016-02-22 17:17:29 +00:00
|
|
|
content="This is a template",
|
|
|
|
|
subject_line='Email Subject',
|
|
|
|
|
service=None):
|
2016-04-25 10:38:37 +01:00
|
|
|
if user is None:
|
|
|
|
|
user = sample_user(notify_db, notify_db_session)
|
2016-02-22 17:17:29 +00:00
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
data = {
|
|
|
|
|
'name': template_name,
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
'content': content,
|
2016-04-25 10:38:37 +01:00
|
|
|
'service': service,
|
|
|
|
|
'created_by': user
|
2016-02-22 17:17:29 +00:00
|
|
|
}
|
2016-02-19 15:54:11 +00:00
|
|
|
if subject_line:
|
|
|
|
|
data.update({
|
|
|
|
|
'subject': subject_line
|
|
|
|
|
})
|
2016-01-13 11:04:13 +00:00
|
|
|
template = Template(**data)
|
2016-02-24 11:51:02 +00:00
|
|
|
dao_create_template(template)
|
2016-01-13 11:04:13 +00:00
|
|
|
return template
|
2016-01-14 11:30:45 +00:00
|
|
|
|
|
|
|
|
|
2016-02-29 14:43:44 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_email_template_with_placeholders(notify_db, notify_db_session):
|
2016-04-13 15:31:08 +01:00
|
|
|
return sample_email_template(
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
content="Hello ((name))",
|
|
|
|
|
subject_line="((name))")
|
2016-02-29 14:43:44 +00:00
|
|
|
|
|
|
|
|
|
2016-01-14 11:30:45 +00:00
|
|
|
@pytest.fixture(scope='function')
|
2016-01-19 12:07:00 +00:00
|
|
|
def sample_api_key(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None):
|
2016-01-14 11:30:45 +00:00
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
2016-04-20 17:25:20 +01:00
|
|
|
data = {'service': service, 'name': uuid.uuid4(), 'created_by': service.created_by}
|
2016-01-19 12:07:00 +00:00
|
|
|
api_key = ApiKey(**data)
|
|
|
|
|
save_model_api_key(api_key)
|
|
|
|
|
return api_key
|
2016-01-15 12:16:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_job(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None,
|
2016-03-09 14:52:07 +00:00
|
|
|
template=None,
|
2016-03-14 16:31:57 +00:00
|
|
|
notification_count=1,
|
|
|
|
|
created_at=datetime.utcnow()):
|
2016-01-15 12:16:07 +00:00
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
if template is None:
|
|
|
|
|
template = sample_template(notify_db, notify_db_session,
|
|
|
|
|
service=service)
|
|
|
|
|
data = {
|
|
|
|
|
'id': uuid.uuid4(),
|
|
|
|
|
'service_id': service.id,
|
2016-04-07 13:44:04 +01:00
|
|
|
'service': service,
|
2016-01-15 12:16:07 +00:00
|
|
|
'template_id': template.id,
|
2016-05-13 16:25:05 +01:00
|
|
|
'template_version': template.version,
|
2016-02-22 14:56:09 +00:00
|
|
|
'original_file_name': 'some.csv',
|
2016-03-14 16:31:57 +00:00
|
|
|
'notification_count': notification_count,
|
2016-04-26 16:15:34 +01:00
|
|
|
'created_at': created_at,
|
|
|
|
|
'created_by': service.created_by
|
2016-01-15 12:16:07 +00:00
|
|
|
}
|
|
|
|
|
job = Job(**data)
|
2016-02-24 17:12:30 +00:00
|
|
|
dao_create_job(job)
|
2016-01-15 12:16:07 +00:00
|
|
|
return job
|
2016-01-15 16:22:03 +00:00
|
|
|
|
2016-01-15 17:11:02 +00:00
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_job_with_placeholdered_template(
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None
|
|
|
|
|
):
|
|
|
|
|
return sample_job(
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=service,
|
|
|
|
|
template=sample_template_with_placeholders(notify_db, notify_db_session)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_email_job(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None,
|
|
|
|
|
template=None):
|
|
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
if template is None:
|
|
|
|
|
template = sample_email_template(
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=service)
|
|
|
|
|
job_id = uuid.uuid4()
|
|
|
|
|
data = {
|
|
|
|
|
'id': uuid.uuid4(),
|
|
|
|
|
'service_id': service.id,
|
2016-04-07 13:44:04 +01:00
|
|
|
'service': service,
|
2016-02-25 09:59:50 +00:00
|
|
|
'template_id': template.id,
|
2016-05-13 16:25:05 +01:00
|
|
|
'template_version': template.version,
|
2016-02-25 09:59:50 +00:00
|
|
|
'original_file_name': 'some.csv',
|
2016-04-26 16:15:34 +01:00
|
|
|
'notification_count': 1,
|
|
|
|
|
'created_by': service.created_by
|
2016-02-25 09:59:50 +00:00
|
|
|
}
|
|
|
|
|
job = Job(**data)
|
|
|
|
|
dao_create_job(job)
|
|
|
|
|
return job
|
|
|
|
|
|
|
|
|
|
|
2016-01-27 11:51:02 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def mock_secret_code(mocker):
|
|
|
|
|
def _create():
|
|
|
|
|
return '11111'
|
|
|
|
|
|
|
|
|
|
mock_class = mocker.patch('app.dao.users_dao.create_secret_code', side_effect=_create)
|
|
|
|
|
return mock_class
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_notification(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None,
|
|
|
|
|
template=None,
|
2016-03-01 14:58:27 +00:00
|
|
|
job=None,
|
2016-05-19 10:46:03 +01:00
|
|
|
job_row_number=None,
|
2016-03-09 17:46:01 +00:00
|
|
|
to_field=None,
|
2016-04-08 16:13:10 +01:00
|
|
|
status='sending',
|
2016-03-11 10:19:40 +00:00
|
|
|
reference=None,
|
2016-04-21 11:37:38 +01:00
|
|
|
created_at=datetime.utcnow(),
|
|
|
|
|
provider_name=None,
|
|
|
|
|
content_char_count=160,
|
|
|
|
|
create=True):
|
2016-02-09 12:01:17 +00:00
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
if template is None:
|
|
|
|
|
template = sample_template(notify_db, notify_db_session, service=service)
|
|
|
|
|
if job is None:
|
|
|
|
|
job = sample_job(notify_db, notify_db_session, service=service, template=template)
|
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
notification_id = uuid.uuid4()
|
2016-03-01 14:58:27 +00:00
|
|
|
|
2016-04-21 11:37:38 +01:00
|
|
|
if provider_name is None:
|
2016-05-06 09:09:47 +01:00
|
|
|
provider = mmg_provider() if template.template_type == 'sms' else ses_provider()
|
2016-04-21 11:37:38 +01:00
|
|
|
|
2016-03-01 14:58:27 +00:00
|
|
|
if to_field:
|
|
|
|
|
to = to_field
|
|
|
|
|
else:
|
2016-03-06 12:58:28 +00:00
|
|
|
to = '+447700900855'
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
data = {
|
2016-03-01 13:30:10 +00:00
|
|
|
'id': notification_id,
|
2016-02-09 12:01:17 +00:00
|
|
|
'to': to,
|
2016-02-09 12:48:27 +00:00
|
|
|
'job': job,
|
2016-03-08 15:23:19 +00:00
|
|
|
'service_id': service.id,
|
2016-02-09 12:48:27 +00:00
|
|
|
'service': service,
|
2016-02-25 09:59:50 +00:00
|
|
|
'template': template,
|
2016-05-11 17:04:51 +01:00
|
|
|
'template_version': template.version,
|
2016-03-09 17:46:01 +00:00
|
|
|
'status': status,
|
2016-03-11 10:19:40 +00:00
|
|
|
'reference': reference,
|
2016-04-21 11:37:38 +01:00
|
|
|
'created_at': created_at,
|
|
|
|
|
'content_char_count': content_char_count
|
2016-02-09 12:01:17 +00:00
|
|
|
}
|
2016-05-19 10:46:03 +01:00
|
|
|
if job_row_number:
|
|
|
|
|
data['job_row_number'] = job_row_number
|
2016-02-09 12:01:17 +00:00
|
|
|
notification = Notification(**data)
|
2016-04-21 11:37:38 +01:00
|
|
|
if create:
|
2016-05-06 09:09:47 +01:00
|
|
|
dao_create_notification(notification, template.template_type, provider.identifier)
|
2016-02-09 12:01:17 +00:00
|
|
|
return notification
|
2016-02-17 15:41:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def mock_celery_send_sms_code(mocker):
|
|
|
|
|
return mocker.patch('app.celery.tasks.send_sms_code.apply_async')
|
2016-02-17 17:48:23 +00:00
|
|
|
|
|
|
|
|
|
2016-03-17 13:06:17 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def mock_celery_email_registration_verification(mocker):
|
|
|
|
|
return mocker.patch('app.celery.tasks.email_registration_verification.apply_async')
|
|
|
|
|
|
|
|
|
|
|
2016-04-01 11:12:44 +01:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def mock_celery_send_email(mocker):
|
|
|
|
|
return mocker.patch('app.celery.tasks.send_email.apply_async')
|
|
|
|
|
|
|
|
|
|
|
2016-02-18 09:52:27 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def mock_encryption(mocker):
|
|
|
|
|
return mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:28:19 +01:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def mock_celery_remove_job(mocker):
|
|
|
|
|
return mocker.patch('app.celery.tasks.remove_job.apply_async')
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 11:22:36 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_invited_user(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None,
|
|
|
|
|
to_email_address=None):
|
|
|
|
|
|
|
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
if to_email_address is None:
|
|
|
|
|
to_email_address = 'invited_user@digital.gov.uk'
|
|
|
|
|
|
|
|
|
|
from_user = service.users[0]
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'service': service,
|
|
|
|
|
'email_address': to_email_address,
|
2016-02-29 09:49:12 +00:00
|
|
|
'from_user': from_user,
|
|
|
|
|
'permissions': 'send_messages,manage_service,manage_api_keys'
|
2016-02-25 11:22:36 +00:00
|
|
|
}
|
|
|
|
|
invited_user = InvitedUser(**data)
|
|
|
|
|
save_invited_user(invited_user)
|
|
|
|
|
return invited_user
|
2016-02-26 12:00:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_permission(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None,
|
|
|
|
|
user=None,
|
2016-03-02 15:34:26 +00:00
|
|
|
permission="manage_settings"):
|
2016-02-26 12:00:16 +00:00
|
|
|
if user is None:
|
|
|
|
|
user = sample_user(notify_db, notify_db_session)
|
|
|
|
|
data = {
|
|
|
|
|
'user': user,
|
|
|
|
|
'permission': permission
|
|
|
|
|
}
|
2016-05-06 11:07:11 +01:00
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
2016-02-26 12:00:16 +00:00
|
|
|
if service:
|
|
|
|
|
data['service'] = service
|
2016-03-02 11:10:52 +00:00
|
|
|
p_model = Permission.query.filter_by(
|
|
|
|
|
user=user,
|
|
|
|
|
service=service,
|
|
|
|
|
permission=permission).first()
|
|
|
|
|
if not p_model:
|
|
|
|
|
p_model = Permission(**data)
|
|
|
|
|
db.session.add(p_model)
|
|
|
|
|
db.session.commit()
|
2016-02-26 12:00:16 +00:00
|
|
|
return p_model
|
2016-02-26 15:57:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_service_permission(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None,
|
|
|
|
|
user=None,
|
2016-03-02 15:34:26 +00:00
|
|
|
permission="manage_settings"):
|
2016-02-26 15:57:24 +00:00
|
|
|
if user is None:
|
|
|
|
|
user = sample_user(notify_db, notify_db_session)
|
|
|
|
|
if service is None:
|
2016-03-22 13:14:23 +00:00
|
|
|
service = sample_service(notify_db, notify_db_session, user=user)
|
2016-02-26 15:57:24 +00:00
|
|
|
data = {
|
|
|
|
|
'user': user,
|
|
|
|
|
'service': service,
|
|
|
|
|
'permission': permission
|
|
|
|
|
}
|
2016-03-02 11:10:52 +00:00
|
|
|
p_model = Permission.query.filter_by(
|
|
|
|
|
user=user,
|
|
|
|
|
service=service,
|
|
|
|
|
permission=permission).first()
|
|
|
|
|
if not p_model:
|
|
|
|
|
p_model = Permission(**data)
|
|
|
|
|
db.session.add(p_model)
|
|
|
|
|
db.session.commit()
|
2016-02-26 15:57:24 +00:00
|
|
|
return p_model
|
2016-04-08 13:34:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def fake_uuid():
|
|
|
|
|
return "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"
|
2016-04-21 11:37:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-05-06 09:09:47 +01:00
|
|
|
def ses_provider():
|
|
|
|
|
return ProviderDetails.query.filter_by(identifier='ses').one()
|
2016-04-21 11:37:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-05-06 09:09:47 +01:00
|
|
|
def firetext_provider():
|
|
|
|
|
return ProviderDetails.query.filter_by(identifier='mmg').one()
|
2016-04-28 12:01:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-05-06 09:09:47 +01:00
|
|
|
def mmg_provider():
|
|
|
|
|
return ProviderDetails.query.filter_by(identifier='mmg').one()
|
2016-04-28 12:01:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_provider_statistics(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_service,
|
|
|
|
|
provider=None,
|
|
|
|
|
day=None,
|
|
|
|
|
unit_count=1):
|
2016-05-06 09:09:47 +01:00
|
|
|
|
2016-04-28 12:01:27 +01:00
|
|
|
if provider is None:
|
2016-05-06 09:09:47 +01:00
|
|
|
provider = ProviderDetails.query.filter_by(identifier='mmg').first()
|
2016-04-28 12:01:27 +01:00
|
|
|
if day is None:
|
|
|
|
|
day = date.today()
|
|
|
|
|
stats = ProviderStatistics(
|
|
|
|
|
service=sample_service,
|
2016-05-06 09:09:47 +01:00
|
|
|
provider_id=provider.id,
|
2016-04-28 12:01:27 +01:00
|
|
|
day=day,
|
|
|
|
|
unit_count=unit_count)
|
|
|
|
|
notify_db.session.add(stats)
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
return stats
|
2016-05-05 10:45:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def sample_notification_statistics(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=None,
|
|
|
|
|
day=None,
|
|
|
|
|
emails_requested=2,
|
|
|
|
|
emails_delivered=1,
|
|
|
|
|
emails_failed=1,
|
|
|
|
|
sms_requested=2,
|
|
|
|
|
sms_delivered=1,
|
|
|
|
|
sms_failed=1):
|
|
|
|
|
if service is None:
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
if day is None:
|
|
|
|
|
day = date.today()
|
|
|
|
|
stats = NotificationStatistics(
|
|
|
|
|
service=service,
|
|
|
|
|
day=day,
|
|
|
|
|
emails_requested=emails_requested,
|
|
|
|
|
emails_delivered=emails_delivered,
|
|
|
|
|
emails_failed=emails_failed,
|
|
|
|
|
sms_requested=sms_requested,
|
|
|
|
|
sms_delivered=sms_delivered,
|
|
|
|
|
sms_failed=sms_failed)
|
|
|
|
|
notify_db.session.add(stats)
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
return stats
|