2014-04-24 14:07:31 +02:00
|
|
|
"""HTTP authentication-related tests."""
|
|
|
|
import requests
|
2014-04-24 15:17:23 +02:00
|
|
|
import pytest
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-07-18 13:38:33 +02:00
|
|
|
from utils import http, add_auth, HTTP_OK, TestEnvironment
|
2014-04-24 14:07:31 +02:00
|
|
|
import httpie.input
|
2014-07-18 13:38:33 +02:00
|
|
|
import httpie.cli
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-26 23:04:45 +02:00
|
|
|
class TestAuth:
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_basic_auth(self, httpbin):
|
2014-04-26 23:04:45 +02:00
|
|
|
r = http('--auth=user:password',
|
2014-06-28 16:35:57 +02:00
|
|
|
'GET', httpbin.url + '/basic-auth/user/password')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-26 23:04:45 +02:00
|
|
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-04-24 15:17:23 +02:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
requests.__version__ == '0.13.6',
|
|
|
|
reason='Redirects with prefetch=False are broken in Requests 0.13.6')
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_digest_auth(self, httpbin):
|
2014-04-26 23:04:45 +02:00
|
|
|
r = http('--auth-type=digest', '--auth=user:password',
|
2014-06-28 16:35:57 +02:00
|
|
|
'GET', httpbin.url + '/digest-auth/auth/user/password')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-26 23:04:45 +02:00
|
|
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_password_prompt(self, httpbin):
|
2014-04-24 14:07:31 +02:00
|
|
|
httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password'
|
2014-06-28 16:35:57 +02:00
|
|
|
r = http('--auth', 'user',
|
|
|
|
'GET', httpbin.url + '/basic-auth/user/password')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-26 23:04:45 +02:00
|
|
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_credentials_in_url(self, httpbin):
|
|
|
|
url = add_auth(httpbin.url + '/basic-auth/user/password',
|
|
|
|
auth='user:password')
|
2014-04-26 15:06:51 +02:00
|
|
|
r = http('GET', url)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-26 23:04:45 +02:00
|
|
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_credentials_in_url_auth_flag_has_priority(self, httpbin):
|
2014-04-24 14:07:31 +02:00
|
|
|
"""When credentials are passed in URL and via -a at the same time,
|
|
|
|
then the ones from -a are used."""
|
2014-06-28 16:35:57 +02:00
|
|
|
url = add_auth(httpbin.url + '/basic-auth/user/password',
|
|
|
|
auth='user:wrong')
|
2014-04-26 15:06:51 +02:00
|
|
|
r = http('--auth=user:password', 'GET', url)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-26 23:04:45 +02:00
|
|
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
2014-07-18 13:38:33 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('url', [
|
|
|
|
'username@example.org',
|
|
|
|
'username:@example.org',
|
|
|
|
])
|
|
|
|
def test_only_username_in_url(self, url):
|
|
|
|
"""
|
2015-07-03 18:55:45 +02:00
|
|
|
https://github.com/jkbrzt/httpie/issues/242
|
2014-07-18 13:38:33 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
args = httpie.cli.parser.parse_args(args=[url], env=TestEnvironment())
|
|
|
|
assert args.auth
|
|
|
|
assert args.auth.key == 'username'
|
|
|
|
assert args.auth.value == ''
|