Added some more tests.

Removed the validation in the schema - it was adding complexity, let the unique constraint on the db throw the exception. This should only ever happen on a race condition which seems unlikely (two people changing a service to the same name at the same time)
Do no set debug=true to the test config. If debug=true it changes the behaviour of the error handlers, throwing the exception rather than returning a 500.
This commit is contained in:
Rebecca Law
2016-04-01 13:42:11 +01:00
parent 8df4919029
commit 8493e29acc
3 changed files with 71 additions and 27 deletions

View File

@@ -73,21 +73,6 @@ class ServiceSchema(BaseSchema):
model = models.Service model = models.Service
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", 'old_id') exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", 'old_id')
@validates_schema
def validate_all(self, data):
# There are 2 instances where we want to check
# for duplicate service name. One when they updating
# an existing service and when they are creating a service
name = data.get('name', None)
service = models.Service.query.filter_by(name=name).first()
error_msg = "Duplicate service name '{}'".format(name)
if 'id' in data:
if service and str(service.id) != data['id']:
raise ValidationError(error_msg, 'name')
else:
if service:
raise ValidationError(error_msg, 'name')
class TemplateSchema(BaseSchema): class TemplateSchema(BaseSchema):
class Meta: class Meta:

View File

@@ -83,8 +83,8 @@ class Development(Config):
DEBUG = True DEBUG = True
class Test(Development): class Test(Config):
DEBUG = True pass
configs = { configs = {
'live': 'config_live.Live', 'live': 'config_live.Live',

View File

@@ -1,6 +1,7 @@
import json import json
import uuid import uuid
from flask import url_for from flask import url_for
from app.dao.users_dao import save_model_user from app.dao.users_dao import save_model_user
from app.dao.services_dao import dao_remove_user_from_service from app.dao.services_dao import dao_remove_user_from_service
from app.models import User from app.models import User
@@ -301,7 +302,8 @@ def test_should_not_create_service_with_duplicate_name(notify_api,
'user_id': sample_service.users[0].id, 'user_id': sample_service.users[0].id,
'limit': 1000, 'limit': 1000,
'restricted': False, 'restricted': False,
'active': False} 'active': False,
'email_from': 'sample.service2'}
auth_header = create_authorization_header( auth_header = create_authorization_header(
path='/service', path='/service',
method='POST', method='POST',
@@ -312,10 +314,36 @@ def test_should_not_create_service_with_duplicate_name(notify_api,
'/service', '/service',
data=json.dumps(data), data=json.dumps(data),
headers=headers) headers=headers)
json_resp = json.loads(resp.get_data(as_text=True)) assert resp.status_code == 500
assert resp.status_code == 400 assert 'Key (name)=(Sample service) already exists.' in resp.get_data(as_text=True)
assert "Duplicate service name '{}'".format(
sample_service.name) in json_resp['message']['name']
def test_create_service_should_throw_duplicate_key_constraint_for_existing_email_from(notify_api,
notify_db,
notify_db_session,
service_factory):
first_service = service_factory.get('First service', email_from='first.service')
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'First SERVICE',
'user_id': first_service.users[0].id,
'limit': 1000,
'restricted': False,
'active': False,
'email_from': 'first.service'}
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)
assert resp.status_code == 500
assert 'Key (email_from)=(first.service) already exists.' in resp.get_data(as_text=True)
def test_update_service(notify_api, sample_service): def test_update_service(notify_api, sample_service):
@@ -363,7 +391,7 @@ def test_should_not_update_service_with_duplicate_name(notify_api,
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
service_name = "another name" service_name = "another name"
another_service = create_sample_service( create_sample_service(
notify_db, notify_db,
notify_db_session, notify_db_session,
service_name=service_name, service_name=service_name,
@@ -384,10 +412,41 @@ def test_should_not_update_service_with_duplicate_name(notify_api,
data=json.dumps(data), data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header] headers=[('Content-Type', 'application/json'), auth_header]
) )
json_resp = json.loads(resp.get_data(as_text=True)) assert resp.status_code == 500
assert resp.status_code == 400 assert 'Key (name)=(another name) already exists.' in resp.get_data(as_text=True)
assert "Duplicate service name '{}'".format(
service_name) in json_resp['message']['name']
def test_should_not_update_service_with_duplicate_email_from(notify_api,
notify_db,
notify_db_session,
sample_user,
sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
email_from = "duplicate.name"
create_sample_service(
notify_db,
notify_db_session,
service_name="duplicate name",
user=sample_user,
email_from=email_from)
data = {
'email_from': email_from
}
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),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert resp.status_code == 500
assert 'Key (email_from)=(duplicate.name) already exists.' in resp.get_data(as_text=True)
def test_update_service_should_404_if_id_is_invalid(notify_api, notify_db, notify_db_session): def test_update_service_should_404_if_id_is_invalid(notify_api, notify_db, notify_db_session):