pungi/tests/test_notifier.py
Lubomír Sedlář 7bf12636a0 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>
2016-09-26 15:50:49 +02:00

104 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import mock
import os
import sys
import unittest
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from pungi.notifier import PungiNotifier
class TestNotifier(unittest.TestCase):
@mock.patch('pungi.paths.translate_path')
@mock.patch('kobo.shortcuts.run')
def test_invokes_script(self, run, translate_path):
compose = mock.Mock(
compose_id='COMPOSE_ID',
paths=mock.Mock(
compose=mock.Mock(
topdir=mock.Mock(return_value='/a/b')
)
)
)
run.return_value = (0, None)
translate_path.side_effect = lambda compose, x: x
n = PungiNotifier('run-notify')
n.compose = compose
data = {'foo': 'bar', 'baz': 'quux'}
n.send('cmd', **data)
data['compose_id'] = 'COMPOSE_ID'
data['location'] = '/a/b'
run.assert_called_once_with(('run-notify', 'cmd'),
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)
n.send('cmd', foo='bar', baz='quux')
self.assertFalse(run.called)
@mock.patch('pungi.paths.translate_path')
@mock.patch('kobo.shortcuts.run')
def test_logs_warning_on_failure(self, run, translate_path):
compose = mock.Mock(
compose_id='COMPOSE_ID',
log_warning=mock.Mock(),
paths=mock.Mock(
compose=mock.Mock(
topdir=mock.Mock(return_value='/a/b')
)
)
)
translate_path.side_effect = lambda compose, x: x
run.return_value = (1, None)
n = PungiNotifier('run-notify')
n.compose = compose
n.send('cmd')
run.assert_called_once_with(('run-notify', 'cmd'),
stdin_data=json.dumps({'compose_id': 'COMPOSE_ID', 'location': '/a/b'}),
can_fail=True, return_stdout=False, workdir='/a/b')
self.assertTrue(compose.log_warning.called)
if __name__ == "__main__":
unittest.main()