Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ python:

install:
- "python setup.py install"
- "pip install coverage coveralls pytest mock"
- "curl http://www.cups.org/software/ipptool/ipptool-20130731-linux-ubuntu-x86_64.tar.gz | tar xvzf -"
- "echo \"[main]\nipptool_path = \"$TRAVIS_BUILD_DIR\"/ipptool-20130731/ipptool\" > ~/.pyipptool.cfg"

script: "python setup.py test"
script: "coverage run --source=pyipptool setup.py test"

after_success:
coveralls

notifications:
email: false
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ pyipptool
https://travis-ci.org/ezeep/pyipptool.png?branch=master
:target: https://travis-ci.org/ezeep/pyipptool

.. image:: https://coveralls.io/repos/ezeep/pyipptool/badge.png
:target: https://coveralls.io/r/ezeep/pyipptool


Convenient ipp request generator to interrogate CUPS or IPP devices with the help of ipptool_.

.. _ipptool: http://www.cups.org/documentation.php/doc-1.7/man-ipptool.html
Expand Down
20 changes: 10 additions & 10 deletions pyipptool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
config.read(['/etc/pyipptool/pyipptool.cfg',
os.path.join(os.path.expanduser('~'), '.pyipptool.cfg')])
IPPTOOL_PATH = config.get('main', 'ipptool_path')
try:
LOGIN = config.get('main', 'login')
except ConfigParser.NoOptionError:
LOGIN = ''
try:
PASSWORD = config.get('main', 'password')
except ConfigParser.NoOptionError:
PASSWORD = ''
try:
GRACEFUL_SHUTDOWN_TIME = config.get('main', 'graceful_shutdown_time')
except ConfigParser.NoOptionError:
Expand All @@ -53,17 +61,9 @@ class TimeoutError(Exception):


def authenticate_uri(uri):
try:
login = config.get('main', 'login')
except ConfigParser.NoOptionError:
login = ''
try:
password = config.get('main', 'password')
except ConfigParser.NoOptionError:
password = ''
if login and password:
if LOGIN and PASSWORD:
parsed_url = urlparse.urlparse(uri)
authenticated_netloc = '{}:{}@{}'.format(login, password,
authenticated_netloc = '{}:{}@{}'.format(LOGIN, PASSWORD,
parsed_url.netloc)
authenticated_uri = urlparse.ParseResult(parsed_url[0],
authenticated_netloc,
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def read_that_file(path):
license=' Apache Software License',
packages=('pyipptool',),
install_requires=('deform',),
tests_require=('mock', 'pytest', 'pytest-cov'),
tests_require=('mock', 'pytest', 'coverage',
'pytest-cov', 'coveralls'),
include_package_data=True,
test_suite='tests',
cmdclass = {'test': PyTest},
Expand Down
189 changes: 172 additions & 17 deletions tests/test_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@
import pyipptool


def patch_config(**kwconfig):
def wrapper(fn):
@functools.wraps(fn)
def inner(*args, **kw):
import pyipptool
old_values = {}
for k, v in kwconfig.items():
old_values[k] = getattr(pyipptool, k)
setattr(pyipptool, k, v)
try:
return fn(*args, **kw)
finally:
for k, v in old_values.items():
setattr(pyipptool, k, v)
return inner
return wrapper


@mock.patch.object(pyipptool, '_call_ipptool')
def test_ipptool_create_printer_subscription(_call_ipptool):
from pyipptool import create_printer_subscription
Expand Down Expand Up @@ -71,22 +89,7 @@ def test_get_job_attributes_with_job_uri(_call_ipptool):
assert 'printer-uri' not in request


def patch_timeout(value):
def wrapper(fn):
@functools.wraps(fn)
def inner(*args, **kw):
import pyipptool
previous = pyipptool.TIMEOUT
pyipptool.TIMEOUT = value
try:
return fn(*args, **kw)
finally:
pyipptool.TIMEOUT = previous
return inner
return wrapper


@patch_timeout(1)
@patch_config(TIMEOUT=1)
def test_timeout():
from pyipptool import TimeoutError, _call_ipptool
from pyipptool.forms import get_subscriptions_form
Expand All @@ -108,7 +111,6 @@ def do_POST(self):
thread = threading.Thread(target=httpd.serve_forever)
thread.daemon = True
thread.start()
# time.sleep(.1) # Warmup

request = get_subscriptions_form.render(
{'header':
Expand All @@ -117,3 +119,156 @@ def do_POST(self):
'http://localhost:%s/printers/fake' % PORT}}})
with pytest.raises(TimeoutError):
_call_ipptool('http://localhost:%s/' % PORT, request)


@patch_config(LOGIN='ezeep', PASSWORD='secret')
def test_authentication():
from pyipptool import authenticate_uri

assert (authenticate_uri('http://localhost:631/printers/?arg=value') ==
'http://ezeep:secret@localhost:631/printers/?arg=value')


@mock.patch.object(pyipptool, '_call_ipptool')
def test_release_job(_call_ipptool):
from pyipptool import release_job
_call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]}
release_job('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cancel_job(_call_ipptool):
from pyipptool import cancel_job
_call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]}
cancel_job('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_add_modify_class(_call_ipptool):
from pyipptool import cups_add_modify_class
_call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]}
cups_add_modify_class('https://localhost:631/',
printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_delete_printer(_call_ipptool):
from pyipptool import cups_delete_printer
_call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]}
cups_delete_printer('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_delete_class(_call_ipptool):
from pyipptool import cups_delete_class
_call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]}
cups_delete_class('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_get_classes(_call_ipptool):
from pyipptool import cups_get_classes
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
cups_get_classes('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_get_printers(_call_ipptool):
from pyipptool import cups_get_printers
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
cups_get_printers('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_get_devices(_call_ipptool):
from pyipptool import cups_get_devices
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
cups_get_devices('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_get_ppds(_call_ipptool):
from pyipptool import cups_get_ppds
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
cups_get_ppds('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_move_job(_call_ipptool):
from pyipptool import cups_move_job
_call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]}
cups_move_job('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_cups_reject_jobs(_call_ipptool):
from pyipptool import cups_reject_jobs
_call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]}
cups_reject_jobs('https://localhost:631/')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_get_jobs(_call_ipptool):
from pyipptool import get_jobs
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
get_jobs('https://localhost:631/', printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_get_printer_attributes(_call_ipptool):
from pyipptool import get_printer_attributes
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
get_printer_attributes('https://localhost:631/', printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_get_subscriptions(_call_ipptool):
from pyipptool import get_subscriptions
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
get_subscriptions('https://localhost:631/', printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_pause_printer(_call_ipptool):
from pyipptool import pause_printer
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
pause_printer('https://localhost:631/', printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_hold_new_jobs(_call_ipptool):
from pyipptool import hold_new_jobs
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
hold_new_jobs('https://localhost:631/', printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_release_held_new_jobs(_call_ipptool):
from pyipptool import release_held_new_jobs
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
release_held_new_jobs('https://localhost:631/', printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'


@mock.patch.object(pyipptool, '_call_ipptool')
def test_resume_printer(_call_ipptool):
from pyipptool import resume_printer
_call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]}
resume_printer('https://localhost:631/', printer_uri='')
assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/'