Check the uniqueness of the service name ignoring case.

When the service name changes the email_from changes to.
Renamed find_all_service_names to find_all_service_names_lower.
This commit is contained in:
Rebecca Law
2016-03-31 10:26:03 +01:00
parent ab50ed6368
commit 1871243cc8
7 changed files with 25 additions and 29 deletions

View File

@@ -159,7 +159,7 @@ class AddServiceForm(Form):
) )
def validate_name(self, a): def validate_name(self, a):
if a.data in self._names_func(): if a.data.lower() in self._names_func():
raise ValidationError('This service name is already in use') raise ValidationError('This service name is already in use')

View File

@@ -31,7 +31,7 @@ def add_service():
invite_api_client.accept_invite(service_id, invitation.id) invite_api_client.accept_invite(service_id, invitation.id)
return redirect(url_for('main.service_dashboard', service_id=service_id)) return redirect(url_for('main.service_dashboard', service_id=service_id))
form = AddServiceForm(service_api_client.find_all_service_names) form = AddServiceForm(service_api_client.find_all_service_names_lower)
heading = 'Which service do you want to set up notifications for?' heading = 'Which service do you want to set up notifications for?'
if form.validate_on_submit(): if form.validate_on_submit():
session['service_name'] = form.name.data session['service_name'] = form.name.data

View File

@@ -39,7 +39,7 @@ def service_settings(service_id):
def service_name_change(service_id): def service_name_change(service_id):
service = service_api_client.get_service(service_id)['data'] service = service_api_client.get_service(service_id)['data']
form = ServiceNameForm(service_api_client.find_all_service_names) form = ServiceNameForm(service_api_client.find_all_service_names_lower)
if form.validate_on_submit(): if form.validate_on_submit():
session['service_name_change'] = form.name.data session['service_name_change'] = form.name.data

View File

@@ -142,7 +142,7 @@ class ServiceAPIClient(NotificationsAPIClient):
endpoint = "/service/{0}/template/{1}".format(service_id, template_id) endpoint = "/service/{0}/template/{1}".format(service_id, template_id)
return self.delete(endpoint) return self.delete(endpoint)
def find_all_service_names(self, user_id=None): def find_all_service_names_lower(self, user_id=None):
resp = self.get_services(user_id) resp = self.get_services(user_id)
return [x['name'].lower() for x in resp['data']] return [x['name'].lower() for x in resp['data']]

View File

@@ -10,8 +10,9 @@ class TestClient(FlaskClient):
with self.session_transaction() as session: with self.session_transaction() as session:
session['user_id'] = user.id session['user_id'] = user.id
session['_fresh'] = True session['_fresh'] = True
if mocker and service: if mocker:
mocker.patch('app.user_api_client.get_user', return_value=user) mocker.patch('app.user_api_client.get_user', return_value=user)
if mocker and service:
mocker.patch('app.service_api_client.get_service', return_value={'data': service}) mocker.patch('app.service_api_client.get_service', return_value={'data': service})
login_user(user, remember=True) login_user(user, remember=True)

View File

@@ -3,62 +3,57 @@ from flask import url_for
def test_get_should_render_add_service_template(app_, def test_get_should_render_add_service_template(app_,
api_user_active, api_user_active,
mock_login, mocker):
mock_get_service,
mock_get_services,
mock_get_user_by_email):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active, mocker)
response = client.get(url_for('main.add_service')) response = client.get(url_for('main.add_service'))
assert response.status_code == 200 assert response.status_code == 200
assert 'Which service do you want to set up notifications for?' in response.get_data(as_text=True) assert 'Which service do you want to set up notifications for?' in response.get_data(as_text=True)
def test_should_add_service_and_redirect_to_next_page(app_, def test_should_add_service_and_redirect_to_next_page(app_,
mock_login, mocker,
mock_create_service, mock_create_service,
mock_get_services, mock_get_services,
api_user_active): api_user_active):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active, mocker)
response = client.post( response = client.post(
url_for('main.add_service'), url_for('main.add_service'),
data={'name': 'testing the post'}) data={'name': 'testing the post'})
assert response.status_code == 302 assert response.status_code == 302
assert response.location == url_for('main.service_dashboard', service_id=101, _external=True) assert response.location == url_for('main.service_dashboard', service_id=101, _external=True)
assert mock_get_services.called
mock_create_service.asset_called_once_with('testing the post', False, mock_create_service.asset_called_once_with('testing the post', False,
app_.config['DEFAULT_SERVICE_LIMIT'], app_.config['DEFAULT_SERVICE_LIMIT'],
True, api_user_active.id) True, api_user_active.id)
def test_should_return_form_errors_when_service_name_is_empty(app_, def test_should_return_form_errors_when_service_name_is_empty(app_,
api_user_active, mocker,
mock_get_service, api_user_active):
mock_get_services,
mock_get_user,
mock_get_user_by_email,
mock_login):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active, mocker)
response = client.post(url_for('main.add_service'), data={}) response = client.post(url_for('main.add_service'), data={})
assert response.status_code == 200 assert response.status_code == 200
assert 'Service name cant be empty' in response.get_data(as_text=True) assert 'Service name cant be empty' in response.get_data(as_text=True)
def test_should_return_form_errors_with_duplicate_service_name(app_, def test_should_return_form_errors_with_duplicate_service_name_regardless_of_case(app_,
mock_login, mocker,
mock_get_services, service_one,
mock_get_user, mock_get_services,
api_user_active, api_user_active,
mock_get_user_by_email): mock_create_service):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active, mocker, service_one)
response = client.post( response = client.post(url_for('main.add_service'), data={'name': 'SERVICE_TWO'})
url_for('main.add_service'), data={'name': 'service_one'})
assert response.status_code == 200 assert response.status_code == 200
assert 'This service name is already in use' in response.get_data(as_text=True) assert 'This service name is already in use' in response.get_data(as_text=True)
assert mock_get_services.called assert mock_get_services.called
assert not mock_create_service.called

View File

@@ -66,7 +66,7 @@ def test_should_not_allow_duplicate_names(app_,
service_id = service_one['id'] service_id = service_one['id']
response = client.post( response = client.post(
url_for('main.service_name_change', service_id=service_id), url_for('main.service_name_change', service_id=service_id),
data={'name': "service_one"}) data={'name': "SErvICE_TWO"})
assert response.status_code == 200 assert response.status_code == 200
resp_data = response.get_data(as_text=True) resp_data = response.get_data(as_text=True)