Skip to content

Commit

Permalink
added travis and linter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarosław Śmiejczak committed Jan 31, 2017
1 parent e34d8c3 commit 08a1325
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 38 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
@@ -0,0 +1,12 @@
sudo: false
language: python
python:
- '2.7'
install:
- pip install -U --force setuptools pip
- ./setup.py develop
- pip install -e '.[tests]'

script:
- pylama
- py.test
5 changes: 5 additions & 0 deletions pylama.ini
@@ -0,0 +1,5 @@
[pylama]
linters = pyflakes

[pylama:doc/conf.py]
skip = 1
9 changes: 8 additions & 1 deletion setup.py 100644 → 100755
Expand Up @@ -12,7 +12,11 @@
print "You must have setuptools installed to use setup.py. Exiting..."
raise SystemExit(1)


test_requirements = [
'mock',
'pylama',
'pytest'
]
setup(
name="python-owasp-zap-v2.4",
version="0.0.9.dev1",
Expand All @@ -39,4 +43,7 @@
'Intended Audience :: Developers',
'Intended Audience :: Information Technology',
'Programming Language :: Python'],

tests_require=test_requirements,
extras_require={'tests': test_requirements}
)
4 changes: 1 addition & 3 deletions src/examples/zap-baseline.py
Expand Up @@ -53,7 +53,6 @@
import time
import traceback
import urllib2
from datetime import datetime
from random import randint
from zapv2 import ZAPv2

Expand Down Expand Up @@ -277,7 +276,7 @@ def main(argv):
# Not running in docker, so start one
try:
logging.debug ('Pulling ZAP Weekly Docker image')
ls_output = subprocess.check_output(['docker', 'pull', 'owasp/zap2docker-weekly'])
subprocess.check_output(['docker', 'pull', 'owasp/zap2docker-weekly'])
except OSError:
logging.warning ('Failed to run docker - is it on your path?')
sys.exit(3)
Expand Down Expand Up @@ -350,7 +349,6 @@ def main(argv):
logging.debug ('Ajax Spider complete')

# Wait for passive scanning to complete
rtc = zap.pscan.records_to_scan
logging.debug ('Records to scan...')
while (int(zap.pscan.records_to_scan) > 0):
logging.debug ('Records to passive scan : ' + zap.pscan.records_to_scan)
Expand Down
40 changes: 6 additions & 34 deletions src/zapv2/__init__.py
Expand Up @@ -47,20 +47,14 @@
from stats import stats
from users import users

class ZapError(Exception):
"""
Base ZAP exception.
"""
pass


class ZAPv2(object):
"""
Client API implementation for integrating with ZAP v2.
"""

# base JSON api url
base = 'http://zap/JSON/'

# base OTHER api url
base_other = 'http://zap/OTHER/'

Expand All @@ -71,12 +65,12 @@ def __init__(self, proxies={'http': 'http://127.0.0.1:8080',
:Parameters:
- `proxies`: dictionary of ZAP proxies to use.
Note that all of the other classes in this directory are generated
new ones will need to be manually added to this file
"""
self.__proxies = proxies

self.acsrf = acsrf(self)
self.ajaxSpider = ajaxSpider(self)
self.ascan = ascan(self)
Expand All @@ -101,17 +95,6 @@ def __init__(self, proxies={'http': 'http://127.0.0.1:8080',
self.stats = stats(self)
self.users = users(self)

def _expect_ok(self, json_data):
"""
Checks that we have an OK response, else raises an exception.
:Parameters:
- `json_data`: the json data to look at.
"""
if type(json_data) == type(list()) and json_data[0] == u'OK':
return json_data
raise ZapError(*json_data.values())

def urlopen(self, *args, **kwargs):
"""
Opens a url forcing the proxies to be used.
Expand All @@ -123,26 +106,15 @@ def urlopen(self, *args, **kwargs):
kwargs['proxies'] = self.__proxies
return urllib.urlopen(*args, **kwargs).read()

def status_code(self, *args, **kwargs):
"""
Open a url forcing the proxies to be used.
:Parameters:
- `args`: all non-keyword arguments.
- `kwargs`: all other keyword arguments.
"""
kwargs['proxies'] = self.__proxies
return urllib.urlopen(*args, **kwargs).getcode()

def _request(self, url, get={}):
def _request(self, url, get=None):
"""
Shortcut for a GET request.
:Parameters:
- `url`: the url to GET at.
- `get`: the disctionary to turn into GET variables.
"""
return json.loads(self.urlopen(url + '?' + urllib.urlencode(get)))
return json.loads(self.urlopen(url + '?' + urllib.urlencode(get or {})))

def _request_other(self, url, get={}):
"""
Expand All @@ -152,4 +124,4 @@ def _request_other(self, url, get={}):
- `url`: the url to GET at.
- `get`: the disctionary to turn into GET variables.
"""
return self.urlopen(url + '?' + urllib.urlencode(get))
return self.urlopen(url + '?' + urllib.urlencode(get or {}))
Empty file added tests/__init__.py
Empty file.
Empty file added tests/unit/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions tests/unit/conftest.py
@@ -0,0 +1,17 @@
from mock import patch
import pytest

from zapv2 import ZAPv2

@pytest.yield_fixture
def zap():
"""
All tests will be able to share the instance of client with the same settings."""
yield ZAPv2()


@pytest.yield_fixture
def urllib_mock():
"""Fixture create a mock for urllib library."""
with patch('zapv2.urllib.urlopen') as urllib_mock:
yield urllib_mock
39 changes: 39 additions & 0 deletions tests/unit/test_client.py
@@ -0,0 +1,39 @@
"""
Tests related to the main Zap Client class
"""
from mock import call

TEST_PROXIES = {
'http': 'http://127.0.0.1:8080',
'https': 'http://127.0.0.1:8080',
}


def test_urlopen_proxies(zap, urllib_mock):
"""Check if Zap client passes proxy to urllib call."""
urllib_mock.return_value.read.return_value = 'contents'

assert zap.urlopen() == 'contents'
assert urllib_mock.mock_calls[0][2]['proxies'] == TEST_PROXIES


def test_request_response(zap, urllib_mock):
"""Request method should return a python object from parsed output"""
urllib_mock.return_value.read.return_value = '{"testkey": "testvalue"}'

assert zap._request('http://allizom.org', {'querykey': 'queryvalue'}) == {'testkey': 'testvalue'}
assert urllib_mock.mock_calls == [
call('http://allizom.org?querykey=queryvalue', proxies=TEST_PROXIES),
call().read()
]


def test_request_other(zap, urllib_mock):
"""_request_other should simply return a retrieved content."""
urllib_mock.return_value.read.return_value = '{"testkey": "testvalue"}'

assert zap._request('http://allizom.org', {'querykey': 'queryvalue'}) == {'testkey': 'testvalue'}
assert urllib_mock.mock_calls == [
call('http://allizom.org?querykey=queryvalue', proxies=TEST_PROXIES),
call().read()
]

0 comments on commit 08a1325

Please sign in to comment.