Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error message when destination name already exists #3597

Merged
merged 4 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions redash/handlers/destinations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import make_response, request
from flask_restful import abort
from sqlalchemy.exc import IntegrityError

from redash import models
from redash.destinations import (destinations,
Expand Down Expand Up @@ -45,6 +46,10 @@ def post(self, destination_id):
models.db.session.commit()
except ValidationError:
abort(400)
except IntegrityError as e:
if 'name' in e.message:
abort(400, message=u"Alert Destination with the name {} already exists.".format(req['name']))
abort(500)

return destination.to_dict(all=True)

Expand Down Expand Up @@ -102,6 +107,12 @@ def post(self):
options=config,
user=self.current_user)

models.db.session.add(destination)
models.db.session.commit()
try:
models.db.session.add(destination)
models.db.session.commit()
except IntegrityError as e:
if 'name' in e.message:
abort(400, message=u"Alert Destination with the name {} already exists.".format(req['name']))
abort(500)

return destination.to_dict(all=True)
11 changes: 11 additions & 0 deletions tests/handlers/test_destinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def test_post_requires_admin(self):
rv = self.make_request('post', '/api/destinations', user=self.factory.user, data=data)
self.assertEqual(rv.status_code, 403)

def test_returns_400_when_name_already_exists(self):
d1 = self.factory.create_destination()
data = {
'options': {'addresses': 'test@example.com'},
'name': d1.name,
'type': 'email'
}

rv = self.make_request('post', '/api/destinations', user=self.factory.create_admin(), data=data)
self.assertEqual(rv.status_code, 400)


class TestDestinationResource(BaseTestCase):
def test_get(self):
Expand Down