-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to supply apikey on all reqs and for 2.6.0
- Loading branch information
Showing
4 changed files
with
105 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
from mock import patch | ||
import pytest | ||
|
||
import requests_mock | ||
|
||
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() | ||
yield ZAPv2(apikey='testapikey') | ||
|
||
|
||
@pytest.yield_fixture | ||
def urllib_mock(): | ||
@pytest.yield_fixture(autouse=True) | ||
def client_mock(): | ||
"""Fixture create a mock for urllib library.""" | ||
with patch('zapv2.urllib.urlopen') as urllib_mock: | ||
yield urllib_mock | ||
with requests_mock.mock() as mock: | ||
yield mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,53 @@ | ||
""" | ||
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' | ||
def assert_api_key(response, apikey='testapikey'): | ||
"""Some requests should contain valid ZAP api key.""" | ||
assert response._request.headers['X-ZAP-API-Key'] == apikey | ||
assert 'apikey=%s' % apikey in response.query | ||
|
||
assert zap.urlopen() == 'contents' | ||
assert urllib_mock.mock_calls[0][2]['proxies'] == TEST_PROXIES | ||
|
||
def test_urlopen(zap, client_mock): | ||
"""Request method should return a python object from parsed output""" | ||
api_response ='{"testkey": "testvalue"}' | ||
client_mock.get('http://localhost:8080', text=api_response) | ||
|
||
assert zap.urlopen('http://localhost:8080', {'querykey': 'queryvalue'}) == api_response | ||
|
||
response = client_mock.request_history[0] | ||
|
||
assert 'X-ZAP-API-Key' not in response._request.headers | ||
assert 'testapikey' not in response.query | ||
assert response.proxies == TEST_PROXIES | ||
|
||
|
||
def test_request_response(zap, urllib_mock): | ||
def test_request_response(zap, client_mock): | ||
"""Request method should return a python object from parsed output""" | ||
urllib_mock.return_value.read.return_value = '{"testkey": "testvalue"}' | ||
client_mock.get('http://zap/test', text='{"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() | ||
] | ||
assert zap._request('http://zap/test', {'querykey': 'queryvalue'}) == {'testkey': 'testvalue'} | ||
|
||
response = client_mock.request_history[0] | ||
|
||
def test_request_other(zap, urllib_mock): | ||
assert_api_key(response) | ||
assert response.proxies == TEST_PROXIES | ||
|
||
|
||
def test_request_other(zap, client_mock): | ||
"""_request_other should simply return a retrieved content.""" | ||
urllib_mock.return_value.read.return_value = '{"testkey": "testvalue"}' | ||
api_response = '{"testkey": "testvalue"}' | ||
client_mock.get('http://zap/test', text=api_response) | ||
|
||
assert zap._request_other('http://zap/test', {'querykey': 'queryvalue'}) == api_response | ||
|
||
response = client_mock.request_history[0] | ||
|
||
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() | ||
] | ||
assert_api_key(response) | ||
assert response.proxies == TEST_PROXIES |