Skip to content

Commit

Permalink
Add more plugin API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbrzt committed Nov 23, 2016
1 parent 47fd392 commit 3f7ed35
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion httpie/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def __iter__(self):
auth.add_argument(
'--auth-type', '-A',
choices=_AuthTypeLazyChoices(),
default=_auth_plugins[0].auth_type,
default=None,
help="""
The authentication mechanism to be used. Defaults to "{default}".
Expand Down
4 changes: 3 additions & 1 deletion httpie/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _process_auth(self):
# TODO: refactor
self.args.auth_plugin = None
default_auth_plugin = plugin_manager.get_auth_plugins()[0]
auth_type_set = self.args.auth_type != default_auth_plugin.auth_type
auth_type_set = self.args.auth_type is not None
url = urlsplit(self.args.url)

if self.args.auth is None and not auth_type_set:
Expand All @@ -234,6 +234,8 @@ def _process_auth(self):
)

if self.args.auth is not None or auth_type_set:
if not self.args.auth_type:
self.args.auth_type = default_auth_plugin.auth_type
plugin = plugin_manager.get_auth_plugin(self.args.auth_type)()

if plugin.auth_require and self.args.auth is None:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ def test_only_username_in_url(url):
assert args.auth
assert args.auth.username == 'username'
assert args.auth.password == ''


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
83 changes: 63 additions & 20 deletions tests/test_auth_plugins.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from mock import mock

from utils import http, HTTP_OK
from httpie.input import SEP_CREDENTIALS
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'
USERNAME = 'user'
PASSWORD = 'password'
# Basic auth encoded `USERNAME` and `PASSWORD`
BASIC_AUTH_HEADER_VALUE = 'Basic dXNlcjpwYXNzd29yZA=='
BASIC_AUTH_URL = '/basic-auth/{}/{}'.format(USERNAME, PASSWORD)
AUTH_OK = {'authenticated': True, 'user': USERNAME}


def dummy_auth(auth_header=BASIC_AUTH_HEADER_VALUE):
Expand All @@ -19,7 +25,7 @@ def inner(r):

def test_auth_plugin_parse_auth_false(httpbin):

class ParseFalseAuthPlugin(AuthPlugin):
class Plugin(AuthPlugin):
auth_type = 'parse-false'
auth_parse = False

Expand All @@ -29,21 +35,24 @@ def get_auth(self, username=None, password=None):
assert self.raw_auth == BASIC_AUTH_HEADER_VALUE
return dummy_auth(self.raw_auth)

plugin_manager.register(ParseFalseAuthPlugin)
plugin_manager.register(Plugin)
try:
r = http(
httpbin + BASIC_AUTH_URL,
'--auth-type', 'parse-false',
'--auth', BASIC_AUTH_HEADER_VALUE
'--auth-type',
Plugin.auth_type,
'--auth',
BASIC_AUTH_HEADER_VALUE,
)
assert HTTP_OK in r
assert r.json == AUTH_OK
finally:
plugin_manager.unregister(ParseFalseAuthPlugin)
plugin_manager.unregister(Plugin)


def test_auth_plugin_require_auth_false(httpbin):

class RequireFalseAuthPlugin(AuthPlugin):
class Plugin(AuthPlugin):
auth_type = 'require-false'
auth_require = False

Expand All @@ -53,37 +62,71 @@ def get_auth(self, username=None, password=None):
assert password is None
return dummy_auth()

plugin_manager.register(RequireFalseAuthPlugin)
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):
auth_type = 'require-false-yet-provided'
auth_require = False

def get_auth(self, username=None, password=None):
assert self.raw_auth == USERNAME + SEP_CREDENTIALS + PASSWORD
assert username == USERNAME
assert password == PASSWORD
return dummy_auth()

plugin_manager.register(Plugin)
try:
r = http(
httpbin + BASIC_AUTH_URL,
'--auth-type', 'require-false',
'--auth-type',
Plugin.auth_type,
'--auth',
USERNAME + SEP_CREDENTIALS + PASSWORD,
)
assert HTTP_OK in r
assert r.json == AUTH_OK
finally:
plugin_manager.unregister(RequireFalseAuthPlugin)
plugin_manager.unregister(Plugin)


@mock.patch('httpie.input.AuthCredentials._getpass',
new=lambda self, prompt: 'unexpected prompt response')
def test_auth_plugin_prompt_password_false(httpbin):

class PromptFalseAuthPlugin(AuthPlugin):
class Plugin(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 == ''
assert self.raw_auth == USERNAME
assert username == USERNAME
assert password is None
return dummy_auth()

plugin_manager.register(PromptFalseAuthPlugin)
plugin_manager.register(Plugin)

try:
r = http(
httpbin + BASIC_AUTH_URL,
'--auth-type', 'prompt-false',
'--auth', 'username:'
'--auth-type',
Plugin.auth_type,
'--auth',
USERNAME,
)
assert HTTP_OK in r
assert r.json == AUTH_OK
finally:
plugin_manager.unregister(PromptFalseAuthPlugin)
plugin_manager.unregister(Plugin)

0 comments on commit 3f7ed35

Please sign in to comment.