httpie-cli/tests/test_auth.py

76 lines
2.3 KiB
Python
Raw Normal View History

2014-04-24 14:07:31 +02:00
"""HTTP authentication-related tests."""
2016-03-03 11:47:12 +01:00
import mock
2014-04-24 15:17:23 +02:00
import pytest
2014-04-24 14:07:31 +02:00
from utils import http, add_auth, HTTP_OK, TestEnvironment
2014-04-24 14:07:31 +02:00
import httpie.input
import httpie.cli
2014-04-24 14:07:31 +02:00
def test_basic_auth(httpbin_both):
2016-03-03 11:47:12 +01:00
r = http('--auth=user:password',
'GET', httpbin_both + '/basic-auth/user/password')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
@pytest.mark.parametrize('argument_name', ['--auth-type', '-A'])
def test_digest_auth(httpbin_both, argument_name):
2016-03-03 11:47:12 +01:00
r = http(argument_name + '=digest', '--auth=user:password',
'GET', httpbin_both.url + '/digest-auth/auth/user/password')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
@mock.patch('httpie.input.AuthCredentials._getpass',
new=lambda self, prompt: 'password')
def test_password_prompt(httpbin):
r = http('--auth', 'user',
'GET', httpbin.url + '/basic-auth/user/password')
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
def test_credentials_in_url(httpbin_both):
url = add_auth(httpbin_both.url + '/basic-auth/user/password',
2016-03-03 11:47:12 +01:00
auth='user:password')
r = http('GET', url)
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
def test_credentials_in_url_auth_flag_has_priority(httpbin_both):
2016-03-03 11:47:12 +01:00
"""When credentials are passed in URL and via -a at the same time,
then the ones from -a are used."""
url = add_auth(httpbin_both.url + '/basic-auth/user/password',
2016-03-03 11:47:12 +01:00
auth='user:wrong')
r = http('--auth=user:password', 'GET', url)
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
@pytest.mark.parametrize('url', [
'username@example.org',
'username:@example.org',
])
def test_only_username_in_url(url):
"""
2017-03-10 11:27:38 +01:00
https://github.com/jakubroztocil/httpie/issues/242
2016-03-03 11:47:12 +01:00
"""
args = httpie.cli.parser.parse_args(args=[url], env=TestEnvironment())
assert args.auth
assert args.auth.username == 'username'
assert args.auth.password == ''
2016-11-23 23:09:45 +01:00
def test_missing_auth(httpbin):
r = http(
'--auth-type=basic',
'GET',
httpbin + '/basic-auth/user/password',
error_exit_ok=True
)
assert HTTP_OK not in r
assert '--auth required' in r.stderr