mirror of
https://github.com/httpie/cli.git
synced 2025-08-09 21:58:40 +02:00
Extend auth plugin API
This extends the `AuthPlugin` API by the following attributes: * `auth_require`: set to `False` to make `--auth, -a` optional * `auth_parse`: set to `False` to disable `username:password` parsing (access the raw value passed to `-a` via `self.raw_auth`). * `prompt_password`: set to`False` to disable password prompt when no password provided (only relevant when `auth_parse == True`) These changes should be 100% backwards-compatible. What needs more testing is auth support in sessions. Close #433 Close #431 Close #378 Ping teracyhq/httpie-jwt-auth#3
This commit is contained in:
89
tests/test_auth_plugins.py
Normal file
89
tests/test_auth_plugins.py
Normal file
@ -0,0 +1,89 @@
|
||||
from utils import http, HTTP_OK
|
||||
from httpie.plugins import AuthPlugin, plugin_manager
|
||||
|
||||
# TODO: run all these tests in session mode as well
|
||||
|
||||
# Basic auth encoded 'username' and 'password'
|
||||
BASIC_AUTH_HEADER_VALUE = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='
|
||||
BASIC_AUTH_URL = '/basic-auth/username/password'
|
||||
|
||||
|
||||
def dummy_auth(auth_header=BASIC_AUTH_HEADER_VALUE):
|
||||
|
||||
def inner(r):
|
||||
r.headers['Authorization'] = auth_header
|
||||
return r
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
def test_auth_plugin_parse_false(httpbin):
|
||||
|
||||
class ParseFalseAuthPlugin(AuthPlugin):
|
||||
auth_type = 'parse-false'
|
||||
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
|
||||
return dummy_auth(self.raw_auth)
|
||||
|
||||
plugin_manager.register(ParseFalseAuthPlugin)
|
||||
try:
|
||||
r = http(
|
||||
httpbin + BASIC_AUTH_URL,
|
||||
'--auth-type', 'parse-false',
|
||||
'--auth', BASIC_AUTH_HEADER_VALUE
|
||||
)
|
||||
assert HTTP_OK in r
|
||||
finally:
|
||||
plugin_manager.unregister(ParseFalseAuthPlugin)
|
||||
|
||||
|
||||
def test_auth_plugin_require_false(httpbin):
|
||||
|
||||
class RequireFalseAuthPlugin(AuthPlugin):
|
||||
auth_type = 'require-false'
|
||||
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
|
||||
return dummy_auth()
|
||||
|
||||
plugin_manager.register(RequireFalseAuthPlugin)
|
||||
try:
|
||||
r = http(
|
||||
httpbin + BASIC_AUTH_URL,
|
||||
'--auth-type', 'require-false',
|
||||
)
|
||||
assert HTTP_OK in r
|
||||
finally:
|
||||
plugin_manager.unregister(RequireFalseAuthPlugin)
|
||||
|
||||
|
||||
def test_auth_plugin_prompt_false(httpbin):
|
||||
|
||||
class PromptFalseAuthPlugin(AuthPlugin):
|
||||
auth_type = 'prompt-false'
|
||||
prompt_password = False
|
||||
|
||||
def get_auth(self, username=None, password=None):
|
||||
assert self.raw_auth == 'username:'
|
||||
assert username == 'username'
|
||||
assert password == ''
|
||||
return dummy_auth()
|
||||
|
||||
plugin_manager.register(PromptFalseAuthPlugin)
|
||||
|
||||
try:
|
||||
r = http(
|
||||
httpbin + BASIC_AUTH_URL,
|
||||
'--auth-type', 'prompt-false',
|
||||
'--auth', 'username:'
|
||||
)
|
||||
assert HTTP_OK in r
|
||||
finally:
|
||||
plugin_manager.unregister(PromptFalseAuthPlugin)
|
Reference in New Issue
Block a user