rhevm-qe-automation / pytest_jira

py.test plugin to integrate with JIRA
GNU General Public License v2.0
29 stars 25 forks source link

Okta authentication service causes JSONDecodeError #124

Open JaroslawKaczor opened 2 years ago

JaroslawKaczor commented 2 years ago

When I'm starting tests from outside the internal company network, then Jira redirects to the Okta authentication service, and JSONDecodeError is raised, because in the response data there is no valid JSON from Jira, but HTML from the authentication service.

The check_connection function fails: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) at the 223 line: elif not r.json()['permissions']['BROWSE_PROJECTS']['havePermission']

r.text variable (from pytest_jira.py, line 216) looks like this:

'<html>\n 
<head>\n
</head>\n
<body onload="document.forms[0].submit()">\n           
<noscript><p>\n
<strong>Note:</strong> Your browser does not support JavaScript,\n                
Press Continue to proceed... </p>\n
</noscript>\n            
<form method="POST" action="https://dolby.okta.com/app/###edited###/sso/saml">\n
<div>\n
<input type="hidden" name="SAMLRequest" value="PHNhbW###edited###0Pg=="/> </div>\n                
<noscript><div>\n
<input type="submit" value="Continue"/>\n                
</div>\n
</noscript>\n</form>\n
</body>\n
</html>'

so it's a not a JSON response, as expected.

It would be great to add additional check, if the response content is valid, and if not, then behave same way as there is no access to the Jira server.

Stacktrace:

INTERNALERROR>   File "/venv/lib/python3.8/site-packages/_pytest/main.py", line 240, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/_pytest/main.py", line 295, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
INTERNALERROR>     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/_pytest/main.py", line 306, in pytest_collection
INTERNALERROR>     session.perform_collect()
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/_pytest/main.py", line 518, in perform_collect
INTERNALERROR>     hook.pytest_collection_modifyitems(
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
INTERNALERROR>     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pytest_jira.py", line 114, in pytest_collection_modifyitems
INTERNALERROR>     if not self.is_issue_resolved(issue_id):
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pytest_jira.py", line 72, in is_issue_resolved
INTERNALERROR>     self.issue_cache[issue_id] = self.conn.get_issue(
INTERNALERROR>   File "<decorator-gen-2>", line 2, in get_issue
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/retry/api.py", line 73, in retry_decorator
INTERNALERROR>     return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/retry/api.py", line 33, in __retry_internal
INTERNALERROR>     return f()
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pytest_jira.py", line 233, in get_issue
INTERNALERROR>     self.check_connection()
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/pytest_jira.py", line 223, in check_connection
INTERNALERROR>     elif not r.json()['permissions']['BROWSE_PROJECTS']['havePermission']:
INTERNALERROR>   File "/venv/lib/python3.8/site-packages/requests/models.py", line 900, in json
INTERNALERROR>     return complexjson.loads(self.text, **kwargs)
INTERNALERROR>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
INTERNALERROR>     return _default_decoder.decode(s)
INTERNALERROR>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
INTERNALERROR>     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
INTERNALERROR>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode
INTERNALERROR>     raise JSONDecodeError("Expecting value", s, err.value) from None
INTERNALERROR> json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)