Translate paths without double slash

If the path in `translate_paths` config ends with a slash, we would
create public path with double slash.

Fixes: https://pagure.io/pungi/issue/408
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-09-26 15:50:49 +02:00
parent 160df7f89a
commit 7bf12636a0
2 changed files with 30 additions and 0 deletions

View File

@ -36,6 +36,8 @@ def translate_path(compose, path):
for prefix, newvalue in mapping:
prefix = os.path.normpath(prefix)
# Strip trailing slashes: the prefix has them stripped by `normpath`.
newvalue = newvalue.rstrip('/')
if normpath.startswith(prefix):
# We can't call os.path.normpath on result since it is not actually
# a path - http:// would get changed to http:/ and so on.

View File

@ -39,6 +39,34 @@ class TestNotifier(unittest.TestCase):
stdin_data=json.dumps(data),
can_fail=True, return_stdout=False, workdir='/a/b')
@mock.patch('kobo.shortcuts.run')
def test_translates_path(self, run):
compose = mock.Mock(
compose_id='COMPOSE_ID',
paths=mock.Mock(
compose=mock.Mock(
topdir=mock.Mock(return_value='/root/a/b')
)
),
conf={
"translate_paths": [("/root/", "http://example.com/compose/")],
}
)
run.return_value = (0, None)
n = PungiNotifier('run-notify')
n.compose = compose
data = {'foo': 'bar', 'baz': 'quux'}
n.send('cmd', **data)
data['compose_id'] = 'COMPOSE_ID'
data['location'] = 'http://example.com/compose/a/b'
run.assert_called_once_with(('run-notify', 'cmd'),
stdin_data=json.dumps(data),
can_fail=True, return_stdout=False,
workdir='/root/a/b')
@mock.patch('kobo.shortcuts.run')
def test_does_not_run_without_config(self, run):
n = PungiNotifier(None)