Skip to content

Commit

Permalink
Merge pull request #7 from fundakol/cert-verification
Browse files Browse the repository at this point in the history
added support for SSL Verification in Jira authentication
  • Loading branch information
fundakol committed Apr 24, 2021
2 parents 511ed08 + 24b7e20 commit f7d8d24
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ def test_one():
assert True
```

Set system environment:
```commandline
Set system environments (Basic authentication):
```shell
export XRAY_API_BASE_URL=<jira URL>
export XRAY_API_USER=<jria username>
export XRAY_API_PASSWORD=<user password>
```

To disable SSL certificate verification, at the client side (no case-sensitive):
```shell
export XRAY_API_VERIFY_SSL=False
```

Or you can provide path to certificate file
```shell
export XRAY_API_VERIFY_SSL=</path/to/PEM file>
```

Upload results to new test execution:
```commandline
pytest . --jira-xray
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name=pytest-jira-xray
version=0.1.1
version=0.1.2
url=https://github.com/fundakol/pytest-jira-xray
author=Lukasz Fundakowski
author_email[email protected]
Expand Down
30 changes: 26 additions & 4 deletions src/pytest_xray/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from os import environ
from typing import List
from typing import List, Dict, Any

from _pytest.config import Config
from _pytest.config.argparsing import Parser
Expand All @@ -15,14 +16,35 @@
from pytest_xray.xray_publisher import XrayPublisher


def get_request_options() -> Dict[str, Any]:
options = {}
jira_url = environ["XRAY_API_BASE_URL"]
user, password = environ["XRAY_API_USER"], environ["XRAY_API_PASSWORD"]
verify = os.environ.get('XRAY_API_VERIFY_SSL', 'True')
if verify.upper() == 'TRUE':
verify = True
elif verify.upper() == 'FALSE':
verify = False
else:
if not os.path.exists(verify):
raise FileNotFoundError(f'Cannot find certificate file "{verify}"')
options['BASE_URL'] = jira_url
options['USER'] = user
options['PASSWORD'] = password
options['VERIFY'] = verify

return options


def pytest_configure(config: Config) -> None:
if not config.getoption(JIRA_XRAY_FLAG):
return

jira_url = environ["XRAY_API_BASE_URL"]
auth = (environ["XRAY_API_USER"], environ["XRAY_API_PASSWORD"])
options = get_request_options()
plugin = XrayPublisher(base_url=options['BASE_URL'],
auth=(options['USER'], options['PASSWORD']),
verify=options['VERIFY'])

plugin = XrayPublisher(base_url=jira_url, auth=auth)
config.pluginmanager.register(plugin, XRAY_PLUGIN)

config.addinivalue_line(
Expand Down
12 changes: 8 additions & 4 deletions src/pytest_xray/xray_publisher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
from typing import Union

Expand All @@ -19,9 +18,14 @@ class XrayError(Exception):

class XrayPublisher:

def __init__(self, base_url: str, auth: Union[AuthBase, tuple]) -> None:
def __init__(self, base_url: str,
auth: Union[AuthBase, tuple],
verify: Union[bool, str] = True) -> None:
if base_url.endswith('/'):
base_url = base_url[:-1]
self.base_url = base_url
self.auth = auth
self.verify = verify

@property
def endpoint_url(self) -> str:
Expand All @@ -30,9 +34,9 @@ def endpoint_url(self) -> str:
def publish_xray_results(self, url: str, auth: AuthBase, data: dict) -> dict:
headers = {'Accept': 'application/json',
'Content-Type': 'application/json'}
data = json.dumps(data)
try:
response = requests.request(method='POST', url=url, headers=headers, data=data, auth=auth)
response = requests.request(method='POST', url=url, headers=headers, json=data,
auth=auth, verify=self.verify)
except requests.exceptions.ConnectionError as e:
_logger.exception('ConnectionError to JIRA service %s', self.base_url)
raise XrayError(e)
Expand Down

0 comments on commit f7d8d24

Please sign in to comment.