Skip to content

Commit 89a72c7

Browse files
authored
Merge pull request #8 from jotes/add_tests
added travis and linter.
2 parents e34d8c3 + 08a1325 commit 89a72c7

File tree

9 files changed

+88
-38
lines changed

9 files changed

+88
-38
lines changed

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sudo: false
2+
language: python
3+
python:
4+
- '2.7'
5+
install:
6+
- pip install -U --force setuptools pip
7+
- ./setup.py develop
8+
- pip install -e '.[tests]'
9+
10+
script:
11+
- pylama
12+
- py.test

pylama.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pylama]
2+
linters = pyflakes
3+
4+
[pylama:doc/conf.py]
5+
skip = 1

setup.py

100644100755
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
print "You must have setuptools installed to use setup.py. Exiting..."
1313
raise SystemExit(1)
1414

15-
15+
test_requirements = [
16+
'mock',
17+
'pylama',
18+
'pytest'
19+
]
1620
setup(
1721
name="python-owasp-zap-v2.4",
1822
version="0.0.9.dev1",
@@ -39,4 +43,7 @@
3943
'Intended Audience :: Developers',
4044
'Intended Audience :: Information Technology',
4145
'Programming Language :: Python'],
46+
47+
tests_require=test_requirements,
48+
extras_require={'tests': test_requirements}
4249
)

src/examples/zap-baseline.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import time
5454
import traceback
5555
import urllib2
56-
from datetime import datetime
5756
from random import randint
5857
from zapv2 import ZAPv2
5958

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

352351
# Wait for passive scanning to complete
353-
rtc = zap.pscan.records_to_scan
354352
logging.debug ('Records to scan...')
355353
while (int(zap.pscan.records_to_scan) > 0):
356354
logging.debug ('Records to passive scan : ' + zap.pscan.records_to_scan)

src/zapv2/__init__.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,14 @@
4747
from stats import stats
4848
from users import users
4949

50-
class ZapError(Exception):
51-
"""
52-
Base ZAP exception.
53-
"""
54-
pass
55-
5650

5751
class ZAPv2(object):
5852
"""
5953
Client API implementation for integrating with ZAP v2.
6054
"""
61-
6255
# base JSON api url
6356
base = 'http://zap/JSON/'
57+
6458
# base OTHER api url
6559
base_other = 'http://zap/OTHER/'
6660

@@ -71,12 +65,12 @@ def __init__(self, proxies={'http': 'http://127.0.0.1:8080',
7165
7266
:Parameters:
7367
- `proxies`: dictionary of ZAP proxies to use.
74-
68+
7569
Note that all of the other classes in this directory are generated
7670
new ones will need to be manually added to this file
7771
"""
7872
self.__proxies = proxies
79-
73+
8074
self.acsrf = acsrf(self)
8175
self.ajaxSpider = ajaxSpider(self)
8276
self.ascan = ascan(self)
@@ -101,17 +95,6 @@ def __init__(self, proxies={'http': 'http://127.0.0.1:8080',
10195
self.stats = stats(self)
10296
self.users = users(self)
10397

104-
def _expect_ok(self, json_data):
105-
"""
106-
Checks that we have an OK response, else raises an exception.
107-
108-
:Parameters:
109-
- `json_data`: the json data to look at.
110-
"""
111-
if type(json_data) == type(list()) and json_data[0] == u'OK':
112-
return json_data
113-
raise ZapError(*json_data.values())
114-
11598
def urlopen(self, *args, **kwargs):
11699
"""
117100
Opens a url forcing the proxies to be used.
@@ -123,26 +106,15 @@ def urlopen(self, *args, **kwargs):
123106
kwargs['proxies'] = self.__proxies
124107
return urllib.urlopen(*args, **kwargs).read()
125108

126-
def status_code(self, *args, **kwargs):
127-
"""
128-
Open a url forcing the proxies to be used.
129-
130-
:Parameters:
131-
- `args`: all non-keyword arguments.
132-
- `kwargs`: all other keyword arguments.
133-
"""
134-
kwargs['proxies'] = self.__proxies
135-
return urllib.urlopen(*args, **kwargs).getcode()
136-
137-
def _request(self, url, get={}):
109+
def _request(self, url, get=None):
138110
"""
139111
Shortcut for a GET request.
140112
141113
:Parameters:
142114
- `url`: the url to GET at.
143115
- `get`: the disctionary to turn into GET variables.
144116
"""
145-
return json.loads(self.urlopen(url + '?' + urllib.urlencode(get)))
117+
return json.loads(self.urlopen(url + '?' + urllib.urlencode(get or {})))
146118

147119
def _request_other(self, url, get={}):
148120
"""
@@ -152,4 +124,4 @@ def _request_other(self, url, get={}):
152124
- `url`: the url to GET at.
153125
- `get`: the disctionary to turn into GET variables.
154126
"""
155-
return self.urlopen(url + '?' + urllib.urlencode(get))
127+
return self.urlopen(url + '?' + urllib.urlencode(get or {}))

tests/__init__.py

Whitespace-only changes.

tests/unit/__init__.py

Whitespace-only changes.

tests/unit/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from mock import patch
2+
import pytest
3+
4+
from zapv2 import ZAPv2
5+
6+
@pytest.yield_fixture
7+
def zap():
8+
"""
9+
All tests will be able to share the instance of client with the same settings."""
10+
yield ZAPv2()
11+
12+
13+
@pytest.yield_fixture
14+
def urllib_mock():
15+
"""Fixture create a mock for urllib library."""
16+
with patch('zapv2.urllib.urlopen') as urllib_mock:
17+
yield urllib_mock

tests/unit/test_client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Tests related to the main Zap Client class
3+
"""
4+
from mock import call
5+
6+
TEST_PROXIES = {
7+
'http': 'http://127.0.0.1:8080',
8+
'https': 'http://127.0.0.1:8080',
9+
}
10+
11+
12+
def test_urlopen_proxies(zap, urllib_mock):
13+
"""Check if Zap client passes proxy to urllib call."""
14+
urllib_mock.return_value.read.return_value = 'contents'
15+
16+
assert zap.urlopen() == 'contents'
17+
assert urllib_mock.mock_calls[0][2]['proxies'] == TEST_PROXIES
18+
19+
20+
def test_request_response(zap, urllib_mock):
21+
"""Request method should return a python object from parsed output"""
22+
urllib_mock.return_value.read.return_value = '{"testkey": "testvalue"}'
23+
24+
assert zap._request('http://allizom.org', {'querykey': 'queryvalue'}) == {'testkey': 'testvalue'}
25+
assert urllib_mock.mock_calls == [
26+
call('http://allizom.org?querykey=queryvalue', proxies=TEST_PROXIES),
27+
call().read()
28+
]
29+
30+
31+
def test_request_other(zap, urllib_mock):
32+
"""_request_other should simply return a retrieved content."""
33+
urllib_mock.return_value.read.return_value = '{"testkey": "testvalue"}'
34+
35+
assert zap._request('http://allizom.org', {'querykey': 'queryvalue'}) == {'testkey': 'testvalue'}
36+
assert urllib_mock.mock_calls == [
37+
call('http://allizom.org?querykey=queryvalue', proxies=TEST_PROXIES),
38+
call().read()
39+
]

0 commit comments

Comments
 (0)