Skip to content

Commit

Permalink
Merge branch 'master' into fix-status-code-on-name-conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Klein committed May 8, 2018
2 parents 277cc41 + a627811 commit 67c59a0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v2.0.1 (UNRLEASED)
==================

- prevent creation of databases with an empty password as those cannot be removed.

v2.0.0
======

Expand Down
4 changes: 4 additions & 0 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json
import logging

import psycopg2
Expand Down Expand Up @@ -132,6 +133,9 @@ def post(self):
parser.add_argument('db_pwd', required=True, type=str, help='pass of the db user')
args = parser.parse_args()

if not args['db_pwd']:
abort(400, msg='The password may not be empty.')

if DBInstance.query.filter_by(postgraas_instance_name=args['postgraas_instance_name']
).first():
abort(
Expand Down
45 changes: 33 additions & 12 deletions tests/test_integration/test_postgras_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,24 @@ def docker_setup(request, tmpdir):
class PostgraasApiTestBase:
def get_postgraas_by_name(self, name, client):
headers = {'Content-Type': 'application/json'}
list = client.get('/api/v2/postgraas_instances', headers=headers)
for instance in json.loads(list.get_data(as_text=True)):
instances = client.get('/api/v2/postgraas_instances', headers=headers)
for instance in json.loads(instances.get_data(as_text=True)):
if instance["postgraas_instance_name"] == name:
return instance["id"]
return None

def delete_instance_by_name(self, db_credentials, client):
id = self.get_postgraas_by_name(db_credentials["postgraas_instance_name"], client)
db_pwd = db_credentials["db_pwd"]
headers = {'Content-Type': 'application/json'}
client.delete(
'/api/v2/postgraas_instances/' + str(id),
data=json.dumps({
'db_pwd': db_pwd
}),
headers=headers
)
instance_id = self.get_postgraas_by_name(db_credentials["postgraas_instance_name"], client)
if instance_id is not None:
db_pwd = db_credentials["db_pwd"]
headers = {'Content-Type': 'application/json'}
client.delete(
'/api/v2/postgraas_instances/' + str(instance_id),
data=json.dumps({
'db_pwd': db_pwd
}),
headers=headers
)


@pytest.mark.usefixtures('docker_setup')
Expand Down Expand Up @@ -382,3 +384,22 @@ def test_return_postgres_instance_api(self):
assert actual_data == expected

self.delete_instance_by_name(db_credentials, self.app_client)

def test_empty_password(self):
instance_name = "test_empty_password"
db_credentials = {
"postgraas_instance_name": instance_name,
"db_name": self.db_name,
"db_username": self.username,
"db_pwd": "",
}
self.delete_instance_by_name(db_credentials, self.app_client)
headers = {'Content-Type': 'application/json'}
result = self.app_client.post(
'/api/v2/postgraas_instances', headers=headers, data=json.dumps(db_credentials)
)
created_db = json.loads(result.get_data(as_text=True))

assert result.status_code == 400
print(created_db)
assert 'password may not be empty' in created_db["msg"]

0 comments on commit 67c59a0

Please sign in to comment.