2021-04-30 15:08:27 +02:00
|
|
|
from unittest import mock
|
2016-11-24 00:09:45 +02:00
|
|
|
|
2019-08-31 15:17:10 +02:00
|
|
|
from httpie.cli.constants import SEPARATOR_CREDENTIALS
|
2020-05-26 10:07:34 +02:00
|
|
|
from httpie.plugins import AuthPlugin
|
|
|
|
from httpie.plugins.registry import plugin_manager
|
2021-05-05 14:13:39 +02:00
|
|
|
from .utils import http, HTTP_OK
|
2016-11-23 23:01:58 +02:00
|
|
|
|
2020-05-26 10:07:34 +02:00
|
|
|
|
2016-11-23 23:01:58 +02:00
|
|
|
# TODO: run all these tests in session mode as well
|
|
|
|
|
2016-11-24 00:09:45 +02:00
|
|
|
USERNAME = 'user'
|
|
|
|
PASSWORD = 'password'
|
|
|
|
# Basic auth encoded `USERNAME` and `PASSWORD`
|
2016-11-24 01:58:41 +02:00
|
|
|
# noinspection SpellCheckingInspection
|
2016-11-24 00:09:45 +02:00
|
|
|
BASIC_AUTH_HEADER_VALUE = 'Basic dXNlcjpwYXNzd29yZA=='
|
2021-05-26 14:09:38 +02:00
|
|
|
BASIC_AUTH_URL = f'/basic-auth/{USERNAME}/{PASSWORD}'
|
2016-11-24 00:09:45 +02:00
|
|
|
AUTH_OK = {'authenticated': True, 'user': USERNAME}
|
2016-11-23 23:01:58 +02:00
|
|
|
|
|
|
|
|
2016-11-24 00:15:18 +02:00
|
|
|
def basic_auth(header=BASIC_AUTH_HEADER_VALUE):
|
2016-11-23 23:01:58 +02:00
|
|
|
|
|
|
|
def inner(r):
|
2016-11-24 00:15:18 +02:00
|
|
|
r.headers['Authorization'] = header
|
2016-11-23 23:01:58 +02:00
|
|
|
return r
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
2016-11-23 23:29:36 +02:00
|
|
|
def test_auth_plugin_parse_auth_false(httpbin):
|
2016-11-23 23:01:58 +02:00
|
|
|
|
2016-11-24 00:09:45 +02:00
|
|
|
class Plugin(AuthPlugin):
|
2016-11-24 00:15:18 +02:00
|
|
|
auth_type = 'test-parse-false'
|
2016-11-23 23:01:58 +02:00
|
|
|
auth_parse = False
|
|
|
|
|
|
|
|
def get_auth(self, username=None, password=None):
|
|
|
|
assert username is None
|
|
|
|
assert password is None
|
|
|
|
assert self.raw_auth == BASIC_AUTH_HEADER_VALUE
|
2016-11-24 00:15:18 +02:00
|
|
|
return basic_auth(self.raw_auth)
|
2016-11-23 23:01:58 +02:00
|
|
|
|
2016-11-24 00:09:45 +02:00
|
|
|
plugin_manager.register(Plugin)
|
2016-11-23 23:01:58 +02:00
|
|
|
try:
|
|
|
|
r = http(
|
|
|
|
httpbin + BASIC_AUTH_URL,
|
2016-11-24 00:09:45 +02:00
|
|
|
'--auth-type',
|
|
|
|
Plugin.auth_type,
|
|
|
|
'--auth',
|
|
|
|
BASIC_AUTH_HEADER_VALUE,
|
2016-11-23 23:01:58 +02:00
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
2016-11-24 00:09:45 +02:00
|
|
|
assert r.json == AUTH_OK
|
2016-11-23 23:01:58 +02:00
|
|
|
finally:
|
2016-11-24 00:09:45 +02:00
|
|
|
plugin_manager.unregister(Plugin)
|
2016-11-23 23:01:58 +02:00
|
|
|
|
|
|
|
|
2016-11-23 23:29:36 +02:00
|
|
|
def test_auth_plugin_require_auth_false(httpbin):
|
2016-11-23 23:01:58 +02:00
|
|
|
|
2016-11-24 00:09:45 +02:00
|
|
|
class Plugin(AuthPlugin):
|
2016-11-24 00:15:18 +02:00
|
|
|
auth_type = 'test-require-false'
|
2016-11-23 23:01:58 +02:00
|
|
|
auth_require = False
|
|
|
|
|
|
|
|
def get_auth(self, username=None, password=None):
|
|
|
|
assert self.raw_auth is None
|
|
|
|
assert username is None
|
|
|
|
assert password is None
|
2016-11-24 00:15:18 +02:00
|
|
|
return basic_auth()
|
2016-11-23 23:01:58 +02:00
|
|
|
|
2016-11-24 00:09:45 +02:00
|
|
|
plugin_manager.register(Plugin)
|
|
|
|
try:
|
|
|
|
r = http(
|
|
|
|
httpbin + BASIC_AUTH_URL,
|
|
|
|
'--auth-type',
|
|
|
|
Plugin.auth_type,
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json == AUTH_OK
|
|
|
|
finally:
|
|
|
|
plugin_manager.unregister(Plugin)
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_plugin_require_auth_false_and_auth_provided(httpbin):
|
|
|
|
|
|
|
|
class Plugin(AuthPlugin):
|
2016-11-24 00:15:18 +02:00
|
|
|
auth_type = 'test-require-false-yet-provided'
|
2016-11-24 00:09:45 +02:00
|
|
|
auth_require = False
|
|
|
|
|
|
|
|
def get_auth(self, username=None, password=None):
|
2019-08-31 15:17:10 +02:00
|
|
|
assert self.raw_auth == USERNAME + SEPARATOR_CREDENTIALS + PASSWORD
|
2016-11-24 00:09:45 +02:00
|
|
|
assert username == USERNAME
|
|
|
|
assert password == PASSWORD
|
2016-11-24 00:15:18 +02:00
|
|
|
return basic_auth()
|
2016-11-24 00:09:45 +02:00
|
|
|
|
|
|
|
plugin_manager.register(Plugin)
|
2016-11-23 23:01:58 +02:00
|
|
|
try:
|
|
|
|
r = http(
|
|
|
|
httpbin + BASIC_AUTH_URL,
|
2016-11-24 00:09:45 +02:00
|
|
|
'--auth-type',
|
|
|
|
Plugin.auth_type,
|
|
|
|
'--auth',
|
2019-08-31 15:17:10 +02:00
|
|
|
USERNAME + SEPARATOR_CREDENTIALS + PASSWORD,
|
2016-11-23 23:01:58 +02:00
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
2016-11-24 00:09:45 +02:00
|
|
|
assert r.json == AUTH_OK
|
2016-11-23 23:01:58 +02:00
|
|
|
finally:
|
2016-11-24 00:09:45 +02:00
|
|
|
plugin_manager.unregister(Plugin)
|
2016-11-23 23:01:58 +02:00
|
|
|
|
|
|
|
|
2019-08-31 15:17:10 +02:00
|
|
|
@mock.patch('httpie.cli.argtypes.AuthCredentials._getpass',
|
2016-11-24 00:15:18 +02:00
|
|
|
new=lambda self, prompt: 'UNEXPECTED_PROMPT_RESPONSE')
|
2016-11-23 23:29:36 +02:00
|
|
|
def test_auth_plugin_prompt_password_false(httpbin):
|
2016-11-23 23:01:58 +02:00
|
|
|
|
2016-11-24 00:09:45 +02:00
|
|
|
class Plugin(AuthPlugin):
|
2016-11-24 00:15:18 +02:00
|
|
|
auth_type = 'test-prompt-false'
|
2016-11-23 23:01:58 +02:00
|
|
|
prompt_password = False
|
|
|
|
|
|
|
|
def get_auth(self, username=None, password=None):
|
2016-11-24 00:09:45 +02:00
|
|
|
assert self.raw_auth == USERNAME
|
|
|
|
assert username == USERNAME
|
|
|
|
assert password is None
|
2016-11-24 00:15:18 +02:00
|
|
|
return basic_auth()
|
2016-11-23 23:01:58 +02:00
|
|
|
|
2016-11-24 00:09:45 +02:00
|
|
|
plugin_manager.register(Plugin)
|
2016-11-23 23:01:58 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
r = http(
|
|
|
|
httpbin + BASIC_AUTH_URL,
|
2016-11-24 00:09:45 +02:00
|
|
|
'--auth-type',
|
|
|
|
Plugin.auth_type,
|
|
|
|
'--auth',
|
|
|
|
USERNAME,
|
2016-11-23 23:01:58 +02:00
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
2016-11-24 00:09:45 +02:00
|
|
|
assert r.json == AUTH_OK
|
2016-11-23 23:01:58 +02:00
|
|
|
finally:
|
2016-11-24 00:09:45 +02:00
|
|
|
plugin_manager.unregister(Plugin)
|