Files
notifications-api/tests/app/conftest.py
Rebecca Law b5c662eca8 Change services.id to a UUID
Ideally all the primary keys in the db would be UUID in order to guarantee unique ids across distributed dbs.
This updates the services.id to a UUID. All the tables with a foreign key to the services.id are also updated.
The endpoints no longer state a data type of the <service_id> path param.
All the tests are updated to reflect this update.

The thing to pay attention to is the 0011_uuid_service_id.py migration script.
This commit must go with a commit on the notifications_admin app to keep things working.
There will be a small outage until both deploys have happened.
2016-02-02 14:22:22 +00:00

183 lines
5.6 KiB
Python

import pytest
from flask import jsonify
from app.models import (User, Service, Template, ApiKey, Job, VerifyCode)
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
from app.dao.services_dao import save_model_service
from app.dao.templates_dao import save_model_template
from app.dao.api_key_dao import save_model_api_key
from app.dao.jobs_dao import save_job
import uuid
@pytest.fixture(scope='function')
def sample_user(notify_db,
notify_db_session,
email="notify@digital.cabinet-office.gov.uk"):
data = {
'name': 'Test User',
'email_address': email,
'password': 'password',
'mobile_number': '+447700900986',
'state': 'active'
}
usr = User.query.filter_by(email_address=email).first()
if not usr:
usr = User(**data)
save_model_user(usr)
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
@pytest.fixture(scope='function')
def sample_service(notify_db,
notify_db_session,
service_name="Sample service",
user=None):
if user is None:
user = sample_user(notify_db, notify_db_session)
data = {
'name': service_name,
'users': [user],
'limit': 1000,
'active': False,
'restricted': False}
service = Service.query.filter_by(name=service_name).first()
if not service:
service = Service(**data)
save_model_service(service)
return service
@pytest.fixture(scope='function')
def sample_template(notify_db,
notify_db_session,
template_name="Template Name",
template_type="sms",
content="This is a template",
service=None):
if service is None:
service = sample_service(notify_db, notify_db_session)
sample_api_key(notify_db, notify_db_session, service=service)
data = {
'name': template_name,
'template_type': template_type,
'content': content,
'service': service
}
template = Template(**data)
save_model_template(template)
return template
@pytest.fixture(scope='function')
def sample_api_key(notify_db,
notify_db_session,
service=None):
if service is None:
service = sample_service(notify_db, notify_db_session)
data = {'service_id': service.id, 'name': uuid.uuid4()}
api_key = ApiKey(**data)
save_model_api_key(api_key)
return api_key
@pytest.fixture(scope='function')
def sample_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_template(notify_db, notify_db_session,
service=service)
job_id = uuid.uuid4()
bucket_name = 'service-{}-notify'.format(service.id)
file_name = '{}.csv'.format(job_id)
data = {
'id': uuid.uuid4(),
'service_id': service.id,
'template_id': template.id,
'bucket_name': bucket_name,
'file_name': file_name,
'original_file_name': 'some.csv'
}
job = Job(**data)
save_job(job)
return job
@pytest.fixture(scope='function')
def sample_admin_service_id(notify_db, notify_db_session):
admin_user = sample_user(notify_db, notify_db_session, email="notify_admin@digital.cabinet-office.gov.uk")
admin_service = sample_service(notify_db, notify_db_session, service_name="Sample Admin Service", user=admin_user)
data = {'service': admin_service, 'name': 'sample admin key'}
api_key = ApiKey(**data)
save_model_api_key(api_key)
return admin_service.id
@pytest.fixture(scope='function')
def mock_notify_client_send_sms(mocker):
def _send(mobile_number, message):
pass
mock_class = mocker.patch('app.notify_alpha_client.send_sms', side_effect=_send)
return mock_class
@pytest.fixture(scope='function')
def mock_notify_client_send_email(mocker):
def _send(email_address, message, from_address, subject):
pass
mock_class = mocker.patch('app.notify_alpha_client.send_email', side_effect=_send)
return mock_class
@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