Skip to content

Commit

Permalink
Add error message when destination name already exists (#3597)
Browse files Browse the repository at this point in the history
* Return 400 when destination name already exists

* Remove whitespace

* Unicode 1

Co-Authored-By: gabrieldutra <nesk.frz@gmail.com>

* Unicode 2

Co-Authored-By: gabrieldutra <nesk.frz@gmail.com>
  • Loading branch information
gabrieldutra authored and arikfr committed Mar 27, 2019
1 parent 872d0ca commit 375e61f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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

0 comments on commit 375e61f

Please sign in to comment.