2016-01-08 17:51:46 +00:00
|
|
|
import json
|
2016-02-02 14:16:08 +00:00
|
|
|
import uuid
|
2016-02-26 17:11:30 +00:00
|
|
|
from flask import url_for
|
2016-02-19 15:53:45 +00:00
|
|
|
from app.dao.users_dao import save_model_user
|
2016-02-25 12:11:51 +00:00
|
|
|
from app.dao.services_dao import dao_remove_user_from_service
|
2016-02-22 12:55:18 +00:00
|
|
|
from app.models import User
|
2016-01-15 16:22:03 +00:00
|
|
|
from tests import create_authorization_header
|
2016-01-08 17:51:46 +00:00
|
|
|
|
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
def test_get_service_list(notify_api, service_factory):
|
2016-01-08 17:51:46 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-19 15:53:45 +00:00
|
|
|
service_factory.get('one')
|
|
|
|
|
service_factory.get('two')
|
|
|
|
|
service_factory.get('three')
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
response = client.get(
|
|
|
|
|
'/service',
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
2016-01-08 17:51:46 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-02-19 15:53:45 +00:00
|
|
|
assert len(json_resp['data']) == 3
|
|
|
|
|
assert json_resp['data'][0]['name'] == 'one'
|
|
|
|
|
assert json_resp['data'][1]['name'] == 'two'
|
|
|
|
|
assert json_resp['data'][2]['name'] == 'three'
|
2016-01-08 17:51:46 +00:00
|
|
|
|
|
|
|
|
|
2016-02-25 12:11:51 +00:00
|
|
|
def test_get_service_list_by_user(notify_api, sample_user, service_factory):
|
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
service_factory.get('one', sample_user)
|
|
|
|
|
service_factory.get('two', sample_user)
|
|
|
|
|
service_factory.get('three', sample_user)
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
response = client.get(
|
|
|
|
|
'/service?user_id='.format(sample_user.id),
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert len(json_resp['data']) == 3
|
|
|
|
|
assert json_resp['data'][0]['name'] == 'one'
|
|
|
|
|
assert json_resp['data'][1]['name'] == 'two'
|
|
|
|
|
assert json_resp['data'][2]['name'] == 'three'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_service_list_by_user_should_return_empty_list_if_no_services(notify_api, service_factory, sample_user):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
new_user = User(
|
|
|
|
|
name='Test User',
|
|
|
|
|
email_address='new_user@digital.cabinet-office.gov.uk',
|
|
|
|
|
password='password',
|
|
|
|
|
mobile_number='+447700900986'
|
|
|
|
|
)
|
|
|
|
|
save_model_user(new_user)
|
|
|
|
|
|
|
|
|
|
service_factory.get('one', sample_user)
|
|
|
|
|
service_factory.get('two', sample_user)
|
|
|
|
|
service_factory.get('three', sample_user)
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
response = client.get(
|
|
|
|
|
'/service?user_id={}'.format(new_user.id),
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert len(json_resp['data']) == 0
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 12:11:51 +00:00
|
|
|
def test_get_service_list_should_return_empty_list_if_no_services(notify_api, notify_db, notify_db_session):
|
2016-02-19 15:53:45 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
response = client.get(
|
|
|
|
|
'/service',
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert len(json_resp['data']) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_service_by_id(notify_api, sample_service):
|
2016-01-08 17:51:46 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-19 15:53:45 +00:00
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(sample_service.id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}'.format(sample_service.id),
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
2016-01-08 17:51:46 +00:00
|
|
|
assert resp.status_code == 200
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert json_resp['data']['name'] == sample_service.name
|
2016-02-02 14:16:08 +00:00
|
|
|
assert json_resp['data']['id'] == str(sample_service.id)
|
2016-01-08 17:51:46 +00:00
|
|
|
|
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
def test_get_service_by_id_should_404_if_no_service(notify_api, notify_db):
|
2016-01-22 12:47:59 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-19 15:53:45 +00:00
|
|
|
service_id = str(uuid.uuid4())
|
2016-01-22 12:47:59 +00:00
|
|
|
auth_header = create_authorization_header(
|
2016-02-19 15:53:45 +00:00
|
|
|
path='/service/{}'.format(service_id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}'.format(service_id),
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 404
|
2016-01-22 12:47:59 +00:00
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2016-02-19 15:53:45 +00:00
|
|
|
assert json_resp['result'] == 'error'
|
2016-02-25 12:11:51 +00:00
|
|
|
assert json_resp['message'] == 'Service not found for service id: {} '.format(service_id)
|
2016-01-22 12:47:59 +00:00
|
|
|
|
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
def test_get_service_by_id_and_user(notify_api, service_factory, sample_user):
|
2016-01-11 15:07:13 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-19 15:53:45 +00:00
|
|
|
service = service_factory.get('new service', sample_user)
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(service.id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}?user_id={}'.format(service.id, sample_user.id),
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 200
|
2016-01-11 15:07:13 +00:00
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert json_resp['data']['name'] == service.name
|
2016-02-19 15:53:45 +00:00
|
|
|
assert json_resp['data']['id'] == str(service.id)
|
2016-01-08 17:51:46 +00:00
|
|
|
|
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
def test_get_service_by_id_should_404_if_no_service_for_user(notify_api, sample_user):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
service_id = str(uuid.uuid4())
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(service_id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}?user_id={}'.format(service_id, sample_user.id),
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 404
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert json_resp['result'] == 'error'
|
2016-02-25 12:11:51 +00:00
|
|
|
assert json_resp['message'] == \
|
|
|
|
|
'Service not found for service id: {0} and for user id: {1}'.format(service_id, sample_user.id)
|
2016-02-19 15:53:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_service(notify_api, sample_user):
|
2016-01-12 09:28:01 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'created service',
|
2016-02-19 15:53:45 +00:00
|
|
|
'user_id': sample_user.id,
|
2016-01-12 09:28:01 +00:00
|
|
|
'limit': 1000,
|
|
|
|
|
'restricted': False,
|
|
|
|
|
'active': False}
|
2016-02-19 15:53:45 +00:00
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='POST',
|
|
|
|
|
request_body=json.dumps(data)
|
|
|
|
|
)
|
2016-01-15 16:22:03 +00:00
|
|
|
headers = [('Content-Type', 'application/json'), auth_header]
|
2016-01-12 09:28:01 +00:00
|
|
|
resp = client.post(
|
2016-02-19 15:53:45 +00:00
|
|
|
'/service',
|
2016-01-12 09:28:01 +00:00
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=headers)
|
2016-02-19 15:53:45 +00:00
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2016-01-12 09:28:01 +00:00
|
|
|
assert resp.status_code == 201
|
2016-02-19 15:53:45 +00:00
|
|
|
assert json_resp['data']['id']
|
|
|
|
|
assert json_resp['data']['name'] == 'created service'
|
2016-02-19 17:07:59 +00:00
|
|
|
assert json_resp['data']['email_from'] == 'created.service'
|
2016-02-19 15:53:45 +00:00
|
|
|
|
|
|
|
|
auth_header_fetch = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(json_resp['data']['id']),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}?user_id={}'.format(json_resp['data']['id'], sample_user.id),
|
|
|
|
|
headers=[auth_header_fetch]
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 200
|
2016-01-12 09:28:01 +00:00
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2016-02-19 15:53:45 +00:00
|
|
|
assert json_resp['data']['name'] == 'created service'
|
2016-01-12 09:28:01 +00:00
|
|
|
|
2016-01-12 09:56:42 +00:00
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
def test_should_not_create_service_with_missing_user_id_field(notify_api):
|
2016-01-12 09:28:01 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
data = {
|
2016-02-19 15:53:45 +00:00
|
|
|
'email_from': 'service',
|
2016-01-12 09:28:01 +00:00
|
|
|
'name': 'created service',
|
|
|
|
|
'limit': 1000,
|
|
|
|
|
'restricted': False,
|
2016-02-19 15:53:45 +00:00
|
|
|
'active': False
|
|
|
|
|
}
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='POST',
|
|
|
|
|
request_body=json.dumps(data)
|
|
|
|
|
)
|
2016-01-15 16:22:03 +00:00
|
|
|
headers = [('Content-Type', 'application/json'), auth_header]
|
2016-01-12 09:28:01 +00:00
|
|
|
resp = client.post(
|
2016-02-19 15:53:45 +00:00
|
|
|
'/service',
|
2016-01-12 09:28:01 +00:00
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=headers)
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2016-02-19 15:53:45 +00:00
|
|
|
assert resp.status_code == 400
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
assert 'Missing data for required field.' in json_resp['message']['user_id']
|
2016-01-12 09:28:01 +00:00
|
|
|
|
|
|
|
|
|
2016-02-25 12:11:51 +00:00
|
|
|
def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(notify_api, notify_db, notify_db_session):
|
2016-01-11 17:19:06 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
data = {
|
2016-02-19 15:53:45 +00:00
|
|
|
'email_from': 'service',
|
|
|
|
|
'user_id': 1234,
|
|
|
|
|
'name': 'created service',
|
2016-01-11 17:19:06 +00:00
|
|
|
'limit': 1000,
|
|
|
|
|
'restricted': False,
|
2016-02-19 15:53:45 +00:00
|
|
|
'active': False
|
|
|
|
|
}
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='POST',
|
|
|
|
|
request_body=json.dumps(data)
|
|
|
|
|
)
|
2016-01-15 16:22:03 +00:00
|
|
|
headers = [('Content-Type', 'application/json'), auth_header]
|
2016-02-19 15:53:45 +00:00
|
|
|
resp = client.post(
|
|
|
|
|
'/service',
|
2016-01-11 17:19:06 +00:00
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=headers)
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2016-02-19 15:53:45 +00:00
|
|
|
assert resp.status_code == 400
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
assert 'not found' in json_resp['message']['user_id']
|
2016-01-12 09:28:01 +00:00
|
|
|
|
|
|
|
|
|
2016-02-19 17:07:59 +00:00
|
|
|
def test_should_not_create_service_if_missing_data(notify_api, sample_user):
|
2016-01-12 10:59:27 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
data = {
|
2016-02-19 15:53:45 +00:00
|
|
|
'user_id': sample_user.id
|
|
|
|
|
}
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='POST',
|
|
|
|
|
request_body=json.dumps(data)
|
|
|
|
|
)
|
2016-01-15 16:22:03 +00:00
|
|
|
headers = [('Content-Type', 'application/json'), auth_header]
|
2016-02-19 15:53:45 +00:00
|
|
|
resp = client.post(
|
|
|
|
|
'/service',
|
2016-01-12 09:28:01 +00:00
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=headers)
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2016-02-19 15:53:45 +00:00
|
|
|
assert resp.status_code == 400
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
assert 'Missing data for required field.' in json_resp['message']['name']
|
|
|
|
|
assert 'Missing data for required field.' in json_resp['message']['active']
|
|
|
|
|
assert 'Missing data for required field.' in json_resp['message']['limit']
|
|
|
|
|
assert 'Missing data for required field.' in json_resp['message']['restricted']
|
2016-01-12 09:28:01 +00:00
|
|
|
|
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
def test_update_service(notify_api, sample_service):
|
2016-01-12 09:28:01 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-19 15:53:45 +00:00
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(sample_service.id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
2016-01-12 09:28:01 +00:00
|
|
|
data = {
|
2016-02-19 15:53:45 +00:00
|
|
|
'name': 'updated service name'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(sample_service.id),
|
|
|
|
|
method='POST',
|
|
|
|
|
request_body=json.dumps(data)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
'/service/{}'.format(sample_service.id),
|
2016-01-12 09:28:01 +00:00
|
|
|
data=json.dumps(data),
|
2016-02-19 15:53:45 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
result = json.loads(resp.get_data(as_text=True))
|
2016-01-12 09:28:01 +00:00
|
|
|
assert resp.status_code == 200
|
2016-02-19 15:53:45 +00:00
|
|
|
assert result['data']['name'] == 'updated service name'
|
2016-01-12 10:39:49 +00:00
|
|
|
|
|
|
|
|
|
2016-02-25 12:11:51 +00:00
|
|
|
def test_update_service_should_404_if_id_is_invalid(notify_api, notify_db, notify_db_session):
|
2016-01-12 10:39:49 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-19 15:53:45 +00:00
|
|
|
data = {
|
|
|
|
|
'name': 'updated service name'
|
|
|
|
|
}
|
2016-01-12 10:59:27 +00:00
|
|
|
|
2016-02-02 14:16:08 +00:00
|
|
|
missing_service_id = uuid.uuid4()
|
2016-01-13 12:14:21 +00:00
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(missing_service_id),
|
|
|
|
|
method='POST',
|
|
|
|
|
request_body=json.dumps(data)
|
|
|
|
|
)
|
2016-01-13 12:14:21 +00:00
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
resp = client.post(
|
|
|
|
|
'/service/{}'.format(missing_service_id),
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 404
|
2016-02-23 17:52:55 +00:00
|
|
|
|
|
|
|
|
|
2016-02-25 12:11:51 +00:00
|
|
|
def test_get_users_by_service(notify_api, notify_db, notify_db_session, sample_service):
|
2016-02-23 17:52:55 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-25 12:11:51 +00:00
|
|
|
user_on_service = sample_service.users[0]
|
2016-02-23 17:52:55 +00:00
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users'.format(sample_service.id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}/users'.format(sample_service.id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
result = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert len(result['data']) == 1
|
2016-02-25 12:11:51 +00:00
|
|
|
assert result['data'][0]['name'] == user_on_service.name
|
|
|
|
|
assert result['data'][0]['email_address'] == user_on_service.email_address
|
|
|
|
|
assert result['data'][0]['mobile_number'] == user_on_service.mobile_number
|
2016-02-24 10:30:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_users_for_service_returns_empty_list_if_no_users_associated_with_service(notify_api,
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
2016-02-25 12:11:51 +00:00
|
|
|
sample_service):
|
2016-02-24 10:30:00 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-02-25 12:11:51 +00:00
|
|
|
dao_remove_user_from_service(sample_service, sample_service.users[0])
|
2016-02-24 10:30:00 +00:00
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users'.format(sample_service.id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
|
|
|
|
'/service/{}/users'.format(sample_service.id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
result = json.loads(response.get_data(as_text=True))
|
2016-02-25 12:19:48 +00:00
|
|
|
assert response.status_code == 200
|
2016-02-24 10:30:00 +00:00
|
|
|
assert result['data'] == []
|
2016-02-25 12:11:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_users_for_service_returns_404_when_service_does_not_exist(notify_api, notify_db, notify_db_session):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
service_id = uuid.uuid4()
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users'.format(service_id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
|
|
|
|
'/service/{}/users'.format(service_id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
result = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert result['result'] == 'error'
|
|
|
|
|
assert result['message'] == 'Service not found for id: {}'.format(service_id)
|
2016-02-26 17:11:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_default_permissions_are_added_for_user_service(notify_api,
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_service,
|
|
|
|
|
sample_user):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'created service',
|
|
|
|
|
'user_id': sample_user.id,
|
|
|
|
|
'limit': 1000,
|
|
|
|
|
'restricted': False,
|
|
|
|
|
'active': False}
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service',
|
|
|
|
|
method='POST',
|
|
|
|
|
request_body=json.dumps(data)
|
|
|
|
|
)
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
|
|
auth_header_fetch = create_authorization_header(
|
|
|
|
|
path='/service/{}'.format(json_resp['data']['id']),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}?user_id={}'.format(json_resp['data']['id'], sample_user.id),
|
|
|
|
|
headers=[auth_header_fetch]
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
header = create_authorization_header(
|
|
|
|
|
path=url_for('user.get_user', user_id=sample_user.id),
|
|
|
|
|
method='GET')
|
|
|
|
|
response = client.get(
|
|
|
|
|
url_for('user.get_user', user_id=sample_user.id),
|
|
|
|
|
headers=[header])
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
service_permissions = json_resp['data']['permissions'][str(sample_service.id)]
|
|
|
|
|
from app.dao.permissions_dao import default_service_permissions
|
|
|
|
|
assert sorted(default_service_permissions) == sorted(service_permissions)
|
2016-02-29 17:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_existing_user_to_another_service(notify_api, notify_db, notify_db_session, sample_service):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
|
|
|
|
|
# check which users part of service
|
|
|
|
|
user_already_in_service = sample_service.users[0]
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users'.format(sample_service.id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}/users'.format(sample_service.id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
result = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert len(result['data']) == 1
|
|
|
|
|
assert result['data'][0]['email_address'] == user_already_in_service.email_address
|
|
|
|
|
|
|
|
|
|
# add new user to service
|
|
|
|
|
user_to_add = User(
|
|
|
|
|
name='Invited User',
|
|
|
|
|
email_address='invited@digital.cabinet-office.gov.uk',
|
|
|
|
|
password='password',
|
|
|
|
|
mobile_number='+4477123456'
|
|
|
|
|
)
|
|
|
|
|
# they must exist in db first
|
|
|
|
|
save_model_user(user_to_add)
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users/{}'.format(sample_service.id, user_to_add.id),
|
|
|
|
|
method='POST'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
'/service/{}/users/{}'.format(sample_service.id, user_to_add.id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 201
|
|
|
|
|
|
|
|
|
|
# check new user added to service
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users'.format(sample_service.id),
|
|
|
|
|
method='GET'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.get(
|
|
|
|
|
'/service/{}/users'.format(sample_service.id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
result = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert len(result['data']) == 2
|
|
|
|
|
assert _user_email_in_list(result['data'], user_already_in_service.email_address)
|
|
|
|
|
assert _user_email_in_list(result['data'], user_to_add.email_address)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_existing_user_to_non_existing_service_returns404(notify_api, notify_db, notify_db_session):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
|
|
|
|
|
user_to_add = User(
|
|
|
|
|
name='Invited User',
|
|
|
|
|
email_address='invited@digital.cabinet-office.gov.uk',
|
|
|
|
|
password='password',
|
|
|
|
|
mobile_number='+4477123456'
|
|
|
|
|
)
|
|
|
|
|
save_model_user(user_to_add)
|
|
|
|
|
|
|
|
|
|
incorrect_id = uuid.uuid4()
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users/{}'.format(incorrect_id, user_to_add.id),
|
|
|
|
|
method='POST'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
'/service/{}/users/{}'.format(incorrect_id, user_to_add.id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
expected_message = 'Service not found for id: {}'.format(incorrect_id)
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 404
|
|
|
|
|
assert result['result'] == 'error'
|
|
|
|
|
assert result['message'] == expected_message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_existing_user_of_service_to_service_returns400(notify_api, notify_db, notify_db_session, sample_service):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
|
|
|
|
|
existing_user_id = sample_service.users[0].id
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users/{}'.format(sample_service.id, existing_user_id),
|
|
|
|
|
method='POST'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
'/service/{}/users/{}'.format(sample_service.id, existing_user_id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
expected_message = 'User id: {} already part of service id: {}'.format(existing_user_id, sample_service.id)
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
assert result['result'] == 'error'
|
|
|
|
|
assert result['message'] == expected_message
|
|
|
|
|
|
|
|
|
|
|
2016-03-01 15:51:22 +00:00
|
|
|
def test_add_unknown_user_to_service_returns404(notify_api, notify_db, notify_db_session, sample_service):
|
2016-03-01 15:36:31 +00:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
|
|
|
|
|
incorrect_id = 9876
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
path='/service/{}/users/{}'.format(sample_service.id, incorrect_id),
|
|
|
|
|
method='POST'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
'/service/{}/users/{}'.format(sample_service.id, incorrect_id),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
expected_message = 'User not found for id: {}'.format(incorrect_id)
|
|
|
|
|
|
2016-03-01 15:51:22 +00:00
|
|
|
assert resp.status_code == 404
|
2016-03-01 15:36:31 +00:00
|
|
|
assert result['result'] == 'error'
|
|
|
|
|
assert result['message'] == expected_message
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 17:38:02 +00:00
|
|
|
def _user_email_in_list(user_list, email_address):
|
|
|
|
|
for user in user_list:
|
|
|
|
|
if user['email_address'] == email_address:
|
|
|
|
|
return True
|
|
|
|
|
return False
|