Skip to content

Commit

Permalink
Allow the sensitive URL regex to be specified in a configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
davehunt committed Oct 8, 2015
1 parent d2b7f89 commit fe6efb0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Release Notes
=============

**1.0b3**

* Allow the sensitive URL regex to be specified in a configuration file.

**1.0b2 (2015-10-06)**

* Added support for non ASCII characters in log files. (#33)
Expand Down
17 changes: 12 additions & 5 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,23 @@ Indicating Sensitive Environments

Sensitive environments are indicated by a regular expression applied to the
base URL or any URLs discovered in the history of redirects when retrieving
the base URL. By default this matches all URLs, but can be configured on by
setting the ``SENSITIVE_URL`` environment variable, or by using the command
line:
the base URL. By default this matches all URLs, but can be configured by
setting the ``SENSITIVE_URL`` environment variable, using a configuration file,
or by using the command line.

An example using a configuration file:

.. code-block:: ini
[pytest]
sensitive_url = example\.com
An example using the command line:

.. code-block:: bash
$ py.test --sensitive-url "example\.com"
To disable skipping for senstive URLs, this can be set to ``None``.

Specifying a Browser
********************

Expand Down
2 changes: 1 addition & 1 deletion pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def format_log(log):


def pytest_addoption(parser):
parser.addini('base_url', help='base url')
parser.addini('base_url', help='base url for the application under test.')

_capture_choices = ('never', 'failure', 'always')
parser.addini('selenium_capture_debug',
Expand Down
18 changes: 14 additions & 4 deletions pytest_selenium/safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@


def pytest_addoption(parser):
parser.addini('sensitive_url',
help='regular expression for identifying sensitive urls.')

group = parser.getgroup('safety', 'safety')
group._addoption('--sensitive-url',
default=os.environ.get('SENSITIVE_URL', '.*'),
help='regular expression for identifying sensitive urls.'
' (default: %default)')
help='regular expression for identifying sensitive urls.')


def pytest_configure(config):
Expand All @@ -35,14 +36,23 @@ def sensitive_url(request, base_url):
return False
# consider this environment sensitive if the base url or any redirection
# history matches the regular expression
sensitive = '.*'
config = request.config
if config.option.sensitive_url:
sensitive = config.option.sensitive_url
elif config.getini('sensitive_url'):
sensitive = config.getini('sensitive_url')
elif os.getenv('SENSITIVE_URL'):
sensitive = os.getenv('SENSITIVE_URL')

urls = [base_url]
try:
response = requests.get(base_url)
urls.append(response.url)
urls.extend([history.url for history in response.history])
except requests.exceptions.RequestException:
pass # ignore exceptions if this URL is unreachable
search = partial(re.search, request.config.option.sensitive_url)
search = partial(re.search, sensitive)
matches = map(search, urls)
if any(matches):
# return the first match
Expand Down
37 changes: 34 additions & 3 deletions testing/test_destructive.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ def test_skip_destructive_by_default(testdir):
testdir.quick_qa(file_test, passed=0, failed=0, skipped=1)


def test_skip_destructive_when_sensitive_command_line(testdir, webserver):
file_test = testdir.makepyfile('def test_pass(): pass')
testdir.quick_qa('--sensitive-url', 'localhost', file_test, passed=0,
failed=0, skipped=1)


def test_skip_destructive_when_sensitive_config_file(testdir, webserver):
testdir.makefile('.ini', pytest='[pytest]\nsensitive_url=localhost')
file_test = testdir.makepyfile('def test_pass(): pass')
testdir.quick_qa(file_test, passed=0, failed=0, skipped=1)


def test_skip_destructive_when_sensitive_env(testdir, webserver, monkeypatch):
monkeypatch.setenv('SENSITIVE_URL', 'localhost')
file_test = testdir.makepyfile('def test_pass(): pass')
testdir.quick_qa(file_test, passed=0, failed=0, skipped=1)


def test_run_non_destructive_by_default(testdir):
file_test = testdir.makepyfile("""
import pytest
Expand All @@ -21,9 +39,22 @@ def test_pass(): pass
testdir.quick_qa(file_test, passed=1)


def test_run_destructive_when_not_sensitive(testdir):
def test_run_destructive_when_not_sensitive_command_line(testdir, webserver):
file_test = testdir.makepyfile('def test_pass(): pass')
testdir.quick_qa('--sensitive-url', 'foo', file_test, passed=1)


def test_run_destructive_when_not_sensitive_config_file(testdir, webserver):
testdir.makefile('.ini', pytest='[pytest]\nsensitive_url=foo')
file_test = testdir.makepyfile('def test_pass(): pass')
testdir.quick_qa(file_test, passed=1, failed=0, skipped=0)


def test_run_destructive_when_not_sensitive_env(testdir, webserver,
monkeypatch):
monkeypatch.setenv('SENSITIVE_URL', 'foo')
file_test = testdir.makepyfile('def test_pass(): pass')
testdir.quick_qa('--sensitive-url', None, file_test, passed=1)
testdir.quick_qa(file_test, passed=1, failed=0, skipped=0)


def test_run_destructive_and_non_destructive_when_not_sensitive(testdir):
Expand All @@ -33,4 +64,4 @@ def test_run_destructive_and_non_destructive_when_not_sensitive(testdir):
def test_pass1(): pass
def test_pass2(): pass
""")
testdir.quick_qa('--sensitive-url', None, file_test, passed=2)
testdir.quick_qa('--sensitive-url', 'foo', file_test, passed=2)

0 comments on commit fe6efb0

Please sign in to comment.