Skip to content

Commit

Permalink
Assert db_connection schema is postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Jan 12, 2023
1 parent 259ff62 commit e45a0e0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion doc/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Body parameters:
- if QML style is used in this request, it must list all attributes contained in given data file
- *db_connection*, string
- exactly one of `file` or `db_connection` must be set
- format `postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>` is expected query parameters `table` and `geo_column` specified
- format `postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>` is expected with schema `postgresql` and query parameters `table` and `geo_column` specified
- *name*, string
- computer-friendly identifier of the layer
- must be unique among all layers of one workspace
Expand Down
11 changes: 11 additions & 0 deletions src/layman/layer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ def get_same_or_missing_prop_names(workspace, layername):

def parse_and_validate_connection_string(connection_string):
connection = parse.urlparse(connection_string, )
if connection.scheme != 'postgresql':
raise LaymanError(2, {
'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have schema `postgresql`',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'db_connection': connection_string,
'schema': connection.scheme,
}
})

params = parse.parse_qs(connection.query)
url = connection._replace(query='').geturl()
result = ConnectionString(url=url,
Expand Down
30 changes: 10 additions & 20 deletions src/layman/layer/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,65 +177,55 @@ def test_parse_connection_string(connection_string, exp_result):
('', {'http_code': 400,
'code': 2,
'detail': {'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have `url` part and `table` and `geo_column` query parameters',
'message': 'Parameter `db_connection` is expected to have schema `postgresql`',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'db_connection': '',
'url': '',
'table': None,
'geo_column': None,
'schema': '',
},
},
}),
(' ', {'http_code': 400,
'code': 2,
'detail': {'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have `url` part and `table` and `geo_column` query parameters',
'message': 'Parameter `db_connection` is expected to have schema `postgresql`',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'db_connection': ' ',
'url': ' ',
'table': None,
'geo_column': None,
'schema': '',
},
},
}),
('_', {'http_code': 400,
'code': 2,
'detail': {'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have `url` part and `table` and `geo_column` query parameters',
'message': 'Parameter `db_connection` is expected to have schema `postgresql`',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'db_connection': '_',
'url': '_',
'table': None,
'geo_column': None,
'schema': '',
},
},
}),
('$^&*(', {'http_code': 400,
'code': 2,
'detail': {'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have `url` part and `table` and `geo_column` query parameters',
'message': 'Parameter `db_connection` is expected to have schema `postgresql`',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'db_connection': '$^&*(',
'url': '$^&*(',
'table': None,
'geo_column': None,
'schema': '',
},
},
}),
('ščžýžý', {'http_code': 400,
'code': 2,
'detail': {'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have `url` part and `table` and `geo_column` query parameters',
'message': 'Parameter `db_connection` is expected to have schema `postgresql`',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'db_connection': 'ščžýžý',
'url': 'ščžýžý',
'table': None,
'geo_column': None,
'schema': '',
},
},
}),
Expand Down
17 changes: 8 additions & 9 deletions tests/dynamic_data/publications/wrong_input/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,15 +1408,14 @@
'sync': True,
'code': 2,
'message': 'Wrong parameter value',
'detail': {
'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have `url` part and `table` and `geo_column` query parameters',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'url': 'db_connection',
'table': None,
'geo_column': None,
}},
'detail': {'parameter': 'db_connection',
'message': 'Parameter `db_connection` is expected to have schema `postgresql`',
'expected': 'postgresql://<username>:<password>@<host>:<port>/dbname?table=<table_name>&geo_column=<geo_column_name>',
'found': {
'db_connection': 'db_connection',
'schema': '',
},
},
},
},
},
Expand Down

0 comments on commit e45a0e0

Please sign in to comment.