Skip to content

Commit

Permalink
SSL verification and plugin disabling
Browse files Browse the repository at this point in the history
1) if connection to Jira wasn't established, plugin won't be loaded
2) it's possible to disable ssl verification to jira
  • Loading branch information
vkondula committed Jun 6, 2016
1 parent b7408b4 commit 795a169
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ submitting feature requests or issues to [issues][githubissues].

## Requires
* pytest >= 2.2.3
* jira >= 0.13
* jira >= 0.43
* six

## Installation
Expand All @@ -43,6 +43,7 @@ submitting feature requests or issues to [issues][githubissues].
url = https://jira.atlassian.com
username = USERNAME (or blank for no authentication)
password = PASSWORD (or blank for no authentication)
# ssl_verification = True/False
```

Options can be overridden with command line options.
Expand Down
40 changes: 33 additions & 7 deletions pytest_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
import re
import six
import pytest
import logging
from jira.client import JIRA

logger = logging.getLogger('pytest_jira')

class JiraHooks(object):
issue_re = r"([A-Z]+-[0-9]+)"

def __init__(self, url, username=None, password=None):
def __init__(self, url, username=None, password=None, verify=True):
self.url = url
self.username = username
self.password = password
self.verify = verify

# Speed up JIRA lookups for duplicate issues
self.issue_cache = dict()
Expand All @@ -35,8 +38,15 @@ def __init__(self, url, username=None, password=None):
# TODO - use requests REST API instead to drop a dependency
# (https://confluence.atlassian.com/display/DOCSPRINT/
# The+Simplest+Possible+JIRA+REST+Examples)
self.jira = JIRA(options=dict(server=self.url),
basic_auth=basic_auth)
try:
self.jira = JIRA(options=dict(server=self.url, verify=self.verify),
basic_auth=basic_auth, validate=True, max_retries=1)
except Exception as ex:
logger.error('Unable to connect to Jira: %s', ex)
self.jira = None

def is_connected(self):
return self.jira is not None

def get_jira_issues(self, item):
issue_pattern = re.compile(self.issue_re)
Expand Down Expand Up @@ -146,6 +156,14 @@ def pytest_addoption(parser):
if os.path.exists('jira.cfg'):
config.read('jira.cfg')

try:
verify = config.getboolean('DEFAULT', 'ssl_verification')
except six.moves.configparser.NoOptionError:
verify = True
except ValueError:
logger.error('Wrong argument for ssl_verification!')
verify = True

group.addoption('--jira-url',
action='store',
dest='jira_url',
Expand All @@ -164,6 +182,12 @@ def pytest_addoption(parser):
default=_get_value(config, 'DEFAULT', 'password'),
metavar='password',
help='JIRA password.')
group.addoption('--jira-no-verification',
action='store_false',
dest='jira_verify',
default=verify,
help='Disable SSL verification to Jira'
)


def pytest_configure(config):
Expand All @@ -181,10 +205,12 @@ def pytest_configure(config):
"When 'run' is False, the test will be skipped prior to execution. "
"See https://github.com/rhevm-qe-automation/pytest_jira"
)

if config.getvalue("jira") and config.getvalue('jira_url'):
jira_plugin = JiraHooks(config.getvalue('jira_url'),
config.getvalue('jira_username'),
config.getvalue('jira_password'))
ok = config.pluginmanager.register(jira_plugin, "jira_plugin")
assert ok
config.getvalue('jira_password'),
config.getvalue('jira_verify'))
if jira_plugin.is_connected():
# if connection to jira fails, plugin won't be loaded
ok = config.pluginmanager.register(jira_plugin)
assert ok
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest>=2.2.4
jira>=0.13
jira>=0.43
six
ordereddict; python_version=='2.6'

0 comments on commit 795a169

Please sign in to comment.