def test_no_cookies(tmpdir): JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) assert dict() == _load_cookies(JIRA.COOKIE_CACHE_FILE_PATH) def session_callback(request, _, headers): assert 'user:pass' == base64.b64decode( request.headers['Authorization'].split(' ')[-1]).decode('ascii') headers['Set-Cookie'] = 'JSESSIONID=ABC123; Path=/' return 200, headers, '{}' httpretty.register_uri(httpretty.GET, re.compile('.*/serverInfo'), body='{"versionNumbers":[6,4,0]}') httpretty.register_uri(httpretty.POST, re.compile('.*/session'), body=session_callback) with JIRA() as j: assert j.ABORTED_BY_USER is False assert j.authentication_failed is False assert getattr(j, '_JIRA__authenticated_with_cookies') is False assert getattr(j, '_JIRA__authenticated_with_password') is True assert dict(JSESSIONID='ABC123') == _load_cookies( JIRA.COOKIE_CACHE_FILE_PATH)
def test_force_user(tmpdir): jira_context._prompt = lambda f, p: ( 0 / 0 ) if f == INPUT else 'pass' # ZeroDivisionError if prompted for user. JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) JIRA.FORCE_USER = '******' assert dict() == _load_cookies(JIRA.COOKIE_CACHE_FILE_PATH) def session_callback(request, _, headers): assert 'test_account:pass' == base64.b64decode( request.headers['Authorization'].split(' ')[-1]).decode('ascii') headers['Set-Cookie'] = 'JSESSIONID=ABC123; Path=/' return 200, headers, '{}' httpretty.register_uri(httpretty.GET, re.compile('.*/serverInfo'), body='{"versionNumbers":[6,4,0]}') httpretty.register_uri(httpretty.POST, re.compile('.*/session'), body=session_callback) with JIRA() as j: assert j.ABORTED_BY_USER is False assert j.authentication_failed is False assert getattr(j, '_JIRA__authenticated_with_cookies') is False assert getattr(j, '_JIRA__authenticated_with_password') is True assert dict(JSESSIONID='ABC123') == _load_cookies( JIRA.COOKIE_CACHE_FILE_PATH)
def test_unknown_error_give_up(tmpdir, capsys): JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) assert dict() == _load_cookies(JIRA.COOKIE_CACHE_FILE_PATH) def session_callback(request, _, headers): assert 'user:pass' == base64.b64decode( request.headers['Authorization'].split(' ')[-1]).decode('ascii') jira_context._prompt = lambda *_: '' # Simulate an empty user/pass on the next iteration. return 500, headers, '{}' httpretty.register_uri(httpretty.GET, re.compile('.*/serverInfo'), body='{"versionNumbers":[6,4,0]}') httpretty.register_uri(httpretty.POST, re.compile('.*/session'), body=session_callback) with JIRA() as j: assert j.ABORTED_BY_USER is True assert j.authentication_failed is True assert getattr(j, '_JIRA__authenticated_with_cookies') is False assert getattr(j, '_JIRA__authenticated_with_password') is False assert dict() == _load_cookies(JIRA.COOKIE_CACHE_FILE_PATH) stdout, stderr = capsys.readouterr() assert '' == stdout assert ('Error occurred, try again.\n' == stderr)
def test_no_prompt_no_cookies(tmpdir): JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) with JIRA(prompt_for_credentials=False) as j: assert j.ABORTED_BY_USER is False assert j.authentication_failed is True assert getattr(j, '_JIRA__authenticated_with_cookies') is False assert getattr(j, '_JIRA__authenticated_with_password') is False assert dict() == _load_cookies(JIRA.COOKIE_CACHE_FILE_PATH)
def test_no_prompt_good_cookies(tmpdir): JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) _save_cookies(JIRA.COOKIE_CACHE_FILE_PATH, dict(JSESSIONID='ABC123')) stub_get(re.compile('.*/serverInfo'), body='{"versionNumbers":[6,4,0]}') stub_get(re.compile('.*/session'), body='{}', status=200) with JIRA(prompt_for_credentials=False) as j: assert 'JSESSIONID=ABC123' == last_request().headers['Cookie'] assert j.ABORTED_BY_USER is False assert j.authentication_failed is False assert getattr(j, '_JIRA__authenticated_with_cookies') is True assert getattr(j, '_JIRA__authenticated_with_password') is False assert dict(JSESSIONID='ABC123') == _load_cookies(JIRA.COOKIE_CACHE_FILE_PATH)
def test_bad_cookies_bad_password_x2_good_password_success(tmpdir, capsys): JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) _save_cookies(JIRA.COOKIE_CACHE_FILE_PATH, dict(JSESSIONID='ABC000')) def first_session_callback(request, _, headers): assert 'Authorization' not in request.headers assert 'JSESSIONID=ABC000' == request.headers['Cookie'] return 401, headers, '{}' def second_and_third_session_callback(request, _, headers): assert 'user:pass' == base64.b64decode( request.headers['Authorization'].split(' ')[-1]).decode('ascii') return 401, headers, '{}' def fourth_session_callback(request, _, headers): assert 'user:pass' == base64.b64decode( request.headers['Authorization'].split(' ')[-1]).decode('ascii') headers['Set-Cookie'] = 'JSESSIONID=ABC123; Path=/' return 200, headers, '{}' responses = [ httpretty.Response(body=f) for f in (second_and_third_session_callback, second_and_third_session_callback, fourth_session_callback) ] httpretty.register_uri(httpretty.GET, re.compile('.*/serverInfo'), body='{"versionNumbers":[6,4,0]}') httpretty.register_uri(httpretty.GET, re.compile('.*/session'), body=first_session_callback) httpretty.register_uri(httpretty.POST, re.compile('.*/session'), responses=responses) with JIRA() as j: assert j.ABORTED_BY_USER is False assert j.authentication_failed is False assert getattr(j, '_JIRA__authenticated_with_cookies') is False assert getattr(j, '_JIRA__authenticated_with_password') is True assert dict(JSESSIONID='ABC123') == _load_cookies( JIRA.COOKIE_CACHE_FILE_PATH) stdout, stderr = capsys.readouterr() assert '' == stdout assert ( 'Authentication failed or bad password, try again.\nAuthentication failed or bad password, try again.\n' == stderr)
def test_aborted_during_password(tmpdir): jira_context._prompt = lambda m, p: 'user' if m == INPUT else '' JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) with JIRA() as j: assert j.ABORTED_BY_USER is True
def test_aborted_before(tmpdir): JIRA.ABORTED_BY_USER = True JIRA.COOKIE_CACHE_FILE_PATH = str(tmpdir.join('.jira_session_json')) with JIRA() as j: assert j.ABORTED_BY_USER is True