Skip to content

Commit

Permalink
Auth: Refactor MultiAuth tests to reduce duplication
Browse files Browse the repository at this point in the history
Also attempts to increase codecov coverage
  • Loading branch information
cognifloyd committed Feb 12, 2019
1 parent d825c55 commit 80894f7
Showing 1 changed file with 100 additions and 76 deletions.
176 changes: 100 additions & 76 deletions tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

# Local imports
from uplink import auth
from uplink import utils


class TestGetAuth(object):
def test_none(self):
authentication = auth.get_auth(None)
assert authentication == utils.no_op

def test_tuple(self):
authentication = auth.get_auth(("username", "password"))
assert isinstance(authentication, auth.BasicAuth)
Expand Down Expand Up @@ -65,6 +70,7 @@ def __init__(self, token):

def test_api_token_header_subclass_with_prefix(request_builder):
# Setup

class ApiTokenHeaderSubclass(auth.ApiTokenHeader):
_header = "Token-Header"
_prefix = "Prefix"
Expand Down Expand Up @@ -108,79 +114,97 @@ def test_bearer_token(request_builder):
assert request_builder.info["headers"]["Authorization"] == auth_str


def multi_auth_with_one_methods(request_builder):
# Setup
basic_auth = auth.BasicAuth("apiuser", "apipass")

multi_auth = auth.MultiAuth(basic_auth)

# Verify
assert len(multi_auth) == 1
assert multi_auth[0] == basic_auth

multi_auth(request_builder)
basic_auth_str = basic_auth._header_value
assert request_builder.info["headers"]["Authorization"] == basic_auth_str


def multi_auth_with_three_methods(request_builder):
# Setup
token_param_auth = auth.ApiTokenParam(param="token-param", token="token-value")
basic_auth = auth.BasicAuth("apiuser", "apipass")
proxy_auth = auth.ProxyAuth("proxyuser", "proxypass")

multi_auth = auth.MultiAuth(token_param_auth, basic_auth, proxy_auth)

# Verify
assert len(multi_auth) == 3
assert multi_auth[0] == token_param_auth
assert multi_auth[1] == basic_auth
assert multi_auth[2] == proxy_auth

multi_auth(request_builder)
assert request_builder.info["params"]["token-param"] == "token-value"
basic_auth_str = basic_auth._header_value
assert request_builder.info["headers"]["Authorization"] == basic_auth_str
proxy_auth_str = proxy_auth._header_value
assert request_builder.info["headers"]["Proxy-Authorization"] == proxy_auth_str


def multi_auth_append(request_builder):
# Setup
basic_auth = auth.BasicAuth("apiuser", "apipass")
proxy_auth = auth.ProxyAuth("proxyuser", "proxypass")

multi_auth = auth.MultiAuth()
multi_auth.append(basic_auth)
multi_auth.append(proxy_auth)

# Verify
assert len(multi_auth) == 2
assert multi_auth[0] == basic_auth
assert multi_auth[1] == proxy_auth

multi_auth(request_builder)
basic_auth_str = basic_auth._header_value
assert request_builder.info["headers"]["Authorization"] == basic_auth_str
proxy_auth_str = proxy_auth._header_value
assert request_builder.info["headers"]["Proxy-Authorization"] == proxy_auth_str


def multi_auth_extend(request_builder):
# Setup
basic_auth = auth.BasicAuth("apiuser", "apipass")
proxy_auth = auth.ProxyAuth("proxyuser", "proxypass")

multi_auth = auth.MultiAuth()
multi-auth.extend([basic_auth, proxy_auth])

# Verify
assert len(multi_auth) == 2
assert multi_auth[0] == basic_auth
assert multi_auth[1] == proxy_auth

multi_auth(request_builder)
basic_auth_str = basic_auth._header_value
assert request_builder.info["headers"]["Authorization"] == basic_auth_str
proxy_auth_str = proxy_auth._header_value
assert request_builder.info["headers"]["Proxy-Authorization"] == proxy_auth_str
class TestMultiAuth(object):
def setup_basic_auth(self):
return auth.BasicAuth("apiuser", "apipass")

def verify_basic_auth(self, basic_auth, request_builder):
basic_auth_str = basic_auth._header_value
assert request_builder.info["headers"]["Authorization"] == basic_auth_str

def setup_proxy_auth(self):
return auth.ProxyAuth("proxyuser", "proxypass")

def verify_proxy_auth(self, proxy_auth, request_builder):
proxy_auth_str = proxy_auth._header_value
assert request_builder.info["headers"]["Proxy-Authorization"] == proxy_auth_str

def setup_param_auth(self):
return auth.ApiTokenParam(param="token-param", token="token-value")

def verify_param_auth(self, request_builder):
assert request_builder.info["params"]["token-param"] == "token-value"

def test_len(self):
multi_auth = auth.MultiAuth()
assert len(multi_auth) == 0

def test_none(self):
multi_auth = auth.MultiAuth(None)
assert len(multi_auth) == 1
assert multi_auth[0] == utils.no_op

def test_one_method(self, request_builder):
# Setup
basic_auth = self.setup_basic_auth()
multi_auth = auth.MultiAuth(basic_auth)

# Verify
assert len(multi_auth) == 1
assert multi_auth[0] == basic_auth

multi_auth(request_builder)
self.verify_basic_auth(basic_auth, request_builder)

def test_four_methods(self, request_builder):
# Setup
param_auth = self.setup_param_auth()
basic_auth = self.setup_basic_auth()
proxy_auth = self.setup_proxy_auth()
multi_auth = auth.MultiAuth(None, param_auth, basic_auth, proxy_auth)

# Verify
assert len(multi_auth) == 4
assert multi_auth[0] == utils.no_op
assert multi_auth[1] == param_auth
assert multi_auth[2] == basic_auth
assert multi_auth[3] == proxy_auth

multi_auth(request_builder)
self.verify_param_auth(request_builder)
self.verify_basic_auth(basic_auth, request_builder)
self.verify_proxy_auth(proxy_auth, request_builder)

def test_append(self, request_builder):
# Setup
basic_auth = self.setup_basic_auth()
proxy_auth = self.setup_proxy_auth()
multi_auth = auth.MultiAuth()
multi_auth.append(basic_auth)
multi_auth.append(proxy_auth)

# Verify
assert len(multi_auth) == 2
assert multi_auth[0] == basic_auth
assert multi_auth[1] == proxy_auth

multi_auth(request_builder)
self.verify_basic_auth(basic_auth, request_builder)
self.verify_proxy_auth(proxy_auth, request_builder)


def test_extend(self, request_builder):
# Setup
basic_auth = self.setup_basic_auth()
proxy_auth = self.setup_proxy_auth()
multi_auth = auth.MultiAuth()
multi_auth.extend([basic_auth, proxy_auth])

# Verify
assert len(multi_auth) == 2
assert multi_auth[0] == basic_auth
assert multi_auth[1] == proxy_auth

multi_auth(request_builder)
self.verify_basic_auth(basic_auth, request_builder)
self.verify_proxy_auth(proxy_auth, request_builder)

0 comments on commit 80894f7

Please sign in to comment.